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