MongoDB  2.7.0
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 
60  template<typename T>
62  public:
63  BSONFieldValue(const std::string& name, const T& t)
64  : _name(name), _t(t) { }
65 
66  const T& value() const { return _t; }
67  const std::string& name() const { return _name; }
68 
69  private:
70  std::string _name;
71  T _t;
72  };
73 
74  template<typename T>
75  class BSONField {
76  public:
77  BSONField(const std::string& name)
78  : _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 
121  private:
122  std::string _name;
123  T _default;
124  bool _defaultSet;
125  };
126 
127 } // namespace mongo
Definition: bson_field.h:75
A BSONField holds the name and the type intended for a given BSON element.
Definition: bson_field.h:61