• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "model/devices/device.h"
25 
26 namespace test_vendor_lib {
27 
28 // Create customized devices from a centralized shop.
29 class DeviceBoutique {
30  public:
31   DeviceBoutique();
32   virtual ~DeviceBoutique() = default;
33 
34   // Register a constructor for a device type.
35   static bool Register(const std::string& device_type, const std::function<std::shared_ptr<Device>()> method);
36 
37   // Call the constructor that matches arg[0], then call dev->Initialize(args).
38   static std::shared_ptr<Device> Create(const std::vector<std::string>& args);
39 
40   template <typename D>
41   struct Registrar {
RegistrarRegistrar42     explicit Registrar(std::string const& name) {
43       DeviceBoutique::Register(name, &D::Create);
44     }
45     static Registrar<D> registrar_;
46   };
47 
48  private:
49   static std::unordered_map<std::string, std::function<std::shared_ptr<Device>()>>& GetMap();
50 };
51 
52 }  // namespace test_vendor_lib
53