From 88480a4fb234fb4f31475a9c2b0b1b1ab723254f Mon Sep 17 00:00:00 2001 From: randomdan Date: Sun, 9 Dec 2012 15:26:05 +0000 Subject: Support default values of different type to target values in options Support direct assignment of values to targets without lexical casts --- project2/common/options.h | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/project2/common/options.h b/project2/common/options.h index b4cbaf8..2ccb7ec 100644 --- a/project2/common/options.h +++ b/project2/common/options.h @@ -7,6 +7,27 @@ #include #include +namespace std { + // + // Should be part of the standard headers, but seems to be missing + // + template + class __is_assignable_helper : public __sfinae_types { + template + static decltype(declval<_Tp1>() = declval<_Up1>(), __one()) __test(int); + + template + static __two __test(...); + + public: + static constexpr bool value = sizeof(__test<_Tp, _Up>(0)) == 1; + }; + /// is_assignable + template + struct is_assignable : public integral_constant::value> + { }; +} + class Options { public: class Target; @@ -43,10 +64,19 @@ class Options { *target = T(); } void assign(const Glib::ustring & value) const { - *target = boost::lexical_cast(value); + doAssign(value); } protected: T * target; + private: + template + void doAssign(const Glib::ustring & value, typename boost::disable_if, dummy>::type = 0) const { + *target = boost::lexical_cast(value); + } + template + void doAssign(const Glib::ustring & value, typename boost::enable_if, dummy>::type = 0) const { + *target = value; + } }; template class TypedTarget > : public InstanceTarget { @@ -66,9 +96,9 @@ class Options { VofT * target; }; - template class DefaultTypedTarget : public TypedTarget { + template class DefaultTypedTarget : public TypedTarget { public: - DefaultTypedTarget(T * t, const T & d) : + DefaultTypedTarget(T * t, const D & d) : TypedTarget(t), defValue(d) { } @@ -104,7 +134,7 @@ class Options { template static TargetPtr value(T * t, const D & d) { - return new DefaultTypedTarget(t, d); + return new DefaultTypedTarget(t, d); } void reset() const; -- cgit v1.2.3