Skip to content

Commit 21e6485

Browse files
committed
Replace cpptoml with tomlplusplus for TOML config parsing
Migrate all TOML parsing from cpptoml to tomlplusplus. Introduce a TomlConfig typedef to insulate downstream code from the concrete library. Keep cpptoml in third_party/ with a deprecation pragma so existing includes still compile. Move BDM_ASSIGN_CONFIG_VALUE macros to a new toml_config.h and turn the old cpptoml.h utility header into a forwarding shim with a deprecation warning.
1 parent 2f8b8ad commit 21e6485

File tree

12 files changed

+174
-160
lines changed

12 files changed

+174
-160
lines changed

cmake/utils.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ function(install_inside_build)
348348
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cpptoml
349349
${CMAKE_SOURCE_DIR}/third_party/cpptoml/cpptoml.h
350350
)
351+
add_copy_files(copy_files_bdm
352+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tomlplusplus
353+
${CMAKE_SOURCE_DIR}/third_party/tomlplusplus/toml.hpp
354+
)
351355

352356
# Simulation and demos
353357
add_copy_directory(copy_files_bdm

src/core/param/command_line_options.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void CommandLineOptions::HandleCoreOptions() {
201201
Log::Fatal("CommandLineOptions::HandleCoreOptions",
202202
"Specified TOML file (", toml_file, ") does not exist.");
203203
}
204-
auto toml = cpptoml::parse_file(toml_file);
204+
auto toml = toml::parse_file(toml_file);
205205
Param param;
206206
param.AssignFromConfig(toml);
207207
std::cout << param.ToJsonString() << std::endl;

src/core/param/param.cc

Lines changed: 96 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
#include "core/multi_simulation/optimization_param.h"
2222
#include "core/param/param.h"
23-
#include "core/util/cpptoml.h"
2423
#include "core/util/log.h"
24+
#include "core/util/toml_config.h"
2525

2626
using nlohmann::json;
2727

@@ -146,78 +146,67 @@ void Param::MergeJsonPatch(const std::string& patch) {
146146
}
147147

148148
// -----------------------------------------------------------------------------
149-
void AssignThreadSafetyMechanism(const std::shared_ptr<cpptoml::table>& config,
150-
Param* param) {
151-
const std::string config_key = "simulation.thread_safety_mechanism";
152-
if (config->contains_qualified(config_key)) {
153-
auto value = config->get_qualified_as<std::string>(config_key);
154-
if (!value) {
155-
return;
156-
}
157-
auto str_value = *value;
158-
if (str_value == "none") {
159-
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kNone;
160-
} else if (str_value == "user-specified") {
161-
param->thread_safety_mechanism =
162-
Param::ThreadSafetyMechanism::kUserSpecified;
163-
} else if (str_value == "automatic") {
164-
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kAutomatic;
165-
}
149+
void AssignThreadSafetyMechanism(const TomlConfig& config, Param* param) {
150+
auto value =
151+
config.at_path("simulation.thread_safety_mechanism").value<std::string>();
152+
if (!value) {
153+
return;
154+
}
155+
auto str_value = *value;
156+
if (str_value == "none") {
157+
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kNone;
158+
} else if (str_value == "user-specified") {
159+
param->thread_safety_mechanism =
160+
Param::ThreadSafetyMechanism::kUserSpecified;
161+
} else if (str_value == "automatic") {
162+
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kAutomatic;
166163
}
167164
}
168165

169166
// -----------------------------------------------------------------------------
170-
void AssignMappedDataArrayMode(const std::shared_ptr<cpptoml::table>& config,
171-
Param* param) {
172-
const std::string config_key = "performance.mapped_data_array_mode";
173-
if (config->contains_qualified(config_key)) {
174-
auto value = config->get_qualified_as<std::string>(config_key);
175-
if (!value) {
176-
return;
177-
}
178-
auto str_value = *value;
179-
if (str_value == "zero-copy") {
180-
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
181-
} else if (str_value == "cache") {
182-
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
183-
} else if (str_value == "copy") {
184-
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
185-
} else {
186-
Log::Fatal(
187-
"Param",
188-
Concat(
189-
"Parameter mapped_data_array_mode was set to an invalid value (",
190-
str_value, ")."));
191-
}
167+
void AssignMappedDataArrayMode(const TomlConfig& config, Param* param) {
168+
auto value =
169+
config.at_path("performance.mapped_data_array_mode").value<std::string>();
170+
if (!value) {
171+
return;
172+
}
173+
auto str_value = *value;
174+
if (str_value == "zero-copy") {
175+
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
176+
} else if (str_value == "cache") {
177+
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
178+
} else if (str_value == "copy") {
179+
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
180+
} else {
181+
Log::Fatal(
182+
"Param",
183+
Concat("Parameter mapped_data_array_mode was set to an invalid value (",
184+
str_value, ")."));
192185
}
193186
}
194187

195188
// -----------------------------------------------------------------------------
196-
void AssignBoundSpaceMode(const std::shared_ptr<cpptoml::table>& config,
197-
Param* param) {
198-
const std::string config_key = "simulation.bound_space";
199-
if (config->contains_qualified(config_key)) {
200-
auto value = config->get_qualified_as<std::string>(config_key);
201-
if (!value) {
202-
return;
203-
}
204-
auto str_value = *value;
205-
if (str_value == "open") {
206-
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
207-
} else if (str_value == "closed") {
208-
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
209-
} else if (str_value == "torus") {
210-
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
211-
} else {
212-
Log::Fatal("Param",
213-
Concat("Parameter bound_space was set to an invalid value (",
214-
str_value, ")."));
215-
}
189+
void AssignBoundSpaceMode(const TomlConfig& config, Param* param) {
190+
auto value = config.at_path("simulation.bound_space").value<std::string>();
191+
if (!value) {
192+
return;
193+
}
194+
auto str_value = *value;
195+
if (str_value == "open") {
196+
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
197+
} else if (str_value == "closed") {
198+
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
199+
} else if (str_value == "torus") {
200+
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
201+
} else {
202+
Log::Fatal("Param",
203+
Concat("Parameter bound_space was set to an invalid value (",
204+
str_value, ")."));
216205
}
217206
}
218207

219208
// -----------------------------------------------------------------------------
220-
void Param::AssignFromConfig(const std::shared_ptr<cpptoml::table>& config) {
209+
void Param::AssignFromConfig(const TomlConfig& config) {
221210
// group parameters
222211
for (auto& el : groups_) {
223212
el.second->AssignFromConfig(config);
@@ -261,77 +250,67 @@ void Param::AssignFromConfig(const std::shared_ptr<cpptoml::table>& config) {
261250
"visualization.compress_pv_files");
262251

263252
// visualize_agents
264-
auto visualize_agentstarr = config->get_table_array("visualize_agent");
265-
if (visualize_agentstarr) {
266-
for (const auto& table : *visualize_agentstarr) {
267-
// We do a 'redundant' check here, because `get_as` on Mac OS does not
268-
// catch the exception when the "name" is not defined in the bdm.toml
269-
// Same goes for all the other redundant checks
270-
if (table->contains("name")) {
271-
auto name = table->get_as<std::string>("name");
272-
if (!name) {
273-
Log::Warning("AssignFromConfig",
274-
"Missing name for attribute visualize_agent");
275-
continue;
276-
}
277-
278-
if (table->contains("additional_data_members")) {
279-
auto dm_option =
280-
table->get_array_of<std::string>("additional_data_members");
253+
if (auto visualize_agentstarr = config["visualize_agent"].as_array()) {
254+
for (auto& elem : *visualize_agentstarr) {
255+
auto* table = elem.as_table();
256+
if (!table) {
257+
continue;
258+
}
259+
auto name = (*table)["name"].value<std::string>();
260+
if (!name) {
261+
Log::Warning("AssignFromConfig",
262+
"Missing name for attribute visualize_agent");
263+
continue;
264+
}
281265

282-
std::set<std::string> data_members;
283-
for (const auto& val : *dm_option) {
284-
data_members.insert(val);
266+
std::set<std::string> data_members;
267+
if (auto dm_arr = (*table)["additional_data_members"].as_array()) {
268+
for (auto& dm : *dm_arr) {
269+
if (auto s = dm.value<std::string>()) {
270+
data_members.insert(*s);
285271
}
286-
visualize_agents[*name] = data_members;
287-
} else {
288-
std::set<std::string> data_members;
289-
visualize_agents[*name] = data_members;
290272
}
291273
}
274+
visualize_agents[*name] = data_members;
292275
}
293276
}
294277

295278
// visualize_diffusion
296-
auto visualize_diffusiontarr = config->get_table_array("visualize_diffusion");
297-
if (visualize_diffusiontarr) {
298-
for (const auto& table : *visualize_diffusiontarr) {
299-
if (table->contains("name")) {
300-
auto name = table->get_as<std::string>("name");
301-
if (!name) {
302-
Log::Warning("AssignFromConfig",
303-
"Missing name for attribute visualize_diffusion");
304-
continue;
305-
}
306-
307-
VisualizeDiffusion vd;
308-
vd.name = *name;
279+
if (auto visualize_diffusiontarr = config["visualize_diffusion"].as_array()) {
280+
for (auto& elem : *visualize_diffusiontarr) {
281+
auto* table = elem.as_table();
282+
if (!table) {
283+
continue;
284+
}
285+
auto name = (*table)["name"].value<std::string>();
286+
if (!name) {
287+
Log::Warning("AssignFromConfig",
288+
"Missing name for attribute visualize_diffusion");
289+
continue;
290+
}
309291

310-
if (table->contains("concentration")) {
311-
auto concentration = table->get_as<bool>("concentration");
312-
if (concentration) {
313-
vd.concentration = *concentration;
314-
}
315-
}
316-
if (table->contains("gradient")) {
317-
auto gradient = table->get_as<bool>("gradient");
318-
if (gradient) {
319-
vd.gradient = *gradient;
320-
}
321-
}
292+
VisualizeDiffusion vd;
293+
vd.name = *name;
322294

323-
visualize_diffusion.push_back(vd);
295+
if (auto concentration = (*table)["concentration"].value<bool>()) {
296+
vd.concentration = *concentration;
297+
}
298+
if (auto gradient = (*table)["gradient"].value<bool>()) {
299+
vd.gradient = *gradient;
324300
}
301+
302+
visualize_diffusion.push_back(vd);
325303
}
326304
}
327305

328306
// unschedule_default_operations
329-
if (config->get_table("simulation")) {
330-
auto disabled_ops =
331-
config->get_table("simulation")
332-
->get_array_of<std::string>("unschedule_default_operations");
333-
for (const auto& op : *disabled_ops) {
334-
unschedule_default_operations.push_back(op);
307+
if (auto sim_tbl = config["simulation"].as_table()) {
308+
if (auto ops_arr = (*sim_tbl)["unschedule_default_operations"].as_array()) {
309+
for (auto& elem : *ops_arr) {
310+
if (auto op = elem.value<std::string>()) {
311+
unschedule_default_operations.push_back(*op);
312+
}
313+
}
335314
}
336315
}
337316

src/core/param/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ struct Param {
645645
bool plot_memory_layout = false;
646646

647647
/// Assign values from config file to variables
648-
void AssignFromConfig(const std::shared_ptr<cpptoml::table>&);
648+
void AssignFromConfig(const TomlConfig&);
649649

650650
private:
651651
friend class DiffusionTest_CopyOldData_Test;

src/core/param/param_group.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ ParamGroupUid ParamGroupUidGenerator::NewUid() { return counter_++; }
2828

2929
ParamGroup::~ParamGroup() = default;
3030

31-
void ParamGroup::AssignFromConfig(const std::shared_ptr<cpptoml::table>&) {}
31+
void ParamGroup::AssignFromConfig(const TomlConfig&) {}
3232

3333
} // namespace bdm

src/core/param/param_group.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@
1717

1818
#include <memory>
1919
#include "core/util/root.h"
20-
#include "cpptoml/cpptoml.h"
20+
#include "tomlplusplus/toml.hpp"
2121

2222
namespace bdm {
2323

24+
/// Typedef for TOML config table, insulating downstream code from the
25+
/// concrete TOML library in use.
26+
/// Migration note: cpptoml has been replaced by tomlplusplus.
27+
/// Update downstream AssignFromConfig overrides:
28+
/// void AssignFromConfig(const cpptoml::table&) // old -- will not compile
29+
/// void AssignFromConfig(const TomlConfig&) // new
30+
using TomlConfig = toml::table;
31+
2432
struct Param;
2533

2634
using ParamGroupUid = uint64_t;
@@ -52,7 +60,7 @@ struct ParamGroup {
5260
protected:
5361
/// Assign values from a toml config file.\n
5462
/// Can be omitted if toml file support is not required.
55-
virtual void AssignFromConfig(const std::shared_ptr<cpptoml::table>&);
63+
virtual void AssignFromConfig(const TomlConfig&);
5664

5765
private:
5866
friend struct Param;

src/core/simulation.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "core/simulation.h"
1616

17-
#include <cpptoml/cpptoml.h>
1817
#include <omp.h>
1918
#include <algorithm>
2019
#include <cmath>
@@ -28,6 +27,8 @@
2827
#include <utility>
2928
#include <vector>
3029

30+
#include <tomlplusplus/toml.hpp>
31+
3132
#include "bdm_version.h"
3233
#include "core/agent/agent_uid_generator.h"
3334
#include "core/analysis/time_series.h"
@@ -507,7 +508,7 @@ void Simulation::LoadConfigFiles(const std::vector<std::string>& ctor_configs,
507508
if (configs.size()) {
508509
for (auto& config : configs) {
509510
if (EndsWith(config, ".toml")) {
510-
auto toml = cpptoml::parse_file(config);
511+
auto toml = toml::parse_file(config);
511512
param_->AssignFromConfig(toml);
512513
} else if (EndsWith(config, ".json")) {
513514
std::ifstream ifs(config);

src/core/util/cpptoml.h

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,12 @@
1212
//
1313
// -----------------------------------------------------------------------------
1414

15-
#ifndef CORE_UTIL_CPPTOML_H_
16-
#define CORE_UTIL_CPPTOML_H_
15+
// Deprecated: cpptoml has been replaced by tomlplusplus.
16+
// Update your includes from "core/util/cpptoml.h" to "core/util/toml_config.h"
17+
// and use TomlConfig (from param_group.h) instead of cpptoml::table.
18+
#if !defined(__ROOTCLING__) && !defined(__CLING__)
19+
#pragma message( \
20+
"cpptoml.h is deprecated. Use #include \"core/util/toml_config.h\" instead.")
21+
#endif
1722

18-
#define BDM_ASSIGN_CONFIG_VALUE(variable, config_key) \
19-
{ \
20-
if (config->contains_qualified(config_key)) { \
21-
auto value = config->get_qualified_as<decltype(variable)>(config_key); \
22-
if (value) { \
23-
variable = *value; \
24-
} \
25-
} \
26-
}
27-
28-
#define BDM_ASSIGN_CONFIG_DOUBLE3_VALUE(variable, config_key) \
29-
{ \
30-
if (config->contains_qualified(config_key)) { \
31-
auto value = config->get_array_of<real_t>(config_key); \
32-
if (value) { \
33-
auto vector = *value; \
34-
if (vector.size() == variable.size()) { \
35-
for (uint64_t i = 0; i < vector.size(); i++) { \
36-
variable[i] = vector[i]; \
37-
} \
38-
} else { \
39-
Log::Fatal("cpptoml parameter parsing", \
40-
"An error occurred during parameter parsing of (", \
41-
config_key, ". Array dimensions do not match"); \
42-
} \
43-
} \
44-
} \
45-
}
46-
47-
#endif // CORE_UTIL_CPPTOML_H_
23+
#include "core/util/toml_config.h"

0 commit comments

Comments
 (0)