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
|
/* Copyright 2012 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <string>
#include "mongo/bson/bsonobj.h"
namespace mongo {
/**
* A BSONField holds the name and the type intended for a given BSON element. The
* class helps documenting and enforcing that field's type.
*
* Example usages:
*
* In a header file:
* // Determines the types for the fields used in a collection.
* static const string MyColl;
* struct MyCollFields {
* static BSONField<string> name;
* static BSONField<bool> draining;
* static BSONField<int> count;
* };
*
* In a cpp file:
* const string MyColl = "my_collection_name";
*
* // determines the names used for the fields
* BSONField<string> MyCollFields::name("_id");
* BSONField<bool> MyCollFields::draining("draining");
* BSONField<int> MyCollFields::count("count");
*
* In an insert:
* conn->insert(myColl,
* BSON(MyCollFields::name("id_for_this_doc") <<
* MyCollFields::draining(true) <<
* MyCollFields::count(0)));
*
* In a query:
* conn->findOne(myColl, BSON(MyCollFields::count.gt(10))) ;
*
* In a command:
* conn->ensureIndex(mycoll, BSON(MyCollFields::draining() << 1), true);
*/
template<typename T>
class BSONFieldValue {
public:
BSONFieldValue(const std::string& name, const T& t)
: _name(name), _t(t) { }
const T& value() const { return _t; }
const std::string& name() const { return _name; }
private:
std::string _name;
T _t;
};
template<typename T>
class BSONField {
public:
BSONField(const std::string& name)
: _name(name), _defaultSet(false) {}
BSONField(const std::string& name, const T& defaultVal)
: _name(name), _default(defaultVal) , _defaultSet(true) {}
BSONFieldValue<T> make(const T& t) const {
return BSONFieldValue<T>(_name, t);
}
BSONFieldValue<T> operator()(const T& t) const {
return BSONFieldValue<T>(_name, t);
}
const std::string& name() const {
return _name;
}
const T& getDefault() const {
return _default;
}
bool hasDefault() const {
return _defaultSet;
}
std::string operator()() const {
return _name;
}
BSONFieldValue<BSONObj> query(const char * q, const T& t) const;
BSONFieldValue<BSONObj> gt(const T& t) const {
return query("$gt", t);
}
BSONFieldValue<BSONObj> lt(const T& t) const {
return query("$lt", t);
}
BSONFieldValue<BSONObj> ne(const T& t) const {
return query("$ne", t);
}
private:
std::string _name;
T _default;
bool _defaultSet;
};
} // namespace mongo
|