summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bsonelement.h
diff options
context:
space:
mode:
authorA. Jesse Jiryu Davis <jesse@mongodb.com>2020-12-15 13:41:29 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-12-15 20:17:40 +0000
commit0c132588e9907c7b123f986a61c3a43cd087381f (patch)
treea83d514dff0689f97e7d9db7b503912767ec9cc9 /src/mongo/bson/bsonelement.h
parentc425bdcf1862d642460211fcf450664233a9e6d0 (diff)
downloadmongo-0c132588e9907c7b123f986a61c3a43cd087381f.tar.gz
SERVER-52545 Define listIndexes with IDL
Diffstat (limited to 'src/mongo/bson/bsonelement.h')
-rw-r--r--src/mongo/bson/bsonelement.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h
index 7c6b86071c7..b3a39c04ce3 100644
--- a/src/mongo/bson/bsonelement.h
+++ b/src/mongo/bson/bsonelement.h
@@ -29,6 +29,7 @@
#pragma once
+#include <algorithm>
#include <cmath>
#include <cstdint>
#include <fmt/format.h>
@@ -377,6 +378,13 @@ public:
*/
int numberInt() const;
+ /** Like numberInt() but with well-defined behavior for doubles that
+ * are NaNs, or too large/small to be represented as int.
+ * NaNs -> 0
+ * very large doubles -> INT_MAX
+ * very small doubles -> INT_MIN */
+ int safeNumberInt() const;
+
/**
* Retrieves the value of this element as a 64 bit integer. If the BSON type is non-numeric,
* returns zero. If the element holds a double, truncates the fractional part.
@@ -950,6 +958,11 @@ inline int BSONElement::numberInt() const {
}
}
+inline int BSONElement::safeNumberInt() const {
+ return static_cast<int>(std::clamp<long long>(
+ safeNumberLong(), std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
+}
+
inline long long BSONElement::numberLong() const {
switch (type()) {
case NumberDouble: