// properties_base.h #if !defined(BASIC_PROPERTIES_BASE___H) #define BASIC_PROPERTIES_BASE___H #include <string> #include <sstream> #include <stdexcept> namespace Private { // convert anything from string to a type. template< class type, class char_type> inline type from_string( const std::basic_string< char_type> & strVal) { type t = type(); std::basic_istringstream< char_type> in( strVal); in >> t; if ( !in) throw std::runtime_error( "invalid property conversion"); return t; } template<> inline std::string from_string< std::string, char>( const std::string & strVal) { return strVal; } template<> inline std::wstring from_string< std::wstring, wchar_t>( const std::wstring & strVal) { return strVal; } }; /* contains application properties: that are constant throughout the application */ template< class char_type> class basic_properties_base { public: typedef std::basic_string< char_type> string_type; protected: // note: might throw! virtual void internal_get( const string_type & strName, string_type & str) const = 0; public: // note: might throw, if this property does not exist. template< class type> void get( const string_type & strName, type & val) const { string_type str; internal_get( strName, str); val = Private::from_string< type>( str); } }; typedef basic_properties_base< char> properties_base; typedef basic_properties_base< wchar_t> wproperties_base; #endif |