Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[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 "src/simgrid/module.hpp"
10 #include "src/simgrid/sg_config.hpp"
11
12 #include <algorithm>
13 #include <sstream>
14
15 XBT_LOG_NEW_CATEGORY(plugin, "Common category for the logging of all plugins");
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_help);
17
18 using namespace simgrid;
19
20 void ModuleGroup::create_flag(const std::string& opt_name, const std::string& descr, const std::string& default_value,
21                               bool init_now)
22 {
23   opt_name_               = opt_name;
24   std::string description = descr + ". Possible values (other compilation flags may activate more " + get_kind() +
25                             "s): " + existing_values() +
26                             ".\n       (use 'help' as a value to see the long description of each one)";
27
28   simgrid::config::declare_flag<std::string>(
29       opt_name, description, default_value, [this, default_value, init_now](const std::string& value) {
30         xbt_assert(_sg_cfg_init_status < 2, "Cannot load a %s after the initialization", kind_.c_str());
31
32         if (value == default_value)
33           return;
34
35         if (value == "help") {
36           help();
37           exit(0);
38         }
39
40         if (init_now)
41           by_name(value).init();
42         else
43           by_name(value); // Simply ensure that this value exists, it will be picked up later
44       });
45 }
46 void ModuleGroup::init_from_flag_value() const
47 {
48   by_name(simgrid::config::get_value<std::string>(opt_name_)).init();
49 }
50
51 ModuleGroup& ModuleGroup::add(const char* id, const char* desc, std::function<void()> init)
52 {
53   table_.emplace_back(id, desc, std::move(init));
54   return *this;
55 }
56
57 Module const& ModuleGroup::by_name(const std::string& name) const
58 {
59   if (auto pos = std::find_if(table_.begin(), table_.end(), [&name](const Module& item) { return item.name_ == name; });
60       pos != table_.end())
61     return *pos;
62
63   xbt_die("Unable to find %s '%s'. Valid values are: %s.", kind_.c_str(), name.c_str(), existing_values().c_str());
64 }
65 /** Displays the long description of all registered models, and quit */
66 void ModuleGroup::help() const
67 {
68   XBT_HELP("Long description of the %s accepted by this simulator:", kind_.c_str());
69   for (auto const& item : table_)
70     XBT_HELP("  %s: %s", item.name_, item.description_);
71 }
72 std::string ModuleGroup::existing_values() const
73 {
74   std::stringstream ss;
75   std::string sep;
76   for (auto const& item : table_) {
77     ss << sep + item.name_;
78     sep = ", ";
79   }
80   return ss.str();
81 }