1 /*
2 * Copyright (c) 2023 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 DISTRIBUTEDDATAMGR_APPDATAMGR_JSPROXY_H
17 #define DISTRIBUTEDDATAMGR_APPDATAMGR_JSPROXY_H
18 #include <memory>
19 namespace OHOS::JSProxy {
20 template<typename T>
21 class JSCreator {
22 public:
23     // This method will be used to call different subclasses for implementation
24     virtual std::shared_ptr<T> Create() = 0;
25 
26 protected:
27     // It is not allowed to directly use parent class objects for construction and destruction
28     JSCreator() = default;
29     ~JSCreator() = default;
30 };
31 
32 template<typename T>
33 class JSProxy {
34 public:
SetInstance(std::shared_ptr<T> instance)35     void SetInstance(std::shared_ptr<T> instance)
36     {
37         instance_ = std::move(instance);
38     }
39 
GetInstance()40     std::shared_ptr<T> GetInstance() const
41     {
42         return instance_;
43     }
44 
45 protected:
46     // It is not allowed to directly use parent class objects for construction and destruction
47     JSProxy() = default;
48     ~JSProxy() = default;
49 
50 private:
51     std::shared_ptr<T> instance_;
52 };
53 
54 template<typename T, typename U = T>
55 class JSEntity : public JSCreator<U>, public JSProxy<T> {
56 protected:
57     // It is not allowed to directly use parent class objects for construction and destruction
58     JSEntity() = default;
59     ~JSEntity() = default;
60 };
61 } // namespace OHOS::JSProxy
62 #endif // DISTRIBUTEDDATAMGR_APPDATAMGR_JSPROXY_H
63