OMSim
Geant4 for IceCube optical module studies
OMSimCommandArgsTable.hh
Go to the documentation of this file.
1 
7 #pragma once
8 
13 #define WRITE_TO_JSON_IF_TYPE_MATCHES(VARIANT, TYPE) \
14  if (type == typeid(TYPE)) \
15  { \
16  outputFile << "\t\"" << kv.first << "\": "; \
17  if (typeid(TYPE) == typeid(std::string)) \
18  { \
19  outputFile << "\"" << boost::any_cast<TYPE>(VARIANT) << "\""; \
20  } \
21  else \
22  { \
23  outputFile << boost::any_cast<TYPE>(VARIANT); \
24  } \
25  }
26 
27 #include "OMSimLogger.hh"
28 
29 #include <fstream>
30 #include <boost/any.hpp>
31 #include <sys/time.h>
32 #include <map>
33 
34 // Forward declaration of the class
36 
37 // Declaration and definition of the inline global pointer
38 inline OMSimCommandArgsTable* g_commandArgsTable = nullptr;
39 
40 
51 {
52  OMSimCommandArgsTable() = default;
54  OMSimCommandArgsTable &operator=(const OMSimCommandArgsTable &) = delete;
55  ~OMSimCommandArgsTable() = default;
56 
57 
58 public:
59  using Key = std::string;
60  using Value = boost::any; // Using boost::any to hold any type
61 private:
62  bool m_finalized = false;
63  std::map<Key, Value> m_parameters;
64 
65 public:
66 
67  static void init();
68  static void shutdown();
70  void setParameter(const Key &p_key, const Value &p_value);
71  bool keyExists(const Key &p_key);
72  void writeToJson(std::string p_fileName);
73  void finalize();
74 
81  template <typename T>
82  T get(const std::string &p_key)
83  {
84  try
85  {
86  return boost::any_cast<T>(m_parameters.at(p_key));
87  }
88  catch (const boost::bad_any_cast &e)
89  {
90  log_error(("Failed to get parameter '" + p_key + "' as type " + typeid(T).name()).c_str());
91  throw std::invalid_argument("Failed to get parameter " + p_key + " as type " + typeid(T).name());
92  }
93  catch (const std::out_of_range &e)
94  {
95  log_error(("Parameter '" + p_key + "'does not exist").c_str());
96  throw std::invalid_argument("Parameter '" + p_key + "' does not exist");
97  }
98  }
99 
100 };
A class used to hold OMSim command arguments with global instance access.
Definition: OMSimCommandArgsTable.hh:51
void writeToJson(std::string p_fileName)
Writes the parameters to a JSON-formatted file.
Definition: OMSimCommandArgsTable.cc:62
T get(const std::string &p_key)
Retrieves a parameter from the table.
Definition: OMSimCommandArgsTable.hh:82
static void init()
Initializes the global instance of OMSimCommandArgsTable.
Definition: OMSimCommandArgsTable.cc:19
void setParameter(const Key &p_key, const Value &p_value)
Sets a parameter in the arg table.
Definition: OMSimCommandArgsTable.cc:42
static OMSimCommandArgsTable & getInstance()
Definition: OMSimCommandArgsTable.cc:7
void finalize()
Finalizes the table, setting a random seed if none was provided. m_finalized is set to true preventin...
Definition: OMSimCommandArgsTable.cc:107
static void shutdown()
Deletes the global instance of OMSimCommandArgsTable.
Definition: OMSimCommandArgsTable.cc:30