Flutter Impeller
archive.h
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 
5 #ifndef FLUTTER_IMPELLER_ARCHIVIST_ARCHIVE_H_
6 #define FLUTTER_IMPELLER_ARCHIVIST_ARCHIVE_H_
7 
8 #include <functional>
9 #include <memory>
10 #include <optional>
11 #include <string>
12 #include <type_traits>
13 
15 
16 namespace impeller {
17 
18 class ArchiveLocation;
19 class ArchiveDatabase;
20 
21 class Archive {
22  public:
23  explicit Archive(const std::string& path);
24 
25  ~Archive();
26 
27  bool IsValid() const;
28 
29  template <class T,
30  class = std::enable_if_t<std::is_base_of<Archivable, T>::value>>
31  [[nodiscard]] bool Write(const T& archivable) {
32  const ArchiveDef& def = T::kArchiveDefinition;
33  return ArchiveInstance(def, archivable).has_value();
34  }
35 
36  template <class T,
37  class = std::enable_if_t<std::is_base_of<Archivable, T>::value>>
38  [[nodiscard]] bool Read(PrimaryKey name, T& archivable) {
39  const ArchiveDef& def = T::kArchiveDefinition;
40  return UnarchiveInstance(def, name, archivable);
41  }
42 
43  using UnarchiveStep = std::function<bool(ArchiveLocation&)>;
44 
45  template <class T,
46  class = std::enable_if_t<std::is_base_of<Archivable, T>::value>>
47  [[nodiscard]] size_t Read(const UnarchiveStep& stepper) {
48  const ArchiveDef& def = T::kArchiveDefinition;
49  return UnarchiveInstances(def, stepper);
50  }
51 
52  private:
53  std::unique_ptr<ArchiveDatabase> database_;
54  int64_t transaction_count_ = 0;
55 
56  friend class ArchiveLocation;
57 
58  std::optional<int64_t /* row id */> ArchiveInstance(
59  const ArchiveDef& definition,
60  const Archivable& archivable);
61 
62  bool UnarchiveInstance(const ArchiveDef& definition,
63  PrimaryKey name,
64  Archivable& archivable);
65 
66  size_t UnarchiveInstances(const ArchiveDef& definition,
67  const UnarchiveStep& stepper,
68  PrimaryKey primary_key = std::nullopt);
69 
70  Archive(const Archive&) = delete;
71 
72  Archive& operator=(const Archive&) = delete;
73 };
74 
75 } // namespace impeller
76 
77 #endif // FLUTTER_IMPELLER_ARCHIVIST_ARCHIVE_H_
impeller::Archive
Definition: archive.h:21
impeller::Archivable
Instances of Archivables can be read from and written to a persistent archive.
Definition: archivable.h:28
impeller::ArchiveDef
Definition: archivable.h:15
archivable.h
impeller::PrimaryKey
std::optional< int64_t > PrimaryKey
Definition: archivable.h:22
impeller::Archive::UnarchiveStep
std::function< bool(ArchiveLocation &)> UnarchiveStep
Definition: archive.h:43
impeller::Archive::~Archive
~Archive()
Definition: archive.cc:17
impeller::Archive::IsValid
bool IsValid() const
Definition: archive.cc:22
impeller::ArchiveLocation
Definition: archive_location.h:21
impeller::Archive::Write
bool Write(const T &archivable)
Definition: archive.h:31
impeller::Archive::Archive
Archive(const std::string &path)
Definition: archive.cc:14
impeller::Archive::Read
bool Read(PrimaryKey name, T &archivable)
Definition: archive.h:38
impeller::Archive::Read
size_t Read(const UnarchiveStep &stepper)
Definition: archive.h:47
impeller
Definition: aiks_context.cc:10