jsonPrimitive.hh 1.6 KB

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