ledger-core
Preferences.hpp
1 /*
2  *
3  * Preferences
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 11/01/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_PREFERENCES_HPP
32 #define LEDGER_CORE_PREFERENCES_HPP
33 
34 #include "../api/Preferences.hpp"
35 #include "../api/PreferencesEditor.hpp"
36 #include "PreferencesBackend.hpp"
37 #include "PreferencesEditor.hpp"
38 #include "../utils/Option.hpp"
39 #include <cereal/cereal.hpp>
40 #include <cereal/archives/portable_binary.hpp>
41 #include <cereal/types/set.hpp>
42 #include <boost/iostreams/device/array.hpp>
43 #include <boost/iostreams/stream.hpp>
44 
45 namespace ledger {
46  namespace core {
47  class PreferencesBackend;
48  class PreferencesEditor;
49 
50  class Preferences : public api::Preferences {
51  public:
52  Preferences(PreferencesBackend& backend, const std::vector<uint8_t>& keyPrefix);
53 
54  std::string getString(const std::string &key, const std::string &fallbackValue) override;
55 
56  int32_t getInt(const std::string &key, int32_t fallbackValue) override;
57 
58  int64_t getLong(const std::string &key, int64_t fallbackValue) override;
59 
60  bool getBoolean(const std::string &key, bool fallbackValue) override;
61 
62  std::vector<std::string>
63  getStringArray(const std::string &key, const std::vector<std::string> &fallbackValue) override;
64 
65  bool contains(const std::string &key) override;
66 
67  std::shared_ptr<api::PreferencesEditor> edit() override;
68  std::shared_ptr<PreferencesEditor> editor();
69 
70  std::vector<uint8_t> getData(const std::string &key, const std::vector<uint8_t> &fallbackValue) override;
71 
72  template <typename T>
73  Option<T> getObject(const std::string& key) {
74  auto data = getData(key, {});
75  if (data.size() == 0) {
76  return Option<T>();
77  }
78  T object;
79  boost::iostreams::array_source my_vec_source(reinterpret_cast<char*>(&data[0]), data.size());
80  boost::iostreams::stream<boost::iostreams::array_source> is(my_vec_source);
81  ::cereal::PortableBinaryInputArchive archive(is);
82  archive(object);
83  return Option<T>(object);
84  };
85 
86  void iterate(std::function<bool (leveldb::Slice&&, leveldb::Slice&&)> f, Option<std::string> begin = Option<std::string>());
87 
88  template <typename T>
89  void iterate(std::function<bool (leveldb::Slice&& key, const T& value)> f, Option<std::string> begin = Option<std::string>()) {
90  iterate([f] (leveldb::Slice&& key, leveldb::Slice&& value) {
91  T object;
92  try {
93  boost::iostreams::array_source my_vec_source(reinterpret_cast<const char *>(&value.data()[0]),
94  value.size());
95  boost::iostreams::stream<boost::iostreams::array_source> is(my_vec_source);
96  ::cereal::PortableBinaryInputArchive archive(is);
97  archive(object);
98  } catch (const std::exception& ex) {
99  return true;
100  }
101  return f(std::move(key), object);
102  }, begin);
103  }
104 
105  std::shared_ptr<Preferences> getSubPreferences(std::string prefix);
106 
107  protected:
108  std::vector<uint8_t> wrapKey(const std::string& key) const;
109 
110  private:
111  PreferencesBackend& _backend;
112  std::vector<uint8_t> _keyPrefix;
113  friend class ledger::core::PreferencesEditor;
114  };
115  }
116 }
117 
118 
119 #endif //LEDGER_CORE_PREFERENCES_HPP
std::vector< std::string > getStringArray(const std::string &key, const std::vector< std::string > &fallbackValue) override
Definition: Preferences.cpp:73
std::vector< uint8_t > getData(const std::string &key, const std::vector< uint8_t > &fallbackValue) override
Definition: Preferences.cpp:135
std::shared_ptr< api::PreferencesEditor > edit() override
Definition: Preferences.cpp:93
Definition: Option.hpp:49
Definition: PreferencesBackend.hpp:70
Definition: PreferencesEditor.hpp:46
bool getBoolean(const std::string &key, bool fallbackValue) override
Definition: Preferences.cpp:64
Definition: Preferences.hpp:50
bool contains(const std::string &key) override
Definition: Preferences.cpp:89
Definition: Account.cpp:8
std::string getString(const std::string &key, const std::string &fallbackValue) override
Definition: Preferences.cpp:41
Definition: Preferences.hpp:29
int32_t getInt(const std::string &key, int32_t fallbackValue) override
Definition: Preferences.cpp:48
int64_t getLong(const std::string &key, int64_t fallbackValue) override
Definition: Preferences.cpp:56