Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename ENUM>
- class Stringifier<ENUM, typename boost::enable_if<boost::is_enum<ENUM> >::type> {
- static const char * values[]; // array with the enum strings.
- static std::size_t size; // Number of elements of the ENUM string arrays.
- public:
- /// Global static instance of the Stringifier.
- static Stringifier & getInstance()
- {
- static Stringifier globalInstance;
- return globalInstance;
- }
- // Returns the string representation of the ENUM value \a e as a C string.
- // If string is not available an exception is thrown.
- virtual void str(ENUM const & e, std::string & s) const
- {
- if(e >= 0 && e < int(size))
- s = values[e];
- else // throw exception
- ;
- }
- // Returns the ENUM value of the string representation of an ENUM value if possible,
- // ENUM(0) otherwise or ENUM(size) if you like.
- virtual bool value(std::string const & str, ENUM & v) const
- {
- std::size_t i = 0;
- for(; i < size; ++i)
- if(values[i] == str) break;
- bool ok = (i != size);
- v = ok ? ENUM(i) : ENUM(0);
- return ok;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement