MongoDB  2.7.0
element-inl.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 namespace mongo {
19 namespace mutablebson {
20 
21  inline Element Element::operator[](size_t n) const {
22  return findNthChild(n);
23  }
24 
25  inline Element Element::operator[](const StringData& name) const {
26  return findFirstChildNamed(name);
27  }
28 
29  inline double Element::getValueDouble() const {
30  dassert(hasValue() && isType(mongo::NumberDouble));
31  return getValue()._numberDouble();
32  }
33 
34  inline StringData Element::getValueString() const {
35  dassert(hasValue() && isType(mongo::String));
36  return getValueStringOrSymbol();
37  }
38 
40  dassert(hasValue() && isType(mongo::Object));
41  return getValue().Obj();
42  }
43 
45  dassert(hasValue() && isType(mongo::Array));
46  return BSONArray(getValue().Obj());
47  }
48 
49  inline bool Element::isValueUndefined() const {
50  return isType(mongo::Undefined);
51  }
52 
53  inline OID Element::getValueOID() const {
54  dassert(hasValue() && isType(mongo::jstOID));
55  return getValue().__oid();
56  }
57 
58  inline bool Element::getValueBool() const {
59  dassert(hasValue() && isType(mongo::Bool));
60  return getValue().boolean();
61  }
62 
63  inline Date_t Element::getValueDate() const {
64  dassert(hasValue() && isType(mongo::Date));
65  return getValue().date();
66  }
67 
68  inline bool Element::isValueNull() const {
69  return isType(mongo::jstNULL);
70  }
71 
72  inline StringData Element::getValueSymbol() const {
73  dassert(hasValue() && isType(mongo::Symbol));
74  return getValueStringOrSymbol();
75  }
76 
77  inline int32_t Element::getValueInt() const {
78  dassert(hasValue() && isType(mongo::NumberInt));
79  return getValue()._numberInt();
80  }
81 
83  dassert(hasValue() && isType(mongo::Timestamp));
84  return getValue()._opTime();
85  }
86 
87  inline int64_t Element::getValueLong() const {
88  dassert(hasValue() && isType(mongo::NumberLong));
89  return getValue()._numberLong();
90  }
91 
92  inline bool Element::isValueMinKey() const {
93  return isType(mongo::MinKey);
94  }
95 
96  inline bool Element::isValueMaxKey() const {
97  return isType(mongo::MaxKey);
98  }
99 
100  inline bool Element::ok() const {
101  dassert(_doc != NULL);
102  return _repIdx <= kMaxRepIdx;
103  }
104 
106  return *_doc;
107  }
108 
109  inline const Document& Element::getDocument() const {
110  return *_doc;
111  }
112 
113  inline bool Element::isType(BSONType type) const {
114  return (getType() == type);
115  }
116 
117  inline Element::RepIdx Element::getIdx() const {
118  return _repIdx;
119  }
120 
121  inline Element::Element(Document* doc, RepIdx repIdx)
122  : _doc(doc)
123  , _repIdx(repIdx) {
124  dassert(_doc != NULL);
125  }
126 
127  inline StringData Element::getValueStringOrSymbol() const {
128  const BSONElement value = getValue();
129  const char* str = value.valuestr();
130  const size_t size = value.valuestrsize() - 1;
131  return StringData(str, size);
132  }
133 
134  inline bool operator==(const Element& l, const Element& r) {
135  return (l._doc == r._doc) && (l._repIdx == r._repIdx);
136  }
137 
138  inline bool operator!=(const Element& l, const Element& r) {
139  return !(l == r);
140  }
141 
142 
143 } // namespace mutablebson
144 } // namespace mongo
double _numberDouble() const
Return double value for this field.
Definition: bsonelement.h:201
bool hasValue() const
Returns true if 'getValue' can return a valid BSONElement from which a value may be extracted...
Definition: document.cpp:1456
BSONArray getValueArray() const
Get the value from an object valued Element.
Definition: element-inl.h:44
bool isValueUndefined() const
Returns true if this Element is the undefined type.
Definition: element-inl.h:49
larger than all other types
Definition: bsontypes.h:87
bool getValueBool() const
Get the value from a bool valued Element.
Definition: element-inl.h:58
OpTime getValueTimestamp() const
Get the value from a timestamp valued Element.
Definition: element-inl.h:82
bool ok() const
Returns true if this Element represents a valid part of the Document.
Definition: element-inl.h:100
const BSONElement getValue() const
Get the value of this element if available.
Definition: document.cpp:1482
Definition: time_support.h:35
bool isValueNull() const
Returns true if this Element is the null type.
Definition: element-inl.h:68
Updated to a Date with value next OpTime on insert.
Definition: bsontypes.h:81
OID getValueOID() const
Get the value from an OID valued Element.
Definition: element-inl.h:53
date type
Definition: bsontypes.h:65
boolean type
Definition: bsontypes.h:63
const mongo::OID & __oid() const
Retrieve the object ID stored in the object.
Definition: bsonelement.h:232
const char * valuestr() const
Get a string's value.
Definition: bsonelement.h:255
smaller than all other types
Definition: bsontypes.h:45
Date_t getValueDate() const
Get the value from a date valued Element.
Definition: element-inl.h:63
BSONType
the complete list of valid BSON types see also bsonspec.org
Definition: bsontypes.h:43
bool isValueMaxKey() const
Returns true if this Element is the max key type.
Definition: element-inl.h:96
ObjectId.
Definition: bsontypes.h:61
bool isType(BSONType type) const
Returns true if this Element is of the specified type.
Definition: element-inl.h:113
an embedded object
Definition: bsontypes.h:53
32 bit signed integer
Definition: bsontypes.h:79
double precision floating point value
Definition: bsontypes.h:49
null type
Definition: bsontypes.h:67
a programming language (e.g., Python) symbol
Definition: bsontypes.h:75
double getValueDouble() const
Get the value from a double valued Element.
Definition: element-inl.h:29
Document & getDocument()
Returns the Document to which this Element belongs.
Definition: element-inl.h:105
Definition: optime.h:41
Undefined type.
Definition: bsontypes.h:59
StringData getValueSymbol() const
Get the value from a symbol valued Element.
Definition: element-inl.h:72
Element represents a BSON value or object in a mutable BSON Document.
Definition: element.h:99
int32_t getValueInt() const
Get the value from an int valued Element.
Definition: element-inl.h:77
int64_t getValueLong() const
Get the value from a long valued Element.
Definition: element-inl.h:87
64 bit integer
Definition: bsontypes.h:83
an embedded array
Definition: bsontypes.h:55
int _numberInt() const
Return int value for this field.
Definition: bsonelement.h:203
BSONObj getValueObject() const
Get the value from an object valued Element.
Definition: element-inl.h:39
BSONObj Obj() const
Definition: bson-inl.h:801
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:62
BSONType getType() const
Returns the BSONType of this Element.
Definition: document.cpp:1921
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
int valuestrsize() const
Size (length) of a string element.
Definition: bsonelement.h:243
bool isValueMinKey() const
Returns true if this Element is the min key type.
Definition: element-inl.h:92
bool boolean() const
Definition: bsonelement.h:175
StringData getValueString() const
Get the value from a string valued Element.
Definition: element-inl.h:34
Element findNthChild(size_t n) const
Returns the nth child, if any, of this Element.
Definition: document.cpp:1382
Definition: bsonobj.h:585
Object ID type.
Definition: oid.h:42
Date_t date() const
Retrieve a java style date value from the element.
Definition: bsonelement.h:185
Element findFirstChildNamed(const StringData &name) const
Returns the first child, if any, of this Element named 'name'.
Definition: document.cpp:1392
long long _numberLong() const
Return long long value for this field.
Definition: bsonelement.h:205
Mutable BSON Overview.
Definition: document.h:221
character string, stored in utf8
Definition: bsontypes.h:51
RepIdx getIdx() const
Returns the opaque ID for this element.
Definition: element-inl.h:117