11
11
12
12
#include " flutter/fml/macros.h"
13
13
#include " impeller/archivist/archivable.h"
14
- #include " impeller/base/allocation.h"
15
14
16
15
namespace impeller {
17
16
18
17
class ArchiveLocation ;
19
- class ArchiveClassRegistration ;
20
18
class ArchiveDatabase ;
21
- class ArchiveStatement ;
22
-
23
- struct ArchiveDef {
24
- using Member = uint64_t ;
25
- using Members = std::vector<Member>;
26
-
27
- const ArchiveDef* isa = nullptr ;
28
- const std::string table_name;
29
- const bool auto_key = true ;
30
- const Members members;
31
- };
32
19
33
20
static const Archivable::ArchiveName ArchiveNameAuto = 0 ;
34
21
@@ -38,7 +25,7 @@ class Archive {
38
25
39
26
~Archive ();
40
27
41
- bool IsReady () const ;
28
+ bool IsValid () const ;
42
29
43
30
template <class T ,
44
31
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
@@ -83,182 +70,4 @@ class Archive {
83
70
FML_DISALLOW_COPY_AND_ASSIGN (Archive);
84
71
};
85
72
86
- class ArchiveLocation {
87
- public:
88
- template <class T , class = std::enable_if<std::is_integral<T>::value>>
89
- bool Write (ArchiveDef::Member member, T item) {
90
- return WriteIntegral (member, static_cast <int64_t >(item));
91
- }
92
-
93
- bool Write (ArchiveDef::Member member, double item);
94
-
95
- bool Write (ArchiveDef::Member member, const std::string& item);
96
-
97
- bool Write (ArchiveDef::Member member, const Allocation& allocation);
98
-
99
- template <class T ,
100
- class = std::enable_if<std::is_base_of<Archivable, T>::value>>
101
- bool WriteArchivable (ArchiveDef::Member member, const T& other) {
102
- const ArchiveDef& otherDef = T::ArchiveDefinition;
103
- return Write (member, otherDef, other);
104
- }
105
-
106
- template <class T , class = std::enable_if<std::is_enum<T>::value>>
107
- bool WriteEnum (ArchiveDef::Member member, const T& item) {
108
- return WriteIntegral (member, static_cast <int64_t >(item));
109
- }
110
-
111
- template <class T ,
112
- class = std::enable_if<std::is_base_of<Archivable, T>::value>>
113
- bool Write (ArchiveDef::Member member, const std::vector<T>& items) {
114
- /*
115
- * All items in the vector are individually encoded and their keys noted
116
- */
117
- std::vector<int64_t > members;
118
- members.reserve (items.size ());
119
-
120
- const ArchiveDef& itemDefinition = T::ArchiveDefinition;
121
- for (const auto & item : items) {
122
- int64_t added = 0 ;
123
- bool result = context_.ArchiveInstance (itemDefinition, item, added);
124
- if (!result) {
125
- return false ;
126
- }
127
- members.emplace_back (added);
128
- }
129
-
130
- /*
131
- * The keys are flattened into the vectors table. Write to that table
132
- */
133
- auto vectorInsert = WriteVectorKeys (std::move (members));
134
-
135
- if (!vectorInsert.first ) {
136
- return false ;
137
- }
138
-
139
- return WriteIntegral (member, vectorInsert.second );
140
- }
141
-
142
- template <class Super ,
143
- class Current ,
144
- class = std::enable_if<std::is_base_of<Archivable, Super>::value &&
145
- std::is_base_of<Archivable, Current>::value>>
146
- bool WriteSuper (const Current& thiz) {
147
- std::string oldClass = current_class_;
148
- current_class_ = Super::ArchiveDefinition.className ;
149
- auto success = thiz.Super ::serialize (*this );
150
- current_class_ = oldClass;
151
- return success;
152
- }
153
-
154
- template <class T , class = std::enable_if<std::is_integral<T>::value>>
155
- bool Read (ArchiveDef::Member member, T& item) {
156
- int64_t decoded = 0 ;
157
- auto result = ReadIntegral (member, decoded);
158
- item = static_cast <T>(decoded);
159
- return result;
160
- }
161
-
162
- bool Read (ArchiveDef::Member member, double & item);
163
-
164
- bool Read (ArchiveDef::Member member, std::string& item);
165
-
166
- bool Read (ArchiveDef::Member member, Allocation& allocation);
167
-
168
- template <class T ,
169
- class = std::enable_if<std::is_base_of<Archivable, T>::value>>
170
- bool ReadArchivable (ArchiveDef::Member member, T& other) {
171
- const ArchiveDef& otherDef = T::ArchiveDefinition;
172
- return decode (member, otherDef, other);
173
- }
174
-
175
- template <class T , class = std::enable_if<std::is_enum<T>::value>>
176
- bool ReadEnum (ArchiveDef::Member member, T& item) {
177
- int64_t desugared = 0 ;
178
- if (ReadIntegral (member, desugared)) {
179
- item = static_cast <T>(desugared);
180
- return true ;
181
- }
182
- return false ;
183
- }
184
-
185
- template <class T ,
186
- class = std::enable_if<std::is_base_of<Archivable, T>::value>>
187
- bool Read (ArchiveDef::Member member, std::vector<T>& items) {
188
- /*
189
- * From the member, find the foreign key of the vector
190
- */
191
- int64_t vectorForeignKey = 0 ;
192
- if (!ReadIntegral (member, vectorForeignKey)) {
193
- return false ;
194
- }
195
-
196
- /*
197
- * Get vector keys
198
- */
199
- std::vector<int64_t > keys;
200
- if (!ReadVectorKeys (vectorForeignKey, keys)) {
201
- return false ;
202
- }
203
-
204
- const ArchiveDef& otherDef = T::ArchiveDefinition;
205
- for (const auto & key : keys) {
206
- items.emplace_back ();
207
-
208
- if (!context_.UnarchiveInstance (otherDef, key, items.back ())) {
209
- return false ;
210
- }
211
- }
212
-
213
- return true ;
214
- }
215
-
216
- template <class Super ,
217
- class Current ,
218
- class = std::enable_if<std::is_base_of<Archivable, Super>::value &&
219
- std::is_base_of<Archivable, Current>::value>>
220
- bool ReadSuper (Current& thiz) {
221
- std::string oldClass = current_class_;
222
- current_class_ = Super::ArchiveDefinition.className ;
223
- auto success = thiz.Super ::deserialize (*this );
224
- current_class_ = oldClass;
225
- return success;
226
- }
227
-
228
- Archivable::ArchiveName Name () const ;
229
-
230
- private:
231
- Archive& context_;
232
- ArchiveStatement& statement_;
233
- const ArchiveClassRegistration& registration_;
234
- Archivable::ArchiveName name_;
235
- std::string current_class_;
236
-
237
- friend class Archive ;
238
-
239
- ArchiveLocation (Archive& context,
240
- ArchiveStatement& statement,
241
- const ArchiveClassRegistration& registration,
242
- Archivable::ArchiveName name);
243
-
244
- bool WriteIntegral (ArchiveDef::Member member, int64_t item);
245
-
246
- bool ReadIntegral (ArchiveDef::Member member, int64_t & item);
247
-
248
- std::pair<bool , int64_t > WriteVectorKeys (std::vector<int64_t >&& members);
249
-
250
- bool ReadVectorKeys (Archivable::ArchiveName name,
251
- std::vector<int64_t >& members);
252
-
253
- bool Write (ArchiveDef::Member member,
254
- const ArchiveDef& otherDef,
255
- const Archivable& other);
256
-
257
- bool Read (ArchiveDef::Member member,
258
- const ArchiveDef& otherDef,
259
- Archivable& other);
260
-
261
- FML_DISALLOW_COPY_AND_ASSIGN (ArchiveLocation);
262
- };
263
-
264
73
} // namespace impeller
0 commit comments