MongoDB  2.7.0
const_element.h
1 /* Copyright 2013 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 "mongo/bson/mutable/element.h"
19 
20 namespace mongo {
21 namespace mutablebson {
22 
36  class ConstElement {
37  public:
38 
39  // This one argument constructor is intentionally not explicit, since we want to be
40  // able to pass Elements to functions taking ConstElements without complaint.
41  inline ConstElement(const Element& basis);
42 
43  inline ConstElement leftChild() const;
44  inline ConstElement rightChild() const;
45  inline bool hasChildren() const;
46  inline ConstElement leftSibling(size_t distance = 1) const;
47  inline ConstElement rightSibling(size_t distance = 1) const;
48  inline ConstElement parent() const;
49  inline ConstElement findNthChild(size_t n) const;
50  inline ConstElement operator[](size_t n) const;
51  inline ConstElement findFirstChildNamed(const StringData& name) const;
52  inline ConstElement operator[](const StringData& n) const;
53  inline ConstElement findElementNamed(const StringData& name) const;
54 
55  inline size_t countSiblingsLeft() const;
56  inline size_t countSiblingsRight() const;
57  inline size_t countChildren() const;
58 
59  inline bool hasValue() const;
60  inline const BSONElement getValue() const;
61 
62  inline double getValueDouble() const;
63  inline StringData getValueString() const;
64  inline BSONObj getValueObject() const;
65  inline BSONArray getValueArray() const;
66  inline bool isValueUndefined() const;
67  inline OID getValueOID() const;
68  inline bool getValueBool() const;
69  inline Date_t getValueDate() const;
70  inline bool isValueNull() const;
71  inline StringData getValueSymbol() const;
72  inline int32_t getValueInt() const;
73  inline OpTime getValueTimestamp() const;
74  inline int64_t getValueLong() const;
75  inline bool isValueMinKey() const;
76  inline bool isValueMaxKey() const;
77  inline SafeNum getValueSafeNum() const;
78 
79  inline int compareWithElement(const ConstElement& other,
80  bool considerFieldName = true) const;
81 
82  inline int compareWithBSONElement(const BSONElement& other,
83  bool considerFieldName = true) const;
84 
85  inline int compareWithBSONObj(const BSONObj& other,
86  bool considerFieldName = true) const;
87 
88  inline void writeTo(BSONObjBuilder* builder) const;
89  inline void writeArrayTo(BSONArrayBuilder* builder) const;
90 
91  inline bool ok() const;
92  inline const Document& getDocument() const;
93  inline BSONType getType() const;
94  inline bool isType(BSONType type) const;
95  inline StringData getFieldName() const;
96  inline Element::RepIdx getIdx() const;
97 
98  inline std::string toString() const;
99 
100  friend bool operator==(const ConstElement&, const ConstElement&);
101 
102  private:
103  friend class Document;
104 
105  template<typename Builder>
106  inline void writeElement(Builder* builder, const StringData* fieldName = NULL) const;
107 
108  Element _basis;
109  };
110 
114  inline bool operator==(const ConstElement& l, const ConstElement& r);
115  inline bool operator!=(const ConstElement& l, const ConstElement& r);
116  inline bool operator==(const Element& l, const ConstElement& r);
117  inline bool operator!=(const Element& l, const ConstElement& r);
118  inline bool operator==(const ConstElement& l, const Element& r);
119  inline bool operator!=(const ConstElement& l, const Element& r);
120 
121 } // namespace mutablebson
122 } // namespace mongo
123 
124 #include "mongo/bson/mutable/const_element-inl.h"
125 
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:52
friend bool operator==(const ConstElement &, const ConstElement &)
See notes for operator==(const Element&, const Element&).
Definition: const_element-inl.h:203
Definition: time_support.h:35
BSONType
the complete list of valid BSON types see also bsonspec.org
Definition: bsontypes.h:43
SafeNum holds and does arithmetic on a number in a safe way, handling overflow and casting for the us...
Definition: safe_num.h:56
Definition: optime.h:41
Element represents a BSON value or object in a mutable BSON Document.
Definition: element.h:99
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:62
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
Definition: bsonobjbuilder.h:705
For an overview of mutable BSON, please see the file document.h in this directory.
Definition: const_element.h:36
Definition: bsonobj.h:585
Object ID type.
Definition: oid.h:42
Mutable BSON Overview.
Definition: document.h:221