1 /*
2 * Copyright (C) 2017 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 <android/hidl/base/1.0/IBase.h>
18 #include <hidl/HidlSupport.h>
19
20 #include <map>
21
22 namespace android {
23 namespace hardware {
24
25 namespace details {
26
27 using IBase = ::android::hidl::base::V1_0::IBase;
28
29 // AdapterFactory(impl) -> adapter
30 using AdapterFactory = std::function<sp<IBase>(sp<IBase>)>;
31 // AdaptersFactory(package@interface)(impl) -> adapter
32 using AdaptersFactory = std::map<std::string, AdapterFactory>;
33
34 int adapterMain(const std::string& package, int argc, char** argv, const AdaptersFactory& adapters);
35
36 sp<IBase> adaptWithDefault(const sp<IBase>& something,
37 const std::function<sp<IBase>()>& makeDefault);
38
39 } // namespace details
40
41 template <typename... Adapters>
adapterMain(const std::string & package,int argc,char ** argv)42 int adapterMain(const std::string& package, int argc, char** argv) {
43 return details::adapterMain(
44 package, argc, argv,
45 {{Adapters::Pure::descriptor, [](sp<::android::hidl::base::V1_0::IBase> base) {
46 return details::adaptWithDefault(
47 base, [&] { return new Adapters(Adapters::Pure::castFrom(base)); });
48 }}...});
49 }
50
51 } // namespace hardware
52 } // namespace android