• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_TEST_SERIALISATION_UTILS_HEADER
17 #define META_TEST_SERIALISATION_UTILS_HEADER
18 
19 #include <base/containers/unordered_map.h>
20 #include <base/containers/vector.h>
21 #include <core/io/intf_file.h>
22 #include <core/log.h>
23 #include <core/resources/intf_resource_manager.h>
24 
25 #include <meta/ext/object.h>
26 #include <meta/interface/intf_object.h>
27 #include <meta/interface/serialization/intf_global_serialization_data.h>
28 
29 META_BEGIN_NAMESPACE()
30 
31 struct MemFile : public CORE_NS::IFile {
32     explicit MemFile(BASE_NS::vector<uint8_t> vec = {});
33 
34     Mode GetMode() const override;
35     void Close() override;
36     uint64_t Read(void* buffer, uint64_t count) override;
37     uint64_t Write(const void* buffer, uint64_t count) override;
38     uint64_t Append(const void* buffer, uint64_t count, uint64_t flushSize) override;
39     uint64_t GetLength() const override;
40     bool Seek(uint64_t offset) override;
41     uint64_t GetPosition() const override;
42 
43     BASE_NS::vector<uint8_t> Data() const;
44 
45 private:
46     void Destroy() override;
47     BASE_NS::vector<uint8_t> data_;
48     size_t pos_ {};
49 };
50 
51 struct TestSerialiser {
52     explicit TestSerialiser(BASE_NS::vector<uint8_t> data = {});
53 
54     bool Export(const IObject::Ptr& object);
55     IObject::Ptr Import();
56 
57     template<typename Interface>
ExportTestSerialiser58     bool Export(const BASE_NS::shared_ptr<Interface>& p)
59     {
60         return Export(interface_pointer_cast<IObject>(p));
61     }
62 
63     template<typename Interface>
ImportTestSerialiser64     typename Interface::Ptr Import()
65     {
66         return interface_pointer_cast<Interface>(Import());
67     }
68 
69     bool LoadFile(BASE_NS::string_view path);
70     void Dump(BASE_NS::string_view file);
71     BASE_NS::string Get() const;
72     BASE_NS::vector<uint8_t> GetData() const;
73 
74     void SetSerializationSettings(SerializationSettings s);
75 
76     BASE_NS::unordered_map<InstanceId, InstanceId> GetInstanceIdMapping() const;
77     void SetInstanceIdMapping(BASE_NS::unordered_map<InstanceId, InstanceId>);
78 
79     void SetResourceManager(CORE_NS::IResourceManager::Ptr);
80 
81     void SetMetadata(SerMetadata);
82     SerMetadata GetMetadata() const;
83 
84 private:
85     MemFile data_;
86     std::size_t writePos_ {};
87     std::size_t readPos_ {};
88     BASE_NS::unordered_map<InstanceId, InstanceId> mapping_;
89     CORE_NS::IResourceManager::Ptr resources_;
90     SerMetadata metadata_;
91 };
92 
93 void WriteToFile(const BASE_NS::vector<uint8_t>&, BASE_NS::string_view file);
94 
95 META_END_NAMESPACE()
96 
97 #endif // META_TEST_SERIALISATION_UTILS_HEADER
98