ledger-core
PreferencesEditor.hpp
1 /*
2  *
3  * PreferencesEditor
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_PREFERENCESEDITOR_HPP
32 #define LEDGER_CORE_PREFERENCESEDITOR_HPP
33 
34 #include "PreferencesBackend.hpp"
35 #include "Preferences.hpp"
36 #include "../api/PreferencesEditor.hpp"
37 #include <memory>
38 #include <cereal/cereal.hpp>
39 #include <cereal/archives/portable_binary.hpp>
40 
41 namespace ledger {
42  namespace core {
43  struct PreferencesChange;
44  class Preferences;
45 
46  class PreferencesEditor : public api::PreferencesEditor, public std::enable_shared_from_this<PreferencesEditor> {
47  public:
48  PreferencesEditor(Preferences& preferences);
49 
50  std::shared_ptr<api::PreferencesEditor>
51  putString(const std::string &key, const std::string &value) override;
52 
53  std::shared_ptr<api::PreferencesEditor> putInt(const std::string &key, int32_t value) override;
54 
55  std::shared_ptr<api::PreferencesEditor> putLong(const std::string &key, int64_t value) override;
56 
57  std::shared_ptr<api::PreferencesEditor> putBoolean(const std::string &key, bool value) override;
58 
59  std::shared_ptr<api::PreferencesEditor>
60  putStringArray(const std::string &key, const std::vector<std::string> &value) override;
61 
62  std::shared_ptr<api::PreferencesEditor> remove(const std::string &key) override;
63 
64  template <typename T>
65  std::shared_ptr<PreferencesEditor> putObject(const std::string& key, T& object) {
66  std::stringstream is;
67  ::cereal::PortableBinaryOutputArchive archive(is);
68  archive(object);
69  auto savedState = is.str();
70  putData(key, std::vector<uint8_t>((const uint8_t *)savedState.data(),(const uint8_t *)savedState.data() + savedState.length()));
71  return shared_from_this();
72  };
73 
74  std::shared_ptr<api::PreferencesEditor>
75  putData(const std::string &key, const std::vector<uint8_t> &value) override;
76 
77  void commit() override;
78 
80  void clear() override;
81 
82  private:
83  std::vector<PreferencesChange> _changes;
84  Preferences& _preferences;
85  };
86  }
87 }
88 
89 
90 #endif //LEDGER_CORE_PREFERENCESEDITOR_HPP
std::shared_ptr< api::PreferencesEditor > putString(const std::string &key, const std::string &value) override
Definition: PreferencesEditor.cpp:38
std::shared_ptr< api::PreferencesEditor > putLong(const std::string &key, int64_t value) override
Definition: PreferencesEditor.cpp:60
void commit() override
Definition: PreferencesEditor.cpp:104
Definition: PreferencesEditor.hpp:22
Definition: PreferencesEditor.hpp:46
std::shared_ptr< api::PreferencesEditor > putStringArray(const std::string &key, const std::vector< std::string > &value) override
Definition: PreferencesEditor.cpp:83
std::shared_ptr< api::PreferencesEditor > putData(const std::string &key, const std::vector< uint8_t > &value) override
Definition: PreferencesEditor.cpp:119
Definition: Preferences.hpp:50
void clear() override
Clear all preferences.
Definition: PreferencesEditor.cpp:109
Definition: Account.cpp:8
std::shared_ptr< api::PreferencesEditor > putBoolean(const std::string &key, bool value) override
Definition: PreferencesEditor.cpp:71
std::shared_ptr< api::PreferencesEditor > putInt(const std::string &key, int32_t value) override
Definition: PreferencesEditor.cpp:49