Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. template<typename ENUM>
  2. class Stringifier<ENUM, typename boost::enable_if<boost::is_enum<ENUM> >::type> {
  3. static const char * values[]; // array with the enum strings.
  4. static std::size_t size; // Number of elements of the ENUM string arrays.
  5. public:
  6. /// Global static instance of the Stringifier.
  7. static Stringifier & getInstance()
  8. {
  9. static Stringifier globalInstance;
  10. return globalInstance;
  11. }
  12. // Returns the string representation of the ENUM value \a e as a C string.
  13. // If string is not available an exception is thrown.
  14. virtual void str(ENUM const & e, std::string & s) const
  15. {
  16. if(e >= 0 && e < int(size))
  17. s = values[e];
  18. else // throw exception
  19. ;
  20. }
  21.  
  22. // Returns the ENUM value of the string representation of an ENUM value if possible,
  23. // ENUM(0) otherwise or ENUM(size) if you like.
  24. virtual bool value(std::string const & str, ENUM & v) const
  25. {
  26. std::size_t i = 0;
  27. for(; i < size; ++i)
  28. if(values[i] == str) break;
  29. bool ok = (i != size);
  30. v = ok ? ENUM(i) : ENUM(0);
  31. return ok;
  32. }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement