121 template <
typename T>
122 void write_val_(T val)
124 if constexpr (std::is_same_v<T, long>) {
125 std::fprintf(this->fp_,
"%ld", val);
126 }
else if constexpr (std::is_same_v<T, int>) {
127 std::fprintf(this->fp_,
"%d", val);
128 }
else if constexpr (std::is_same_v<T, double>) {
129 std::fprintf(this->fp_,
"%.17g", val);
130 }
else if constexpr (std::is_same_v<T, const char*>) {
131 std::string tmp = io_detail::encode_toml_str(val);
132 std::fputs(tmp.c_str(), this->fp_);
134 static_assert(always_false<T>,
"unexpected type");
138 template <
typename T>
139 void record_(
const char* name,
const T* val,
int length)
142 CHOLLA_ERROR(
"can't record any more header attributes after closing the recorder");
144 std::string key = io_detail::encode_toml_key(name);
145 std::fprintf(this->fp_,
"%s = ", key.c_str());
147 this->write_val_<T>(*val);
149 std::fputc(
'[', this->fp_);
150 for (
int i = 0; i < length; i++) {
152 std::fputc(
',', this->fp_);
153 std::fputc(
' ', this->fp_);
157 std::fputc(
']', this->fp_);
159 std::fputc(
'\n', this->fp_);
167 CHOLLA_ASSERT(fp !=
nullptr,
"received nullptr");
168 std::fputs(
"BEGIN-HEADER\n", this->fp_);
181 if (not this->is_closed_) {
182 std::fputs(
"END-HEADER\n", this->fp_);
183 this->is_closed_ =
true;
191 void record_arr(
const char* name,
const double* arr,
int length)
override { this->record_(name, arr, length); }
193 void record_arr(
const char* name,
const int* arr,
int length)
override { this->record_(name, arr, length); }
195 void record_arr(
const char* name,
const long* arr,
int length)
override { this->record_(name, arr, length); }
197 void record(
const char* name,
const char* val)
override { this->record_(name, &val, -1); }
198 void record(
const char* name,
double val)
override { this->record_(name, &val, -1); }
199 void record(
const char* name,
int val)
override { this->record_(name, &val, -1); }
200 void record(
const char* name,
long val)
override { this->record_(name, &val, -1); }
Definition AttrRecorderInterface.h:116
void ensure_closed()
Definition AttrRecorderInterface.h:179
void record_arr(const char *name, const double *arr, int length) override
Definition AttrRecorderInterface.h:191
void record(const char *name, const char *val) override
Definition AttrRecorderInterface.h:197