Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include <mutex> | ||
4 | #include <string> | ||
5 | |||
6 | #include "Object.h" | ||
7 | |||
8 | namespace iguana { | ||
9 | |||
10 | /// @brief a globally accessible parameter | ||
11 | /// | ||
12 | /// A global parameter has the following properties | ||
13 | /// - a default value | ||
14 | /// - may be changed only _one time_ | ||
15 | /// - may be read from _anywhere_ | ||
16 | /// | ||
17 | /// @par Available global parameters | ||
18 | /// - `iguana::GlobalConcurrencyModel` | ||
19 | template <typename T> | ||
20 | class GlobalParam : public Object { | ||
21 | |||
22 | public: | ||
23 | |||
24 | /// @param val the initial value of this parameter | ||
25 |
1/2✓ Branch 0 (3→4) taken 70 times.
✗ Branch 1 (3→6) not taken.
|
140 | GlobalParam(T val) : Object("IGUANA"), m_val(val) {} |
26 | |||
27 | /// @brief assign a new value to this parameter | ||
28 | /// @warning this may _only_ be used one time; a second attempt to set the parameter will fail | ||
29 | /// @param val the new value of this parameter | ||
30 | /// @returns `*this` | ||
31 | 19 | GlobalParam<T>& operator=(T const& val) | |
32 | { | ||
33 | 19 | std::lock_guard<std::mutex> lock(m_mutex); | |
34 |
1/2✓ Branch 0 (3→4) taken 19 times.
✗ Branch 1 (3→6) not taken.
|
19 | std::call_once(m_once, [&]() { m_val = val; }); |
35 | 19 | return *this; | |
36 | } | ||
37 | |||
38 | /// @brief get the value of the parameter | ||
39 | /// @returns the value of the parameter | ||
40 | 154 | T const operator()() | |
41 | { | ||
42 | 154 | std::lock_guard<std::mutex> lock(m_mutex); | |
43 | 154 | return m_val; | |
44 | } | ||
45 | |||
46 | private: | ||
47 | |||
48 | T m_val; | ||
49 | std::once_flag m_once; | ||
50 | std::mutex m_mutex; | ||
51 | |||
52 | }; | ||
53 | |||
54 | // ================================================================================== | ||
55 | // IGUANA GLOBAL PARAMETERS (see source file 'GlobalParam.cc' for their default values) | ||
56 | // ================================================================================== | ||
57 | |||
58 | /// @brief The concurrency model, for running certain algorithms in a thread-safe way | ||
59 | /// @par Available Models | ||
60 | /// - "single": no thread safety, but optimal for single-threaded users | ||
61 | /// - "memoize": thread-safe lazy loading of configuration parameters | ||
62 | /// - "none": no concurrency model set by user; this is the *default option*, and if this | ||
63 | /// is the choice when `ConcurrentParamFactory::Create` is called, an appropriate | ||
64 | /// option will be _chosen_ by `ConcurrentParamFactory::Create` instead | ||
65 | extern GlobalParam<std::string> GlobalConcurrencyModel; | ||
66 | |||
67 | /// @brief Path to the RCDB | ||
68 | /// @see `iguana::RCDBReader` for details | ||
69 | extern GlobalParam<std::string> GlobalRcdbUrl; | ||
70 | |||
71 | } | ||
72 |