/* * ValueStorage.hpp * * Created on: Jul 22, 2010 * Author: heber */ #ifndef VALUESTORAGE_HPP_ #define VALUESTORAGE_HPP_ #include #include #include #include #include #include #include #include "Actions/OptionTrait.hpp" #include "Actions/OptionRegistry.hpp" #include "Exceptions/IllegalTypeException.hpp" #include "Exceptions/MissingValueException.hpp" #include "Helpers/Assert.hpp" #include "Patterns/Singleton.hpp" class MapOfActionsTest; class Box; class atom; class element; class molecule; class Vector; namespace po = boost::program_options; using boost::lexical_cast; #include "Patterns/Singleton.hpp" /** ValueStorage serves as a mediator to MapOfActions. * This is needed to relax inter-dependencies between the Queries and the Actions. * I.e. this is the interface implemented in MapOfActions which both can safely rely on * to store&retrieve/exchange values. */ class ValueStorage : public Singleton { friend class Singleton; public: bool isCurrentValuePresent(const char *name) const; void queryCurrentValue(const char * name, const atom * &_T); void queryCurrentValue(const char * name, const element * &_T); void queryCurrentValue(const char * name, const molecule * &_T); void queryCurrentValue(const char * name, class Box &_T); void queryCurrentValue(const char * name, class Vector &_T); void queryCurrentValue(const char * name, class BoxVector &_T); void queryCurrentValue(const char * name, std::vector&_T); void queryCurrentValue(const char * name, std::vector&_T); void queryCurrentValue(const char * name, std::vector&_T); void queryCurrentValue(const char * name, boost::filesystem::path&_T); /** Gets a value from the storage * If the value is not present, an ASSERT is thrown unless optional is set to true. * \param _T key of value * \param optional whether this value is optional, i.e. may actually not be in the storage (i.e. may return false in this case). * \return true - value present, false - value not present (only given when optional set to true) */ template void queryCurrentValue(const char * name, T &_T) { if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr if (CurrentValueMap.find(name) == CurrentValueMap.end()) throw MissingValueException(__FILE__, __LINE__); _T = lexical_cast(CurrentValueMap[name].c_str()); CurrentValueMap.erase(name); } else throw IllegalTypeException(__FILE__,__LINE__); } template void queryCurrentValue(const char * name, std::vector &_T) { T temp; if (typeid( std::vector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr if (CurrentValueMap.find(name) == CurrentValueMap.end()) throw MissingValueException(__FILE__, __LINE__); std::istringstream stream(CurrentValueMap[name]); CurrentValueMap.erase(name); while (!stream.fail()) { stream >> temp >> std::ws; _T.push_back(temp); } } else throw IllegalTypeException(__FILE__,__LINE__); } void setCurrentValue(const char * name, const atom * &_T); void setCurrentValue(const char * name, const element * &_T); void setCurrentValue(const char * name, const molecule * &_T); void setCurrentValue(const char * name, class Box &_T); void setCurrentValue(const char * name, class Vector &_T); void setCurrentValue(const char * name, std::vector&_T); void setCurrentValue(const char * name, std::vector&_T); void setCurrentValue(const char * name, std::vector&_T); void setCurrentValue(const char * name, boost::filesystem::path&_T); /** Sets a value in the storage. * \param name key of value * \param _T value */ template void setCurrentValue(const char * name, T &_T) { std::ostringstream stream; if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr stream << _T; CurrentValueMap[name] = stream.str(); } else throw IllegalTypeException(__FILE__,__LINE__); } /** Sets a value in the storage. * \param name key of value * \param _T value */ template void setCurrentValue(const char * name, std::vector &_T) { std::ostringstream stream; if (typeid( std::vector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr std::ostringstream stream; for (typename std::vector::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) { stream << (*iter) << " "; } CurrentValueMap[name] = stream.str(); } else throw IllegalTypeException(__FILE__,__LINE__); } const std::string getCurrentValue(std::string actionname); protected: ValueStorage(); ~ValueStorage(); std::map CurrentValueMap; OptionRegistry &OptionRegistry_instance; }; #endif /* VALUESTORAGE_HPP_ */