Created
December 30, 2018 15:45
-
-
Save blockspacer/90ae4314a5d39f4ce264d7db58bbee07 to your computer and use it in GitHub Desktop.
Configuration variant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <string> | |
#include <variant> | |
#include <vector> | |
struct ConfValue; | |
using ConfObject = std::map<std::string, ConfValue>; | |
using ConfArray = std::vector<ConfValue>; | |
/** | |
* Represents a configuration value. | |
*/ | |
struct ConfValue { | |
std::variant<std::monostate, ConfObject, ConfArray, std::string, long long, double, bool> v; | |
}; | |
/** | |
* Configuration (format influenced by JSON). | |
*/ | |
int main() { | |
std::map<std::string, ConfValue> default_conf = { | |
{"port", ConfValue{1LL}}, | |
{"module", ConfValue{"modHeader"}}, | |
{"modulePath", ConfValue{ConfArray{ConfValue{"../modules/"}, ConfValue{"./modules"}}}} | |
}; | |
// default_conf["port"] = ConfValue{std::string{"hjjl;"}}; | |
auto intPtr = std::get_if<std::string>(&default_conf["port"].v); | |
std::string val = intPtr ? *intPtr : "default"; | |
std::cout << val << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment