Skip to content

Instantly share code, notes, and snippets.

@blockspacer
Created December 30, 2018 15:45
Show Gist options
  • Save blockspacer/90ae4314a5d39f4ce264d7db58bbee07 to your computer and use it in GitHub Desktop.
Save blockspacer/90ae4314a5d39f4ce264d7db58bbee07 to your computer and use it in GitHub Desktop.
Configuration variant
#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