Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
20f712d298a057401c8d62875f784d44f1bead48
[simgrid.git] / src / simgrid / module.cpp
1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <xbt/asserts.h>
7 #include <xbt/log.h>
8
9 #include "simgrid/sg_config.hpp"
10 #include "src/simgrid/module.hpp"
11
12 #include <sstream>
13
14 XBT_LOG_NEW_CATEGORY(plugin, "Common category for the logging of all plugins");
15 XBT_LOG_EXTERNAL_CATEGORY(xbt_help);
16
17 using namespace simgrid;
18
19 void ModuleGroup::create_flag(const std::string& opt_name, const std::string& descr, const std::string& default_value,
20                               bool init_now)
21 {
22   opt_name_               = opt_name;
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)";
26
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());
30
31         if (value == default_value)
32           return;
33
34         if (value == "help") {
35           help();
36           exit(0);
37         }
38
39         if (init_now)
40           by_name(value).init();
41         else
42           by_name(value); // Simply ensure that this value exists, it will be picked up later
43       });
44 }
45 void ModuleGroup::init_from_flag_value() const
46 {
47   by_name(simgrid::config::get_value<std::string>(opt_name_)).init();
48 }
49
50 ModuleGroup& ModuleGroup::add(const char* id, const char* desc, std::function<void()> init)
51 {
52   table_.emplace_back(id, desc, std::move(init));
53   return *this;
54 }
55
56 Module const& ModuleGroup::by_name(const std::string& name) const
57 {
58   if (auto pos = std::find_if(table_.begin(), table_.end(), [&name](const Module& item) { return item.name_ == name; });
59       pos != table_.end())
60     return *pos;
61
62   xbt_die("Unable to find %s '%s'. Valid values are: %s.", kind_.c_str(), name.c_str(), existing_values().c_str());
63 }
64 /** Displays the long description of all registered models, and quit */
65 void ModuleGroup::help() const
66 {
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_);
70 }
71 std::string ModuleGroup::existing_values() const
72 {
73   std::stringstream ss;
74   std::string sep;
75   for (auto const& item : table_) {
76     ss << sep + item.name_;
77     sep = ", ";
78   }
79   return ss.str();
80 }