[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 |
|
---|
| 86 | /** ValueStorage serves as a mediator to MapOfActions.
|
---|
| 87 | * This is needed to relax inter-dependencies between the Queries and the Actions.
|
---|
| 88 | * I.e. this is the interface implemented in MapOfActions which both can safely rely on
|
---|
| 89 | * to store&retrieve/exchange values.
|
---|
[0275ad] | 90 | *
|
---|
[e6c470] | 91 | * \section ValueStorage ValueStorage howto
|
---|
[0275ad] | 92 | *
|
---|
| 93 | * If you ever need to add a particular class to the ValueStorage, do as follows:
|
---|
| 94 | * -# add a specialized queryCurrentValue and setCurrentValue to the definition
|
---|
| 95 | * of ValueStorage.
|
---|
| 96 | * -# implement both in the declaration of ValueStorage.
|
---|
| 97 | * -# in the implementation either directly implement the serializing of the
|
---|
| 98 | * class' members to a stringstream or use an already implemented operator<<
|
---|
| 99 | * or operator<<, respectively.
|
---|
| 100 | *
|
---|
[75dc28] | 101 | */
|
---|
| 102 | class ValueStorage : public Singleton<ValueStorage> {
|
---|
| 103 | friend class Singleton<ValueStorage>;
|
---|
[e5ece4] | 104 | friend std::ostream & operator<<(std::ostream &ost, const ValueStorage &value);
|
---|
[9d33ba] | 105 | public:
|
---|
[e4afb4] | 106 |
|
---|
| 107 | bool isCurrentValuePresent(const char *name) const;
|
---|
| 108 | void queryCurrentValue(const char * name, const atom * &_T);
|
---|
| 109 | void queryCurrentValue(const char * name, const element * &_T);
|
---|
| 110 | void queryCurrentValue(const char * name, const molecule * &_T);
|
---|
| 111 | void queryCurrentValue(const char * name, class Box &_T);
|
---|
| 112 | void queryCurrentValue(const char * name, class Vector &_T);
|
---|
| 113 | void queryCurrentValue(const char * name, class BoxVector &_T);
|
---|
[0275ad] | 114 | void queryCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T);
|
---|
[e4afb4] | 115 | void queryCurrentValue(const char * name, std::vector<const atom *>&_T);
|
---|
| 116 | void queryCurrentValue(const char * name, std::vector<const element *>&_T);
|
---|
| 117 | void queryCurrentValue(const char * name, std::vector<const molecule *>&_T);
|
---|
| 118 | void queryCurrentValue(const char * name, boost::filesystem::path&_T);
|
---|
| 119 |
|
---|
[03c902] | 120 | /** Gets a value from the storage
|
---|
| 121 | * If the value is not present, an ASSERT is thrown unless optional is set to true.
|
---|
| 122 | * \param _T key of value
|
---|
| 123 | * \param optional whether this value is optional, i.e. may actually not be in the storage (i.e. may return false in this case).
|
---|
| 124 | * \return true - value present, false - value not present (only given when optional set to true)
|
---|
| 125 | */
|
---|
[e4afb4] | 126 | template<typename T> void queryCurrentValue(const char * name, T &_T)
|
---|
| 127 | {
|
---|
| 128 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 129 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
[336e33] | 130 | throw MissingValueException() << ValueStorageOptionName(name);
|
---|
[e4afb4] | 131 | _T = lexical_cast<T>(CurrentValueMap[name].c_str());
|
---|
| 132 | CurrentValueMap.erase(name);
|
---|
| 133 | } else
|
---|
[882678] | 134 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 135 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 136 | typeid(_T).name(),
|
---|
| 137 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 138 | );
|
---|
[75dc28] | 139 | }
|
---|
[e4afb4] | 140 | template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T)
|
---|
| 141 | {
|
---|
| 142 | T temp;
|
---|
| 143 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 144 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
[336e33] | 145 | throw MissingValueException() << ValueStorageOptionName(name);
|
---|
[e4afb4] | 146 | std::istringstream stream(CurrentValueMap[name]);
|
---|
| 147 | CurrentValueMap.erase(name);
|
---|
| 148 | while (!stream.fail()) {
|
---|
| 149 | stream >> temp >> std::ws;
|
---|
[78bb14] | 150 | if (!stream.fail()) {
|
---|
| 151 | _T.push_back(temp);
|
---|
| 152 | }
|
---|
[e4afb4] | 153 | }
|
---|
| 154 | } else
|
---|
[882678] | 155 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 156 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 157 | typeid(_T).name(),
|
---|
| 158 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 159 | );
|
---|
[e4afb4] | 160 | }
|
---|
| 161 |
|
---|
| 162 | void setCurrentValue(const char * name, const atom * &_T);
|
---|
| 163 | void setCurrentValue(const char * name, const element * &_T);
|
---|
| 164 | void setCurrentValue(const char * name, const molecule * &_T);
|
---|
| 165 | void setCurrentValue(const char * name, class Box &_T);
|
---|
| 166 | void setCurrentValue(const char * name, class Vector &_T);
|
---|
[0275ad] | 167 | void setCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T);
|
---|
[e4afb4] | 168 | void setCurrentValue(const char * name, std::vector<const atom *>&_T);
|
---|
| 169 | void setCurrentValue(const char * name, std::vector<const element *>&_T);
|
---|
| 170 | void setCurrentValue(const char * name, std::vector<const molecule *>&_T);
|
---|
| 171 | void setCurrentValue(const char * name, boost::filesystem::path&_T);
|
---|
[03c902] | 172 |
|
---|
| 173 | /** Sets a value in the storage.
|
---|
| 174 | * \param name key of value
|
---|
| 175 | * \param _T value
|
---|
| 176 | */
|
---|
[e4afb4] | 177 | template<class T> void setCurrentValue(const char * name, T &_T)
|
---|
| 178 | {
|
---|
| 179 | std::ostringstream stream;
|
---|
| 180 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 181 | stream << _T;
|
---|
| 182 | CurrentValueMap[name] = stream.str();
|
---|
| 183 | } else
|
---|
[882678] | 184 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 185 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 186 | typeid(_T).name(),
|
---|
| 187 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 188 | );
|
---|
| 189 | }
|
---|
[e4afb4] | 190 | /** Sets a value in the storage.
|
---|
| 191 | * \param name key of value
|
---|
| 192 | * \param _T value
|
---|
[03c902] | 193 | */
|
---|
[e4afb4] | 194 | template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
|
---|
| 195 | {
|
---|
| 196 | std::ostringstream stream;
|
---|
| 197 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 198 | std::ostringstream stream;
|
---|
| 199 | for (typename std::vector<T>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) {
|
---|
| 200 | stream << (*iter) << " ";
|
---|
| 201 | }
|
---|
| 202 | CurrentValueMap[name] = stream.str();
|
---|
| 203 | } else
|
---|
[882678] | 204 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 205 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 206 | typeid(_T).name(),
|
---|
| 207 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 208 | );
|
---|
[e4afb4] | 209 | }
|
---|
| 210 |
|
---|
[4885f85] | 211 | const std::string getCurrentValue(std::string actionname);
|
---|
[9d33ba] | 212 |
|
---|
[88ba1f] | 213 | /** Sets a value in the storage directly from a string.
|
---|
| 214 | * We need the templated type to check for consistency
|
---|
| 215 | * \param name key of value
|
---|
| 216 | * \param _T value
|
---|
| 217 | */
|
---|
| 218 | template<class T> void setCurrentValueByString(const char * name, const std::string &_T)
|
---|
| 219 | {
|
---|
| 220 | std::ostringstream stream;
|
---|
| 221 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
| 222 | CurrentValueMap[name] = _T;
|
---|
| 223 | } else
|
---|
| 224 | throw IllegalTypeException() << ValueStorageOptionName(name)
|
---|
| 225 | << ValueStoragePairTypeName(std::make_pair(
|
---|
| 226 | typeid(_T).name(),
|
---|
| 227 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str())
|
---|
| 228 | );
|
---|
| 229 | }
|
---|
[e5ece4] | 230 |
|
---|
[75dc28] | 231 | protected:
|
---|
| 232 | ValueStorage();
|
---|
| 233 | ~ValueStorage();
|
---|
[03c902] | 234 |
|
---|
[e4afb4] | 235 | std::map<std::string, std::string> CurrentValueMap;
|
---|
| 236 |
|
---|
[ce7fdc] | 237 | MoleCuilder::OptionRegistry &OptionRegistry_instance;
|
---|
[75dc28] | 238 | };
|
---|
| 239 |
|
---|
[e5ece4] | 240 | std::ostream & operator<<(std::ostream &ost, const ValueStorage &value);
|
---|
| 241 |
|
---|
| 242 |
|
---|
[75dc28] | 243 | #endif /* VALUESTORAGE_HPP_ */
|
---|