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