Aさん
Revision | a090ce0908dc821eb41b2d0355478e233a6c1461 (tree) |
---|---|
Zeit | 2011-01-14 03:18:27 |
Autor | berupon <berupon@gmai...> |
Commiter | berupon |
base interface
@@ -0,0 +1,117 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | +// http://bsonspec.org/#/specification | |
4 | + | |
5 | +namespace BSON { | |
6 | + | |
7 | +enum ElementType { | |
8 | + ElementType_Double = 0x01, | |
9 | + ElementType_String = 0x02, | |
10 | + ElementType_Document = 0x03, | |
11 | + ElementType_Array = 0x04, | |
12 | + ElementType_Binary = 0x05, | |
13 | + ElementType_ObjectId = 0x07, | |
14 | + ElementType_Boolean = 0x08, | |
15 | + ElementType_UTCdatetime = 0x09, | |
16 | + ElementType_Null = 0x0A, | |
17 | + ElementType_Regex = 0x0B, | |
18 | + ElementType_JavaScriptCode = 0x0D, | |
19 | + ElementType_Symbol = 0x0E, | |
20 | + ElementType_JavaScriptCodeWithScope = 0x0F, | |
21 | + ElementType_Int32 = 0x10, | |
22 | + ElementType_TimeStamp = 0x11, | |
23 | + ElementType_Int64 = 0x12, | |
24 | + ElementType_MinKey = 0xFF, | |
25 | + ElementType_MaxKey = 0x7F, | |
26 | +}; | |
27 | + | |
28 | +struct IReader { | |
29 | + const char* GetName() const; | |
30 | + ElementType GetType() const; | |
31 | + const char* GetDataPtr() const; | |
32 | + const char** GetCurrentPath() const; | |
33 | + | |
34 | + bool ReadNextElement(); | |
35 | + bool EnterDocument(); | |
36 | + bool LeaveDocument(); | |
37 | +}; | |
38 | + | |
39 | +enum BinarySubType { | |
40 | + BinarySubType_Generic = 0x00, | |
41 | + BinarySubType_Function = 0x01, | |
42 | + BinarySubType_OldBinary = 0x02, | |
43 | + BinarySubType_UUID = 0x03, | |
44 | + BinarySubType_MD5 = 0x05, | |
45 | + BinarySubType_UserDefined = 0x80, | |
46 | +}; | |
47 | + | |
48 | +struct IWriter { | |
49 | + void WriteElement_Double(const char* name, double val) { WriteElementHead(ElementType_Double, name); WriteDouble(val); } | |
50 | + void WriteElement_String(const char* name, const char* str, uint32_t strLen) { WriteElementHead(ElementType_String, name); WriteString(str, strLen); } | |
51 | + void WriteElement_Document(const char* name, uint32_t nBytes, const char* elements) { WriteElementHead(ElementType_Document, name); WriteDocument(nBytes, elements); } | |
52 | + void WriteElement_Binary(const char* name, uint32_t nBytes, BinarySubType subType, const char* data) | |
53 | + { WriteElementHead(ElementType_Binary, name); WriteBinary(nBytes, subType, data); } | |
54 | + void WriteElement_Boolean(const char* name, bool val) { WriteElementHead(ElementType_Boolean, name); WriteInt8(val ? 1 : 0); } | |
55 | + void WriteElement_Null(const char* name) { WriteElementHead(ElementType_Null, name); } | |
56 | + void WriteElement_Regex(const char* name, const char* pattern, const char* option) { WriteElementHead(ElementType_Regex, name); WriteCString(pattern); WriteCString(option); } | |
57 | + void WriteElement_Int32(const char* name, int32_t val) { WriteElementHead(ElementType_Int32, name); WriteInt32(val); } | |
58 | + void WriteElement_Int64(const char* name, int64_t val) { WriteElementHead(ElementType_Int64, name); WriteInt64(val); } | |
59 | + void WriteElement(const char* name, ElementType type, const char* data) { | |
60 | + WriteElementHead(type, name); | |
61 | + size_t len; | |
62 | + switch (type) { | |
63 | + case ElementType_String: | |
64 | + case ElementType_JavaScriptCode: | |
65 | + case ElementType_Symbol: | |
66 | + break; | |
67 | + case ElementType_Document: | |
68 | + break; | |
69 | + case ElementType_Binary: | |
70 | + break; | |
71 | + case ElementType_Boolean: | |
72 | + len = 1; | |
73 | + break; | |
74 | + case ElementType_Null: | |
75 | + len = 0; | |
76 | + break; | |
77 | + case ElementType_Regex: | |
78 | + break; | |
79 | + case ElementType_Int32: | |
80 | + len = 4; | |
81 | + break; | |
82 | + case ElementType_Double: | |
83 | + case ElementType_UTCdatetime: | |
84 | + case ElementType_TimeStamp: | |
85 | + case ElementType_Int64: | |
86 | + len = 8; | |
87 | + break; | |
88 | + } | |
89 | + Write(data, len); | |
90 | + } | |
91 | + void EnterDocument(); // start writing document | |
92 | + void LeaveDocument(); | |
93 | + | |
94 | + // device | |
95 | + void Write(const char* data, uint32_t len); | |
96 | + | |
97 | + // impl | |
98 | + void WriteElementHead(ElementType type, const char* name) { WriteInt8(type); WriteCString(name); } | |
99 | + void WriteDouble(double val) { Write(reinterpret_cast<const char*>(&val), 8); } | |
100 | + void WriteInt8(int8_t val) { Write(reinterpret_cast<const char*>(&val), 1); } | |
101 | + void WriteInt32(int32_t val) { Write(reinterpret_cast<const char*>(&val), 4); } | |
102 | + void WriteInt64(int64_t val) { Write(reinterpret_cast<const char*>(&val), 8); } | |
103 | + void WriteString(const char* str, uint32_t strLen) { WriteInt32(strLen); Write(str, strLen); WriteInt8(0x00); } | |
104 | + void WriteCString(const char* str) { Write(str, strlen(str)); } | |
105 | + void WriteDocument(uint32_t nBytes, const char* elements) { WriteInt32(nBytes); Write(elements, nBytes-4-1); WriteInt8(0x00); } | |
106 | + void WriteBinary(uint32_t nBytes, BinarySubType subType, const char* data) { WriteInt32(nBytes); WriteInt8(subType); Write(data, nBytes); } | |
107 | + | |
108 | +}; | |
109 | + | |
110 | +struct FileReader : IReader { | |
111 | + const char* name_; | |
112 | + ElementType type_; | |
113 | + std::vector<const char*> path_; | |
114 | +}; | |
115 | + | |
116 | + | |
117 | +} // namespace BSON |
@@ -0,0 +1,7 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | +namespace BSON { | |
4 | + | |
5 | + | |
6 | + | |
7 | +} // namespace BSON |