HidlBinderAdapter.h 1.7 KB

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