Last active
December 18, 2015 19:39
-
-
Save Shaptic/5834511 to your computer and use it in GitHub Desktop.
Generic option class that can act as any 'plain-old-data' type.
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 <string> | |
#include <ostream> | |
// This is in a different header, but we include it here | |
// for completeness-sake. | |
namespace math | |
{ | |
static inline | |
bool compf(const float a, const float b, | |
const float threshold = 0.0001) | |
{ | |
return (a + threshold > b && a - threshold < b); | |
} | |
} | |
typedef std::string string_t; | |
/// Used internally by CSettings to manage options of all types. | |
class COption | |
{ | |
public: | |
COption(); | |
COption(const string_t& value); | |
COption(const COption& Opt); | |
COption& operator=(const COption& Opt); ///< Create from option | |
COption& operator=(const string_t& name); ///< Create from string | |
COption& operator=(const char* name); ///< Create from C-string | |
COption& operator=(const bool name); ///< Create from bool | |
/// Create from numeric types. | |
template<typename T> | |
inline COption& operator=(const T& name) | |
{ | |
m_value = std::to_string(name); | |
return (*this); | |
} | |
// Implicit conversion operators. | |
inline operator string_t() const { return m_value; } | |
inline operator int() const { return std::stoi(m_value); } | |
inline operator size_t() const { return std::stoi(m_value); } | |
inline operator real_t() const { return std::stof(m_value); } | |
inline operator bool() const { return m_value != "0"; } | |
// Comparison operators | |
bool operator==(const COption& value) const; | |
bool operator==(const string_t& value) const; | |
bool operator==(const bool value) const; | |
bool operator==(const float value) const; | |
template<typename T> inline | |
bool operator==(const T& value) const | |
{ return (std::to_string(value) == m_value); } | |
/// Outputting an option value | |
friend std::ostream& | |
operator<<(std::ostream& o, const COption& Opt); | |
private: | |
string_t m_value; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment