ledger-core
DynamicObject.hpp
1 /*
2  *
3  * DynamicObject
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 08/03/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_DYNAMICOBJECT_HPP
32 #define LEDGER_CORE_DYNAMICOBJECT_HPP
33 
34 #include "../api/DynamicArray.hpp"
35 #include "../api/DynamicObject.hpp"
36 #include "../api/DynamicType.hpp"
37 #include <cereal/cereal.hpp>
38 #include "../collections/collections.hpp"
39 #include "DynamicValue.hpp"
40 #include "DynamicArray.hpp"
41 #include <cereal/types/map.hpp>
42 #include <cereal/types/unordered_map.hpp>
43 
44 namespace ledger {
45  namespace core {
46  class DynamicObject : public api::DynamicObject, public std::enable_shared_from_this<DynamicObject> {
47  public:
48  DynamicObject() : _readOnly(false) {}
49 
51  template <typename T>
52  optional<T> get(const std::string& key) {
53  const auto v = optional<DynamicValue>(_values.lift(key));
54 
55  if (v) {
56  return v->get<T>();
57  } else {
58  return optional<T>();
59  }
60  }
61 
63  template <typename T>
64  std::shared_ptr<DynamicObject> put(const std::string& key, T value) {
65  if (!_readOnly) {
66  _values[key] = DynamicValue(value);
67  }
68 
69  return shared_from_this();
70  }
71 
72  optional<std::string> getString(const std::string &key) override;
73  optional<int32_t> getInt(const std::string &key) override;
74  optional<int64_t> getLong(const std::string &key) override;
75  optional<double> getDouble(const std::string &key) override;
76  optional<bool> getBoolean(const std::string &key) override;
77  optional<std::vector<uint8_t>> getData(const std::string &key) override;
78  std::shared_ptr<api::DynamicObject> getObject(const std::string &key) override;
79  std::shared_ptr<api::DynamicArray> getArray(const std::string &key) override;
80 
81  std::shared_ptr<api::DynamicObject> putString(const std::string &key, const std::string &value) override;
82  std::shared_ptr<api::DynamicObject> putInt(const std::string &key, int32_t value) override;
83  std::shared_ptr<api::DynamicObject> putLong(const std::string &key, int64_t value) override;
84  std::shared_ptr<api::DynamicObject> putDouble(const std::string &key, double value) override;
85  std::shared_ptr<api::DynamicObject> putBoolean(const std::string &key, bool value) override;
86  std::shared_ptr<api::DynamicObject> putData(const std::string &key, const std::vector<uint8_t> &value) override;
87  std::shared_ptr<api::DynamicObject> putObject(const std::string &key, const std::shared_ptr<api::DynamicObject> &value) override;
88  std::shared_ptr<api::DynamicObject> putArray(const std::string &key, const std::shared_ptr<api::DynamicArray> &value) override;
89 
90  bool contains(const std::string &key) override;
91 
92  bool remove(const std::string &key) override;
93 
94  std::vector<std::string> getKeys() override;
95 
96  optional<api::DynamicType> getType(const std::string &key) override;
97 
98  std::string dump() override;
99 
100  std::vector<uint8_t> serialize() override;
101 
102  bool isReadOnly() override;
103  void setReadOnly(bool enable);
104 
105  std::shared_ptr<api::DynamicObject> updateWithConfiguration(const std::shared_ptr<DynamicObject> &configuration);
106  int64_t size() override;
107 
108  std::ostream& dump(std::ostream& ss, int depth) const;
109 
110  template <class Archive>
111  void serialize(Archive& ar) {
112  ar(_values.getContainer());
113  }
114 
115  private:
117  bool _readOnly;
118  };
119  }
120 }
121 
122 #endif //LEDGER_CORE_DYNAMICOBJECT_HPP
std::shared_ptr< api::DynamicObject > putInt(const std::string &key, int32_t value) override
Definition: DynamicObject.cpp:78
optional< std::string > getString(const std::string &key) override
Definition: DynamicObject.cpp:42
Definition: DynamicObject.hpp:30
std::shared_ptr< api::DynamicObject > putBoolean(const std::string &key, bool value) override
Definition: DynamicObject.cpp:94
optional< int64_t > getLong(const std::string &key) override
Definition: DynamicObject.cpp:50
std::shared_ptr< api::DynamicObject > putString(const std::string &key, const std::string &value) override
Definition: DynamicObject.cpp:74
std::shared_ptr< DynamicObject > put(const std::string &key, T value)
A generic and type-safe put.
Definition: DynamicObject.hpp:64
int64_t size() override
Definition: DynamicObject.cpp:162
optional< bool > getBoolean(const std::string &key) override
Definition: DynamicObject.cpp:58
Definition: MapLike.hpp:44
std::shared_ptr< api::DynamicObject > putDouble(const std::string &key, double value) override
Definition: DynamicObject.cpp:86
optional< api::DynamicType > getType(const std::string &key) override
Definition: DynamicObject.cpp:119
std::vector< std::string > getKeys() override
Definition: DynamicObject.cpp:115
Definition: DynamicObject.hpp:46
std::string dump() override
Definition: DynamicObject.cpp:126
std::shared_ptr< api::DynamicObject > putLong(const std::string &key, int64_t value) override
Definition: DynamicObject.cpp:82
Definition: DynamicValue.hpp:48
optional< std::vector< uint8_t > > getData(const std::string &key) override
Definition: DynamicObject.cpp:62
bool contains(const std::string &key) override
Definition: DynamicObject.cpp:107
std::shared_ptr< api::DynamicObject > putData(const std::string &key, const std::vector< uint8_t > &value) override
Definition: DynamicObject.cpp:90
optional< double > getDouble(const std::string &key) override
Definition: DynamicObject.cpp:54
Definition: Account.cpp:8
std::shared_ptr< api::DynamicObject > getObject(const std::string &key) override
Definition: DynamicObject.cpp:66
bool isReadOnly() override
Definition: DynamicObject.cpp:166
std::vector< uint8_t > serialize() override
Definition: DynamicObject.cpp:132
optional< int32_t > getInt(const std::string &key) override
Definition: DynamicObject.cpp:46
Definition: optional.hpp:177
std::shared_ptr< api::DynamicArray > getArray(const std::string &key) override
Definition: DynamicObject.cpp:70