From 2ca14246638d3b0599dd34825f8b024467eb30f2 Mon Sep 17 00:00:00 2001 From: Jens Luedicke Date: Wed, 8 Sep 2010 23:29:37 +0200 Subject: [PATCH] Initial commit. --- settable.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 settable.hpp diff --git a/settable.hpp b/settable.hpp new file mode 100644 index 0000000..683d3cd --- /dev/null +++ b/settable.hpp @@ -0,0 +1,62 @@ +#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 */ +