summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bsonobjbuilder.h
diff options
context:
space:
mode:
authorAlya Berciu <alya.berciu@mongodb.com>2019-06-07 10:26:48 -0400
committerAlya Berciu <alya.berciu@mongodb.com>2019-07-03 12:53:07 -0400
commit990b2ef30d7b8fa4db6af6d79da80e3664df9c21 (patch)
tree6af2f09e7271d41e86f4228dd54f35aeade71171 /src/mongo/bson/bsonobjbuilder.h
parent5813046272efb614ac78beb68885f1cb0ec58425 (diff)
downloadmongo-990b2ef30d7b8fa4db6af6d79da80e3664df9c21.tar.gz
SERVER-9763 Remove BSONObjBuilder append unsigned method
Diffstat (limited to 'src/mongo/bson/bsonobjbuilder.h')
-rw-r--r--src/mongo/bson/bsonobjbuilder.h75
1 files changed, 20 insertions, 55 deletions
diff --git a/src/mongo/bson/bsonobjbuilder.h b/src/mongo/bson/bsonobjbuilder.h
index fe8c0a5b555..083d7580998 100644
--- a/src/mongo/bson/bsonobjbuilder.h
+++ b/src/mongo/bson/bsonobjbuilder.h
@@ -39,6 +39,7 @@
#include <cstdint>
#include <limits>
#include <map>
+#include <type_traits>
#include "mongo/base/data_view.h"
#include "mongo/base/parse_number.h"
@@ -48,8 +49,8 @@
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/util/builder.h"
#include "mongo/platform/decimal128.h"
-#include "mongo/stdx/type_traits.h"
#include "mongo/util/decimal_counter.h"
+#include "mongo/util/if_constexpr.h"
namespace mongo {
@@ -268,56 +269,28 @@ public:
return *this;
}
- /** Append a boolean element */
- BSONObjBuilder& append(StringData fieldName, bool val) {
- _b.appendNum((char)Bool);
- _b.appendStr(fieldName);
- _b.appendNum((char)(val ? 1 : 0));
- return *this;
- }
-
- /** Append a 32 bit integer element */
- BSONObjBuilder& append(StringData fieldName, int n) {
- _b.appendNum((char)NumberInt);
- _b.appendStr(fieldName);
- _b.appendNum(n);
- return *this;
- }
-
- /** Append a 32 bit unsigned element - cast to a signed int. */
- BSONObjBuilder& append(StringData fieldName, unsigned n) {
- return append(fieldName, (int)n);
- }
-
- /** Append a NumberDecimal */
- BSONObjBuilder& append(StringData fieldName, Decimal128 n) {
- _b.appendNum(static_cast<char>(NumberDecimal));
- _b.appendStr(fieldName);
- // Make sure we write data in a Little Endian conforming manner
- _b.appendNum(n);
- return *this;
- }
-
- /** Append a NumberLong */
- BSONObjBuilder& append(StringData fieldName, long long n) {
- _b.appendNum((char)NumberLong);
+ /** Append elements that have the BSONObjAppendFormat trait */
+ template <typename T, typename = std::enable_if_t<IsBSONObjAppendable<T>::value>>
+ BSONObjBuilder& append(StringData fieldName, const T& n) {
+ constexpr BSONType type = BSONObjAppendFormat<T>::value;
+ _b.appendNum(static_cast<char>(type));
_b.appendStr(fieldName);
- _b.appendNum(n);
+ IF_CONSTEXPR(type == Bool) {
+ _b.appendNum(static_cast<char>(n));
+ }
+ else IF_CONSTEXPR(type == NumberInt) {
+ _b.appendNum(static_cast<int>(n));
+ }
+ else {
+ _b.appendNum(n);
+ }
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;
- }
+ template <typename T,
+ typename = std::enable_if_t<!IsBSONObjAppendable<T>::value && std::is_integral_v<T>>,
+ typename = void>
+ BSONObjBuilder& append(StringData fieldName, const T& n) = delete;
/** appends a number. if n < max(int)/2 then uses int, otherwise long long */
BSONObjBuilder& appendIntOrLL(StringData fieldName, long long n) {
@@ -374,14 +347,6 @@ public:
return *this;
}
- /** Append a double element */
- BSONObjBuilder& append(StringData fieldName, double n) {
- _b.appendNum((char)NumberDouble);
- _b.appendStr(fieldName);
- _b.appendNum(n);
- return *this;
- }
-
/** Append a BSON Object ID (OID type).
@deprecated Generally, it is preferred to use the append append(name, oid)
method for this.