1/*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_REFLECT_REGISTRATION_H
17#define OHOS_SHARING_REFLECT_REGISTRATION_H
18
19#include <atomic>
20#include <functional>
21#include <mutex>
22#include <singleton.h>
23#include <string>
24#include <unordered_map>
25
26namespace OHOS {
27namespace Sharing {
28
29using Constructor = std::function<std::shared_ptr<void>()>;
30
31class ReflectRegistration : public Singleton<ReflectRegistration> {
32    friend class Singleton<ReflectRegistration>;
33
34public:
35    ReflectRegistration() = default;
36    explicit ReflectRegistration(ReflectRegistration &&ref) {}
37    explicit ReflectRegistration(const ReflectRegistration &ref) {}
38    ~ReflectRegistration();
39
40    void Unregister(const std::string &descriptor);
41    bool Register(const std::string &descriptor, Constructor creator);
42    std::shared_ptr<void> CreateObject(const std::string &descriptor);
43
44private:
45    ReflectRegistration &operator=(ReflectRegistration &&) = delete;
46    ReflectRegistration &operator=(const ReflectRegistration &) = delete;
47
48private:
49    std::mutex creatorMutex_;
50    std::unordered_map<std::string, Constructor> creators_;
51};
52
53class ReflectClassRegister {
54public:
55    ReflectClassRegister(const std::string &name, Constructor func)
56    {
57        ReflectRegistration::GetInstance().Register(name, func);
58    };
59
60private:
61    ReflectClassRegister() = delete;
62};
63
64#define REGISTER_CLASS_REFLECTOR(className)                    \
65    static std::shared_ptr<className> className##Creator(void) \
66    {                                                          \
67        auto obj = std::make_shared<className>();              \
68        obj->SetClassName(#className);                         \
69        return obj;                                            \
70    }                                                          \
71    const ReflectClassRegister className##Register(#className, className##Creator)
72
73template <class T>
74class ClassReflector {
75public:
76    static std::shared_ptr<T> Class2Instance(const std::string &name)
77    {
78        return std::static_pointer_cast<T>(ReflectRegistration::GetInstance().CreateObject(name));
79    };
80};
81
82} // namespace Sharing
83} // namespace OHOS
84#endif