• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /// Helper class to use export/import functions to construct value serializer
31 template<typename Type, typename ExportFunc, typename ImportFunc>
32 struct ValueSerializer : IntroduceInterfaces<IValueSerializer> {
ValueSerializerValueSerializer33     ValueSerializer(ExportFunc e, ImportFunc i) : export_(BASE_NS::move(e)), import_(BASE_NS::move(i)) {}
GetTypeIdValueSerializer34     TypeId GetTypeId() const override
35     {
36         return UidFromType<Type>();
37     }
ExportValueSerializer38     ISerNode::Ptr Export(IExportFunctions& f, const IAny& value) override
39     {
40         Type v {};
41         if (value.GetValue(v)) {
42             return export_(f, v);
43         }
44         return nullptr;
45     }
ImportValueSerializer46     IAny::Ptr Import(IImportFunctions& f, const ISerNode::ConstPtr& node) override
47     {
48         Type v {};
49         return import_(f, node, v) ? IAny::Ptr(new Any<Type>(v)) : nullptr;
50     }
51 
52 private:
53     ExportFunc export_;
54     ImportFunc import_;
55 };
56 
57 template<typename Type, typename Export, typename Import>
RegisterSerializer(IGlobalSerializationData & data,Export e,Import i)58 void RegisterSerializer(IGlobalSerializationData& data, Export e, Import i)
59 {
60     data.RegisterValueSerializer(
61         CreateShared<ValueSerializer<Type, Export, Import>>(BASE_NS::move(e), BASE_NS::move(i)));
62 }
63 
64 template<typename Type, typename Export, typename Import>
RegisterSerializer(Export e,Import i)65 void RegisterSerializer(Export e, Import i)
66 {
67     RegisterSerializer<Type>(GetObjectRegistry().GetGlobalSerializationData(), BASE_NS::move(e), BASE_NS::move(i));
68 }
69 
70 template<typename Type>
UnregisterSerializer(IGlobalSerializationData & data)71 void UnregisterSerializer(IGlobalSerializationData& data)
72 {
73     data.UnregisterValueSerializer(UidFromType<Type>());
74 }
75 
76 template<typename Type>
UnregisterSerializer()77 void UnregisterSerializer()
78 {
79     UnregisterSerializer<Type>(GetObjectRegistry().GetGlobalSerializationData());
80 }
81 
82 META_END_NAMESPACE()
83 
84 #endif
85