• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_API_PROPERTY_CUSTOM_VALUE_H
17 #define META_API_PROPERTY_CUSTOM_VALUE_H
18 
19 #include <meta/interface/interface_helpers.h>
20 #include <meta/interface/property/intf_property.h>
21 
META_BEGIN_NAMESPACE()22 META_BEGIN_NAMESPACE()
23 
24 /// Helper that maps IValue GetVAlue/SetValue to corresponding function pointers
25 template<typename Type, typename GetFunc, typename SetFunc>
26 class CustomValue : public IntroduceInterfaces<IValue> {
27 public:
28     CustomValue(GetFunc gfunc, SetFunc sfunc) : gfunc_(BASE_NS::move(gfunc)), sfunc_(BASE_NS::move(sfunc)) {}
29 
30     AnyReturnValue SetValue(const IAny& any) override
31     {
32         Type value {};
33         auto res = any.GetValue(value);
34         if (res) {
35             if (sfunc_(value)) {
36                 res = any_.SetValue(value);
37             } else {
38                 res = AnyReturn::FAIL;
39             }
40         }
41         return res;
42     }
43     const IAny& GetValue() const override
44     {
45         auto value = gfunc_();
46         if (any_.SetValue(value)) {
47             return any_;
48         }
49         return GetObjectRegistry().GetPropertyRegister().InvalidAny();
50     }
51 
52     bool IsCompatible(const TypeId& id) const override
53     {
54         return any_.IsCompatible(id);
55     }
56 
57 private:
58     GetFunc gfunc_;
59     SetFunc sfunc_;
60     mutable Any<Type> any_;
61 };
62 
63 /// Add custom value to property
64 template<typename Type, typename GetFunc, typename SetFunc>
AddCustomValue(const IProperty::Ptr & p,GetFunc gf,SetFunc sf)65 bool AddCustomValue(const IProperty::Ptr& p, GetFunc gf, SetFunc sf)
66 {
67     if (auto i = interface_cast<IStackProperty>(p)) {
68         auto val = CreateShared<CustomValue<Type, GetFunc, SetFunc>>(BASE_NS::move(gf), BASE_NS::move(sf));
69         return i->PushValue(val);
70     }
71     return false;
72 }
73 
74 /// Add custom value that calls member functions for property
75 template<typename Type, typename Obj>
AddGetSetCustomValue(const Property<Type> & p,Obj * obj,Type (Obj::* getf)()const,bool (Obj::* setf)(const Type &))76 bool AddGetSetCustomValue(const Property<Type>& p, Obj* obj, Type (Obj::*getf)() const, bool (Obj::*setf)(const Type&))
77 {
78     IStackProperty::Ptr sp = interface_pointer_cast<IStackProperty>(p);
79     if (!sp) {
80         return false;
81     }
82     auto gf = [obj, getf, weak = BASE_NS::weak_ptr<IObject>(obj->GetSelf())]() -> Type {
83         if (auto lock = weak.lock()) {
84             return (obj->*getf)();
85         }
86         return Type {};
87     };
88     auto sf = [obj, setf, weak = BASE_NS::weak_ptr<IObject>(obj->GetSelf())](const Type& value) -> bool {
89         if (auto lock = weak.lock()) {
90             return (obj->*setf)(value);
91         }
92         return false;
93     };
94     return AddCustomValue<Type>(p.GetProperty(), BASE_NS::move(gf), BASE_NS::move(sf));
95 }
96 
97 META_END_NAMESPACE()
98 
99 #endif
100