-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_object_msgpack_source.cpp
174 lines (137 loc) · 6.15 KB
/
script_object_msgpack_source.cpp
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
/*
* This file is part of libscriptobject.
*
* libscriptobject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* libscriptobject 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libscriptobject. If not, see <http://www.gnu.org/licenses/>.
**/
#include "script_object_msgpack_source.h"
#include "script_array_msgpack_source.h"
#include "script_object_factory.h"
#include "script_array_factory.h"
rs::scriptobject::ScriptObjectMsgpackSource::ScriptObjectMsgpackSource(const char* data, std::size_t length) : result_(msgpack::unpack(data, length)) {
auto obj = result_.get();
if (obj.type != msgpack::type::object_type::MAP) {
throw ScriptObjectMsgpackSourceParseException{"Bad source object type"};
}
map_ = obj.via.map.ptr;
count_ = obj.via.map.size;
}
rs::scriptobject::ScriptObjectMsgpackSource::ScriptObjectMsgpackSource(msgpack::object_kv* obj, unsigned count) : map_(obj), count_(count) {
}
unsigned rs::scriptobject::ScriptObjectMsgpackSource::count() const {
return count_;
}
rs::scriptobject::ScriptObjectType rs::scriptobject::ScriptObjectMsgpackSource::type(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
switch (map_[index].val.type) {
case msgpack::type::object_type::ARRAY: return ScriptObjectType::Array; break;
case msgpack::type::object_type::BOOLEAN: return ScriptObjectType::Boolean; break;
case msgpack::type::object_type::FLOAT: return ScriptObjectType::Double; break;
case msgpack::type::object_type::MAP: return ScriptObjectType::Object; break;
case msgpack::type::object_type::NEGATIVE_INTEGER: return ScriptObjectType::Double; break;
case msgpack::type::object_type::NIL: return ScriptObjectType::Null; break;
case msgpack::type::object_type::POSITIVE_INTEGER: return ScriptObjectType::Double; break;
case msgpack::type::object_type::STR: return ScriptObjectType::String; break;
default: return ScriptObjectType::Undefined; break;
}
}
const char* rs::scriptobject::ScriptObjectMsgpackSource::name(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
if (map_[index].key.type != msgpack::type::object_type::STR) {
throw ScriptObjectMsgpackSourceParseException{"Bad source field name"};
}
return map_[index].key.via.str.ptr;
}
unsigned rs::scriptobject::ScriptObjectMsgpackSource::length(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
if (map_[index].key.type != msgpack::type::object_type::STR) {
throw ScriptObjectMsgpackSourceParseException{"Bad source field name"};
}
return map_[index].key.via.str.size;
}
bool rs::scriptobject::ScriptObjectMsgpackSource::getBoolean(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
if (map_[index].val.type != msgpack::type::object_type::BOOLEAN) {
throw TypeCastException{};
}
return map_[index].val.via.boolean;
}
std::int32_t rs::scriptobject::ScriptObjectMsgpackSource::getInt32(int index) const {
throw TypeCastException{};
}
std::uint32_t rs::scriptobject::ScriptObjectMsgpackSource::getUInt32(int index) const {
throw TypeCastException{};
}
std::int64_t rs::scriptobject::ScriptObjectMsgpackSource::getInt64(int index) const {
throw TypeCastException{};
}
std::uint64_t rs::scriptobject::ScriptObjectMsgpackSource::getUInt64(int index) const {
throw TypeCastException{};
}
double rs::scriptobject::ScriptObjectMsgpackSource::getDouble(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
switch (map_[index].val.type) {
case msgpack::type::object_type::FLOAT: return map_[index].val.via.f64;
case msgpack::type::object_type::NEGATIVE_INTEGER: return map_[index].val.via.i64;
case msgpack::type::object_type::POSITIVE_INTEGER: return map_[index].val.via.u64;
default: throw TypeCastException{};
}
}
const char* rs::scriptobject::ScriptObjectMsgpackSource::getString(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
if (map_[index].val.type != msgpack::type::object_type::STR) {
throw TypeCastException{};
}
return map_[index].val.via.str.ptr;
}
int rs::scriptobject::ScriptObjectMsgpackSource::getStringLength(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
if (map_[index].val.type != msgpack::type::object_type::STR) {
throw TypeCastException{};
}
return map_[index].val.via.str.size;
}
const rs::scriptobject::ScriptObjectPtr rs::scriptobject::ScriptObjectMsgpackSource::getObject(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
if (map_[index].val.type != msgpack::type::object_type::MAP) {
throw TypeCastException{};
}
ScriptObjectMsgpackSource source{map_[index].val.via.map.ptr, map_[index].val.via.map.size};
return ScriptObjectFactory::CreateObject(source);
}
const rs::scriptobject::ScriptArrayPtr rs::scriptobject::ScriptObjectMsgpackSource::getArray(int index) const {
if (index >= count_) {
throw UnknownScriptObjectFieldException{};
}
if (map_[index].val.type != msgpack::type::object_type::ARRAY) {
throw TypeCastException{};
}
ScriptArrayMsgpackSource source{map_[index].val.via.array.ptr, map_[index].val.via.array.size};
return ScriptArrayFactory::CreateArray(source);
}