From 558f32d2d55907a85e8a2d4351817902a335463c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 6 Mar 2013 14:13:21 +0100 Subject: [PATCH] throw a runtime error when a wrong type is requested for an option --- src/option_manager.cc | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/option_manager.cc b/src/option_manager.cc index 817d5409..9471f03c 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -93,8 +93,21 @@ private: Option::Option(OptionManager& manager, String name) : m_manager(manager), m_name(std::move(name)) {} -template const T& Option::get() const { return dynamic_cast*>(this)->get(); } -template void Option::set(const T& val) { return dynamic_cast*>(this)->set(val); } +template const T& Option::get() const +{ + auto* typed_opt = dynamic_cast*>(this); + if (not typed_opt) + throw runtime_error("option " + name() + " is not of type " + typeid(T).name()); + return typed_opt->get(); +} + +template void Option::set(const T& val) +{ + auto* typed_opt = dynamic_cast*>(this); + if (not typed_opt) + throw runtime_error("option " + name() + " is not of type " + typeid(T).name()); + return typed_opt->set(val); +} template const String& Option::get() const; template void Option::set(const String&);