From 5dd75fae0c3c237262539ddec4b832cce7bb1285 Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Wed, 14 Feb 2018 12:30:59 +0000 Subject: [PATCH] clean up object type dispatcher for ::put --- src/shogun/base/SGObject.cpp | 23 ++++++----------------- src/shogun/base/SGObject.h | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/shogun/base/SGObject.cpp b/src/shogun/base/SGObject.cpp index e099045a1e0..b963c295013 100644 --- a/src/shogun/base/SGObject.cpp +++ b/src/shogun/base/SGObject.cpp @@ -1018,24 +1018,13 @@ void CSGObject::put(const std::string& name, CSGObject* value) { REQUIRE(value, "Cannot set %s::%s, no object provided.\n", get_name(), name.c_str()); + if (put_sgobject_type_dispatcher(name, value)) + return; + if (put_sgobject_type_dispatcher(name, value)) + return; - if (dynamic_cast(value)) - put(Tag(name), (CKernel*) value); - else if (dynamic_cast(value)) - { - if (has(name)) - { - SG_REF(value); - CDistance* old = get(name); - SG_UNREF(old); - } - put(Tag(name), (CDistance*) value); - } - else - { - SG_WARNING("Could not match %s with any base-type when putting %s::%s, trying as SGObject.\n",value->get_name(),get_name(), name.c_str()); - put(Tag(name), value); - } + SG_WARNING("Could not match %s with any base-type when putting %s::%s, trying as SGObject.\n",value->get_name(),get_name(), name.c_str()); + put(Tag(name), value); } namespace shogun diff --git a/src/shogun/base/SGObject.h b/src/shogun/base/SGObject.h index 0614df64c51..24c003b369f 100644 --- a/src/shogun/base/SGObject.h +++ b/src/shogun/base/SGObject.h @@ -369,6 +369,25 @@ class CSGObject } } +#ifndef SWIG + template + bool put_sgobject_type_dispatcher(const std::string& name, CSGObject* value) + { + if (dynamic_cast(value)) + { + if (has(name)) + { + SG_REF(value); + T* old = get(name); + SG_UNREF(old); + } + put(Tag(name), (T*) value); + return true; + } + return false; + } +#endif // SWIG + /** Untyped setter for an object class parameter, identified by a name. * Will attempt to convert passed object to appropriate type. *