#pragma once #include "CesiumUtility/Assert.h" #include "IntegerJsonHandler.h" #include "Library.h" #include "ObjectJsonHandler.h" #include #include namespace CesiumJsonReader { /** * @brief Reads the keys and values of a JSON object into a * `std::map` or an `std::unordered_map`. * * @tparam T The type of values in the map. * @tparam THandler The type of the \ref IJsonHandler to handle the map values. */ template class CESIUMJSONREADER_API DictionaryJsonHandler : public ObjectJsonHandler { public: /** * @brief Creates a new \ref DictionaryJsonHandler, passing the specified * arguments to the constructor of THandler. */ template DictionaryJsonHandler(Ts&&... args) noexcept : ObjectJsonHandler(), _item(std::forward(args)...) {} /** * @brief Resets the parent of this \ref IJsonHandler and sets the destination * pointer of this handler to an `std::unordered_map`. * * @warning Technically, there is no reason why you can't call `reset` twice * on the same \ref DictionaryJsonHandler, once with an `std::map` and once * with an `std::unordered_map`. In practice, if a pointer to an * `std::unordered_map` is present, it will always be used as the destination * and the pointer to an `std::map` will be ignored. */ void reset( IJsonHandler* pParent, std::unordered_map* pDictionary) { ObjectJsonHandler::reset(pParent); this->_pDictionary1 = pDictionary; } /** * @brief Resets the parent of this \ref IJsonHandler and sets the destination * pointer of this handler to an `std::map`. */ void reset(IJsonHandler* pParent, std::map* pDictionary) { ObjectJsonHandler::reset(pParent); this->_pDictionary2 = pDictionary; } /** @copydoc IJsonHandler::readObjectKey */ virtual IJsonHandler* readObjectKey(const std::string_view& str) override { CESIUM_ASSERT(this->_pDictionary1 || this->_pDictionary2); if (this->_pDictionary1) { auto it = this->_pDictionary1->emplace(str, T()).first; return this->property(it->first.c_str(), this->_item, it->second); } auto it = this->_pDictionary2->emplace(str, T()).first; return this->property(it->first.c_str(), this->_item, it->second); } private: std::unordered_map* _pDictionary1 = nullptr; std::map* _pDictionary2 = nullptr; THandler _item; }; } // namespace CesiumJsonReader