| 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 |  | 
|---|
| 11 | // include config.h | 
|---|
| 12 | #ifdef HAVE_CONFIG_H | 
|---|
| 13 | #include <config.h> | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 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> | 
|---|
| 25 |  | 
|---|
| 26 | #include "Actions/OptionTrait.hpp" | 
|---|
| 27 | #include "Actions/OptionRegistry.hpp" | 
|---|
| 28 | #include "Exceptions/IllegalTypeException.hpp" | 
|---|
| 29 | #include "Exceptions/MissingValueException.hpp" | 
|---|
| 30 | #include "CodePatterns/Assert.hpp" | 
|---|
| 31 | #include "CodePatterns/Singleton.hpp" | 
|---|
| 32 |  | 
|---|
| 33 | class MapOfActionsTest; | 
|---|
| 34 |  | 
|---|
| 35 | class Box; | 
|---|
| 36 | class atom; | 
|---|
| 37 | class element; | 
|---|
| 38 | class molecule; | 
|---|
| 39 | class Vector; | 
|---|
| 40 | class RandomNumberDistribution_Parameters; | 
|---|
| 41 |  | 
|---|
| 42 | namespace po = boost::program_options; | 
|---|
| 43 |  | 
|---|
| 44 | using boost::lexical_cast; | 
|---|
| 45 |  | 
|---|
| 46 | #include "CodePatterns/Singleton.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | /** ValueStorage serves as a mediator to MapOfActions. | 
|---|
| 49 | * This is needed to relax inter-dependencies between the Queries and the Actions. | 
|---|
| 50 | * I.e. this is the interface implemented in MapOfActions which both can safely rely on | 
|---|
| 51 | * to store&retrieve/exchange values. | 
|---|
| 52 | * | 
|---|
| 53 | * \section <ValueStorage> (ValueStorage howto) | 
|---|
| 54 | * | 
|---|
| 55 | * If you ever need to add a particular class to the ValueStorage, do as follows: | 
|---|
| 56 | * -# add a specialized queryCurrentValue and setCurrentValue to the definition | 
|---|
| 57 | *    of ValueStorage. | 
|---|
| 58 | * -# implement both in the declaration of ValueStorage. | 
|---|
| 59 | * -# in the implementation either directly implement the serializing of the | 
|---|
| 60 | *    class' members to a stringstream or use an already implemented operator<< | 
|---|
| 61 | *    or operator<<, respectively. | 
|---|
| 62 | * | 
|---|
| 63 | */ | 
|---|
| 64 | class ValueStorage : public Singleton<ValueStorage> { | 
|---|
| 65 | friend class Singleton<ValueStorage>; | 
|---|
| 66 |  | 
|---|
| 67 | public: | 
|---|
| 68 |  | 
|---|
| 69 | bool isCurrentValuePresent(const char *name) const; | 
|---|
| 70 | void queryCurrentValue(const char * name, const atom * &_T); | 
|---|
| 71 | void queryCurrentValue(const char * name, const element * &_T); | 
|---|
| 72 | void queryCurrentValue(const char * name, const molecule * &_T); | 
|---|
| 73 | void queryCurrentValue(const char * name, class Box &_T); | 
|---|
| 74 | void queryCurrentValue(const char * name, class Vector &_T); | 
|---|
| 75 | void queryCurrentValue(const char * name, class BoxVector &_T); | 
|---|
| 76 | void queryCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T); | 
|---|
| 77 | void queryCurrentValue(const char * name, std::vector<const atom *>&_T); | 
|---|
| 78 | void queryCurrentValue(const char * name, std::vector<const element *>&_T); | 
|---|
| 79 | void queryCurrentValue(const char * name, std::vector<const molecule *>&_T); | 
|---|
| 80 | void queryCurrentValue(const char * name, boost::filesystem::path&_T); | 
|---|
| 81 |  | 
|---|
| 82 | /** Gets a value from the storage | 
|---|
| 83 | * If the value is not present, an ASSERT is thrown unless optional is set to true. | 
|---|
| 84 | * \param _T key of value | 
|---|
| 85 | * \param optional whether this value is optional, i.e. may actually not be in the storage (i.e. may return false in this case). | 
|---|
| 86 | * \return true - value present, false - value not present (only given when optional set to true) | 
|---|
| 87 | */ | 
|---|
| 88 | template<typename T> void queryCurrentValue(const char * name, T &_T) | 
|---|
| 89 | { | 
|---|
| 90 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
| 91 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
| 92 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
| 93 | _T = lexical_cast<T>(CurrentValueMap[name].c_str()); | 
|---|
| 94 | CurrentValueMap.erase(name); | 
|---|
| 95 | } else | 
|---|
| 96 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
| 97 | } | 
|---|
| 98 | template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T) | 
|---|
| 99 | { | 
|---|
| 100 | T temp; | 
|---|
| 101 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
| 102 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
| 103 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
| 104 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
| 105 | CurrentValueMap.erase(name); | 
|---|
| 106 | while (!stream.fail()) { | 
|---|
| 107 | stream >> temp >> std::ws; | 
|---|
| 108 | if (!stream.fail()) { | 
|---|
| 109 | _T.push_back(temp); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 | } else | 
|---|
| 113 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void setCurrentValue(const char * name, const atom * &_T); | 
|---|
| 117 | void setCurrentValue(const char * name, const element * &_T); | 
|---|
| 118 | void setCurrentValue(const char * name, const molecule * &_T); | 
|---|
| 119 | void setCurrentValue(const char * name, class Box &_T); | 
|---|
| 120 | void setCurrentValue(const char * name, class Vector &_T); | 
|---|
| 121 | void setCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T); | 
|---|
| 122 | void setCurrentValue(const char * name, std::vector<const atom *>&_T); | 
|---|
| 123 | void setCurrentValue(const char * name, std::vector<const element *>&_T); | 
|---|
| 124 | void setCurrentValue(const char * name, std::vector<const molecule *>&_T); | 
|---|
| 125 | void setCurrentValue(const char * name, boost::filesystem::path&_T); | 
|---|
| 126 |  | 
|---|
| 127 | /** Sets a value in the storage. | 
|---|
| 128 | * \param name key of value | 
|---|
| 129 | * \param _T value | 
|---|
| 130 | */ | 
|---|
| 131 | template<class T> void setCurrentValue(const char * name, T &_T) | 
|---|
| 132 | { | 
|---|
| 133 | std::ostringstream stream; | 
|---|
| 134 | if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {  // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
| 135 | stream << _T; | 
|---|
| 136 | CurrentValueMap[name] = stream.str(); | 
|---|
| 137 | } else | 
|---|
| 138 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
| 139 | } | 
|---|
| 140 | /** Sets a value in the storage. | 
|---|
| 141 | * \param name key of value | 
|---|
| 142 | * \param _T value | 
|---|
| 143 | */ | 
|---|
| 144 | template<class T> void setCurrentValue(const char * name, std::vector<T> &_T) | 
|---|
| 145 | { | 
|---|
| 146 | std::ostringstream stream; | 
|---|
| 147 | if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {  // constructor of type_info is private, hence can only store by ref or ptr | 
|---|
| 148 | std::ostringstream stream; | 
|---|
| 149 | for (typename std::vector<T>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) { | 
|---|
| 150 | stream << (*iter) << " "; | 
|---|
| 151 | } | 
|---|
| 152 | CurrentValueMap[name] = stream.str(); | 
|---|
| 153 | } else | 
|---|
| 154 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | const std::string getCurrentValue(std::string actionname); | 
|---|
| 158 |  | 
|---|
| 159 | protected: | 
|---|
| 160 | ValueStorage(); | 
|---|
| 161 | ~ValueStorage(); | 
|---|
| 162 |  | 
|---|
| 163 | std::map<std::string, std::string> CurrentValueMap; | 
|---|
| 164 |  | 
|---|
| 165 | OptionRegistry &OptionRegistry_instance; | 
|---|
| 166 | }; | 
|---|
| 167 |  | 
|---|
| 168 | #endif /* VALUESTORAGE_HPP_ */ | 
|---|