-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathhelper.h
More file actions
163 lines (143 loc) · 4.79 KB
/
helper.h
File metadata and controls
163 lines (143 loc) · 4.79 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
#pragma once
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <bson.h>
using json = rapidjson::Document;
namespace rosbridge2cpp {
class Helper {
public:
Helper() = default;
~Helper() = default;
std::string static get_string_from_rapidjson(json &d)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
return buffer.GetString();
}
std::string static get_string_from_rapidjson(const json &d)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
return buffer.GetString();
}
// dot_notation refers to MongoDB dot notation
// returns "" and sets success to true if suitable data can't be found via the dot notation
std::string static get_utf8_by_key(const char *dot_notation, bson_t &b, bool &success)
{
bson_iter_t iter;
bson_iter_t val;
if (bson_iter_init(&iter, &b) &&
bson_iter_find_descendant(&iter, dot_notation, &val) &&
BSON_ITER_HOLDS_UTF8(&val)) {
success = true;
return std::string(bson_iter_utf8(&val, NULL));
}
success = false;
return "";
}
// dot_notation refers to MongoDB dot notation
// returns INT32_MAX and sets success to 'false' if suitable data can't be found via the dot notation
int32_t static get_int32_by_key(const char *dot_notation, bson_t &b, bool &success)
{
bson_iter_t iter;
bson_iter_t val;
if (bson_iter_init(&iter, &b) &&
bson_iter_find_descendant(&iter, dot_notation, &val) &&
BSON_ITER_HOLDS_INT32(&val)) {
success = true;
return bson_iter_int32(&val);
}
success = false;
return INT32_MAX;
}
// dot_notation refers to MongoDB dot notation
// returns -1 and sets success to 'false' if suitable data can't be found via the dot notation
double static get_double_by_key(const char *dot_notation, bson_t &b, bool &success)
{
bson_iter_t iter;
bson_iter_t val;
// if (bson_iter_init (&iter, &b) &&
// bson_iter_find_descendant (&iter, dot_notation, &val) &&
// BSON_ITER_HOLDS_DOUBLE (&val)) {
if (bson_iter_init(&iter, &b) &&
bson_iter_find_descendant(&iter, dot_notation, &val)) {
if (!BSON_ITER_HOLDS_DOUBLE(&val)) {
std::cout << "Key found, but not double typed" << std::endl;
}
else {
//std::cout << "Key " << dot_notation << " found. success is = " << success << std::endl;
//std::cout << "Key " << dot_notation << " found." << std::endl;
success = true;
//std::cout << " success is now = " << success << std::endl;
return bson_iter_double(&val);
}
}
std::cout << "Couldn't find descendant" << std::endl;
success = false;
return -1.0;
}
// dot_notation refers to MongoDB dot notation
// returns false and sets success to 'false' if suitable data can't be found via the dot notation
bool static get_bool_by_key(const char *dot_notation, bson_t &b, bool &success)
{
bson_iter_t iter;
bson_iter_t val;
if (bson_iter_init(&iter, &b) &&
bson_iter_find_descendant(&iter, dot_notation, &val) &&
BSON_ITER_HOLDS_BOOL(&val)) {
success = true;
return bson_iter_bool(&val);
}
success = false;
return false;
}
// dot_notation refers to MongoDB dot notation
// returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation
//
// binary_data_length holds the size of the buffer where the returned pointer points to.
static const uint8_t * get_binary_by_key(const char *dot_notation, bson_t &b, uint32_t &binary_data_length, bool &success)
{
bson_iter_t iter;
bson_iter_t val;
if (bson_iter_init(&iter, &b) &&
bson_iter_find_descendant(&iter, dot_notation, &val) &&
BSON_ITER_HOLDS_BINARY(&val)) {
bson_subtype_t subtype;
const uint8_t *binary;
bson_iter_binary(&val, &subtype, &binary_data_length, &binary);
assert(binary);
success = true;
return binary;
}
success = false;
return nullptr;
}
// dot_notation refers to MongoDB dot notation
// returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation
//
// array_size holds the size in byte of the buffer where the returned pointer points to.
static const uint8_t * get_array_by_key(const char *dot_notation, bson_t &b, uint32_t &array_size, bool &success)
{
bson_iter_t iter;
bson_iter_t val;
if (bson_iter_init(&iter, &b) &&
bson_iter_find_descendant(&iter, dot_notation, &val) &&
BSON_ITER_HOLDS_ARRAY(&val)) {
const uint8_t *binary;
bson_iter_array(&val, &array_size, &binary);
assert(binary);
success = true;
return binary;
}
success = false;
return nullptr;
}
bool static bson_has_key(bson_t &b, const char *key)
{
return bson_has_field(&b, key);
}
};
}