Skip to content

Commit

Permalink
Fix prettyprint
Browse files Browse the repository at this point in the history
  • Loading branch information
Robadob committed Sep 21, 2024
1 parent fac14c0 commit 5302e31
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/flamegpu/io/JSONRunPlanReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JSONRunPlanReader {
* @param input_filepath Path on disk to read the file from
* @param model The model used to initialise the RunPlanVector
*/
static RunPlanVector load(const std::string &input_filepath, const ModelDescription& model);
static RunPlanVector load(const std::string &input_filepath, const ModelDescription &model);
};
} // namespace io
} // namespace flamegpu
Expand Down
14 changes: 11 additions & 3 deletions include/flamegpu/io/JSONRunPlanWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ namespace io {
* JSON format writer of RunPlanVector
*/
class JSONRunPlanWriter {
/**
* Utility method for writing out the outer structure of a RunPlanVector
* @param writer An initialised RapidJSON writer.
* @param rpv RunPlanVector to be written
* @tparam T Should be rapidjson::Writer or rapidjson::PrettyWriter (one does not inherit the other)
*/
template <typename T>
static void writeCommon(std::unique_ptr<T> &writer, const RunPlanVector &rpv);
/**
* Utility method for writing out a single RunPlan
* @param writer An initialised RapidJSON writer.
* @param rp RunPlan to be writer
* @tparam T Should be rapidjson::Writer
* @tparam T Should be rapidjson::Writer or rapidjson::PrettyWriter (one does not inherit the other)
*/
template <typename T>
static void writeRunPlan(std::unique_ptr<T>& writer, const RunPlan& rp);
static void writeRunPlan(std::unique_ptr<T> &writer, const RunPlan &rp);

public:
/**
* Exports the provided RunPlanVector in JSON format to the specified output_filepath
* @param rpv The RunPlanVector to be exported
* @param output_filepath Location on disk to export the file
* @param pretty Whether the exported JSON is "prettified" or "minified"
* @param pretty Whether the exported JSON is "prettified" (true) or "minified" (false), defaults true.
*/
static void save(const RunPlanVector &rpv, const std::string &output_filepath, bool pretty = true);
};
Expand Down
47 changes: 24 additions & 23 deletions src/flamegpu/io/JSONRunPlanWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@

namespace flamegpu {
namespace io {

// Typedef for the writer used, as the full template specification is way too long
typedef rapidjson::Writer<rapidjson::StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag> GenericJSONWriter;
typedef rapidjson::PrettyWriter<rapidjson::StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag> PrettyJSONWriter;

void JSONRunPlanWriter::save(const RunPlanVector& rpv, const std::string& output_filepath, const bool pretty_print) {
// Init writer
auto buffer = rapidjson::StringBuffer();
std::unique_ptr<GenericJSONWriter> writer;
if (pretty_print) {
auto t_writer = std::make_unique<rapidjson::PrettyWriter<rapidjson::StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag>>(buffer);
t_writer->SetIndent('\t', 1);
writer = std::move(t_writer);
} else {
writer = std::make_unique<rapidjson::Writer<rapidjson::StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag>>(buffer);
}
template <typename T>
void JSONRunPlanWriter::writeCommon(std::unique_ptr<T> &writer, const RunPlanVector &rpv) {
writer->StartObject();
writer->Key("RunPlanVector");
writer->StartArray();
// Write out RunPlan records
for (const auto &rp : rpv) {
for (const auto& rp : rpv) {
writeRunPlan(writer, rp);
}
// Finalise and dump to file
writer->EndArray();
writer->EndObject();
std::ofstream out(output_filepath, std::ofstream::trunc);
if (!out.is_open()) {
THROW exception::InvalidFilePath("Unable to open '%s' for writing, in JSONRunPlanWriter::save().", output_filepath.c_str());
}
out << buffer.GetString();
out.close();
// Cleanup (redundant in a static method)
writer.reset();
buffer.Clear();
}

template <typename T>
Expand Down Expand Up @@ -104,6 +86,25 @@ void JSONRunPlanWriter::writeRunPlan(std::unique_ptr<T> &writer, const RunPlan &
writer->EndObject();
writer->EndObject();
}
template void JSONRunPlanWriter::writeRunPlan(std::unique_ptr<GenericJSONWriter>& writer, const RunPlan& rp);
void JSONRunPlanWriter::save(const RunPlanVector &rpv, const std::string &output_filepath, const bool pretty_print) {
// Init writer
auto buffer = rapidjson::StringBuffer();
if (pretty_print) {
std::unique_ptr<PrettyJSONWriter> writer = std::make_unique<PrettyJSONWriter>(buffer);
writer->SetIndent('\t', 1);
writeCommon(writer, rpv);
} else {
std::unique_ptr<GenericJSONWriter> writer = std::make_unique<GenericJSONWriter>(buffer);
writeCommon(writer, rpv);
}
std::ofstream out(output_filepath, std::ofstream::trunc);
if (!out.is_open()) {
THROW exception::InvalidFilePath("Unable to open '%s' for writing, in JSONRunPlanWriter::save().", output_filepath.c_str());
}
out << buffer.GetString();
// Redundant cleanup
out.close();
buffer.Clear();
}
} // namespace io
} // namespace flamegpu

0 comments on commit 5302e31

Please sign in to comment.