1 /*
2  * Copyright (c) 2024 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 META_SRC_ENGINE_ENGINE_VALUE_MANAGER_H
17 #define META_SRC_ENGINE_ENGINE_VALUE_MANAGER_H
18 
19 #include <base/containers/unordered_map.h>
20 
21 #include <meta/base/interface_macros.h>
22 #include <meta/interface/engine/intf_engine_value_manager.h>
23 #include <meta/interface/intf_task_queue.h>
24 
25 #include "../meta_object.h"
26 #include "engine_value.h"
27 
META_BEGIN_NAMESPACE()28 META_BEGIN_NAMESPACE()
29 
30 namespace Internal {
31 
32 class EngineValueManager
33     : public Internal::BaseObjectFwd<EngineValueManager, META_NS::ClassId::EngineValueManager, IEngineValueManager> {
34 public:
35     META_NO_COPY_MOVE(EngineValueManager)
36     EngineValueManager() = default;
37     ~EngineValueManager() override;
38 
39     void SetNotificationQueue(const ITaskQueue::WeakPtr&) override;
40     bool ConstructValues(CORE_NS::IPropertyHandle* handle, EngineValueOptions) override;
41     bool ConstructValues(IValue::Ptr value, EngineValueOptions) override;
42     bool ConstructValue(EnginePropertyParams property, EngineValueOptions) override;
43     bool ConstructValue(CORE_NS::IPropertyHandle* handle, BASE_NS::string_view path, EngineValueOptions) override;
44 
45     bool RemoveValue(BASE_NS::string_view name) override;
46     void RemoveAll() override;
47 
48     IProperty::Ptr ConstructProperty(BASE_NS::string_view name) const override;
49     IEngineValue::Ptr GetEngineValue(BASE_NS::string_view name) const override;
50 
51     BASE_NS::vector<IProperty::Ptr> ConstructAllProperties() const override;
52     BASE_NS::vector<IEngineValue::Ptr> GetAllEngineValues() const override;
53 
54     bool Sync(EngineSyncDirection) override;
55 
56 private:
57     void NotifySyncs();
58     IProperty::Ptr PropertyFromValue(BASE_NS::string_view name, const IEngineValue::Ptr&) const;
59     void AddValue(EnginePropertyParams p, EngineValueOptions options);
60     bool ConstructValueImpl(CORE_NS::IPropertyHandle* handle, BASE_NS::string pathTaken, BASE_NS::string_view path,
61         EngineValueOptions options);
62     bool ConstructValueImpl(
63         EnginePropertyParams params, BASE_NS::string pathTaken, BASE_NS::string_view path, EngineValueOptions options);
64 
65 private:
66     mutable std::shared_mutex mutex_;
67     ITaskQueue::WeakPtr queue_;
68     struct ValueInfo {
69         IEngineValue::Ptr value;
70         bool notify {};
71     };
72     BASE_NS::unordered_map<BASE_NS::string, ValueInfo> values_;
73     ITaskQueue::Token task_token_ {};
74 };
75 
76 } // namespace Internal
77 
78 META_END_NAMESPACE()
79 
80 #endif