| [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 |  | 
|---|
|  | 21 | #include <map> | 
|---|
|  | 22 | #include <set> | 
|---|
|  | 23 | #include <vector> | 
|---|
|  | 24 | #include <typeinfo> | 
|---|
| [882678] | 25 | #include <utility> | 
|---|
| [e4afb4] | 26 |  | 
|---|
|  | 27 | #include "Actions/OptionTrait.hpp" | 
|---|
|  | 28 | #include "Actions/OptionRegistry.hpp" | 
|---|
| [ad011c] | 29 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 30 | #include "CodePatterns/Singleton.hpp" | 
|---|
| [e4afb4] | 31 |  | 
|---|
| [882678] | 32 | #include <exception> | 
|---|
| [e5c233] | 33 | #include <boost/exception/all.hpp> | 
|---|
| [882678] | 34 |  | 
|---|
|  | 35 | class MatrixContent; | 
|---|
|  | 36 | class Vector; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | /** Base type for all exceptions regarding ValueStorage class. | 
|---|
|  | 39 | * | 
|---|
|  | 40 | */ | 
|---|
|  | 41 | struct ValueStorageException : virtual std::exception, virtual boost::exception { }; | 
|---|
|  | 42 |  | 
|---|
|  | 43 | /** ========================== General error information ================== */ | 
|---|
|  | 44 |  | 
|---|
|  | 45 | /** Exception information for ValueStorageException: name of option. | 
|---|
|  | 46 | */ | 
|---|
|  | 47 | typedef boost::error_info<struct tag_ValueStorageOptionName, const char *> ValueStorageOptionName; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | /** Exception information for ValueStorageException: name of type. | 
|---|
|  | 50 | */ | 
|---|
|  | 51 | typedef boost::error_info<struct tag_ValueStorageTypeName, const char *> ValueStorageTypeName; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | /** Exception information for ValueStorageException: pair of names of two types. | 
|---|
|  | 54 | */ | 
|---|
|  | 55 | typedef boost::error_info< | 
|---|
|  | 56 | struct tag_ValueStorageTypeName, | 
|---|
|  | 57 | std::pair<const char *,const char *> > ValueStoragePairTypeName; | 
|---|
|  | 58 |  | 
|---|
|  | 59 | /** ============================ Specific exceptions ====================== */ | 
|---|
|  | 60 |  | 
|---|
|  | 61 | /** Exception thrown when ValueStorage is given an illegal type for a specific option. | 
|---|
|  | 62 | */ | 
|---|
|  | 63 | struct IllegalTypeException : virtual ValueStorageException { }; | 
|---|
|  | 64 |  | 
|---|
| [336e33] | 65 | /** Exception thrown when ValueStorage is missing a requested value. | 
|---|
|  | 66 | */ | 
|---|
|  | 67 | struct MissingValueException : virtual ValueStorageException { }; | 
|---|
|  | 68 |  | 
|---|
|  | 69 |  | 
|---|
| [e4afb4] | 70 | class MapOfActionsTest; | 
|---|
|  | 71 |  | 
|---|
|  | 72 | class Box; | 
|---|
|  | 73 | class atom; | 
|---|
|  | 74 | class element; | 
|---|
|  | 75 | class molecule; | 
|---|
|  | 76 | class Vector; | 
|---|
| [0275ad] | 77 | class RandomNumberDistribution_Parameters; | 
|---|
| [e4afb4] | 78 |  | 
|---|
|  | 79 | namespace po = boost::program_options; | 
|---|
|  | 80 |  | 
|---|
|  | 81 | using boost::lexical_cast; | 
|---|
|  | 82 |  | 
|---|
| [ad011c] | 83 | #include "CodePatterns/Singleton.hpp" | 
|---|
| [75dc28] | 84 |  | 
|---|
|  | 85 | /** ValueStorage serves as a mediator to MapOfActions. | 
|---|
|  | 86 | * This is needed to relax inter-dependencies between the Queries and the Actions. | 
|---|
|  | 87 | * I.e. this is the interface implemented in MapOfActions which both can safely rely on | 
|---|
|  | 88 | * to store&retrieve/exchange values. | 
|---|
| [0275ad] | 89 | * | 
|---|
|  | 90 | * \section <ValueStorage> (ValueStorage howto) | 
|---|
|  | 91 | * | 
|---|
|  | 92 | * If you ever need to add a particular class to the ValueStorage, do as follows: | 
|---|
|  | 93 | * -# add a specialized queryCurrentValue and setCurrentValue to the definition | 
|---|
|  | 94 | *    of ValueStorage. | 
|---|
|  | 95 | * -# implement both in the declaration of ValueStorage. | 
|---|
|  | 96 | * -# in the implementation either directly implement the serializing of the | 
|---|
|  | 97 | *    class' members to a stringstream or use an already implemented operator<< | 
|---|
|  | 98 | *    or operator<<, respectively. | 
|---|
|  | 99 | * | 
|---|
| [75dc28] | 100 | */ | 
|---|
|  | 101 | class ValueStorage : public Singleton<ValueStorage> { | 
|---|
|  | 102 | friend class Singleton<ValueStorage>; | 
|---|
|  | 103 |  | 
|---|
| [9d33ba] | 104 | public: | 
|---|
| [e4afb4] | 105 |  | 
|---|
|  | 106 | bool isCurrentValuePresent(const char *name) const; | 
|---|
|  | 107 | void queryCurrentValue(const char * name, const atom * &_T); | 
|---|
|  | 108 | void queryCurrentValue(const char * name, const element * &_T); | 
|---|
|  | 109 | void queryCurrentValue(const char * name, const molecule * &_T); | 
|---|
|  | 110 | void queryCurrentValue(const char * name, class Box &_T); | 
|---|
|  | 111 | void queryCurrentValue(const char * name, class Vector &_T); | 
|---|
|  | 112 | void queryCurrentValue(const char * name, class BoxVector &_T); | 
|---|
| [0275ad] | 113 | void queryCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T); | 
|---|
| [e4afb4] | 114 | void queryCurrentValue(const char * name, std::vector<const atom *>&_T); | 
|---|
|  | 115 | void queryCurrentValue(const char * name, std::vector<const element *>&_T); | 
|---|
|  | 116 | void queryCurrentValue(const char * name, std::vector<const molecule *>&_T); | 
|---|
|  | 117 | void queryCurrentValue(const char * name, boost::filesystem::path&_T); | 
|---|
|  | 118 |  | 
|---|
| [03c902] | 119 | /** Gets a value from the storage | 
|---|
|  | 120 | * If the value is not present, an ASSERT is thrown unless optional is set to true. | 
|---|
|  | 121 | * \param _T key of value | 
|---|
|  | 122 | * \param optional whether this value is optional, i.e. may actually not be in the storage (i.e. may return false in this case). | 
|---|
|  | 123 | * \return true - value present, false - value not present (only given when optional set to true) | 
|---|
|  | 124 | */ | 
|---|
| [e4afb4] | 125 | template<typename T> void queryCurrentValue(const char * name, T &_T) | 
|---|
|  | 126 | { | 
|---|
|  | 127 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
|  | 128 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
| [336e33] | 129 | throw MissingValueException() << ValueStorageOptionName(name); | 
|---|
| [e4afb4] | 130 | _T = lexical_cast<T>(CurrentValueMap[name].c_str()); | 
|---|
|  | 131 | CurrentValueMap.erase(name); | 
|---|
|  | 132 | } else | 
|---|
| [882678] | 133 | throw IllegalTypeException() << ValueStorageOptionName(name) | 
|---|
|  | 134 | << ValueStoragePairTypeName(std::make_pair( | 
|---|
|  | 135 | typeid(_T).name(), | 
|---|
|  | 136 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str()) | 
|---|
|  | 137 | ); | 
|---|
| [75dc28] | 138 | } | 
|---|
| [e4afb4] | 139 | template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T) | 
|---|
|  | 140 | { | 
|---|
|  | 141 | T temp; | 
|---|
|  | 142 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
|  | 143 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
| [336e33] | 144 | throw MissingValueException() << ValueStorageOptionName(name); | 
|---|
| [e4afb4] | 145 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 146 | CurrentValueMap.erase(name); | 
|---|
|  | 147 | while (!stream.fail()) { | 
|---|
|  | 148 | stream >> temp >> std::ws; | 
|---|
| [78bb14] | 149 | if (!stream.fail()) { | 
|---|
|  | 150 | _T.push_back(temp); | 
|---|
|  | 151 | } | 
|---|
| [e4afb4] | 152 | } | 
|---|
|  | 153 | } else | 
|---|
| [882678] | 154 | throw IllegalTypeException() << ValueStorageOptionName(name) | 
|---|
|  | 155 | << ValueStoragePairTypeName(std::make_pair( | 
|---|
|  | 156 | typeid(_T).name(), | 
|---|
|  | 157 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str()) | 
|---|
|  | 158 | ); | 
|---|
| [e4afb4] | 159 | } | 
|---|
|  | 160 |  | 
|---|
|  | 161 | void setCurrentValue(const char * name, const atom * &_T); | 
|---|
|  | 162 | void setCurrentValue(const char * name, const element * &_T); | 
|---|
|  | 163 | void setCurrentValue(const char * name, const molecule * &_T); | 
|---|
|  | 164 | void setCurrentValue(const char * name, class Box &_T); | 
|---|
|  | 165 | void setCurrentValue(const char * name, class Vector &_T); | 
|---|
| [0275ad] | 166 | void setCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T); | 
|---|
| [e4afb4] | 167 | void setCurrentValue(const char * name, std::vector<const atom *>&_T); | 
|---|
|  | 168 | void setCurrentValue(const char * name, std::vector<const element *>&_T); | 
|---|
|  | 169 | void setCurrentValue(const char * name, std::vector<const molecule *>&_T); | 
|---|
|  | 170 | void setCurrentValue(const char * name, boost::filesystem::path&_T); | 
|---|
| [03c902] | 171 |  | 
|---|
|  | 172 | /** Sets a value in the storage. | 
|---|
|  | 173 | * \param name key of value | 
|---|
|  | 174 | * \param _T value | 
|---|
|  | 175 | */ | 
|---|
| [e4afb4] | 176 | template<class T> void setCurrentValue(const char * name, T &_T) | 
|---|
|  | 177 | { | 
|---|
|  | 178 | std::ostringstream stream; | 
|---|
|  | 179 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {  // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
|  | 180 | stream << _T; | 
|---|
|  | 181 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 182 | } else | 
|---|
| [882678] | 183 | throw IllegalTypeException() << ValueStorageOptionName(name) | 
|---|
|  | 184 | << ValueStoragePairTypeName(std::make_pair( | 
|---|
|  | 185 | typeid(_T).name(), | 
|---|
|  | 186 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str()) | 
|---|
|  | 187 | ); | 
|---|
|  | 188 | } | 
|---|
| [e4afb4] | 189 | /** Sets a value in the storage. | 
|---|
|  | 190 | * \param name key of value | 
|---|
|  | 191 | * \param _T value | 
|---|
| [03c902] | 192 | */ | 
|---|
| [e4afb4] | 193 | template<class T> void setCurrentValue(const char * name, std::vector<T> &_T) | 
|---|
|  | 194 | { | 
|---|
|  | 195 | std::ostringstream stream; | 
|---|
|  | 196 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {  // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
|  | 197 | std::ostringstream stream; | 
|---|
|  | 198 | for (typename std::vector<T>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) { | 
|---|
|  | 199 | stream << (*iter) << " "; | 
|---|
|  | 200 | } | 
|---|
|  | 201 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 202 | } else | 
|---|
| [882678] | 203 | throw IllegalTypeException() << ValueStorageOptionName(name) | 
|---|
|  | 204 | << ValueStoragePairTypeName(std::make_pair( | 
|---|
|  | 205 | typeid(_T).name(), | 
|---|
|  | 206 | OptionRegistry_instance.getOptionByName(name)->getTypeName().c_str()) | 
|---|
|  | 207 | ); | 
|---|
| [e4afb4] | 208 | } | 
|---|
|  | 209 |  | 
|---|
| [4885f85] | 210 | const std::string getCurrentValue(std::string actionname); | 
|---|
| [9d33ba] | 211 |  | 
|---|
| [75dc28] | 212 | protected: | 
|---|
|  | 213 | ValueStorage(); | 
|---|
|  | 214 | ~ValueStorage(); | 
|---|
| [03c902] | 215 |  | 
|---|
| [e4afb4] | 216 | std::map<std::string, std::string> CurrentValueMap; | 
|---|
|  | 217 |  | 
|---|
| [ce7fdc] | 218 | MoleCuilder::OptionRegistry &OptionRegistry_instance; | 
|---|
| [75dc28] | 219 | }; | 
|---|
|  | 220 |  | 
|---|
|  | 221 | #endif /* VALUESTORAGE_HPP_ */ | 
|---|