1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <xbt/asserts.h>
9 #include "src/simgrid/module.hpp"
10 #include "src/simgrid/sg_config.hpp"
14 XBT_LOG_NEW_CATEGORY(plugin, "Common category for the logging of all plugins");
15 XBT_LOG_EXTERNAL_CATEGORY(xbt_help);
17 using namespace simgrid;
19 void ModuleGroup::create_flag(const std::string& opt_name, const std::string& descr, const std::string& default_value,
23 std::string description = descr + ". Possible values (other compilation flags may activate more " + get_kind() +
24 "s): " + existing_values() +
25 ".\n (use 'help' as a value to see the long description of each one)";
27 simgrid::config::declare_flag<std::string>(
28 opt_name, description, default_value, [this, default_value, init_now](const std::string& value) {
29 xbt_assert(_sg_cfg_init_status < 2, "Cannot load a %s after the initialization", kind_.c_str());
31 if (value == default_value)
34 if (value == "help") {
40 by_name(value).init();
42 by_name(value); // Simply ensure that this value exists, it will be picked up later
45 void ModuleGroup::init_from_flag_value() const
47 by_name(simgrid::config::get_value<std::string>(opt_name_)).init();
50 ModuleGroup& ModuleGroup::add(const char* id, const char* desc, std::function<void()> init)
52 table_.emplace_back(id, desc, std::move(init));
56 Module const& ModuleGroup::by_name(const std::string& name) const
58 if (auto pos = std::find_if(table_.begin(), table_.end(), [&name](const Module& item) { return item.name_ == name; });
62 xbt_die("Unable to find %s '%s'. Valid values are: %s.", kind_.c_str(), name.c_str(), existing_values().c_str());
64 /** Displays the long description of all registered models, and quit */
65 void ModuleGroup::help() const
67 XBT_HELP("Long description of the %s accepted by this simulator:", kind_.c_str());
68 for (auto const& item : table_)
69 XBT_HELP(" %s: %s", item.name_, item.description_);
71 std::string ModuleGroup::existing_values() const
75 for (auto const& item : table_) {
76 ss << sep + item.name_;