• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HDI_OBJECT_COLLECTOR_H
17 #define HDI_OBJECT_COLLECTOR_H
18 
19 #include <string>
20 #include <hdi_base.h>
21 #include <iremote_object.h>
22 #include <map>
23 #include <mutex>
24 #include <refbase.h>
25 
26 namespace OHOS {
27 namespace HDI {
28 class ObjectCollector {
29 public:
30     using Constructor = std::function<sptr<IRemoteObject>(const sptr<HdiBase> &interface)>;
31 
32     static ObjectCollector &GetInstance();
33 
34     bool ConstructorRegister(const std::u16string &interfaceName, const Constructor &constructor);
35     void ConstructorUnRegister(const std::u16string &interfaceName);
36     sptr<IRemoteObject> NewObject(const sptr<HdiBase> &interface, const std::u16string &interfaceName);
37     sptr<IRemoteObject> GetOrNewObject(const sptr<HdiBase> &interface, const std::u16string &interfaceName);
38     bool RemoveObject(const sptr<HdiBase> &interface);
39 
40 private:
41     ObjectCollector() = default;
42     sptr<IRemoteObject> NewObjectLocked(const sptr<HdiBase> &interface, const std::u16string &interfaceName);
43     static ObjectCollector *instance_;
44     std::map<const std::u16string, const Constructor> constructorMapper_;
45     std::map<HdiBase *, IRemoteObject *> interfaceObjectCollector_;
46     std::mutex mutex_;
47 };
48 
49 template <typename OBJECT, typename INTERFACE>
50 class ObjectDelegator {
51 public:
52     ObjectDelegator();
53     ~ObjectDelegator();
54 
55 private:
56     ObjectDelegator(const ObjectDelegator &) = delete;
57     ObjectDelegator(ObjectDelegator &&) = delete;
58     ObjectDelegator &operator=(const ObjectDelegator &) = delete;
59     ObjectDelegator &operator=(ObjectDelegator &&) = delete;
60 };
61 
62 template <typename OBJECT, typename INTERFACE>
ObjectDelegator()63 ObjectDelegator<OBJECT, INTERFACE>::ObjectDelegator()
64 {
65     ObjectCollector::GetInstance().ConstructorRegister(
66         INTERFACE::GetDescriptor(), [](const sptr<HdiBase> &interface) -> sptr<IRemoteObject> {
67             return new OBJECT(static_cast<INTERFACE *>(interface.GetRefPtr()));
68         });
69 }
70 
71 template <typename OBJECT, typename INTERFACE>
~ObjectDelegator()72 ObjectDelegator<OBJECT, INTERFACE>::~ObjectDelegator()
73 {
74     ObjectCollector::GetInstance().ConstructorUnRegister(INTERFACE::GetDescriptor());
75 }
76 } // namespace HDI
77 } // namespace OHOS
78 
79 #endif // HDI_OBJECT_MAPPER_H
80