cesium-native 0.43.0
Loading...
Searching...
No Matches
IntegerJsonHandler.h
1#pragma once
2
3#include "CesiumUtility/Assert.h"
4#include "JsonHandler.h"
5#include "Library.h"
6
7#include <cmath>
8
9namespace CesiumJsonReader {
13template <typename T>
14class CESIUMJSONREADER_API IntegerJsonHandler : public JsonHandler {
15public:
16 IntegerJsonHandler() noexcept : JsonHandler() {}
17
22 void reset(IJsonHandler* pParent, T* pInteger) {
23 JsonHandler::reset(pParent);
24 this->_pInteger = pInteger;
25 }
26
30 T* getObject() { return this->_pInteger; }
31
33 virtual IJsonHandler* readInt32(int32_t i) override {
34 CESIUM_ASSERT(this->_pInteger);
35 *this->_pInteger = static_cast<T>(i);
36 return this->parent();
37 }
39 virtual IJsonHandler* readUint32(uint32_t i) override {
40 CESIUM_ASSERT(this->_pInteger);
41 *this->_pInteger = static_cast<T>(i);
42 return this->parent();
43 }
45 virtual IJsonHandler* readInt64(int64_t i) override {
46 CESIUM_ASSERT(this->_pInteger);
47 *this->_pInteger = static_cast<T>(i);
48 return this->parent();
49 }
51 virtual IJsonHandler* readUint64(uint64_t i) override {
52 CESIUM_ASSERT(this->_pInteger);
53 *this->_pInteger = static_cast<T>(i);
54 return this->parent();
55 }
57 virtual IJsonHandler* readDouble(double d) override {
58 CESIUM_ASSERT(this->_pInteger);
59 double intPart;
60 double fractPart = std::modf(d, &intPart);
61 if (fractPart != 0) {
62 return JsonHandler::readDouble(d);
63 }
64 *this->_pInteger = static_cast<T>(intPart);
65 return this->parent();
66 }
67
69 virtual void reportWarning(
70 const std::string& warning,
71 std::vector<std::string>&& context) override {
72 context.push_back("(expecting an integer)");
73 this->parent()->reportWarning(warning, std::move(context));
74 }
75
76private:
77 T* _pInteger = nullptr;
78};
79} // namespace CesiumJsonReader
Base interface for all JSON handlers. Types that need to be deserialized from JSON should implement I...
IJsonHandler for reading integer values.
virtual IJsonHandler * readUint32(uint32_t i) override
Called when the JSON parser encounters a uint32 value.
virtual IJsonHandler * readDouble(double d) override
Called when the JSON parser encounters a double value.
virtual IJsonHandler * readUint64(uint64_t i) override
Called when the JSON parser encounters a uint64 value.
void reset(IJsonHandler *pParent, T *pInteger)
Resets the parent IJsonHandler of this instance and sets the pointer to its destination integer value...
virtual IJsonHandler * readInt32(int32_t i) override
Called when the JSON parser encounters an int32 value.
virtual IJsonHandler * readInt64(int64_t i) override
Called when the JSON parser encounters an int64 value.
virtual void reportWarning(const std::string &warning, std::vector< std::string > &&context) override
Report a warning while reading JSON.
T * getObject()
Obtains the integer pointer set on this handler by reset.
A dummy implementation of IJsonHandler that will report a warning and return its parent when any of i...
Definition JsonHandler.h:19
Classes for reading JSON.