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_EXT_ANY_BUILDER_H 17 #define META_EXT_ANY_BUILDER_H 18 19 #include <meta/base/meta_types.h> 20 #include <meta/interface/detail/any.h> 21 #include <meta/interface/intf_object_registry.h> 22 #include <meta/interface/property/intf_property_register.h> 23 META_BEGIN_NAMESPACE()24META_BEGIN_NAMESPACE() 25 26 /// The default version of any builder that is used for registering any types 27 template<typename AnyType> 28 class DefaultAnyBuilder : public AnyBuilder { 29 public: 30 IAny::Ptr Construct() override 31 { 32 return IAny::Ptr(new AnyType); 33 } 34 ObjectId GetObjectId() const override 35 { 36 return AnyType::StaticGetClassId(); 37 } 38 BASE_NS::string GetTypeName() const override 39 { 40 return BASE_NS::string(AnyType::StaticGetTypeIdString()); 41 } 42 }; 43 44 /// Register user defined any type 45 template<typename AnyType> RegisterUserAny()46void RegisterUserAny() 47 { 48 GetObjectRegistry().GetPropertyRegister().RegisterAny(CreateShared<DefaultAnyBuilder<AnyType>>()); 49 } 50 51 /// Register user type using built-in any type 52 template<typename Type> RegisterTypeForBuiltinAny()53void RegisterTypeForBuiltinAny() 54 { 55 GetObjectRegistry().GetPropertyRegister().RegisterAny(CreateShared<DefaultAnyBuilder<AnyType<Type>>>()); 56 } 57 58 /// Register user type using built-in array any type 59 template<typename Type> RegisterTypeForBuiltinArrayAny()60void RegisterTypeForBuiltinArrayAny() 61 { 62 GetObjectRegistry().GetPropertyRegister().RegisterAny(CreateShared<DefaultAnyBuilder<ArrayAnyType<Type>>>()); 63 } 64 65 /// Unregister, counter part for RegisterUserAny 66 template<typename Type> UnregisterUserAny()67void UnregisterUserAny() 68 { 69 GetObjectRegistry().GetPropertyRegister().UnregisterAny(Type::StaticGetClassId()); 70 } 71 72 /// Unregister, counter part for RegisterTypeForBuiltinAny 73 template<typename Type> UnregisterTypeForBuiltinAny()74void UnregisterTypeForBuiltinAny() 75 { 76 GetObjectRegistry().GetPropertyRegister().UnregisterAny(AnyType<Type>::StaticGetClassId()); 77 } 78 79 /// Unregister, counter part for RegisterTypeForBuiltinArrayAny 80 template<typename Type> UnregisterTypeForBuiltinArrayAny()81void UnregisterTypeForBuiltinArrayAny() 82 { 83 GetObjectRegistry().GetPropertyRegister().UnregisterAny(ArrayAnyType<Type>::StaticGetClassId()); 84 } 85 86 META_END_NAMESPACE() 87 88 #endif 89