• 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 #include "device_boutique.h"
18 
19 #include "log.h"
20 
21 using std::vector;
22 
23 namespace rootcanal {
24 
25 std::unordered_map<std::string, std::function<std::shared_ptr<Device>(const vector<std::string>&)>>&
GetMap()26 DeviceBoutique::GetMap() {
27   static std::unordered_map<std::string,
28                             std::function<std::shared_ptr<Device>(const vector<std::string>&)>>
29           impl;
30   return impl;
31 }
32 
33 // Register a constructor for a device type.
Register(const std::string & device_type,const std::function<std::shared_ptr<Device> (const vector<std::string> &)> method)34 bool DeviceBoutique::Register(
35         const std::string& device_type,
36         const std::function<std::shared_ptr<Device>(const vector<std::string>&)> method) {
37   INFO("Registering {}", device_type);
38   GetMap()[device_type] = method;
39   return true;
40 }
41 
Create(const vector<std::string> & args)42 std::shared_ptr<Device> DeviceBoutique::Create(const vector<std::string>& args) {
43   ASSERT(!args.empty());
44 
45   auto device = GetMap().find(args[0]);
46 
47   if (device == GetMap().end()) {
48     WARNING("No constructor registered for {}", args[0]);
49     return std::shared_ptr<Device>(nullptr);
50   }
51 
52   return device->second(args);
53 }
54 
55 }  // namespace rootcanal
56