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 template<typename AnyType> 27 class DefaultAnyBuilder : public AnyBuilder { 28 public: 29 IAny::Ptr Construct() override 30 { 31 return IAny::Ptr(new AnyType); 32 } 33 ObjectId GetObjectId() const override 34 { 35 return AnyType::StaticGetClassId(); 36 } 37 BASE_NS::string GetTypeName() const override 38 { 39 return BASE_NS::string(AnyType::StaticGetTypeIdString()); 40 } 41 }; 42 43 template<typename AnyType> RegisterUserAny()44void RegisterUserAny() 45 { 46 GetObjectRegistry().GetPropertyRegister().RegisterAny(CreateShared<DefaultAnyBuilder<AnyType>>()); 47 } 48 49 template<typename Type> RegisterTypeForBuiltinAny()50void RegisterTypeForBuiltinAny() 51 { 52 GetObjectRegistry().GetPropertyRegister().RegisterAny(CreateShared<DefaultAnyBuilder<Any<Type>>>()); 53 } 54 55 template<typename Type> RegisterTypeForBuiltinArrayAny()56void RegisterTypeForBuiltinArrayAny() 57 { 58 GetObjectRegistry().GetPropertyRegister().RegisterAny(CreateShared<DefaultAnyBuilder<ArrayAny<Type>>>()); 59 } 60 61 template<typename Type> UnregisterUserAny()62void UnregisterUserAny() 63 { 64 GetObjectRegistry().GetPropertyRegister().UnregisterAny(Type::StaticGetClassId()); 65 } 66 67 template<typename Type> UnregisterTypeForBuiltinAny()68void UnregisterTypeForBuiltinAny() 69 { 70 GetObjectRegistry().GetPropertyRegister().UnregisterAny(Any<Type>::StaticGetClassId()); 71 } 72 73 template<typename Type> UnregisterTypeForBuiltinArrayAny()74void UnregisterTypeForBuiltinArrayAny() 75 { 76 GetObjectRegistry().GetPropertyRegister().UnregisterAny(ArrayAny<Type>::StaticGetClassId()); 77 } 78 79 META_END_NAMESPACE() 80 81 #endif 82