jsonPrimitive.hh 1.8 KB

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