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_TEST_SERIALISATION_UTILS_HEADER
17 #define META_TEST_SERIALISATION_UTILS_HEADER
18 
19 #include <base/containers/vector.h>
20 #include <core/io/intf_file.h>
21 #include <core/log.h>
22 
23 #include <meta/interface/intf_object.h>
24 #include <meta/interface/serialization/intf_global_serialization_data.h>
25 
26 META_BEGIN_NAMESPACE()
27 
28 struct MemFile : public CORE_NS::IFile {
29     explicit MemFile(BASE_NS::vector<char> vec = {});
30 
31     Mode GetMode() const override;
32     void Close() override;
33     uint64_t Read(void* buffer, uint64_t count) override;
34     uint64_t Write(const void* buffer, uint64_t count) override;
35     uint64_t GetLength() const override;
36     bool Seek(uint64_t offset) override;
37     uint64_t GetPosition() const override;
38 
39     BASE_NS::vector<char> Data() const;
40 private:
41     void Destroy() override;
42     BASE_NS::vector<char> data_;
43     size_t pos_ {};
44 };
45 
46 struct TestSerialiser {
47     explicit TestSerialiser(BASE_NS::vector<char> data = {});
48 
49     bool Export(const IObject::Ptr& object);
50     IObject::Ptr Import();
51 
52     template<typename Interface>
ExportTestSerialiser53     bool Export(const BASE_NS::shared_ptr<Interface>& p)
54     {
55         return Export(interface_pointer_cast<IObject>(p));
56     }
57 
58     template<typename Interface>
ImportTestSerialiser59     typename Interface::Ptr Import()
60     {
61         return interface_pointer_cast<Interface>(Import());
62     }
63 
64     bool LoadFile(BASE_NS::string_view path);
65     BASE_NS::string Get() const;
66 
67     void SetSerializationSettings(SerializationSettings s);
68 
69 private:
70     MemFile data_;
71     std::size_t writePos_ {};
72     std::size_t readPos_ {};
73 };
74 
75 void WriteToFile(const BASE_NS::vector<char>&, BASE_NS::string_view file);
76 
77 META_END_NAMESPACE()
78 
79 #endif