Skip to main content
Mod Moved Comments To Chat
deleted 95 characters in body
Source Link
user52292
user52292

The above would require altering ConfigMapImpl to be some_map<std::string, ConfigValue<T>>ConfigValue<T>&>. The reference is important not to make copy ;)

Full tested code in VS and Wandbox (unfortunatelly IdeOne currently knows only C++14 and not C++17)

#include <string>
#include <unordered_map>
#include <iostream>

template<class T> class ConfigValue;
template<class T> using ConfigMapImpl = std::unordered_map<std::string, ConfigValue<T>>;ConfigValue<T>&>;
template<class T> struct ConfigMapStore { static inline ConfigMapImpl<T> it = {}; };

template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value) : value(value) {
        ConfigMapStore<T>::it.insertemplace({ name, *this });
    }
    // find copy and swap idiom for improvements, I will keep it simple here
    ConfigValue& operator=(const T& rhs) { value = rhs; return *this; }
    operator /*maybe const*/const T&() /*maybe const*/const { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
    // you can define macro for the above, but I would not do so
};

int main()
{
    Configuration cfg;
    std::cout << cfg.someValue << " ";" << ConfigMapStore<long>::it.at("someValue") << std::endl;
    cfg.someValue = 4321;
    std::cout << cfg.someValue << " " << ConfigMapStore<long>::it.at("someValue") << std::endl;
    std::cin.get();
}

The above would require altering ConfigMapImpl to be some_map<std::string, ConfigValue<T>>.

Full tested code in VS and Wandbox (unfortunatelly IdeOne currently knows only C++14 and not C++17)

#include <string>
#include <unordered_map>
#include <iostream>

template<class T> class ConfigValue;
template<class T> using ConfigMapImpl = std::unordered_map<std::string, ConfigValue<T>>;
template<class T> struct ConfigMapStore { static inline ConfigMapImpl<T> it = {}; };

template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value) : value(value) {
        ConfigMapStore<T>::it.insert({ name, *this });
    }
    // find copy and swap idiom for improvements, I will keep it simple here
    ConfigValue& operator=(const T& rhs) { value = rhs; return *this; }
    operator /*maybe const*/ T&() /*maybe const*/ { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
    // you can define macro for the above, but I would not do so
};

int main()
{
    Configuration cfg;
    std::cout << cfg.someValue << " ";
    cfg.someValue = 4321;
    std::cout << cfg.someValue << std::endl;
    std::cin.get();
}

The above would require altering ConfigMapImpl to be some_map<std::string, ConfigValue<T>&>. The reference is important not to make copy ;)

Full tested code in VS

#include <string>
#include <unordered_map>
#include <iostream>

template<class T> class ConfigValue;
template<class T> using ConfigMapImpl = std::unordered_map<std::string, ConfigValue<T>&>;
template<class T> struct ConfigMapStore { static inline ConfigMapImpl<T> it = {}; };

template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value) : value(value) {
        ConfigMapStore<T>::it.emplace(name, *this); }
    ConfigValue& operator=(const T& rhs) { value = rhs; return *this; }
    operator const T&() const { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
};

int main()
{
    Configuration cfg;
    std::cout << cfg.someValue << " " << ConfigMapStore<long>::it.at("someValue") << std::endl;
    cfg.someValue = 4321;
    std::cout << cfg.someValue << " " << ConfigMapStore<long>::it.at("someValue") << std::endl;
    std::cin.get();
}
added 1222 characters in body
Source Link
user52292
user52292

Full tested code in VS and Wandbox (unfortunatelly IdeOne currently knows only C++14 and not C++17)

#include <string>
#include <unordered_map>
#include <iostream>

template<class T> class ConfigValue;
template<class T> using ConfigMapImpl = std::unordered_map<std::string, ConfigValue<T>>;
template<class T> struct ConfigMapStore { static inline ConfigMapImpl<T> it = {}; };

template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value) : value(value) {
        ConfigMapStore<T>::it.insert({ name, *this });
    }
    // find copy and swap idiom for improvements, I will keep it simple here
    ConfigValue& operator=(const T& rhs) { value = rhs; return *this; }
    operator /*maybe const*/ T&() /*maybe const*/ { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
    // you can define macro for the above, but I would not do so
};

int main()
{
    Configuration cfg;
    std::cout << cfg.someValue << " ";
    cfg.someValue = 4321;
    std::cout << cfg.someValue << std::endl;
    std::cin.get();
}

Full tested code in VS and Wandbox (unfortunatelly IdeOne currently knows only C++14 and not C++17)

#include <string>
#include <unordered_map>
#include <iostream>

template<class T> class ConfigValue;
template<class T> using ConfigMapImpl = std::unordered_map<std::string, ConfigValue<T>>;
template<class T> struct ConfigMapStore { static inline ConfigMapImpl<T> it = {}; };

template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value) : value(value) {
        ConfigMapStore<T>::it.insert({ name, *this });
    }
    // find copy and swap idiom for improvements, I will keep it simple here
    ConfigValue& operator=(const T& rhs) { value = rhs; return *this; }
    operator /*maybe const*/ T&() /*maybe const*/ { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
    // you can define macro for the above, but I would not do so
};

int main()
{
    Configuration cfg;
    std::cout << cfg.someValue << " ";
    cfg.someValue = 4321;
    std::cout << cfg.someValue << std::endl;
    std::cin.get();
}
added 14 characters in body
Source Link
user52292
user52292
template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value): value(value) {
        ConfigMapStore<T>::it.include({name, *this}); }
    // find copy and swap idiom for improvements, I will keep it simple here
    ConfigValue& operator=(const T& rhs) { value = rhs; return *this; }
    operator /*maybe const*/ T&() /*maybe const*/ { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
    // you can define macro for the above, but I would not do so
};
  
template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value): value(value) {
        ConfigMapStore<T>::it.include({name, *this}); }
    // find copy and swap idiom for improvements, I will keep it simple here
    ConfigValue& operator=(const T& rhs) { value = rhs; }
    operator /*maybe const*/ T&() /*maybe const*/ { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
    // you can define macro for the above, but I would not do so
};
  
template<class T> class ConfigValue {
    T value;
public:
    ConfigValue(std::string name, const T& value): value(value) {
        ConfigMapStore<T>::it.include({name, *this}); }
    // find copy and swap idiom for improvements, I will keep it simple here
    ConfigValue& operator=(const T& rhs) { value = rhs; return *this; }
    operator /*maybe const*/ T&() /*maybe const*/ { return value; }
};
struct Configuration {
    ConfigValue<long> someValue = { "someValue", 1234 };
    // you can define macro for the above, but I would not do so
};
  
deleted 16 characters in body
Source Link
user52292
user52292
Loading
added 1441 characters in body
Source Link
user52292
user52292
Loading
Source Link
user52292
user52292
Loading