diff options
author | Mathias Stearn <mathias@10gen.com> | 2016-06-02 17:55:48 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2016-06-09 13:00:49 -0400 |
commit | b354fbae71aea007759e174c31a869f55fe6502f (patch) | |
tree | 4dad411c68a1b6dd9184438e7777140519d6c65a /src | |
parent | 6335744b8af97e4f5b3fb15118f24833df6e7047 (diff) | |
download | mongo-b354fbae71aea007759e174c31a869f55fe6502f.tar.gz |
SERVER-4536 Support appending an int64_t to a BSONObjBuilder
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/bson/bsonobjbuilder.h | 15 | ||||
-rw-r--r-- | src/mongo/bson/bsonobjbuilder_test.cpp | 10 | ||||
-rw-r--r-- | src/mongo/bson/util/builder.h | 10 |
3 files changed, 35 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonobjbuilder.h b/src/mongo/bson/bsonobjbuilder.h index 3a3f91a60cf..74593287942 100644 --- a/src/mongo/bson/bsonobjbuilder.h +++ b/src/mongo/bson/bsonobjbuilder.h @@ -35,6 +35,7 @@ #pragma once #include <cmath> +#include <cstdint> #include <limits> #include <map> @@ -45,6 +46,7 @@ #include "mongo/bson/bsonmisc.h" #include "mongo/bson/bsonobj.h" #include "mongo/platform/decimal128.h" +#include "mongo/stdx/type_traits.h" #include "mongo/util/itoa.h" namespace mongo { @@ -270,6 +272,19 @@ public: return *this; } + /** + * Append a NumberLong (if int64_t isn't the same as long long) + */ + template <typename Int64_t, + typename = stdx::enable_if_t<std::is_same<Int64_t, int64_t>::value && + !std::is_same<int64_t, long long>::value>> + BSONObjBuilder& append(StringData fieldName, Int64_t n) { + _b.appendNum((char)NumberLong); + _b.appendStr(fieldName); + _b.appendNum(n); + return *this; + } + /** appends a number. if n < max(int)/2 then uses int, otherwise long long */ BSONObjBuilder& appendIntOrLL(StringData fieldName, long long n) { // extra () to avoid max macro on windows diff --git a/src/mongo/bson/bsonobjbuilder_test.cpp b/src/mongo/bson/bsonobjbuilder_test.cpp index f9bad62059f..4f11df260bb 100644 --- a/src/mongo/bson/bsonobjbuilder_test.cpp +++ b/src/mongo/bson/bsonobjbuilder_test.cpp @@ -36,6 +36,7 @@ #include "mongo/unittest/unittest.h" #include <sstream> +namespace mongo { namespace { using mongo::BSONElement; @@ -73,6 +74,14 @@ void assertBSONTypeEquals(BSONType actual, BSONType expected, T value, int i) { } } +TEST(BSONObjBuilder, AppendInt64T) { + auto obj = BSON("a" << int64_t{5} << "b" << int64_t{1ll << 40}); + ASSERT_EQ(obj["a"].type(), NumberLong); + ASSERT_EQ(obj["b"].type(), NumberLong); + ASSERT_EQ(obj["a"].Long(), 5); + ASSERT_EQ(obj["b"].Long(), 1ll << 40); +} + /** * current conversion ranges in append(unsigned n) * dbl/int max/min in comments refer to max/min encodable constants @@ -323,3 +332,4 @@ TEST(BSONObjBuilderTest, ResetToEmptyForNestedBuilderOnlyResetsInnerObj) { } } // unnamed namespace +} // namespace mongo diff --git a/src/mongo/bson/util/builder.h b/src/mongo/bson/util/builder.h index f440e9b8fcb..924d96e7f8b 100644 --- a/src/mongo/bson/util/builder.h +++ b/src/mongo/bson/util/builder.h @@ -30,6 +30,7 @@ #pragma once #include <cfloat> +#include <cstdint> #include <sstream> #include <stdio.h> #include <string.h> @@ -41,6 +42,7 @@ #include "mongo/base/string_data.h" #include "mongo/bson/inline_decls.h" #include "mongo/platform/decimal128.h" +#include "mongo/stdx/type_traits.h" #include "mongo/util/allocator.h" #include "mongo/util/assert_util.h" @@ -209,6 +211,14 @@ public: static_assert(sizeof(long long) == 8, "sizeof(long long) == 8"); appendNumImpl(j); } + + template <typename Int64_t, + typename = stdx::enable_if_t<std::is_same<Int64_t, int64_t>::value && + !std::is_same<int64_t, long long>::value>> + void appendNum(Int64_t j) { + appendNumImpl(j); + } + void appendNum(unsigned long long j) { appendNumImpl(j); } |