summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bson_field.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/bson/bson_field.h')
-rw-r--r--src/mongo/bson/bson_field.h210
1 files changed, 106 insertions, 104 deletions
diff --git a/src/mongo/bson/bson_field.h b/src/mongo/bson/bson_field.h
index eea8fa7027d..16f3cff72ab 100644
--- a/src/mongo/bson/bson_field.h
+++ b/src/mongo/bson/bson_field.h
@@ -33,107 +33,109 @@
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 std::string MyColl;
- * struct MyCollFields {
- * static BSONField<std::string> name;
- * static BSONField<bool> draining;
- * static BSONField<int> count;
- * };
- *
- * In a cpp file:
- * const std::string MyColl = "my_collection_name";
- *
- * // determines the names used for the fields
- * BSONField<std::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
+/**
+ * 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 std::string MyColl;
+ * struct MyCollFields {
+ * static BSONField<std::string> name;
+ * static BSONField<bool> draining;
+ * static BSONField<int> count;
+ * };
+ *
+ * In a cpp file:
+ * const std::string MyColl = "my_collection_name";
+ *
+ * // determines the names used for the fields
+ * BSONField<std::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