Skip to content

Commit

Permalink
Changes per review
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Sep 5, 2023
1 parent e4997fa commit f3e9e36
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
51 changes: 34 additions & 17 deletions src/stan/callbacks/json_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ class json_writer final : public structured_writer {

template <typename T>
void write_int_like(const std::string& key, T value) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
*output_ << value;
Expand Down Expand Up @@ -190,8 +191,9 @@ class json_writer final : public structured_writer {
* Writes "{", initial token of a JSON record.
*/
void begin_record() {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
*output_ << "{";
record_depth_++;
Expand All @@ -203,8 +205,9 @@ class json_writer final : public structured_writer {
* @param[in] key The name of the record.
*/
void begin_record(const std::string& key) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
*output_ << "{";
Expand All @@ -215,8 +218,9 @@ class json_writer final : public structured_writer {
* Writes "}", final token of a JSON record.
*/
void end_record() {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
record_depth_--;
*output_ << "\n" << std::string(record_depth_ * 2, ' ') << "}";
if (record_depth_ > 0) {
Expand All @@ -232,8 +236,9 @@ class json_writer final : public structured_writer {
* @param key Name of the value pair
*/
void write(const std::string& key) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
*output_ << "null";
Expand All @@ -245,8 +250,9 @@ class json_writer final : public structured_writer {
* @param value string to write.
*/
void write(const std::string& key, const std::string& value) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
std::string processsed_string = process_string(value);
write_sep();
write_key(key);
Expand All @@ -259,8 +265,9 @@ class json_writer final : public structured_writer {
* @param value pointer to chars to write.
*/
void write(const std::string& key, const char* value) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
std::string processsed_string = process_string(value);
write_sep();
write_key(key);
Expand All @@ -273,8 +280,9 @@ class json_writer final : public structured_writer {
* @param value bool to write.
*/
void write(const std::string& key, bool value) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
*output_ << (value ? "true" : "false");
Expand Down Expand Up @@ -322,8 +330,9 @@ class json_writer final : public structured_writer {
* @param value double to write.
*/
void write(const std::string& key, double value) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
write_value(value);
Expand All @@ -335,8 +344,9 @@ class json_writer final : public structured_writer {
* @param value complex value to write.
*/
void write(const std::string& key, const std::complex<double>& value) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
write_complex_value(value);
Expand All @@ -348,8 +358,9 @@ class json_writer final : public structured_writer {
* @param values vector to write.
*/
void write(const std::string& key, const std::vector<std::string>& v) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);

Expand All @@ -370,8 +381,9 @@ class json_writer final : public structured_writer {
* @param values vector to write.
*/
void write(const std::string& key, const std::vector<double>& v) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);

Expand All @@ -394,8 +406,9 @@ class json_writer final : public structured_writer {
* @param values vector to write.
*/
void write(const std::string& key, const std::vector<int>& v) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);

Expand All @@ -417,8 +430,9 @@ class json_writer final : public structured_writer {
*/
void write(const std::string& key,
const std::vector<std::complex<double>>& v) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);

Expand All @@ -440,8 +454,9 @@ class json_writer final : public structured_writer {
* @param vec Eigen Vector to write.
*/
void write(const std::string& key, const Eigen::VectorXd& vec) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
write_eigen_vector(vec);
Expand All @@ -453,8 +468,9 @@ class json_writer final : public structured_writer {
* @param vec Eigen Vector to write.
*/
void write(const std::string& key, const Eigen::RowVectorXd& vec) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
write_eigen_vector(vec);
Expand All @@ -466,8 +482,9 @@ class json_writer final : public structured_writer {
* @param mat Eigen Matrix to write.
*/
void write(const std::string& key, const Eigen::MatrixXd& mat) {
if (output_ == nullptr)
if (output_ == nullptr) {
return;
}
write_sep();
write_key(key);
*output_ << "[ ";
Expand Down
26 changes: 21 additions & 5 deletions src/stan/callbacks/structured_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,51 @@ class structured_writer {
* @param key Name of the value pair
* @param values vector of strings to write.
*/
void write(const std::string& key, const std::vector<std::string>& values) {}
virtual void write(const std::string& key,
const std::vector<std::string>& values) {}

/**
* Write a key-value pair where the value is a vector to be made a list.
* @param key Name of the value pair
* @param values vector to write.
*/
virtual void write(const std::string& key,
const std::vector<std::complex<double>>& v) {}

/**
* Write a key-value pair where the value is a vector to be made a list.
* @param key Name of the value pair
* @param values vector to write.
*/
virtual void write(const std::string& key, const std::vector<int>& v) {}

/**
* Write a key-value pair where the value is an Eigen Matrix.
* @param key Name of the value pair
* @param mat Eigen Matrix to write.
*/
void write(const std::string& key, const Eigen::MatrixXd& mat) {}
virtual void write(const std::string& key, const Eigen::MatrixXd& mat) {}

/**
* Write a key-value pair where the value is an Eigen Vector.
* @param key Name of the value pair
* @param vec Eigen Vector to write.
*/
void write(const std::string& key, const Eigen::VectorXd& vec) {}
virtual void write(const std::string& key, const Eigen::VectorXd& vec) {}

/**
* Write a key-value pair where the value is a Eigen RowVector.
* @param key Name of the value pair
* @param vec Eigen RowVector to write.
*/
void write(const std::string& key, const Eigen::RowVectorXd& vec) {}
virtual void write(const std::string& key, const Eigen::RowVectorXd& vec) {}

/**
* Write a key-value pair where the value is a const char*.
* @param key Name of the value pair
* @param value pointer to chars to write.
*/
void write(const std::string& key, const char* value) {}
virtual void write(const std::string& key, const char* value) {}
};

} // namespace callbacks
Expand Down

0 comments on commit f3e9e36

Please sign in to comment.