Skip to content

Commit

Permalink
fix put and getType for interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Feb 7, 2018
1 parent df63984 commit 07b2130
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 59 deletions.
10 changes: 10 additions & 0 deletions src/interfaces/swig/shogun.i
Expand Up @@ -116,6 +116,16 @@

%include "ParameterObserver.i"

%define SUPPORT_TAG(short_type, type)
%template(put) shogun::CSGObject::put<type, void>;
%template(get_ ## short_type) shogun::CSGObject::get<type, void>;
%enddef
SUPPORT_TAG(double, float64_t)
SUPPORT_TAG(int, int64_t)
SUPPORT_TAG(real_vector, SGVector<float64_t>)
SUPPORT_TAG(real_matrix, SGMatrix<float64_t>)
SUPPORT_TAG(object, CSGObject*)

#if defined(SWIGPERL)
%include "abstract_types_extension.i"
#endif
44 changes: 0 additions & 44 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -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<T> tag(name); \
put(tag, value); \
}

SGOBJECT_PUT_DEFINE(SGVector<int32_t>)
SGOBJECT_PUT_DEFINE(SGVector<float64_t>)
SGOBJECT_PUT_DEFINE(CSGObject*)

#define PUT_DEFINE_CHECK_AND_CAST(T) \
else if (has(Tag<T>(name))) put(Tag<T>(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<numeric_t>(name))) \
put(Tag<numeric_t>(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<numeric_t>(name), value); \
}

SGOBJECT_PUT_DEFINE_WITH_CONVERSION(int32_t)
SGOBJECT_PUT_DEFINE_WITH_CONVERSION(float32_t)
SGOBJECT_PUT_DEFINE_WITH_CONVERSION(float64_t)
};
27 changes: 12 additions & 15 deletions src/shogun/base/SGObject.h
Expand Up @@ -364,21 +364,18 @@ 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<int32_t>)
SGOBJECT_PUT_DECLARE(SGVector<float64_t>)
SGOBJECT_PUT_DECLARE(CSGObject*)
/** 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<typename T, typename U = void>
void put(const std::string& name, const T value) throw(ShogunException)
{
Tag<T> 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.
Expand Down

0 comments on commit 07b2130

Please sign in to comment.