cesium-native  0.41.0
IntegerJsonHandler.h
1 #pragma once
2 
3 #include "CesiumUtility/Assert.h"
4 #include "JsonHandler.h"
5 #include "Library.h"
6 
7 #include <cmath>
8 
9 namespace CesiumJsonReader {
10 template <typename T>
11 class CESIUMJSONREADER_API IntegerJsonHandler : public JsonHandler {
12 public:
13  IntegerJsonHandler() noexcept : JsonHandler() {}
14 
15  void reset(IJsonHandler* pParent, T* pInteger) {
16  JsonHandler::reset(pParent);
17  this->_pInteger = pInteger;
18  }
19 
20  T* getObject() { return this->_pInteger; }
21 
22  virtual IJsonHandler* readInt32(int32_t i) override {
23  CESIUM_ASSERT(this->_pInteger);
24  *this->_pInteger = static_cast<T>(i);
25  return this->parent();
26  }
27  virtual IJsonHandler* readUint32(uint32_t i) override {
28  CESIUM_ASSERT(this->_pInteger);
29  *this->_pInteger = static_cast<T>(i);
30  return this->parent();
31  }
32  virtual IJsonHandler* readInt64(int64_t i) override {
33  CESIUM_ASSERT(this->_pInteger);
34  *this->_pInteger = static_cast<T>(i);
35  return this->parent();
36  }
37  virtual IJsonHandler* readUint64(uint64_t i) override {
38  CESIUM_ASSERT(this->_pInteger);
39  *this->_pInteger = static_cast<T>(i);
40  return this->parent();
41  }
42  virtual IJsonHandler* readDouble(double d) override {
43  CESIUM_ASSERT(this->_pInteger);
44  double intPart;
45  double fractPart = std::modf(d, &intPart);
46  if (fractPart != 0) {
47  return JsonHandler::readDouble(d);
48  }
49  *this->_pInteger = static_cast<T>(intPart);
50  return this->parent();
51  }
52 
53  virtual void reportWarning(
54  const std::string& warning,
55  std::vector<std::string>&& context) override {
56  context.push_back("(expecting an integer)");
57  this->parent()->reportWarning(warning, std::move(context));
58  }
59 
60 private:
61  T* _pInteger = nullptr;
62 };
63 } // namespace CesiumJsonReader
Classes for reading JSON.