96fa29092e603af1551c35d5a6ac431195319e07eb6c9b8394756ca6913b5427e0e0e21887a56665002ace7f3e81e97a577b81a9dda6067aab57f0fdc3c6f7 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef BSER_H
  2. #define BSER_H
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. #include <unordered_map>
  7. #include <memory>
  8. enum BSERType {
  9. BSER_ARRAY = 0x00,
  10. BSER_OBJECT = 0x01,
  11. BSER_STRING = 0x02,
  12. BSER_INT8 = 0x03,
  13. BSER_INT16 = 0x04,
  14. BSER_INT32 = 0x05,
  15. BSER_INT64 = 0x06,
  16. BSER_REAL = 0x07,
  17. BSER_BOOL_TRUE = 0x08,
  18. BSER_BOOL_FALSE = 0x09,
  19. BSER_NULL = 0x0a,
  20. BSER_TEMPLATE = 0x0b
  21. };
  22. class BSERValue;
  23. class BSER {
  24. public:
  25. typedef std::vector<BSER> Array;
  26. typedef std::unordered_map<std::string, BSER> Object;
  27. BSER();
  28. BSER(BSER::Array value);
  29. BSER(BSER::Object value);
  30. BSER(std::string value);
  31. BSER(const char *value);
  32. BSER(int64_t value);
  33. BSER(double value);
  34. BSER(bool value);
  35. BSER(std::istream &iss);
  36. BSER::Array arrayValue();
  37. BSER::Object objectValue();
  38. std::string stringValue();
  39. int64_t intValue();
  40. double doubleValue();
  41. bool boolValue();
  42. void encode(std::ostream &oss);
  43. static int64_t decodeLength(std::istream &iss);
  44. std::string encode();
  45. private:
  46. std::shared_ptr<BSERValue> m_ptr;
  47. };
  48. class BSERValue {
  49. protected:
  50. friend class BSER;
  51. virtual BSER::Array arrayValue() { return BSER::Array(); }
  52. virtual BSER::Object objectValue() { return BSER::Object(); }
  53. virtual std::string stringValue() { return std::string(); }
  54. virtual int64_t intValue() { return 0; }
  55. virtual double doubleValue() { return 0; }
  56. virtual bool boolValue() { return false; }
  57. virtual void encode(std::ostream &oss) {}
  58. virtual ~BSERValue() {}
  59. };
  60. #endif