MongoDB C++ Driver  legacy-1.0.3
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
22 namespace mongo {
23 
58  template<typename T>
60  public:
61  BSONFieldValue(const std::string& name, const T& t)
62  : _name(name), _t(t) { }
63 
64  const T& value() const { return _t; }
65  const std::string& name() const { return _name; }
66 
67  private:
68  std::string _name;
69  T _t;
70  };
71 
72  template<typename T>
73  class BSONField {
74  public:
75  BSONField(const std::string& name)
76  : _name(name), _defaultSet(false) {}
77 
78  BSONField(const std::string& name, const T& defaultVal)
79  : _name(name), _default(defaultVal) , _defaultSet(true) {}
80 
81  BSONFieldValue<T> make(const T& t) const {
82  return BSONFieldValue<T>(_name, t);
83  }
84 
85  BSONFieldValue<T> operator()(const T& t) const {
86  return BSONFieldValue<T>(_name, t);
87  }
88 
89  const std::string& name() const {
90  return _name;
91  }
92 
93  const T& getDefault() const {
94  return _default;
95  }
96 
97  bool hasDefault() const {
98  return _defaultSet;
99  }
100 
101  std::string operator()() const {
102  return _name;
103  }
104 
105  BSONFieldValue<BSONObj> query(const char * q, const T& t) const;
106 
107  BSONFieldValue<BSONObj> gt(const T& t) const {
108  return query("$gt", t);
109  }
110 
111  BSONFieldValue<BSONObj> lt(const T& t) const {
112  return query("$lt", t);
113  }
114 
115  BSONFieldValue<BSONObj> ne(const T& t) const {
116  return query("$ne", t);
117  }
118 
119  private:
120  std::string _name;
121  T _default;
122  bool _defaultSet;
123  };
124 
125 } // namespace mongo
Definition: bson_field.h:73
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
A BSONField holds the name and the type intended for a given BSON element.
Definition: bson_field.h:59