• 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 
17 #ifndef META_API_PROPERTY_CUSTOM_VALUE_H
18 #define META_API_PROPERTY_CUSTOM_VALUE_H
19 
20 #include <meta/interface/interface_helpers.h>
21 #include <meta/interface/property/intf_property.h>
22 
META_BEGIN_NAMESPACE()23 META_BEGIN_NAMESPACE()
24 
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 template<typename Type, typename GetFunc, typename SetFunc>
AddCustomValue(const IProperty::Ptr & p,GetFunc gf,SetFunc sf)64 bool AddCustomValue(const IProperty::Ptr& p, GetFunc gf, SetFunc sf)
65 {
66     if (auto i = interface_cast<IStackProperty>(p)) {
67         auto val = CreateShared<CustomValue<Type, GetFunc, SetFunc>>(BASE_NS::move(gf), BASE_NS::move(sf));
68         return i->PushValue(val);
69     }
70     return false;
71 }
72 
73 template<typename Type, typename Obj>
AddGetSetCustomValue(const Property<Type> & p,Obj * obj,Type (Obj::* getf)()const,bool (Obj::* setf)(const Type &))74 bool AddGetSetCustomValue(const Property<Type>& p, Obj* obj, Type (Obj::*getf)() const, bool (Obj::*setf)(const Type&))
75 {
76     IStackProperty::Ptr sp = interface_pointer_cast<IStackProperty>(p);
77     if (!sp) {
78         return false;
79     }
80     auto gf = [obj, getf, weak = BASE_NS::weak_ptr<IObject>(obj->GetSelf())]() -> Type {
81         if (auto lock = weak.lock()) {
82             return (obj->*getf)();
83         }
84         return Type {};
85     };
86     auto sf = [obj, setf, weak = BASE_NS::weak_ptr<IObject>(obj->GetSelf())](const Type& value) -> bool {
87         if (auto lock = weak.lock()) {
88             return (obj->*setf)(value);
89         }
90         return false;
91     };
92     return AddCustomValue<Type>(p.GetProperty(), BASE_NS::move(gf), BASE_NS::move(sf));
93 }
94 
95 META_END_NAMESPACE()
96 
97 #endif
98