Last active
July 18, 2019 07:28
-
-
Save bryce-ebp/048b3a3c67ee137ae999db407745eb6b to your computer and use it in GitHub Desktop.
Simple option handler for settings class
This file contains 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 <variant> | |
template<typename T> | |
class Option { | |
public: | |
Option(const T val) | |
: variant(val) { } | |
const T &Get() const { | |
return std::get<T>(variant); | |
} | |
operator bool() const { | |
return std::get<T>(variant); | |
} | |
private: | |
std::variant<int, bool, float> variant{ }; | |
}; | |
// example use: | |
class Settings { | |
public: | |
Option<bool> value_{ false }; | |
Option<bool> other_value_{ true }; | |
}; | |
int main( ) { | |
Settings setting{ }; | |
if (setting.value_.Get() | |
|| setting.other_value_.Get()) | |
return 1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment