| 1 |
/* |
| 2 |
* Advanced Simulation Library <http://asl.org.il> |
| 3 |
* |
| 4 |
* Copyright 2015 Avtech Scientific <http://avtechscientific.com> |
| 5 |
* |
| 6 |
* |
| 7 |
* This file is part of Advanced Simulation Library (ASL). |
| 8 |
* |
| 9 |
* ASL is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU Affero General Public License as |
| 11 |
* published by the Free Software Foundation, version 3 of the License. |
| 12 |
* |
| 13 |
* ASL is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Affero General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Affero General Public License |
| 19 |
* along with ASL. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#ifndef ASLPARAMETERSMANAGER_H |
| 25 |
#define ASLPARAMETERSMANAGER_H |
| 26 |
|
| 27 |
#include "aslUValue.h" |
| 28 |
#include <boost/program_options.hpp> |
| 29 |
#include <map> |
| 30 |
#include <typeinfo> |
| 31 |
|
| 32 |
namespace asl |
| 33 |
{ |
| 34 |
|
| 35 |
class PrefixStore; |
| 36 |
|
| 37 |
/** This class stores parameter's value and the information |
| 38 |
needed to extract it from command line and/or parameters file. |
| 39 |
Important: declare Parameters only after declaring ParametersManager or |
| 40 |
ApplicationParametersManager instance and before calling |
| 41 |
ParametersManager::load() because each Parameter adds itself |
| 42 |
to the instance automatically! |
| 43 |
\ingroup LDI */ |
| 44 |
template <typename T> class Parameter |
| 45 |
{ |
| 46 |
public: |
| 47 |
/** |
| 48 |
\p key_ - option key; is used to specify this parameter through |
| 49 |
command line and/or parameters file. |
| 50 |
\p description_ is used in the help output and as comment on |
| 51 |
parameters file generation. |
| 52 |
\p units_ - parameter units; is used to complement the option |
| 53 |
description mentioned above. Might be used for automatic unit |
| 54 |
conversion in future (to this end it is recommended to use the |
| 55 |
notation of the Boost::Units library). |
| 56 |
Since no default value is specified, the parameter is required |
| 57 |
to be specified in the parameters file or command line. |
| 58 |
Use "" to provide a vector asl::AVec<T> through command line, |
| 59 |
e.g.: --size "30 20 50" |
| 60 |
*/ |
| 61 |
Parameter(const char* key_, |
| 62 |
const char* description_, |
| 63 |
const char* units_ = ""); |
| 64 |
/** |
| 65 |
\p defaultValue - default parameter value. Since a default value is |
| 66 |
specified, the parameter is not required to be specified in the |
| 67 |
parameters file or command line. |
| 68 |
\p key_ - option key; is used to specify this parameter through |
| 69 |
command line and/or parameters file. |
| 70 |
\p description_ is used in the help output and as comment on |
| 71 |
parameters file generation. |
| 72 |
\p units_ - parameter units; is used to complement the option |
| 73 |
description mentioned above. Might be used for automatic unit |
| 74 |
conversion in future (to this end it is recommended to use the |
| 75 |
notation of the Boost::Units library). |
| 76 |
*/ |
| 77 |
Parameter(T defaultValue, |
| 78 |
const char* key_, |
| 79 |
const char* description_, |
| 80 |
const char* units_ = ""); |
| 81 |
inline const T & v() const; |
| 82 |
inline T & v(); |
| 83 |
inline std::shared_ptr<T> p(); |
| 84 |
|
| 85 |
private: |
| 86 |
UValue<T> parameter; |
| 87 |
const std::string key; |
| 88 |
const std::string description; |
| 89 |
const std::string units; |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
/** This class automatically accomodates newly created Parameters and then |
| 94 |
can load them from a parameters file. |
| 95 |
It has to be declared before declaring all the parameters it will manage! |
| 96 |
\ingroup Utilities */ |
| 97 |
class ParametersManager |
| 98 |
{ |
| 99 |
public: |
| 100 |
ParametersManager(); |
| 101 |
~ParametersManager(); |
| 102 |
/// Enables parameter loader |
| 103 |
void enable(); |
| 104 |
/// Adds a Parameter to ParametersManager |
| 105 |
template <typename T> void add(UValue<T> parameter, |
| 106 |
const char* key, |
| 107 |
const char* description, |
| 108 |
const char* units); |
| 109 |
/// Adds a group of parameters with common prefix to ParametersManager |
| 110 |
template <typename T> void add(UValue<std::map<std::string, T>> parameter, |
| 111 |
const char* key, |
| 112 |
const char* description, |
| 113 |
const char* units); |
| 114 |
/// Adds a Parameter with a default value to ParametersManager |
| 115 |
template <typename T> void add(UValue<T> parameter, |
| 116 |
T defaultValue, |
| 117 |
const char* key, |
| 118 |
const char* description, |
| 119 |
const char* units); |
| 120 |
/// Adds prefix and the pointer on the respective |
| 121 |
/// Parameter's destinationMap |
| 122 |
template <typename T> |
| 123 |
void addPrefix(const std::string prefix, |
| 124 |
std::shared_ptr<std::map<std::string, T>> destinationMap); |
| 125 |
/// Loads all previously declared parameters |
| 126 |
/// from parameters file \p paramFile |
| 127 |
void load(std::string paramFile); |
| 128 |
/** Get the parameters file directory (absolute, with ending slash). |
| 129 |
It is convenient to write simulation results output together with |
| 130 |
its respective parameters file into the same directory (whose name |
| 131 |
might reflect the specifics of the parameters used) */ |
| 132 |
std::string getDir(); |
| 133 |
|
| 134 |
static ParametersManager * current; |
| 135 |
|
| 136 |
protected: |
| 137 |
boost::program_options::options_description parametersOptions; |
| 138 |
std::string parametersFileDirectory; |
| 139 |
/// Accomodates prefixes (defined by attached "*" wildcard) |
| 140 |
/// using PrefixStore class |
| 141 |
std::vector<std::shared_ptr<PrefixStore>> prefixes; |
| 142 |
|
| 143 |
void populateMaps(boost::program_options::variables_map & vm); |
| 144 |
/// Wrties all parameters and their |
| 145 |
/// default values (if available) to the file \p fileName |
| 146 |
void writeParametersFile(const std::string fileName); |
| 147 |
/// Content of the parameters file |
| 148 |
std::string parametersFileStr; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
/** This class inherits ParametersManager class and thus also automatically |
| 153 |
accomodates newly created Parameters and then can load them from |
| 154 |
a parameters file and/or command line. It silently includes two parameters - |
| 155 |
`platform` and `device` that determine the hardware the application will run on. |
| 156 |
It has to be declared before declaring all the parameters it will manage! |
| 157 |
\ingroup Utilities */ |
| 158 |
class ApplicationParametersManager: public ParametersManager |
| 159 |
{ |
| 160 |
public: |
| 161 |
ApplicationParametersManager(const char* applicationName_, |
| 162 |
const char* applicationVersion_); |
| 163 |
|
| 164 |
/** Loads all previously declared parameters from command line |
| 165 |
and/or parameters file (provided through command line) */ |
| 166 |
void load(int argc, char* argv[]); |
| 167 |
|
| 168 |
private: |
| 169 |
UValue<std::string> platform; |
| 170 |
UValue<std::string> device; |
| 171 |
std::string applicationName; |
| 172 |
std::string applicationVersion; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
//-------------------------- Implementation -------------------------- |
| 177 |
|
| 178 |
|
| 179 |
template <typename T> const T & Parameter<T>::v() const |
| 180 |
{ |
| 181 |
return parameter.v(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
template <typename T> T & Parameter<T>::v() |
| 186 |
{ |
| 187 |
return parameter.v(); |
| 188 |
} |
| 189 |
|
| 190 |
template <typename T> std::shared_ptr<T> Parameter<T>::p() |
| 191 |
{ |
| 192 |
return parameter.p; |
| 193 |
} |
| 194 |
|
| 195 |
} //namespace asl |
| 196 |
#endif // ASLPARAMETERSMANAGER_H |