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 OHOS_IDL_METADATASERIALIZER_H
17 #define OHOS_IDL_METADATASERIALIZER_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 
22 #include "metadata/meta_component.h"
23 #include "metadata/meta_interface.h"
24 #include "metadata/meta_method.h"
25 #include "metadata/meta_namespace.h"
26 #include "metadata/meta_patameter.h"
27 #include "metadata/meta_sequenceable.h"
28 #include "metadata/meta_type.h"
29 
30 
31 namespace OHOS {
32 namespace Idl {
33 class MetadataSerializer {
34 public:
MetadataSerializer(MetaComponent * mc)35     explicit MetadataSerializer(MetaComponent* mc)
36         : metaComponent_(mc),
37           baseAddr_(reinterpret_cast<uintptr_t>(mc))
38     {}
39 
MetadataSerializer(uintptr_t addr)40     explicit MetadataSerializer(uintptr_t addr)
41         : metaComponent_(reinterpret_cast<MetaComponent*>(addr)),
42           baseAddr_(addr)
43     {}
44 
45     ~MetadataSerializer() = default;
46 
47     void Serialize();
48 
49     void Deserialize();
50 
GetData()51     uintptr_t GetData() const
52     {
53         return baseAddr_;
54     }
55 
GetDataSize()56     int GetDataSize() const
57     {
58         return metaComponent_->size_;
59     }
60 
61 private:
62     void SerializeMetaComponent(MetaComponent* mc);
63 
64     void SerializeMetaNamespace(MetaNamespace* mn);
65 
66     void SerializeMetaSequenceable(MetaSequenceable* mp);
67 
68     void SerializeMetaInterface(MetaInterface* mi);
69 
70     void SerializeMetaMethod(MetaMethod* mm);
71 
72     void SerializeMetaParameter(MetaParameter* mp);
73 
74     void SerializeMetaType(MetaType* mt);
75 
76     ptrdiff_t SerializeAdjust(const void* addr);
77 
78     void DeserializeMetaComponent(MetaComponent* mc);
79 
80     void DeserializeMetaNamespace(MetaNamespace* mn);
81 
82     void DeserializeMetaSequenceable(MetaSequenceable* mp);
83 
84     void DeserializeMetaInterface(MetaInterface* mi);
85 
86     void DeserializeMetaMethod(MetaMethod* mm);
87 
88     void DeserializeMetaParameter(MetaParameter* mp);
89 
90     void DeserializeMetaType(MetaType* mt);
91 
92     uintptr_t DeserializeAdjust(const void* addr);
93 
94     MetaComponent* metaComponent_;
95     uintptr_t baseAddr_;
96 };
97 } // namespace Idl
98 } // namespace OHOS
99 #endif // OHOS_IDL_METADATASERIALIZER_H
100