jsonPrimitive.hh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * jsonPrimitive.hh for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #pragma once
  7. #include "jsonElement.hh"
  8. class AJSonPrimitive
  9. {
  10. public:
  11. virtual ~AJSonPrimitive();
  12. };
  13. template <typename T>
  14. class JSonPrimitive: public JSonElement, public AJSonPrimitive
  15. {
  16. public:
  17. JSonPrimitive(JSonContainer *parent, T const &v);
  18. virtual ~JSonPrimitive();
  19. /**
  20. * get value as raw type
  21. **/
  22. T getValue() const;
  23. /**
  24. * get stringified value
  25. **/
  26. std::string stringify() const;
  27. bool operator<(const JSonPrimitive<T> &other) const;
  28. bool operator==(const JSonPrimitive<T> &other) const;
  29. protected:
  30. /**
  31. * convert raw type to string
  32. **/
  33. virtual std::string toString() const;
  34. /**
  35. * raw value
  36. **/
  37. const T value;
  38. /**
  39. * stringified value
  40. **/
  41. const std::string stringValue;
  42. };
  43. template<typename T>
  44. JSonPrimitive<T>::JSonPrimitive(JSonContainer *parent, T const &v): JSonElement(parent), value(v), stringValue(toString())
  45. { }
  46. template<typename T>
  47. T JSonPrimitive<T>::getValue() const
  48. { return value; }
  49. template<typename T>
  50. std::string JSonPrimitive<T>::stringify() const
  51. { return stringValue; }
  52. template<typename T>
  53. bool JSonPrimitive<T>::operator<(const JSonPrimitive<T> &other) const
  54. { return value < other.value; }
  55. template<typename T>
  56. bool JSonPrimitive<T>::operator==(const JSonPrimitive<T> &other) const
  57. { return value == other.value; }