diff --git a/src/interfaces/swig/shogun.i b/src/interfaces/swig/shogun.i index f25432c21e9..7c82a1b43e8 100644 --- a/src/interfaces/swig/shogun.i +++ b/src/interfaces/swig/shogun.i @@ -116,6 +116,16 @@ %include "ParameterObserver.i" +%define SUPPORT_TAG(short_type, type) + %template(put) shogun::CSGObject::put; + %template(get_ ## short_type) shogun::CSGObject::get; +%enddef +SUPPORT_TAG(double, float64_t) +SUPPORT_TAG(int, int64_t) +SUPPORT_TAG(real_vector, SGVector) +SUPPORT_TAG(real_matrix, SGMatrix) +SUPPORT_TAG(object, CSGObject*) + #if defined(SWIGPERL) %include "abstract_types_extension.i" #endif diff --git a/src/shogun/base/SGObject.cpp b/src/shogun/base/SGObject.cpp index 0ccc3b46214..8ea913a01b7 100644 --- a/src/shogun/base/SGObject.cpp +++ b/src/shogun/base/SGObject.cpp @@ -1012,47 +1012,3 @@ CSGObject* CSGObject::create_empty() const SG_REF(object); return object; } - -namespace shogun -{ -#define SGOBJECT_PUT_DEFINE(T) \ - void CSGObject::put(const std::string& name, T const& value) throw( \ - ShogunException) \ - { \ - Tag tag(name); \ - put(tag, value); \ - } - - SGOBJECT_PUT_DEFINE(SGVector) - SGOBJECT_PUT_DEFINE(SGVector) - SGOBJECT_PUT_DEFINE(CSGObject*) - -#define PUT_DEFINE_CHECK_AND_CAST(T) \ - else if (has(Tag(name))) put(Tag(name), (T)value); - -/* Some target languages have problems with scalar numeric types, so allow to - * convert all int/float types into each other. - * - * For example, Octave treats a=1.0 as an integer, and b=1.1 as a float. - * Furthermore, if a user wants to set a registered 16bit integer using a - * literal obj.put("16-bit-var", 2), might complain about a wrong type since - * internally the int literal is represented at a different word length. */ -#define SGOBJECT_PUT_DEFINE_WITH_CONVERSION(numeric_t) \ - void CSGObject::put( \ - const std::string& name, \ - numeric_t const& value) throw(ShogunException) \ - { \ - /* use correct type of possible, otherwise cast-convert */ \ - if (has(Tag(name))) \ - put(Tag(name), value); \ - PUT_DEFINE_CHECK_AND_CAST(int32_t) \ - PUT_DEFINE_CHECK_AND_CAST(float32_t) \ - PUT_DEFINE_CHECK_AND_CAST(float64_t) \ - else /* if nothing works, moan about original type */ \ - put(Tag(name), value); \ - } - - SGOBJECT_PUT_DEFINE_WITH_CONVERSION(int32_t) - SGOBJECT_PUT_DEFINE_WITH_CONVERSION(float32_t) - SGOBJECT_PUT_DEFINE_WITH_CONVERSION(float64_t) -}; diff --git a/src/shogun/base/SGObject.h b/src/shogun/base/SGObject.h index 157da1a0f63..10c23113de7 100644 --- a/src/shogun/base/SGObject.h +++ b/src/shogun/base/SGObject.h @@ -364,21 +364,29 @@ class CSGObject } } -#define SGOBJECT_PUT_DECLARE(T) \ - /** Setter for a class parameter, identified by a name. \ - * Throws an exception if the class does not have such a parameter. \ - * \ - * @param name name of the parameter \ - * @param value value of the parameter along with type information \ - */ \ - void put(const std::string& name, T const& value) throw(ShogunException); - - SGOBJECT_PUT_DECLARE(int32_t) - SGOBJECT_PUT_DECLARE(float32_t) - SGOBJECT_PUT_DECLARE(float64_t) - SGOBJECT_PUT_DECLARE(SGVector) - SGOBJECT_PUT_DECLARE(SGVector) - SGOBJECT_PUT_DECLARE(CSGObject*) +#ifndef SWIG + template ::value>* = nullptr> + [[deprecated ("SGObject parameters should be registered as a base class.")]] + void put(const std::string& name, const T value) + { + CSGObject* v = value; + Tag tag(name); + put(tag, v); + } +#endif + + /** Setter for a class parameter, identified by a name. + * Throws an exception if the class does not have such a parameter. + * + * @param name name of the parameter + * @param value value of the parameter along with type information + */ + template::value>> + void put(const std::string& name, const T value) throw(ShogunException) + { + Tag tag(name); + put(tag, value); + } /** Getter for a class parameter, identified by a Tag. * Throws an exception if the class does not have such a parameter.