MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
bson_field.h
1/* Copyright 2012 10gen Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#pragma once
17
18#include <string>
19
20#include "mongo/bson/bsonobj.h"
21
22namespace mongo {
23
58template <typename T>
60public:
61 BSONFieldValue(const std::string& name, const T& t) : _name(name), _t(t) {}
62
63 const T& value() const {
64 return _t;
65 }
66 const std::string& name() const {
67 return _name;
68 }
69
70private:
71 std::string _name;
72 T _t;
73};
74
75template <typename T>
76class BSONField {
77public:
78 BSONField(const std::string& name) : _name(name), _defaultSet(false) {}
79
80 BSONField(const std::string& name, const T& defaultVal)
81 : _name(name), _default(defaultVal), _defaultSet(true) {}
82
83 BSONFieldValue<T> make(const T& t) const {
84 return BSONFieldValue<T>(_name, t);
85 }
86
87 BSONFieldValue<T> operator()(const T& t) const {
88 return BSONFieldValue<T>(_name, t);
89 }
90
91 const std::string& name() const {
92 return _name;
93 }
94
95 const T& getDefault() const {
96 return _default;
97 }
98
99 bool hasDefault() const {
100 return _defaultSet;
101 }
102
103 std::string operator()() const {
104 return _name;
105 }
106
107 BSONFieldValue<BSONObj> query(const char* q, const T& t) const;
108
109 BSONFieldValue<BSONObj> gt(const T& t) const {
110 return query("$gt", t);
111 }
112
113 BSONFieldValue<BSONObj> lt(const T& t) const {
114 return query("$lt", t);
115 }
116
117 BSONFieldValue<BSONObj> ne(const T& t) const {
118 return query("$ne", t);
119 }
120
121private:
122 std::string _name;
123 T _default;
124 bool _defaultSet;
125};
126
127} // namespace mongo
A BSONField holds the name and the type intended for a given BSON element.
Definition bson_field.h:59
Definition bson_field.h:76
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20