Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
to-kvstore-visitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jan Olszak (j.olszak@samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
25 #ifndef CARGO_SQLITE_TO_KVSTORE_VISITOR_HPP
26 #define CARGO_SQLITE_TO_KVSTORE_VISITOR_HPP
27 
28 #include "cargo-sqlite/kvstore.hpp"
30 #include "cargo/is-visitable.hpp"
31 #include "cargo/is-like-tuple.hpp"
32 #include "cargo/is-streamable.hpp"
33 #include "cargo/exception.hpp"
34 
35 #include <map>
36 
37 namespace cargo {
38 
40 
41 public:
42  ToKVStoreVisitor(KVStore& store, const std::string& prefix)
43  : mStore(store),
44  mKeyPrefix(prefix)
45  {
46  }
47 
49 
50  template<typename T>
51  void visit(const std::string& name, const T& value)
52  {
53  setInternal(key(mKeyPrefix, name), value);
54  }
55 
56 private:
58  std::string mKeyPrefix;
59 
60  ToKVStoreVisitor(const ToKVStoreVisitor& visitor, const std::string& prefix)
61  : mStore(visitor.mStore),
62  mKeyPrefix(prefix)
63  {
64  }
65 
66  template<typename T, typename std::enable_if<isStreamableOut<T>::value, int>::type = 0>
67  void setInternal(const std::string& name, const T& value)
68  {
69  mStore.set(name, toString(value));
70  }
71 
72  template<typename T, typename std::enable_if<isVisitable<T>::value, int>::type = 0>
73  void setInternal(const std::string& name, const T& value)
74  {
75  ToKVStoreVisitor visitor(*this, name);
76  value.accept(visitor);
77  }
78 
79  template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
80  void setInternal(const std::string& name, const T& value)
81  {
82  setInternal(name, static_cast<const typename std::underlying_type<T>::type>(value));
83  }
84 
85  template <typename I>
86  void setRangeInternal(const std::string& name,
87  const I& begin,
88  const I& end,
89  const size_t size) {
90  KVStore::Transaction transaction(mStore);
91 
92  mStore.remove(name);
93  setInternal(name, size);
94  size_t i = 0;
95  for (auto it = begin; it != end; ++it) {
96  const std::string k = key(name, std::to_string(i));
97  setInternal(k, *it);
98  ++i;
99  }
100  transaction.commit();
101  }
102 
103  template<typename T>
104  void setInternal(const std::string& name, const std::vector<T>& values)
105  {
106  setRangeInternal(name, values.begin(), values.end(), values.size());
107  }
108 
109  template<typename T, std::size_t N>
110  void setInternal(const std::string& name, const std::array<T, N>& values) {
111  setRangeInternal(name, values.begin(), values.end(), N);
112  }
113 
114  template<typename V>
115  void setInternal(const std::string& name, const std::map<std::string, V>& values) {
116  KVStore::Transaction transaction(mStore);
117 
118  mStore.remove(name);
119  setInternal(name, values.size());
120  size_t i = 0;
121  for (const auto& it : values) {
122  const std::string k = key(name, i++);
123  setInternal(k, it.first);
124  setInternal(k + ".val", it.second);
125  }
126  transaction.commit();
127  }
128 
129  template<typename T>
130  void setInternal(const std::string& name, const std::initializer_list<T>& values)
131  {
132  setRangeInternal(name, values.begin(), values.end(), values.size());
133  }
134 
135  template<typename T, typename std::enable_if<isLikeTuple<T>::value, int>::type = 0>
136  void setInternal(const std::string& name, const T& values)
137  {
138  KVStore::Transaction transaction(mStore);
139 
140  setInternal(name, std::tuple_size<T>::value);
141 
142  ToKVStoreVisitor recursiveVisitor(*this, name);
143  SetTupleVisitor visitor(recursiveVisitor);
144  visitFields(values, &visitor);
145 
146  transaction.commit();
147  }
148 
150  {
151  public:
152  SetTupleVisitor(ToKVStoreVisitor& visitor) : mVisitor(visitor) {}
153 
154  template<typename T>
155  void visit(T& value)
156  {
157  mVisitor.visit(std::to_string(idx), value);
158  ++idx;
159  }
160 
161  private:
163  size_t idx = 0;
164  };
165 };
166 
167 } // namespace cargo
168 
169 #endif // CARGO_SQLITE_TO_KVSTORE_VISITOR_HPP
void remove(const std::string &key)
Removes values corresponding to the passed key.
Definition: kvstore.cpp:275
Internal configuration helper.
A guard struct for thread synchronization and transaction management.
Definition: kvstore.hpp:49
void setRangeInternal(const std::string &name, const I &begin, const I &end, const size_t size)
Definition: to-kvstore-visitor.hpp:86
Definition: to-kvstore-visitor.hpp:39
SetTupleVisitor(ToKVStoreVisitor &visitor)
Definition: to-kvstore-visitor.hpp:152
Declaration of a class for key-value storage in a sqlite3 database.
std::string toString(const T &value)
Definition: kvstore-visitor-utils.hpp:44
Definition: kvstore.hpp:43
void visit(T &value)
Definition: to-kvstore-visitor.hpp:155
ToKVStoreVisitor(const ToKVStoreVisitor &visitor, const std::string &prefix)
Definition: to-kvstore-visitor.hpp:60
void visit(const std::string &name, const T &value)
Definition: to-kvstore-visitor.hpp:51
Tuple or pair checker.
void setInternal(const std::string &name, const T &values)
Definition: to-kvstore-visitor.hpp:136
ToKVStoreVisitor(KVStore &store, const std::string &prefix)
Definition: to-kvstore-visitor.hpp:42
KVStore & mStore
Definition: to-kvstore-visitor.hpp:57
void setInternal(const std::string &name, const T &value)
Definition: to-kvstore-visitor.hpp:67
std::string key(const Arg1 &a1, const Args &...args)
Concatenates all parameters into one std::string.
Definition: kvstore-visitor-utils.hpp:60
void set(const std::string &key, const std::string &value)
Stores a single value corresponding to the passed key.
Definition: kvstore.cpp:153
void setInternal(const std::string &name, const std::initializer_list< T > &values)
Definition: to-kvstore-visitor.hpp:130
void setInternal(const std::string &name, const std::vector< T > &values)
Definition: to-kvstore-visitor.hpp:104
Exceptions for libcargo.
ToKVStoreVisitor & mVisitor
Definition: to-kvstore-visitor.hpp:162
void setInternal(const std::string &name, const std::map< std::string, V > &values)
Definition: to-kvstore-visitor.hpp:115
KVStore visitors utilities.
Definition: to-kvstore-visitor.hpp:149
Check whether type is accepted by streaming operators.
std::string mKeyPrefix
Definition: to-kvstore-visitor.hpp:58
ToKVStoreVisitor & operator=(const ToKVStoreVisitor &)=delete
size_t idx
Definition: to-kvstore-visitor.hpp:163
void commit()
Definition: kvstore.cpp:126
void visitFields(T &t, F f, A...args)
Definition: visit-fields.hpp:58
void setInternal(const std::string &name, const std::array< T, N > &values)
Definition: to-kvstore-visitor.hpp:110