• 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_INTERFACE_ANY_H
17 #define META_INTERFACE_ANY_H
18 
19 #include <meta/base/expected.h>
20 #include <meta/base/ids.h>
21 #include <meta/base/interface_macros.h>
22 #include <meta/base/namespace.h>
23 #include <meta/base/types.h>
24 
25 META_BEGIN_NAMESPACE()
26 
27 META_REGISTER_INTERFACE(IAny, "9e6d554e-3647-4040-a5c4-ea299fa85b8f")
28 META_REGISTER_INTERFACE(IArrayAny, "d3c49aa9-9314-4680-b23a-7d2a24a0af30")
29 
30 enum class AnyReturn {
31     NOTHING_TO_DO = 2,
32     SUCCESS = 1,
33     UNUSED = 0,
34     FAIL = -1,
35     INVALID_ARGUMENT = -2,
36     INCOMPATIBLE_TYPE = -3,
37     NOT_SUPPORTED = -4,
38     RECURSIVE_CALL = -5
39 };
40 inline bool operator<(AnyReturn l, AnyReturn r)
41 {
42     return int64_t(l) < int64_t(r);
43 }
44 inline bool operator>(AnyReturn l, AnyReturn r)
45 {
46     return int64_t(l) > int64_t(r);
47 }
48 
49 using AnyReturnValue = ReturnValue<AnyReturn>;
50 
51 enum class CompatibilityDirection { GET, SET, BOTH };
52 enum class TypeIdRole { CURRENT, ITEM, ARRAY };
53 enum class CloneValueType { COPY_VALUE, DEFAULT_VALUE };
54 
55 struct AnyCloneOptions {
56     /** Should value be cloned. Ignored if role != TypeIdRole::CURRENT */
57     CloneValueType value { CloneValueType::COPY_VALUE };
58     /** Type of Any (same as source, item or array type) the clone should be. */
59     TypeIdRole role { TypeIdRole::CURRENT };
60 };
61 
62 class IAny : public CORE_NS::IInterface {
63     META_INTERFACE(CORE_NS::IInterface, IAny)
64 public:
65     virtual ObjectId GetClassId() const = 0;
66     virtual const BASE_NS::array_view<const TypeId> GetCompatibleTypes(CompatibilityDirection) const = 0;
67     virtual AnyReturnValue GetData(const TypeId& id, void* data, size_t size) const = 0;
68     virtual AnyReturnValue SetData(const TypeId& id, const void* data, size_t size) = 0;
69     virtual AnyReturnValue CopyFrom(const IAny& any) = 0;
70     virtual AnyReturnValue ResetValue() = 0;
71     virtual IAny::Ptr Clone(const AnyCloneOptions& options) const = 0;
72     virtual TypeId GetTypeId(TypeIdRole role) const = 0;
73     virtual BASE_NS::string GetTypeIdString() const = 0;
74 
GetTypeId()75     TypeId GetTypeId() const
76     {
77         return GetTypeId(TypeIdRole::CURRENT);
78     }
IsArray()79     bool IsArray() const
80     {
81         return GetTypeId(TypeIdRole::CURRENT) == GetTypeId(TypeIdRole::ARRAY);
82     }
Clone()83     IAny::Ptr Clone() const
84     {
85         return Clone({ CloneValueType::COPY_VALUE });
86     }
Clone(bool withValue)87     IAny::Ptr Clone(bool withValue) const
88     {
89         return Clone({ withValue ? CloneValueType::COPY_VALUE : CloneValueType::DEFAULT_VALUE });
90     }
91     template<typename T>
GetValue(T & value)92     AnyReturnValue GetValue(T& value) const
93     {
94         return GetData(UidFromType<T>(), &value, sizeof(T)); /*NOLINT(bugprone-sizeof-expression)*/
95     }
96     template<typename T>
SetValue(const T & value)97     AnyReturnValue SetValue(const T& value)
98     {
99         return SetData(UidFromType<T>(), &value, sizeof(T)); /*NOLINT(bugprone-sizeof-expression)*/
100     }
101 };
102 
103 class IArrayAny : public IAny {
104     META_INTERFACE(IAny, IArrayAny)
105 public:
106     using IndexType = size_t;
107 
108     /// Get compatible types for any constructed with Clone for the array item type
109     virtual const BASE_NS::array_view<const TypeId> GetItemCompatibleTypes(CompatibilityDirection) const = 0;
110 
111     virtual AnyReturnValue GetDataAt(size_t index, const TypeId& id, void* data, size_t size) const = 0;
112     virtual AnyReturnValue SetDataAt(size_t index, const TypeId& id, const void* data, size_t size) = 0;
113     virtual AnyReturnValue SetAnyAt(IndexType index, const IAny& value) = 0;
114     virtual AnyReturnValue GetAnyAt(IndexType index, IAny& value) const = 0;
115     virtual AnyReturnValue InsertAnyAt(IndexType index, const IAny& value) = 0;
116     virtual AnyReturnValue RemoveAt(IndexType index) = 0;
117     virtual void RemoveAll() = 0;
118 
119     virtual IndexType GetSize() const = 0;
120 
121     template<typename T>
GetValueAt(IndexType index,T & value)122     AnyReturnValue GetValueAt(IndexType index, T& value) const
123     {
124         return GetDataAt(index, UidFromType<T>(), &value, sizeof(T)); /*NOLINT(bugprone-sizeof-expression)*/
125     }
126     template<typename T>
SetValueAt(IndexType index,const T & value)127     AnyReturnValue SetValueAt(IndexType index, const T& value)
128     {
129         return SetDataAt(index, UidFromType<T>(), &value, sizeof(T)); /*NOLINT(bugprone-sizeof-expression)*/
130     }
131 };
132 
IsArray(const IAny & any)133 inline bool IsArray(const IAny& any)
134 {
135     return any.GetInterface<IArrayAny>() != nullptr;
136 }
137 
IsArray(const IAny::ConstPtr & any)138 inline bool IsArray(const IAny::ConstPtr& any)
139 {
140     return any && any->GetInterface<IArrayAny>() != nullptr;
141 }
142 
143 template<typename T>
144 inline T GetValue(const IAny& any, NonDeduced_t<T> defaultValue = {})
145 {
146     T t;
147     if (!any.GetValue(t)) {
148         t = defaultValue;
149     }
150     return t;
151 }
152 
153 template<typename T>
154 inline T GetValueAt(const IArrayAny& array, IArrayAny::IndexType index, NonDeduced_t<T> defaultValue = {})
155 {
156     T t;
157     if (!array.GetValueAt(index, t)) {
158         t = defaultValue;
159     }
160     return t;
161 }
162 
163 inline bool IsCompatible(const IAny& any, const TypeId& uid, CompatibilityDirection dir = CompatibilityDirection::BOTH)
164 {
165     for (auto&& v : any.GetCompatibleTypes(dir)) {
166         if (uid == v) {
167             return true;
168         }
169     }
170     return false;
171 }
172 
173 inline bool IsItemCompatible(
174     const IArrayAny& array, const TypeId& uid, CompatibilityDirection dir = CompatibilityDirection::BOTH)
175 {
176     for (auto&& v : array.GetItemCompatibleTypes(dir)) {
177         if (uid == v) {
178             return true;
179         }
180     }
181     return false;
182 }
183 
184 inline bool IsCompatibleWith(
185     const IAny& any, const IAny& other, CompatibilityDirection dir = CompatibilityDirection::BOTH)
186 {
187     for (auto&& v : other.GetCompatibleTypes(dir)) {
188         if (IsCompatible(any, v, dir)) {
189             return true;
190         }
191     }
192     return false;
193 }
194 
195 template<typename T>
196 inline bool IsCompatibleWith(const IAny& any, CompatibilityDirection dir = CompatibilityDirection::BOTH)
197 {
198     return IsCompatible(any, UidFromType<BASE_NS::remove_const_t<BASE_NS::remove_reference_t<T>>>(), dir);
199 }
200 
IsSetCompatible(const IAny & any,const TypeId & uid)201 inline bool IsSetCompatible(const IAny& any, const TypeId& uid)
202 {
203     return IsCompatible(any, uid, CompatibilityDirection::SET);
204 }
205 
IsGetCompatible(const IAny & any,const TypeId & uid)206 inline bool IsGetCompatible(const IAny& any, const TypeId& uid)
207 {
208     return IsCompatible(any, uid, CompatibilityDirection::GET);
209 }
210 
211 template<typename T>
IsSetCompatibleWith(const IAny & any)212 inline bool IsSetCompatibleWith(const IAny& any)
213 {
214     return IsCompatibleWith<T>(any, CompatibilityDirection::SET);
215 }
216 
217 template<typename T>
IsGetCompatibleWith(const IAny & any)218 inline bool IsGetCompatibleWith(const IAny& any)
219 {
220     return IsCompatibleWith<T>(any, CompatibilityDirection::GET);
221 }
222 
223 META_END_NAMESPACE()
224 
225 META_INTERFACE_TYPE(META_NS::IAny)
226 META_INTERFACE_TYPE(META_NS::IArrayAny)
227 
228 #endif
229