Program Listing for File JSONStateWriter.h

Return to documentation for file (include/flamegpu/io/JSONStateWriter.h)

#ifndef INCLUDE_FLAMEGPU_IO_JSONSTATEWRITER_H_
#define INCLUDE_FLAMEGPU_IO_JSONSTATEWRITER_H_

#include <rapidjson/writer.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>

#include <memory>
#include <string>
#include <unordered_map>


#include "flamegpu/io/StateWriter.h"
#include "flamegpu/model/ModelDescription.h"
#include "flamegpu/util/StringPair.h"


namespace flamegpu {
namespace detail {
class CUDAMacroEnvironment;
}
namespace io {
class JSONStateWriter : public StateWriter {
 public:
    JSONStateWriter();
    bool isWriting() override { return writer.get(); }
    void beginWrite(const std::string &output_file, bool pretty_print) override;
    void endWrite() override;

    void writeConfig(const Simulation *sim_instance) override;
    void writeStats(unsigned int iterations) override;
    void writeEnvironment(const std::shared_ptr<const detail::EnvironmentManager>& env_manager) override;
    void writeMacroEnvironment(const std::shared_ptr<const detail::CUDAMacroEnvironment>& macro_env, std::initializer_list<std::string> filter = {}) override;
    void writeAgents(const util::StringPairUnorderedMap<std::shared_ptr<const AgentVector>>& agents_map) override;

 private:
    bool config_written = false;
    bool stats_written = false;
    bool environment_written = false;
    bool macro_environment_written = false;
    bool agents_written = false;
    // Dirty workaround for PrettyWriter overloads not being virtual
    bool newline_purge_required = false;
    std::string outputPath;
    rapidjson::StringBuffer buffer;
    // Typedef because the template is too long
    typedef rapidjson::PrettyWriter<rapidjson::StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag> PrettyWriter;
    std::unique_ptr<PrettyWriter> writer;
};
}  // namespace io
}  // namespace flamegpu

#endif  // INCLUDE_FLAMEGPU_IO_JSONSTATEWRITER_H_