diff options
author | Mathias Stearn <mathias@10gen.com> | 2015-03-16 18:35:22 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2015-03-18 12:16:41 -0400 |
commit | 78541981780b9d3608ce6d43167e230ed03c6444 (patch) | |
tree | 6712a62d3f5e59a070f372e7fb4352be29721ca0 /src/mongo/bson | |
parent | a15dbb65cab3cc896f7dcda75cd471e3783b9bc4 (diff) | |
download | mongo-78541981780b9d3608ce6d43167e230ed03c6444.tar.gz |
SERVER-17640 Add explict BSONElement::operator bool()
Makes it easier to check for a field's existence and use it in a single scan:
if (auto elem = myObj["foo"]) {
// Use elem
}
else {
// default behavior
}
Diffstat (limited to 'src/mongo/bson')
-rw-r--r-- | src/mongo/bson/bsonelement.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h index 9737afd3e0a..d31aacb69f3 100644 --- a/src/mongo/bson/bsonelement.h +++ b/src/mongo/bson/bsonelement.h @@ -114,6 +114,19 @@ namespace mongo { */ bool ok() const { return !eoo(); } + /** + * True if this element has a value (ie not EOO). + * + * Makes it easier to check for a field's existence and use it: + * if (auto elem = myObj["foo"]) { + * // Use elem + * } + * else { + * // default behavior + * } + */ + explicit operator bool() const { return ok(); } + std::string toString( bool includeFieldName = true, bool full=false) const; void toString(StringBuilder& s, bool includeFieldName = true, bool full=false, int depth=0) const; std::string jsonString( JsonStringFormat format, bool includeFieldNames = true, int pretty = 0 ) const; |