• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_TEST_TESTING_OBJECTS_HEADER
17 #define META_TEST_TESTING_OBJECTS_HEADER
18 
19 #include <meta/api/make_callback.h>
20 #include <meta/api/object.h>
21 #include <meta/base/time_span.h>
22 #include <meta/base/types.h>
23 #include <meta/interface/interface_macros.h>
24 #include <meta/interface/intf_container.h>
25 #include <meta/interface/intf_startable.h>
26 #include <meta/interface/object_macros.h>
27 #include <meta/interface/property/intf_property.h>
28 #include <meta/interface/property/property_events.h>
29 
META_BEGIN_NAMESPACE()30 META_BEGIN_NAMESPACE()
31 
32 // Allow creating object instances with CreateInstance in addition to CreateObjectInstance
33 inline auto CreateInstance(const META_NS::ClassInfo& clsi)
34 {
35     return META_NS::CreateObjectInstance(clsi);
36 }
37 
38 // compatibility
39 META_REGISTER_CLASS(EmbeddedTestType, "4a3bd491-1111-4a9a-bcad-92530493be09", ObjectCategoryBits::NO_CATEGORY)
40 META_REGISTER_CLASS(TestType, "766fadc7-1767-42fd-a60a-f974e25e6cc9", ObjectCategoryBits::NO_CATEGORY, "Test type")
41 META_REGISTER_CLASS(
42     TestAttachment, "0c45ff57-14f5-4d31-9a93-2e22eea99f75", ObjectCategoryBits::NO_CATEGORY, "Test attachment")
43 META_REGISTER_CLASS(
44     TestStartable, "e9470f5b-97ce-404c-9d1e-b37a4f1ac594", ObjectCategoryBits::NO_CATEGORY, "Test startable")
45 
46 META_REGISTER_CLASS(TestContainer, "bb8fe547-6f70-4552-b269-9bcb55c27f8a", ObjectCategoryBits::NO_CATEGORY)
47 META_REGISTER_CLASS(TestFlatContainer, "cc9fe547-6f70-4552-b269-9bcb55c27f8a", ObjectCategoryBits::NO_CATEGORY)
48 META_REGISTER_CLASS(TestString, "96f87fd0-aabb-4223-8ccf-24662be1dec1", ObjectCategoryBits::NO_CATEGORY)
49 META_REGISTER_CLASS(TestPtrValue, "f77b3c6b-312a-4102-a8ed-a2442b2a8872", ObjectCategoryBits::NO_CATEGORY)
50 
51 // compatibility
52 META_REGISTER_INTERFACE(IEmbeddedTestType, "f66fbf28-ba2c-49dc-8521-c552cb104815")
53 META_REGISTER_INTERFACE(IEmbeddedTestType2, "f77fbf28-ba2c-49dc-8521-c552cb104815", "Some type")
54 
55 META_REGISTER_INTERFACE(ITestType, "7c327d8f-3930-478f-b91b-8ddef8a52f3e", "This is test type")
56 META_REGISTER_INTERFACE(ITestContainer, "456195cf-f539-4572-ba9d-3fd87e75ff08")
57 META_REGISTER_INTERFACE(ITestAttachment, "0af32c00-9037-47f9-b7dd-3e43679ffdfa", "This is test attachment")
58 META_REGISTER_INTERFACE(ITestStartable, "bb8188a9-aeca-4414-98c3-7a989d54d430", "This is test startable")
59 META_REGISTER_INTERFACE(ITestPtrValue, "55a8c04d-ae8f-4dfe-b604-2f31ea07e23b", "This is test type")
60 
61 using BASE_NS::string;
62 
63 struct MyComparableTestType {
64     float i = 0;
65 
66     constexpr bool operator==(float value) const
67     {
68         return i == value;
69     }
70     constexpr bool operator!=(float value) const
71     {
72         return i != value;
73     }
74     constexpr bool operator==(const MyComparableTestType& other) const
75     {
76         return i == other.i;
77     }
78     constexpr bool operator!=(const MyComparableTestType& other) const
79     {
80         return i != other.i;
81     }
82     constexpr bool operator<(const MyComparableTestType& other) const
83     {
84         return i < other.i;
85     }
86     constexpr bool operator>(const MyComparableTestType& other) const
87     {
88         return i > other.i;
89     }
90     constexpr MyComparableTestType& operator*=(float value)
91     {
92         i *= value;
93         return *this;
94     }
95     constexpr MyComparableTestType& operator/=(float value)
96     {
97         i /= value;
98         return *this;
99     }
100     constexpr MyComparableTestType& operator+=(float value)
101     {
102         i += value;
103         return *this;
104     }
105     constexpr MyComparableTestType& operator-=(float value)
106     {
107         i -= value;
108         return *this;
109     }
110     constexpr MyComparableTestType operator*(float value) const
111     {
112         return MyComparableTestType { i * value };
113     }
114     constexpr MyComparableTestType operator/(float value) const
115     {
116         return MyComparableTestType { i / value };
117     }
118     constexpr MyComparableTestType operator-(float value) const
119     {
120         return MyComparableTestType { i - value };
121     }
122     constexpr MyComparableTestType operator+(float value) const
123     {
124         return MyComparableTestType { i + value };
125     }
126     constexpr MyComparableTestType operator*(MyComparableTestType value) const
127     {
128         return MyComparableTestType { i * value.i };
129     }
130     constexpr MyComparableTestType operator/(MyComparableTestType value) const
131     {
132         return MyComparableTestType { i / value.i };
133     }
134     constexpr MyComparableTestType operator-(MyComparableTestType value) const
135     {
136         return MyComparableTestType { i - value.i };
137     }
138     constexpr MyComparableTestType operator+(MyComparableTestType value) const
139     {
140         return MyComparableTestType { i + value.i };
141     }
142 };
143 
144 class IEmbeddedTestType : public CORE_NS::IInterface {
145     META_INTERFACE(CORE_NS::IInterface, IEmbeddedTestType)
146 
147 public:
148     META_PROPERTY(int, Property)
149 };
150 
151 class ITestAttachment : public CORE_NS::IInterface {
152     META_INTERFACE(CORE_NS::IInterface, ITestAttachment)
153 
154 public:
155     META_PROPERTY(string, Name)
156     virtual void SetName(const BASE_NS::string& name) = 0;
157     virtual uint32_t GetAttachCount() const = 0;
158     virtual uint32_t GetDetachCount() const = 0;
159 };
160 
161 class ITestStartable : public ITestAttachment {
162     META_INTERFACE(ITestAttachment, ITestStartable)
163 
164 public:
165     enum class Operation : uint32_t {
166         ATTACH,
167         START,
168         STOP,
169         DETACH,
170         TICK,
171     };
172 
173     virtual void StartRecording() = 0;
174     virtual BASE_NS::vector<Operation> StopRecording() = 0;
175     virtual BASE_NS::vector<Operation> GetOps() const = 0;
176     virtual TimeSpan GetLastTick() const = 0;
177 
META_EVENT(IOnChanged,OnStarted)178     META_EVENT(IOnChanged, OnStarted)
179     META_EVENT(IOnChanged, OnStopped)
180     META_EVENT(IOnChanged, OnTicked)
181 
182     template<class Callback>
183     void AddOnStarted(Callback&& callback)
184     {
185         OnStarted()->AddHandler(MakeCallback<IOnChanged>(callback));
186     }
187     template<class Callback>
AddOnStopped(Callback && callback)188     void AddOnStopped(Callback&& callback)
189     {
190         OnStopped()->AddHandler(MakeCallback<IOnChanged>(callback));
191     }
192     template<class Callback>
AddOnTicked(Callback && callback)193     void AddOnTicked(Callback&& callback)
194     {
195         OnTicked()->AddHandler(MakeCallback<IOnChanged>(callback));
196     }
197 };
198 
199 std::ostream& operator<<(std::ostream& os, const META_NS::ITestStartable::Operation& op);
200 
201 class ITestType : public CORE_NS::IInterface {
202     META_INTERFACE(CORE_NS::IInterface, ITestType)
203 
204 public:
205     META_PROPERTY(int, First)
206     META_PROPERTY(string, Second)
207     META_READONLY_PROPERTY(int, Third)
208     META_READONLY_PROPERTY(string, Fourth)
209 
210     META_PROPERTY(BASE_NS::Math::Vec3, Vec3Property1)
211     META_PROPERTY(BASE_NS::Math::Vec3, Vec3Property2)
212     META_PROPERTY(BASE_NS::Math::Vec3, Vec3Property3)
213 
214     META_PROPERTY(float, FloatProperty1)
215     META_PROPERTY(float, FloatProperty2)
216 
217     META_PROPERTY(MyComparableTestType, MyTestTypeProperty1)
218     META_PROPERTY(MyComparableTestType, MyTestTypeProperty2)
219 
220     META_PROPERTY(IEmbeddedTestType::Ptr, EmbeddedTestTypeProperty)
221 
222     META_ARRAY_PROPERTY(int, MyIntArray)
223     META_READONLY_ARRAY_PROPERTY(int, MyConstIntArray)
224 
225     META_ARRAY_PROPERTY(IObject::Ptr, MyObjectArray)
226     //    META_PROPERTY(int, NonSerialized)
227 
228     META_PROPERTY(string, Name)
229     virtual void SetName(const BASE_NS::string& name) = 0;
230 
231     META_EVENT(IOnChanged, OnTest)
232 
233     virtual int NormalMember() = 0;
234     virtual int OtherNormalMember(int, int) const = 0;
235 };
236 
237 class ITestContainer : public CORE_NS::IInterface {
238     META_INTERFACE(CORE_NS::IInterface, ITestContainer)
239 
240 public:
241     META_PROPERTY(string, Name)
242     virtual void SetName(const BASE_NS::string& name) = 0;
243 
244     virtual void Increment() = 0;
245     virtual int GetIncrement() const = 0;
246 };
247 
248 class ITestString : public CORE_NS::IInterface {
249     META_INTERFACE(CORE_NS::IInterface, ITestString, "3f05057d-95b3-4599-a88d-5983287a48e6")
250 
251 public:
252     virtual BASE_NS::string GetString() const = 0;
253     virtual void SetString(BASE_NS::string) = 0;
254 };
255 
256 class ITestPtrValue : public CORE_NS::IInterface {
257     META_INTERFACE(CORE_NS::IInterface, ITestPtrValue)
258 
259 public:
260     META_PROPERTY(ITestString::Ptr, Text)
261     META_PROPERTY(ITestString::Ptr, String)
262 };
263 
264 void RegisterTestTypes();
265 void UnregisterTestTypes();
266 ITestType::Ptr CreateTestType(const BASE_NS::string_view name = {});
267 ITestContainer::Ptr CreateTestContainer(
268     const BASE_NS::string_view name = {}, ClassInfo type = META_NS::ClassId::TestContainer);
269 ITestAttachment::Ptr CreateTestAttachment(const BASE_NS::string_view name = {});
270 ITestStartable::Ptr CreateTestStartable(
271     const BASE_NS::string_view name = {}, StartBehavior behavior = StartBehavior::MANUAL);
272 
273 template<class T>
274 typename T::Ptr CreateTestType(const BASE_NS::string_view name = {})
275 {
276     return interface_pointer_cast<T>(CreateTestType(name));
277 }
278 
279 template<class T>
280 typename T::Ptr CreateTestContainer(
281     const BASE_NS::string_view name = {}, ClassInfo type = META_NS::ClassId::TestContainer)
282 {
283     return interface_pointer_cast<T>(CreateTestContainer(name, type));
284 }
285 
286 template<class T>
287 typename T::Ptr CreateTestAttachment(
288     const BASE_NS::string_view name = {}, ClassInfo type = META_NS::ClassId::TestContainer)
289 {
290     return interface_pointer_cast<T>(CreateTestAttachment(name));
291 }
292 
293 template<class T>
294 typename T::Ptr CreateTestStartable(
295     const BASE_NS::string_view name = {}, ClassInfo type = META_NS::ClassId::TestStartable)
296 {
297     return interface_pointer_cast<T>(CreateTestStartable(name));
298 }
299 
300 struct MyTestType {
301     int i = 0;
302 };
303 
304 enum TestEnum { TestEnumA = 1, TestEnumB = 2 };
305 
306 META_END_NAMESPACE()
307 
308 #define META_INTERFACE_TYPE_IMP(a, n)           \
309     META_TYPE_IMPL(a::Ptr, n "::Ptr")           \
310     META_TYPE_IMPL(a::ConstPtr, n "::ConstPtr") \
311     META_TYPE_IMPL(a::WeakPtr, n "::WeakPtr")   \
312     META_TYPE_IMPL(a::ConstWeakPtr, n "::ConstWeakPtr")
313 
314 META_INTERFACE_TYPE_IMP(META_NS::ITestString, "META_NS::Test::ITestString")
315 META_INTERFACE_TYPE_IMP(META_NS::ITestPtrValue, "META_NS::Test::ITestPtrValue")
316 META_INTERFACE_TYPE_IMP(META_NS::ITestType, "META_NS::Test::ITestType")
317 META_INTERFACE_TYPE_IMP(META_NS::IEmbeddedTestType, "META_NS::Test::IEmbeddedTestType")
318 META_TYPE_IMPL(META_NS::MyTestType, "META_NS::Test::MyTestType")
319 META_TYPE_IMPL(META_NS::MyComparableTestType, "META_NS::Test::MyComparableTestType")
320 META_TYPE_IMPL(META_NS::TestEnum, "META_NS::Test::TestEnum")
321 
322 #endif // META_TEST_TESTING_OBJECTS_HEADER
323