-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.h
53 lines (41 loc) · 1.14 KB
/
util.h
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
#ifndef __MONGO_X_CLIENT_UTIL_H
#define __MONGO_X_CLIENT_UTIL_H
#include <bsoncxx/document/view.hpp>
#include <xpack/bson.h>
namespace mongoxc {
class Util {
public:
template<typename T>
static std::string kv(const char *key, const T&val) {
xpack::BsonWriter wr;
xpack::XEncoder<xpack::BsonWriter> en(wr);
en.ob(NULL);
en.add(key, val);
en.oe(NULL);
return en.String();
}
template <typename DATA>
static int ViewToInterface(const bsoncxx::document::view_or_value &v, DATA &result) {
try {
xpack::bson::decode(v.view().data(), 0, result);
return 0;
} catch (...) {
return -1;
}
}
template <typename DATA>
static int CursorToVector(mongocxx::cursor& cursor, std::vector<DATA> &result) {
for (auto iter=cursor.begin(); iter!=cursor.end(); ++iter) {
DATA data;
try {
xpack::bson::decode((*iter).data(), 0, data);
result.push_back(data);
} catch (...) {
continue;
}
}
return 0;
}
};
}
#endif