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_SERIALIZATION_VALUE_SERIALIZER_H
17 #define META_EXT_SERIALIZATION_VALUE_SERIALIZER_H
18 
19 #include <meta/base/meta_types.h>
20 #include <meta/ext/serialization/value_serializer.h>
21 #include <meta/interface/builtin_objects.h>
22 #include <meta/interface/detail/any.h>
23 #include <meta/interface/interface_helpers.h>
24 #include <meta/interface/intf_object_registry.h>
25 #include <meta/interface/serialization/intf_global_serialization_data.h>
26 #include <meta/interface/serialization/intf_value_serializer.h>
27 
28 META_BEGIN_NAMESPACE()
29 
30 template<typename Type, typename ExportFunc, typename ImportFunc>
31 struct ValueSerializer : IntroduceInterfaces<IValueSerializer> {
ValueSerializerValueSerializer32     ValueSerializer(ExportFunc e, ImportFunc i) : export_(BASE_NS::move(e)), import_(BASE_NS::move(i)) {}
GetTypeIdValueSerializer33     TypeId GetTypeId() const override
34     {
35         return UidFromType<Type>();
36     }
ExportValueSerializer37     ISerNode::Ptr Export(IExportFunctions& f, const IAny& value) override
38     {
39         Type v {};
40         if (value.GetValue(v)) {
41             return export_(f, v);
42         }
43         return nullptr;
44     }
ImportValueSerializer45     IAny::Ptr Import(IImportFunctions& f, const ISerNode::ConstPtr& node) override
46     {
47         Type v {};
48         return import_(f, node, v) ? IAny::Ptr(new Any<Type>(v)) : nullptr;
49     }
50 
51 private:
52     ExportFunc export_;
53     ImportFunc import_;
54 };
55 
56 template<typename Type, typename Export, typename Import>
RegisterSerializer(IGlobalSerializationData & data,Export e,Import i)57 void RegisterSerializer(IGlobalSerializationData& data, Export e, Import i)
58 {
59     data.RegisterValueSerializer(
60         CreateShared<ValueSerializer<Type, Export, Import>>(BASE_NS::move(e), BASE_NS::move(i)));
61 }
62 
63 template<typename Type, typename Export, typename Import>
RegisterSerializer(Export e,Import i)64 void RegisterSerializer(Export e, Import i)
65 {
66     RegisterSerializer<Type>(GetObjectRegistry().GetGlobalSerializationData(), BASE_NS::move(e), BASE_NS::move(i));
67 }
68 
69 template<typename Type>
UnregisterSerializer(IGlobalSerializationData & data)70 void UnregisterSerializer(IGlobalSerializationData& data)
71 {
72     data.UnregisterValueSerializer(UidFromType<Type>());
73 }
74 
75 template<typename Type>
UnregisterSerializer()76 void UnregisterSerializer()
77 {
78     UnregisterSerializer<Type>(GetObjectRegistry().GetGlobalSerializationData());
79 }
80 
81 META_END_NAMESPACE()
82 
83 #endif
84