#include "config/fdstore.hpp"
#include "config/types.hpp"
+#include <array>
#include <string>
+#include <vector>
namespace config {
readInternal(value);
}
}
+
+ template<typename T, std::size_t N>
+ void readInternal(std::array<T, N>& values)
+ {
+ for (T& value : values) {
+ readInternal(value);
+ }
+ }
};
} // namespace config
#include <string>
#include <vector>
+#include <array>
#include <memory>
#include <cassert>
#include <glib.h>
}
}
+ template<typename T, std::size_t N>
+ static void fromGVariant(GVariant* object, std::array<T, N>& values)
+ {
+ checkType(object, G_VARIANT_TYPE_ARRAY);
+
+ GVariantIter iter;
+ g_variant_iter_init(&iter, object);
+
+ for (T& value: values) {
+ auto child = makeUnique(g_variant_iter_next_value(&iter));
+ fromGVariant(child.get(), value);
+ }
+ }
+
template<typename T>
static typename std::enable_if<isUnion<T>::value>::type
fromGVariant(GVariant* object, T& value)
#include <string>
#include <cstring>
#include <vector>
+#include <array>
namespace config {
}
template<typename T>
- static void fromJsonObject(json_object* object, std::vector<T>& value)
+ static void fromJsonObject(json_object* object, std::vector<T>& values)
{
checkType(object, json_type_array);
int length = json_object_array_length(object);
- value.resize(static_cast<size_t>(length));
+ values.resize(static_cast<size_t>(length));
for (int i = 0; i < length; ++i) {
- fromJsonObject(json_object_array_get_idx(object, i), value[static_cast<size_t>(i)]);
+ fromJsonObject(json_object_array_get_idx(object, i), values[static_cast<size_t>(i)]);
+ }
+ }
+
+ template<typename T, std::size_t N>
+ static void fromJsonObject(json_object* object, std::array<T, N>& values)
+ {
+ checkType(object, json_type_array);
+
+ for (std::size_t i = 0; i < N; ++i) {
+ fromJsonObject(json_object_array_get_idx(object, i), values[i]);
}
}
}
}
+ template<typename T, std::size_t N>
+ void getValue(const std::string& name, std::array<T, N>& values)
+ {
+ json_object* object = nullptr;
+ if (mObject && json_object_object_get_ex(mObject, name.c_str(), &object)) {
+ checkType(object, json_type_array);
+ }
+
+ std::string k = key(mKeyPrefix, name);
+ FromKVJsonVisitor visitor(*this, name, false);
+ if (mStore.exists(k)) {
+ json_object_put(visitor.mObject);
+ visitor.mObject = nullptr;
+ }
+ for (std::size_t i = 0; i < N; ++i) {
+ visitor.getValue(i, values[i]);
+ }
+ }
+
template<typename T, typename std::enable_if<!isVisitable<T>::value, int>::type = 0>
void getValue(int i, T& t)
{
}
}
+ template<typename T, std::size_t N>
+ void getValue(int i, std::array<T, N>& values)
+ {
+ std::string k = key(mKeyPrefix, std::to_string(i));
+
+ FromKVJsonVisitor visitor(*this, i, false);
+ if (mStore.exists(k)) {
+ json_object_put(visitor.mObject);
+ visitor.mObject = nullptr;
+ }
+ for (std::size_t idx = 0; idx < N; ++idx) {
+ visitor.getValue(idx, values[idx]);
+ }
+ }
+
static void checkType(json_object* object, json_type type)
{
if (type != json_object_get_type(object)) {
void setInternal(const std::string& key, const T& value);
template<typename T>
void setInternal(const std::string& key, const std::vector<T>& values);
+ template<typename T, std::size_t N>
+ void setInternal(const std::string& key, const std::array<T, N>& values);
std::string getInternal(const std::string& key, std::string*);
char* getInternal(const std::string& key, char**);
T getInternal(const std::string& key, T*);
template<typename T>
std::vector<T> getInternal(const std::string& key, std::vector<T>*);
+ template<typename T, std::size_t N>
+ std::array<T, N> getInternal(const std::string& key, std::array<T, N>*);
std::string mPath;
sqlite3::Connection mConn;
setInternal(key, strValues);
}
+template<typename T, std::size_t N>
+void KVStore::setInternal(const std::string& key, const std::array<T, N>& values)
+{
+ std::vector<std::string> strValues(N);
+
+ std::transform(values.begin(),
+ values.end(),
+ strValues.begin(),
+ toString<T>);
+
+ setInternal(key, strValues);
+}
+
template<typename T>
T KVStore::getInternal(const std::string& key, T*)
{
return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
}
+template<typename T, std::size_t N>
+std::array<T, N> KVStore::getInternal(const std::string& key, std::array<T, N>*)
+{
+ std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
+ std::array<T, N> values;
+
+ std::transform(strValues.begin(),
+ strValues.end(),
+ values.begin(),
+ fromString<T>);
+
+ return values;
+}
+
template<typename T>
std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
{
#include "config/fdstore.hpp"
#include "config/types.hpp"
-#include <string>
+#include <array>
#include <cstring>
+#include <string>
+#include <vector>
namespace config {
void writeInternal(const std::vector<T>& values)
{
writeInternal(values.size());
- for (const T& value : values) {
+ for (const T& value: values) {
+ writeInternal(value);
+ }
+ }
+
+ template<typename T, std::size_t N>
+ void writeInternal(const std::array<T, N>& values)
+ {
+ for (const T& value: values) {
writeInternal(value);
}
}
#include "config/is-union.hpp"
#include "config/types.hpp"
+#include <array>
#include <string>
#include <vector>
#include <glib.h>
}
}
+ template<typename T, std::size_t N>
+ void writeInternal(const std::array<T, N>& values)
+ {
+ if (!values.empty()) {
+ g_variant_builder_open(mBuilder, G_VARIANT_TYPE_ARRAY);
+ for (const T& v : values) {
+ writeInternal(v);
+ }
+ g_variant_builder_close(mBuilder);
+ } else {
+ g_variant_builder_add(mBuilder, "as", NULL);
+ }
+ }
+
template<typename T>
typename std::enable_if<isVisitable<T>::value && !isUnion<T>::value>::type
writeInternal(const T& value)
return array;
}
+ template<typename T, std::size_t N>
+ static json_object* toJsonObject(const std::array<T, N>& values)
+ {
+ json_object* array = json_object_new_array();
+ for (const T& v : values) {
+ json_object_array_add(array, toJsonObject(v));
+ }
+ return array;
+ }
+
template<typename T, class = typename std::enable_if<isVisitable<T>::value>::type>
static json_object* toJsonObject(const T& value)
{
std::vector<std::string> stringVector;
std::vector<double> doubleVector;
+ std::array<int, 2> intArray;
+
SubConfig subObj;
std::vector<SubConfig> subVector;
stringVector,
doubleVector,
+ intArray,
+
subObj,
subVector,
"\"intVector\": [ 1, 2, 3 ], "
"\"stringVector\": [ \"a\", \"b\" ], "
"\"doubleVector\": [ 0.000000, 1.000000, 2.000000 ], "
+ "\"intArray\": [ 0, 1 ], "
"\"subObj\": { \"intVal\": 54321, \"intVector\": [ 1, 2 ], \"subSubObj\": { \"intVal\": 234 } }, "
"\"subVector\": [ { \"intVal\": 123, \"intVector\": [ 3, 4 ], \"subSubObj\": { \"intVal\": 345 } }, "
"{ \"intVal\": 456, \"intVector\": [ 5, 6 ], \"subSubObj\": { \"intVal\": 567 } } ], "
"\"intVector\": [ ], "
"\"stringVector\": [ ], "
"\"doubleVector\": [ ], "
+ "\"intArray\": [ ], "
"\"subObj\": { \"intVal\": 0, \"intVector\": [ ], \"subSubObj\": { \"intVal\": 0 } }, "
"\"subVector\": [ ], "
"\"union1\": { \"type\": \"int\", \"value\": 0 }, "
BOOST_REQUIRE_EQUAL(0, testConfig.emptyIntVector.size());
BOOST_REQUIRE_EQUAL(3, testConfig.intVector.size());
- BOOST_CHECK_EQUAL(1, testConfig.intVector[0]);
- BOOST_CHECK_EQUAL(2, testConfig.intVector[1]);
- BOOST_CHECK_EQUAL(3, testConfig.intVector[2]);
+ BOOST_CHECK_EQUAL(1, testConfig.intVector[0]);
+ BOOST_CHECK_EQUAL(2, testConfig.intVector[1]);
+ BOOST_CHECK_EQUAL(3, testConfig.intVector[2]);
BOOST_REQUIRE_EQUAL(2, testConfig.stringVector.size());
BOOST_CHECK_EQUAL("a", testConfig.stringVector[0]);
BOOST_CHECK_CLOSE(1.0, testConfig.doubleVector[1], TOLERANCE);
BOOST_CHECK_CLOSE(2.0, testConfig.doubleVector[2], TOLERANCE);
+ BOOST_REQUIRE_EQUAL(2, testConfig.intArray.size());
+ BOOST_CHECK_EQUAL(0, testConfig.intArray[0]);
+ BOOST_CHECK_EQUAL(1, testConfig.intArray[1]);
+
BOOST_CHECK_EQUAL(54321, testConfig.subObj.intVal);
BOOST_CHECK_EQUAL(2, testConfig.subObj.intVector.size());
BOOST_CHECK_EQUAL(1, testConfig.subObj.intVector[0]);