#ifndef SETTABLE_HPP #define SETTABLE_HPP #include #include #include namespace settable { namespace setter { namespace string2type { namespace helper { template T convert(const std::string &value); } } class abstract_setter { public: virtual void operator=(const std::string &value) = 0; }; template class setter : public abstract_setter { public: setter(void *p) : ptr(p) { BOOST_ASSERT(ptr != 0); } virtual void operator=(const std::string &value) { T *ptrT = static_cast(ptr); BOOST_ASSERT(ptrT != 0); *ptrT = string2type::helper::convert(value); } void *ptr; }; } class settable { public: void set_attribute(const std::string &attr, const std::string &value) { boost::shared_ptr setter = setters[attr]; if (setter) { *setter = value; } } template void register_setter(const std::string &name, void *ptr) { setters.insert(std::make_pair(name, boost::shared_ptr(new setter::setter(ptr)))); } private: std::map< std::string, boost::shared_ptr > setters; }; } #endif /* SETTABLE_HPP */