blob: 46df5541ec373fbb8ea229f68b7a4191d5543254 [file] [log] [blame]
Steven Morelandff189a02017-09-15 16:22:48 -07001/*
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
22namespace android {
23namespace hardware {
24
25namespace details {
26
27using IBase = ::android::hidl::base::V1_0::IBase;
28
29// AdapterFactory(impl) -> adapter
30using AdapterFactory = std::function<sp<IBase>(sp<IBase>)>;
31// AdaptersFactory(package@interface)(impl) -> adapter
32using AdaptersFactory = std::map<std::string, AdapterFactory>;
33
34int adapterMain(const std::string& package, int argc, char** argv, const AdaptersFactory& adapters);
35
36sp<IBase> adaptWithDefault(const sp<IBase>& something,
37 const std::function<sp<IBase>()>& makeDefault);
38
39} // namespace details
40
41template <typename... Adapters>
42int 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