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