[75dc28] | 1 | /*
|
---|
| 2 | * ValueStorage.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jul 22, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef VALUESTORAGE_HPP_
|
---|
| 9 | #define VALUESTORAGE_HPP_
|
---|
| 10 |
|
---|
[56f73b] | 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
| 16 |
|
---|
[e4afb4] | 17 | #include <boost/filesystem.hpp>
|
---|
| 18 | #include <boost/lexical_cast.hpp>
|
---|
| 19 | #include <boost/program_options.hpp>
|
---|
| 20 |
|
---|
[e5ece4] | 21 | #include <iosfwd>
|
---|
[e4afb4] | 22 | #include <map>
|
---|
| 23 | #include <set>
|
---|
| 24 | #include <vector>
|
---|
| 25 | #include <typeinfo>
|
---|
[882678] | 26 | #include <utility>
|
---|
[e4afb4] | 27 |
|
---|
| 28 | #include "Actions/OptionTrait.hpp"
|
---|
| 29 | #include "Actions/OptionRegistry.hpp"
|
---|
[ad011c] | 30 | #include "CodePatterns/Assert.hpp"
|
---|
| 31 | #include "CodePatterns/Singleton.hpp"
|
---|
[e4afb4] | 32 |
|
---|
[882678] | 33 | #include <exception>
|
---|
[e5c233] | 34 | #include <boost/exception/all.hpp>
|
---|
[882678] | 35 |
|
---|
| 36 | class MatrixContent;
|
---|
| 37 | class Vector;
|
---|
| 38 |
|
---|
| 39 | /** Base type for all exceptions regarding ValueStorage class.
|
---|
| 40 | *
|
---|
| 41 | */
|
---|
| 42 | struct ValueStorageException : virtual std::exception, virtual boost::exception { };
|
---|
| 43 |
|
---|
| 44 | /** ========================== General error information ================== */
|
---|
| 45 |
|
---|
| 46 | /** Exception information for ValueStorageException: name of option.
|
---|
| 47 | */
|
---|
| 48 | typedef boost::error_info<struct tag_ValueStorageOptionName, const char *> ValueStorageOptionName;
|
---|
| 49 |
|
---|
| 50 | /** Exception information for ValueStorageException: name of type.
|
---|
| 51 | */
|
---|
| 52 | typedef boost::error_info<struct tag_ValueStorageTypeName, const char *> ValueStorageTypeName;
|
---|
| 53 |
|
---|
| 54 | /** Exception information for ValueStorageException: pair of names of two types.
|
---|
| 55 | */
|
---|
| 56 | typedef boost::error_info<
|
---|
| 57 | struct tag_ValueStorageTypeName,
|
---|
| 58 | std::pair<const char *,const char *> > ValueStoragePairTypeName;
|
---|
| 59 |
|
---|
| 60 | /** ============================ Specific exceptions ====================== */
|
---|
| 61 |
|
---|
| 62 | /** Exception thrown when ValueStorage is given an illegal type for a specific option.
|
---|
| 63 | */
|
---|
| 64 | struct IllegalTypeException : virtual ValueStorageException { };
|
---|
| 65 |
|
---|
[336e33] | 66 | /** Exception thrown when ValueStorage is missing a requested value.
|
---|
| 67 | */
|
---|
| 68 | struct MissingValueException : virtual ValueStorageException { };
|
---|
| 69 |
|
---|
| 70 |
|
---|
[e4afb4] | 71 | class MapOfActionsTest;
|
---|
| 72 |
|
---|
| 73 | class Box;
|
---|
| 74 | class atom;
|
---|
| 75 | class element;
|
---|
| 76 | class molecule;
|
---|
| 77 | class Vector;
|
---|
[0275ad] | 78 | class RandomNumberDistribution_Parameters;
|
---|
[e4afb4] | 79 |
|
---|
| 80 | namespace po = boost::program_options;
|
---|
| 81 |
|
---|
| 82 | using boost::lexical_cast;
|
---|
| 83 |
|
---|
[ad011c] | 84 | #include "CodePatterns/Singleton.hpp"
|
---|
[75dc28] | 85 |
|
---|
[b2c302] | 86 | /** ValueStorage is a container for any type of value.
|
---|
| 87 | *
|
---|
[75dc28] | 88 | * This is needed to relax inter-dependencies between the Queries and the Actions.
|
---|
[0275ad] | 89 | *
|
---|
[b2c302] | 90 | * The ValueStorage is necessary because we do not a-priori know about the
|
---|
| 91 | * type of a value as given by the user. The type is handled via RTTI (run-time
|
---|
| 92 | * type information), and the value is cast to string stored under a specific
|
---|
| 93 | * token in a map inside this class.
|
---|
| 94 | *
|
---|
| 95 | * Not any value can be stored in this container. We rely on OptionRegistry to allow
|
---|
| 96 | * only tokens whose type has been defined beforehand. I.e. for each option we
|
---|
| 97 | * require a specific and unique type.
|
---|
| 98 | *
|
---|
| 99 | * Then, there are three types of functions:
|
---|
| 100 | * -# queryCurrentValue: request a value under a specific token
|
---|
| 101 | * -# setCurrentValue: set/enter a value under a specific token
|
---|
| 102 | * -# isCurrentValuePresent: ask whether a value under a specific token is contained
|
---|
| 103 | *
|
---|
| 104 | * For the first two we have templated version and a number of specialized ones to
|
---|
| 105 | * deal with some types specific to this code: \ref atom, \ref Box, \ref element,
|
---|
| 106 | * \ref molecule, ...
|
---|
| 107 | *
|
---|
| 108 | * \section ValueStorage-extend ValueStorage extension howto
|
---|
[0275ad] | 109 | *
|
---|
| 110 | * If you ever need to add a particular class to the ValueStorage, do as follows:
|
---|
| 111 | * -# add a specialized queryCurrentValue and setCurrentValue to the definition
|
---|
| 112 | * of ValueStorage.
|
---|
| 113 | * -# implement both in the declaration of ValueStorage.
|
---|
| 114 | * -# in the implementation either directly implement the serializing of the
|
---|
| 115 | * class' members to a stringstream or use an already implemented operator<<
|
---|
| 116 | * or operator<<, respectively.
|
---|
| 117 | *
|
---|
[b2c302] | 118 | * \section ValueStorage-further-info Further information
|
---|
| 119 | *
|
---|
| 120 | * If you want to know in detail how this all works with the options, where they
|
---|
| 121 | * come from and how values end up in the ValueStorage, then look at \ref actions
|
---|
| 122 | * and \ref userinterfaces.
|
---|
| 123 | *
|
---|
[75dc28] | 124 | */
|
---|
| 125 | class ValueStorage : public Singleton<ValueStorage> {
|
---|
| 126 | friend class Singleton<ValueStorage>;
|
---|
[e5ece4] | 127 | friend std::ostream & operator<<(std::ostream &ost, const ValueStorage &value);
|
---|
[9d33ba] | 128 | public:
|
---|
[e4afb4] | 129 |
|
---|
| 130 | bool isCurrentValuePresent(const char *name) const;
|
---|
| 131 | void queryCurrentValue(const char * name, const atom * &_T);
|
---|
| 132 | void queryCurrentValue(const char * name, const element * &_T);
|
---|
| 133 | void queryCurrentValue(const char * name, const molecule * &_T);
|
---|
| 134 | void queryCurrentValue(const char * name, class Box &_T);
|
---|
| 135 | void queryCurrentValue(const char * name, class Vector &_T);
|
---|
| 136 | void queryCurrentValue(const char * name, class BoxVector &_T);
|
---|
[0275ad] | 137 | void queryCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T);
|
---|
[e4afb4] | 138 | void queryCurrentValue(const char * name, std::vector<const atom *>&_T);
|
---|
| 139 | void queryCurrentValue(const char * name, std::vector<const element *>&_T);
|
---|
| 140 | void queryCurrentValue(const char * name, std::vector<const molecule *>&_T);
|
---|
| 141 | void queryCurrentValue(const char * name, boost::filesystem::path&_T);
|
---|
| 142 |
|
---|
[03c902] | 143 | /** Gets a value from the storage
|
---|
| 144 | * If the value is not present, an ASSERT is thrown unless optional is set to true.
|
---|
| 145 | * \param _T key of value
|
---|
| 146 | * \param optional whether this value is optional, i.e. may actually not be in the storage (i.e. may return false in this case).
|
---|
| 147 | * \return true - value present, false - value not present (only given when optional set to true)
|
---|
| 148 | */
|
---|
[e4afb4] | 149 | template<typename T> void queryCurrentValue(const char * name, T &_T)
|
---|
| 150 | {
|
---|
| 151 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 152 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
[336e33] | 153 | throw MissingValueException() << ValueStorageOptionName(name);
|
---|
[e4afb4] | 154 | _T = lexical_cast<T>(CurrentValueMap[name].c_str());
|
---|
| 155 | CurrentValueMap.erase(name);
|
---|
| 156 | } else
|
---|
[882678] | 157 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 158 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 159 | typeid(_T).name(),
|
---|
| 160 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 161 | );
|
---|
[75dc28] | 162 | }
|
---|
[e4afb4] | 163 | template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T)
|
---|
| 164 | {
|
---|
| 165 | T temp;
|
---|
| 166 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 167 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
[336e33] | 168 | throw MissingValueException() << ValueStorageOptionName(name);
|
---|
[e4afb4] | 169 | std::istringstream stream(CurrentValueMap[name]);
|
---|
| 170 | CurrentValueMap.erase(name);
|
---|
| 171 | while (!stream.fail()) {
|
---|
| 172 | stream >> temp >> std::ws;
|
---|
[78bb14] | 173 | if (!stream.fail()) {
|
---|
| 174 | _T.push_back(temp);
|
---|
| 175 | }
|
---|
[e4afb4] | 176 | }
|
---|
| 177 | } else
|
---|
[882678] | 178 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 179 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 180 | typeid(_T).name(),
|
---|
| 181 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 182 | );
|
---|
[e4afb4] | 183 | }
|
---|
| 184 |
|
---|
| 185 | void setCurrentValue(const char * name, const atom * &_T);
|
---|
| 186 | void setCurrentValue(const char * name, const element * &_T);
|
---|
| 187 | void setCurrentValue(const char * name, const molecule * &_T);
|
---|
| 188 | void setCurrentValue(const char * name, class Box &_T);
|
---|
| 189 | void setCurrentValue(const char * name, class Vector &_T);
|
---|
[0275ad] | 190 | void setCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T);
|
---|
[e4afb4] | 191 | void setCurrentValue(const char * name, std::vector<const atom *>&_T);
|
---|
| 192 | void setCurrentValue(const char * name, std::vector<const element *>&_T);
|
---|
| 193 | void setCurrentValue(const char * name, std::vector<const molecule *>&_T);
|
---|
| 194 | void setCurrentValue(const char * name, boost::filesystem::path&_T);
|
---|
[03c902] | 195 |
|
---|
| 196 | /** Sets a value in the storage.
|
---|
| 197 | * \param name key of value
|
---|
| 198 | * \param _T value
|
---|
| 199 | */
|
---|
[e4afb4] | 200 | template<class T> void setCurrentValue(const char * name, T &_T)
|
---|
| 201 | {
|
---|
| 202 | std::ostringstream stream;
|
---|
| 203 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 204 | stream << _T;
|
---|
| 205 | CurrentValueMap[name] = stream.str();
|
---|
| 206 | } else
|
---|
[882678] | 207 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 208 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 209 | typeid(_T).name(),
|
---|
| 210 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 211 | );
|
---|
| 212 | }
|
---|
[e4afb4] | 213 | /** Sets a value in the storage.
|
---|
| 214 | * \param name key of value
|
---|
| 215 | * \param _T value
|
---|
[03c902] | 216 | */
|
---|
[e4afb4] | 217 | template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
|
---|
| 218 | {
|
---|
| 219 | std::ostringstream stream;
|
---|
| 220 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 221 | std::ostringstream stream;
|
---|
| 222 | for (typename std::vector<T>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) {
|
---|
| 223 | stream << (*iter) << " ";
|
---|
| 224 | }
|
---|
| 225 | CurrentValueMap[name] = stream.str();
|
---|
| 226 | } else
|
---|
[882678] | 227 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 228 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 229 | typeid(_T).name(),
|
---|
| 230 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 231 | );
|
---|
[e4afb4] | 232 | }
|
---|
| 233 |
|
---|
[4885f85] | 234 | const std::string getCurrentValue(std::string actionname);
|
---|
[9d33ba] | 235 |
|
---|
[88ba1f] | 236 | /** Sets a value in the storage directly from a string.
|
---|
| 237 | * We need the templated type to check for consistency
|
---|
| 238 | * \param name key of value
|
---|
| 239 | * \param _T value
|
---|
| 240 | */
|
---|
| 241 | template<class T> void setCurrentValueByString(const char * name, const std::string &_T)
|
---|
| 242 | {
|
---|
| 243 | std::ostringstream stream;
|
---|
| 244 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 245 | CurrentValueMap[name] = _T;
|
---|
| 246 | } else
|
---|
| 247 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 248 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 249 | typeid(_T).name(),
|
---|
| 250 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 251 | );
|
---|
| 252 | }
|
---|
[e5ece4] | 253 |
|
---|
[75dc28] | 254 | protected:
|
---|
| 255 | ValueStorage();
|
---|
| 256 | ~ValueStorage();
|
---|
[03c902] | 257 |
|
---|
[e4afb4] | 258 | std::map<std::string, std::string> CurrentValueMap;
|
---|
| 259 |
|
---|
[ce7fdc] | 260 | MoleCuilder::OptionRegistry &OptionRegistry_instance;
|
---|
[75dc28] | 261 | };
|
---|
| 262 |
|
---|
[e5ece4] | 263 | std::ostream & operator<<(std::ostream &ost, const ValueStorage &value);
|
---|
| 264 |
|
---|
| 265 |
|
---|
[75dc28] | 266 | #endif /* VALUESTORAGE_HPP_ */
|
---|