jsonPrimitive.hh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. virtual std::string getTypeStr() const =0;
  14. bool sameType(const AJSonPrimitive *other) const;
  15. };
  16. template <typename T>
  17. class JSonPrimitive: public JSonElement, public AJSonPrimitive
  18. {
  19. public:
  20. JSonPrimitive(JSonContainer *parent, T const &v);
  21. virtual ~JSonPrimitive();
  22. /**
  23. * get value as raw type
  24. **/
  25. T getValue() const;
  26. /**
  27. * get stringified value
  28. **/
  29. std::string stringify() const;
  30. std::string getTypeStr() const;
  31. bool operator<(const JSonPrimitive<T> &other) const;
  32. bool operator==(const JSonPrimitive<T> &other) const;
  33. protected:
  34. /**
  35. * convert raw type to string
  36. **/
  37. virtual std::string toString() const;
  38. /**
  39. * raw value
  40. **/
  41. const T value;
  42. /**
  43. * stringified value
  44. **/
  45. const std::string stringValue;
  46. };
  47. template<typename T>
  48. JSonPrimitive<T>::JSonPrimitive(JSonContainer *parent, T const &v): JSonElement(parent), value(v), stringValue(toString())
  49. { }
  50. template<typename T>
  51. T JSonPrimitive<T>::getValue() const
  52. { return value; }
  53. template<typename T>
  54. std::string JSonPrimitive<T>::stringify() const
  55. { return stringValue; }
  56. template<typename T>
  57. bool JSonPrimitive<T>::operator<(const JSonPrimitive<T> &other) const
  58. { return value < other.value; }
  59. template<typename T>
  60. bool JSonPrimitive<T>::operator==(const JSonPrimitive<T> &other) const
  61. { return value == other.value; }