Flutter Impeller
archive_location.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
9 
10 namespace impeller {
11 
12 ArchiveLocation::ArchiveLocation(Archive& context,
13  ArchiveStatement& statement,
14  const ArchiveClassRegistration& registration,
15  PrimaryKey name)
16  : context_(context),
17  statement_(statement),
18  registration_(registration),
19  primary_key_(name) {}
20 
22  return primary_key_;
23 }
24 
25 bool ArchiveLocation::Write(const std::string& member,
26  const std::string& item) {
27  auto index = registration_.FindColumnIndex(member);
28  return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
29 }
30 
31 bool ArchiveLocation::WriteIntegral(const std::string& member, int64_t item) {
32  auto index = registration_.FindColumnIndex(member);
33  return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
34 }
35 
36 bool ArchiveLocation::Write(const std::string& member, double item) {
37  auto index = registration_.FindColumnIndex(member);
38  return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
39 }
40 
41 bool ArchiveLocation::Write(const std::string& member, const Allocation& item) {
42  auto index = registration_.FindColumnIndex(member);
43  return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
44 }
45 
46 bool ArchiveLocation::Write(const std::string& member,
47  const ArchiveDef& otherDef,
48  const Archivable& other) {
49  auto index = registration_.FindColumnIndex(member);
50 
51  if (!index.has_value()) {
52  return false;
53  }
54 
55  /*
56  * We need to fully archive the other instance first because it could
57  * have a name that is auto assigned. In that case, we cannot ask it before
58  * archival (via `other.archiveName()`).
59  */
60  auto row_id = context_.ArchiveInstance(otherDef, other);
61  if (!row_id.has_value()) {
62  return false;
63  }
64 
65  /*
66  * Bind the name of the serializable
67  */
68  if (!statement_.WriteValue(index.value(), row_id.value())) {
69  return false;
70  }
71 
72  return true;
73 }
74 
75 std::optional<int64_t> ArchiveLocation::WriteVectorKeys(
76  std::vector<int64_t>&& members) {
77  ArchiveVector vector(std::move(members));
78  return context_.ArchiveInstance(ArchiveVector::kArchiveDefinition, vector);
79 }
80 
81 bool ArchiveLocation::ReadVectorKeys(PrimaryKey name,
82  std::vector<int64_t>& members) {
83  ArchiveVector vector;
84  if (!context_.UnarchiveInstance(ArchiveVector::kArchiveDefinition, name,
85  vector)) {
86  return false;
87  }
88  const auto& keys = vector.GetKeys();
89  std::move(keys.begin(), keys.end(), std::back_inserter(members));
90  return true;
91 }
92 
93 bool ArchiveLocation::Read(const std::string& member, std::string& item) {
94  auto index = registration_.FindColumnIndex(member);
95  return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
96 }
97 
98 bool ArchiveLocation::ReadIntegral(const std::string& member, int64_t& item) {
99  auto index = registration_.FindColumnIndex(member);
100  return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
101 }
102 
103 bool ArchiveLocation::Read(const std::string& member, double& item) {
104  auto index = registration_.FindColumnIndex(member);
105  return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
106 }
107 
108 bool ArchiveLocation::Read(const std::string& member, Allocation& item) {
109  auto index = registration_.FindColumnIndex(member);
110  return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
111 }
112 
113 bool ArchiveLocation::Read(const std::string& member,
114  const ArchiveDef& otherDef,
115  Archivable& other) {
116  auto index = registration_.FindColumnIndex(member);
117 
118  /*
119  * Make sure a member is present at that column
120  */
121  if (!index.has_value()) {
122  return false;
123  }
124 
125  /*
126  * Try to find the foreign key in the current items row
127  */
128  int64_t foreignKey = 0;
129  if (!statement_.ReadValue(index.value(), foreignKey)) {
130  return false;
131  }
132 
133  /*
134  * Find the other item and unarchive by this foreign key
135  */
136  if (!context_.UnarchiveInstance(otherDef, foreignKey, other)) {
137  return false;
138  }
139 
140  return true;
141 }
142 
143 } // namespace impeller
impeller::ArchiveLocation::GetPrimaryKey
PrimaryKey GetPrimaryKey() const
Definition: archive_location.cc:21
impeller::Archivable
Instances of Archivables can be read from and written to a persistent archive.
Definition: archivable.h:27
archive_location.h
impeller::ArchiveDef
Definition: archivable.h:14
impeller::ArchiveVector::kArchiveDefinition
static ArchiveDef kArchiveDefinition
Definition: archive_vector.h:14
impeller::PrimaryKey
std::optional< int64_t > PrimaryKey
Definition: archivable.h:21
impeller::ArchiveStatement::WriteValue
bool WriteValue(size_t index, const std::string &item)
Definition: archive_statement.cc:99
impeller::ArchiveLocation::Read
bool Read(const std::string &member, T &item)
Definition: archive_location.h:79
impeller::ArchiveStatement::ReadValue
bool ReadValue(size_t index, T &item)
Definition: archive_statement.h:69
archive_class_registration.h
impeller::Allocation
Definition: allocation.h:15
impeller::ArchiveClassRegistration::FindColumnIndex
std::optional< size_t > FindColumnIndex(const std::string &member) const
Definition: archive_class_registration.cc:39
archive_vector.h
impeller::ArchiveLocation::Write
bool Write(const std::string &member, T item)
Definition: archive_location.h:26
impeller
Definition: aiks_context.cc:10