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