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