initial config module

This commit is contained in:
Green Sky 2023-07-21 20:24:23 +02:00
commit d66ad3743b
No known key found for this signature in database
5 changed files with 180 additions and 0 deletions

17
CMakeLists.txt Normal file
View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
project(solanaceae)
add_library(solanaceae_util
./solanaceae/util/config_model.hpp
./solanaceae/util/config_model.inl
./solanaceae/util/simple_config_model.hpp
./solanaceae/util/simple_config_model.cpp
)
target_include_directories(solanaceae_util PUBLIC .)
target_compile_features(solanaceae_util PUBLIC cxx_std_17)
#target_link_libraries(solanaceae_contact PUBLIC
#)

24
LICENSE Normal file
View File

@ -0,0 +1,24 @@
The Code is under the following License, if not stated otherwise:
MIT License
Copyright (c) 2023 Erik Scholz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
README.md Normal file
View File

@ -0,0 +1,3 @@
`plant !`
random utilities for solanaceae

View File

@ -0,0 +1,40 @@
#pragma once
#include "./config_model.inl"
// TODO: events?
struct ConfigModelI {
~ConfigModelI(void) {}
// interface
// level3
// falls back to level2 value, if set
virtual void set(CM_ISV module, CM_ISV category, CM_ISV entry, const bool value) = 0;
virtual void set(CM_ISV module, CM_ISV category, CM_ISV entry, const int64_t value) = 0;
virtual void set(CM_ISV module, CM_ISV category, CM_ISV entry, const double value) = 0;
virtual void set(CM_ISV module, CM_ISV category, CM_ISV entry, CM_ISV value) = 0;
virtual bool has_bool(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
virtual bool has_int(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
virtual bool has_double(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
virtual bool has_string(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
virtual CM_InternalOptional<bool> get_bool(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
virtual CM_InternalOptional<int64_t> get_int(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
virtual CM_InternalOptional<double> get_double(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
virtual CM_InternalOptional<CM_ISV> get_string(CM_ISV module, CM_ISV category, CM_ISV entry) = 0;
// level2
virtual void set(CM_ISV module, CM_ISV category, const bool value) = 0;
virtual void set(CM_ISV module, CM_ISV category, const int64_t value) = 0;
virtual void set(CM_ISV module, CM_ISV category, const double value) = 0;
virtual void set(CM_ISV module, CM_ISV category, CM_ISV value) = 0;
virtual bool has_bool(CM_ISV module, CM_ISV category) = 0;
virtual bool has_int(CM_ISV module, CM_ISV category) = 0;
virtual bool has_double(CM_ISV module, CM_ISV category) = 0;
virtual bool has_string(CM_ISV module, CM_ISV category) = 0;
virtual CM_InternalOptional<bool> get_bool(CM_ISV module, CM_ISV category) = 0;
virtual CM_InternalOptional<int64_t> get_int(CM_ISV module, CM_ISV category) = 0;
virtual CM_InternalOptional<double> get_double(CM_ISV module, CM_ISV category) = 0;
virtual CM_InternalOptional<CM_ISV> get_string(CM_ISV module, CM_ISV category) = 0;
};

View File

@ -0,0 +1,96 @@
#pragma once
#include <string>
#include <string_view>
#include <optional>
#include <type_traits>
#include <cassert>
struct CM_InternalStringView {
const char* start {nullptr};
uint64_t extend {0};
// conversion helpers
CM_InternalStringView(const std::string_view sv) : start(sv.data()), extend(sv.size()) {}
template<std::size_t N>
CM_InternalStringView(const char (&str)[N]) : start(str), extend(N) {}
CM_InternalStringView(const std::string& str) : start(str.data()), extend(str.size()) {}
operator std::string() { return {start, start+extend}; }
operator std::string_view() { return {start, extend}; }
};
static_assert(sizeof(CM_InternalStringView) == sizeof(const char*) + sizeof(uint64_t), "guarantee abi (hope)");
using CM_ISV = CM_InternalStringView;
template<typename T>
struct CM_InternalOptional {
bool has_value {false};
union {
bool b;
int64_t i;
double d;
CM_InternalStringView s;
};
void set(bool v) { has_value = true; b = v; }
void set(int64_t v) { has_value = true; i = v; }
void set(double v) { has_value = true; d = v; }
void set(CM_InternalStringView v) { has_value = true; s = v; }
CM_InternalOptional(T v) : CM_InternalOptional(std::optional<T>{v}) {} // HACK: route through conv
// conversion helpers
CM_InternalOptional(std::nullopt_t) : has_value(false) {}
CM_InternalOptional(std::optional<bool> opt) : has_value(opt.has_value()) { if (opt.has_value()) set(opt.value()); }
CM_InternalOptional(std::optional<int64_t> opt) : has_value(opt.has_value()) { if (opt.has_value()) set(opt.value()); }
CM_InternalOptional(std::optional<double> opt) : has_value(opt.has_value()) { if (opt.has_value()) set(opt.value()); }
CM_InternalOptional(std::optional<CM_InternalStringView> opt) : has_value(opt.has_value()) { if (opt.has_value()) set(opt.value()); }
//operator std::optional<T>(void) const = delete;
operator auto() const {
if (has_value) {
if constexpr (std::is_same_v<T, bool>) {
return std::optional{b};
} else if constexpr (std::is_same_v<T, int64_t>) {
return std::optional{i};
} else if constexpr (std::is_same_v<T, double>) {
return std::optional{d};
} else if constexpr (std::is_same_v<T, CM_InternalStringView>) {
return std::optional{s};
}
}
return std::optional<T>{};
}
T value(void) {
assert(has_value); // use exceptions instead?
if constexpr (std::is_same_v<T, bool>) {
return b;
} else if constexpr (std::is_same_v<T, int64_t>) {
return i;
} else if constexpr (std::is_same_v<T, double>) {
return d;
} else if constexpr (std::is_same_v<T, CM_InternalStringView>) {
return s;
}
}
T value_or(T&& other) {
if (has_value) {
if constexpr (std::is_same_v<T, bool>) {
return b;
} else if constexpr (std::is_same_v<T, int64_t>) {
return i;
} else if constexpr (std::is_same_v<T, double>) {
return d;
} else if constexpr (std::is_same_v<T, CM_InternalStringView>) {
return s;
}
} else {
return other;
}
}
};