knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
bson-helper.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #include <clocale>
7 #include "knowrob/storage/mongo/bson-helper.h"
8 
9 namespace knowrob::mongo {
10  std::optional<double> bson_iterOptionalDouble(const bson_iter_t *iter) {
11  auto iterType = bson_iter_type(iter);
12  if (iterType == BSON_TYPE_DECIMAL128) {
13  bson_decimal128_t decimal;
14  bson_iter_decimal128(iter, &decimal);
15  if (decimal.high == 0x7800000000000000 || decimal.high == 0xf800000000000000) {
16  // infinity
17  return std::nullopt;
18  } else {
19  char buffer[BSON_DECIMAL128_STRING];
20  bson_decimal128_to_string(&decimal, buffer);
21  setlocale(LC_NUMERIC, "C");
22  auto val = atof(buffer);
23  setlocale(LC_NUMERIC, "");
24  return val;
25  }
26  } else if (iterType == BSON_TYPE_DOUBLE) {
27  return bson_iter_double(iter);
28  } else {
29  return std::nullopt;
30  }
31  }
32 }
std::optional< double > bson_iterOptionalDouble(const bson_iter_t *iter)
Definition: bson-helper.cpp:10