-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.cpp
More file actions
218 lines (210 loc) · 6.42 KB
/
element.cpp
File metadata and controls
218 lines (210 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright (c) Nathan Eloe, 2014
This file is part of FastBSON-Cpp.
FastBSON-Cpp is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FastBSON-Cpp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with FastBSON-Cpp. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file element.hpp
* \author Nathan Eloe
* \brief Implementation of the BSON element class
*/
#include "element.h"
#include "typeinfo.h"
#include <memory>
#include <vector>
namespace bson
{
Element::Element (const char *data, const TypeInfo type)
{
m_type = type;
if (!m_type)
m_type = default_type<std::string>();
type_check<std::string>();
m_data = std::static_pointer_cast<void> (std::make_shared<std::string> (std::string (data)));
}
unsigned Element::decode (const unsigned char *bytes, const TypeInfo type)
{
unsigned num_bytes = -1;
m_type = type;
switch (m_type)
{
case INT64:
case DATETIME:
case TIMESTAMP:
num_bytes = deserialize_bytes<long> (bytes);
break;
case INT32:
num_bytes = deserialize_bytes<int> (bytes);
break;
case STRING:
case JS:
case DEPRECATED:
num_bytes = deserialize_bytes<std::string> (bytes);
break;
case BOOL:
num_bytes = deserialize_bytes<bool> (bytes);
break;
case FLOATING:
num_bytes = deserialize_bytes<double> (bytes);
break;
case DOCUMENT:
num_bytes = deserialize_bytes<Document> (bytes);
break;
case ARRAY:
num_bytes = deserialize_bytes<array> (bytes);
break;
case OID:
num_bytes = deserialize_bytes<oid> (bytes);
break;
case NIL:
case MINKEY:
case MAXKEY:
case UNDEF:
num_bytes = deserialize_bytes<void> (bytes);
break;
case REGEX:
num_bytes = deserialize_bytes<regex> (bytes);
break;
case DBPTR:
num_bytes = deserialize_bytes<dbptr> (bytes);
break;
case JS_SCOPE:
num_bytes = deserialize_bytes<jscode_scope> (bytes);
break;
case BINARY:
num_bytes = deserialize_bytes<binary> (bytes);
break;
case _UNKNOWN:
throw type_UNKNOWN();
break;
}
return num_bytes;
}
void Element::encode (std::ostringstream &oss) const
{
switch (m_type)
{
case INT64:
case DATETIME:
case TIMESTAMP:
serialize_bson<long> (oss);
break;
case INT32:
serialize_bson<int> (oss);
break;
case STRING:
case JS:
case DEPRECATED:
serialize_bson<std::string> (oss);
break;
case BOOL:
serialize_bson<bool> (oss);
break;
case FLOATING:
serialize_bson<double> (oss);
break;
case DOCUMENT:
serialize_bson<Document> (oss);
break;
case ARRAY:
serialize_bson<array> (oss);
break;
case OID:
serialize_bson<oid> (oss);
break;
case NIL:
case MINKEY:
case MAXKEY:
case UNDEF:
serialize_bson<void> (oss);
break;
case REGEX:
serialize_bson<regex> (oss);
break;
case DBPTR:
serialize_bson<dbptr> (oss);
break;
case JS_SCOPE:
serialize_bson<jscode_scope> (oss);
break;
case BINARY:
serialize_bson<binary> (oss);
break;
case _UNKNOWN:
throw type_UNKNOWN();
break;
}
return;
}
Element::operator std::string() const
{
std::string result;
switch (m_type)
{
case INT64:
case DATETIME:
case TIMESTAMP:
result = _to_std_str<long>();
break;
case INT32:
result = _to_std_str<int>();
break;
case STRING:
case JS:
case DEPRECATED:
result = _to_std_str<std::string>();
break;
case BOOL:
result = _to_std_str<bool>();
break;
case FLOATING:
result = _to_std_str<double>();
break;
case DOCUMENT:
result = _to_std_str<Document>();
break;
case ARRAY:
result = _to_std_str<array>();
break;
case OID:
result = _to_std_str<oid>();
break;
case NIL:
case MINKEY:
case MAXKEY:
case UNDEF:
result = _to_std_str<void>();
break;
case REGEX:
result = _to_std_str<regex>();
break;
case DBPTR:
result = _to_std_str<dbptr>();
break;
case JS_SCOPE:
result = _to_std_str<jscode_scope>();
break;
case BINARY:
result = _to_std_str<binary>();
break;
case _UNKNOWN:
throw type_UNKNOWN();
break;
}
return result;
}
void Element::make_void (const TypeInfo type)
{
m_type = type == _UNKNOWN ? default_type<void>() : type;
type_check<void>();
}
}