1 #pragma once
2 
3 #include <stdint.h>
4 #include <string>
5 #include <cassert>
6 
7 namespace aconfig_storage {
8 
9 /// Storage file type enum, to be consistent with the one defined in
10 /// aconfig_storage_file/src/lib.rs
11 enum StorageFileType {
12   package_map,
13   flag_map,
14   flag_val,
15   flag_info
16 };
17 
18 /// Flag type enum, to be consistent with the one defined in
19 /// aconfig_storage_file/src/lib.rs
20 enum StoredFlagType {
21   ReadWriteBoolean = 0,
22   ReadOnlyBoolean = 1,
23   FixedReadOnlyBoolean = 2,
24 };
25 
26 /// Flag value type enum, to be consistent with the one defined in
27 /// aconfig_storage_file/src/lib.rs
28 enum FlagValueType {
29   Boolean = 0,
30 };
31 
32 /// Flag info enum, to be consistent with the one defined in
33 /// aconfig_storage_file/src/flag_info.rs
34 enum FlagInfoBit {
35   HasServerOverride = 1<<0,
36   IsReadWrite = 1<<1,
37   HasLocalOverride = 1<<2,
38 };
39 
40 /// Mapped storage file
41 struct MappedStorageFile {
42   void* file_ptr;
43   size_t file_size;
44   virtual ~MappedStorageFile();
45 };
46 
47 /// Package read context query result
48 struct PackageReadContext {
49   bool package_exists;
50   uint32_t package_id;
51   uint32_t boolean_start_index;
52 };
53 
54 /// Flag read context query result
55 struct FlagReadContext {
56   bool flag_exists;
57   StoredFlagType flag_type;
58   uint16_t flag_index;
59 };
60 
61 
62 template <class T>
63 class Result {
64   public:
65 
Result()66   Result()
67       : data()
68       , errmsg()
69       , has_data(false)
70   {}
71 
Result(T const & value)72   Result(T const& value)
73       : data(value)
74       , errmsg()
75       , has_data(true)
76   {}
77 
ok()78   bool ok() {
79     return has_data;
80   }
81 
operator *()82   T& operator*() {
83     assert(has_data);
84     return data;
85   }
86 
operator ->()87   T* operator->() {
88     assert(has_data);
89     return &data;
90   }
91 
error()92   std::string const& error() {
93     assert(!has_data);
94     return errmsg;
95   }
96 
97   T data;
98   std::string errmsg;
99   bool has_data;
100 };
101 
102 /// DO NOT USE APIS IN THE FOLLOWING NAMESPACE DIRECTLY
103 namespace private_internal_api {
104 
105 Result<MappedStorageFile*> get_mapped_file_impl(
106     std::string const& pb_file,
107     std::string const& container,
108     StorageFileType file_type);
109 } // namespace private_internal_api
110 
111 /// Map a storage file
112 Result<MappedStorageFile*> map_storage_file(
113     std::string const& file);
114 
115 
116 /// Map from StoredFlagType to FlagValueType
117 /// \input stored_type: stored flag type in the storage file
118 /// \returns the flag value type enum
119 Result<FlagValueType> map_to_flag_value_type(
120     StoredFlagType stored_type);
121 
122 /// Get mapped storage file
123 /// \input container: stoarge container name
124 /// \input file_type: storage file type enum
125 /// \returns a MappedStorageFileQuery
126 Result<MappedStorageFile*> get_mapped_file(
127     std::string const& container,
128     StorageFileType file_type);
129 
130 /// Get storage file version number
131 /// \input file_path: the path to the storage file
132 /// \returns the storage file version
133 Result<uint32_t> get_storage_file_version(
134     std::string const& file_path);
135 
136 /// Get package read context
137 /// \input file: mapped storage file
138 /// \input package: the flag package name
139 /// \returns a package read context
140 Result<PackageReadContext> get_package_read_context(
141     MappedStorageFile const& file,
142     std::string const& package);
143 
144 /// Get flag read context
145 /// \input file: mapped storage file
146 /// \input package_id: the flag package id obtained from package offset query
147 /// \input flag_name: flag name
148 /// \returns the flag read context
149 Result<FlagReadContext> get_flag_read_context(
150     MappedStorageFile const& file,
151     uint32_t package_id,
152     std::string const& flag_name);
153 
154 /// Get boolean flag value
155 /// \input file: mapped storage file
156 /// \input index: the boolean flag index in the file
157 /// \returns the boolean flag value
158 Result<bool> get_boolean_flag_value(
159     MappedStorageFile const& file,
160     uint32_t index);
161 
162 /// Get boolean flag attribute
163 /// \input file: mapped storage file
164 /// \input value_type: flag value type
165 /// \input index: the boolean flag index in the file
166 /// \returns the boolean flag attribute
167 Result<uint8_t> get_flag_attribute(
168     MappedStorageFile const& file,
169     FlagValueType value_type,
170     uint32_t index);
171 } // namespace aconfig_storage
172