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 #include "serialisation_utils.h"
17
18 #include <algorithm>
19 #include <cstring>
20
21 #include <core/io/intf_file_manager.h>
22
23 #include <meta/interface/builtin_objects.h>
24 #include <meta/interface/intf_object_registry.h>
25 #include <meta/interface/serialization/intf_exporter.h>
26 #include <meta/interface/serialization/intf_importer.h>
27
28 META_BEGIN_NAMESPACE()
29
MemFile(BASE_NS::vector<char> vec)30 MemFile::MemFile(BASE_NS::vector<char> vec) : data_(BASE_NS::move(vec)) {}
31
GetMode() const32 MemFile::Mode MemFile::GetMode() const
33 {
34 return Mode::READ_WRITE;
35 }
Close()36 void MemFile::Close()
37 {
38 data_.clear();
39 }
Read(void* buffer, uint64_t count)40 uint64_t MemFile::Read(void* buffer, uint64_t count)
41 {
42 size_t read = std::min<size_t>(count, data_.size() - pos_);
43 pos_ += read;
44 return read;
45 }
Write(const void* buffer, uint64_t count)46 uint64_t MemFile::Write(const void* buffer, uint64_t count)
47 {
48 if (data_.size() < pos_ + count) {
49 data_.resize(pos_ + count);
50 }
51 pos_ += count;
52 return count;
53 }
GetLength() const54 uint64_t MemFile::GetLength() const
55 {
56 return data_.size();
57 }
Seek(uint64_t offset)58 bool MemFile::Seek(uint64_t offset)
59 {
60 bool ret = offset < data_.size();
61 if (ret) {
62 pos_ = offset;
63 }
64 return ret;
65 }
66 uint64_t MemFile::GetPosition() const
67 {
68 return pos_;
69 }
70 BASE_NS::vector<char> MemFile::Data() const
71 {
72 return data_;
73 }
74 void MemFile::Destroy()
75 {
76 delete this;
77 }
78
79 TestSerialiser::TestSerialiser(BASE_NS::vector<char> vec) : data_(BASE_NS::move(vec)) {}
80
81 bool TestSerialiser::Export(const IObject::Ptr& object)
82 {
83 data_.Seek(writePos_);
84 auto exporter = GetObjectRegistry().Create<IFileExporter>(META_NS::ClassId::JsonExporter);
85 bool ret = exporter->Export(data_, object);
86 writePos_ = data_.GetPosition();
87 return ret;
88 }
89
SetSerializationSettings(SerializationSettings s)90 void TestSerialiser::SetSerializationSettings(SerializationSettings s)
91 {
92 auto& ctx = GetObjectRegistry().GetGlobalSerializationData();
93 ctx.SetDefaultSettings(s);
94 }
95
Import()96 IObject::Ptr TestSerialiser::Import()
97 {
98 data_.Seek(readPos_);
99 auto importer = GetObjectRegistry().Create<IFileImporter>(META_NS::ClassId::JsonImporter);
100 IObject::Ptr result = importer->Import(data_);
101 readPos_ = data_.GetPosition();
102 return result;
103 }
104
LoadFile(BASE_NS::string_view path)105 bool TestSerialiser::LoadFile(BASE_NS::string_view path)
106 {
107 auto f = CORE_NS::GetPluginRegister().GetFileManager().OpenFile(path);
108 if (!f) {
109 return false;
110 }
111
112 BASE_NS::vector<char> vec;
113 vec.resize(f->GetLength());
114 f->Read(vec.data(), vec.size());
115 data_ = MemFile { BASE_NS::move(vec) };
116 return true;
117 }
118
Dump(BASE_NS::string_view file)119 void TestSerialiser::Dump(BASE_NS::string_view file)
120 {
121 WriteToFile(data_.Data(), file);
122 }
123
Get() const124 BASE_NS::string TestSerialiser::Get() const
125 {
126 return BASE_NS::string(BASE_NS::string_view(data_.Data().data(), data_.Data().size()));
127 }
128
WriteToFile(const BASE_NS::vector<char>& vec, BASE_NS::string_view file)129 void WriteToFile(const BASE_NS::vector<char>& vec, BASE_NS::string_view file)
130 {
131 auto f = CORE_NS::GetPluginRegister().GetFileManager().CreateFile(file);
132 if (f) {
133 f->Write(vec.data(), vec.size());
134 f->Close();
135 }
136 }
137
138 META_END_NAMESPACE()