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 2f488e4
Show file tree
Hide file tree
Showing 3 changed files with 33 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)
};
38 changes: 23 additions & 15 deletions src/shogun/base/SGObject.h
Expand Up @@ -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<int32_t>)
SGOBJECT_PUT_DECLARE(SGVector<float64_t>)
SGOBJECT_PUT_DECLARE(CSGObject*)
#ifndef SWIG
template <typename T, std::enable_if_t<std::is_convertible<T, CSGObject*>::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<CSGObject*> 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<typename T, typename U = typename std::enable_if_t<!std::is_convertible<T, CSGObject*>::value>>
void put(const std::string& name, const T value) throw(ShogunException)

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Feb 8, 2018

Author Member

@lisitsyn so once we have owning policy shouldn't we actually starting implementing rvalue version of this as well, so that actually we dont do copies of elements but just move the whole thing into the any? and then just use any's move ctor?

This comment has been minimized.

Copy link
@lisitsyn

lisitsyn Feb 8, 2018

Member

Current owning policy is pointer based so it always allocates a copy. rvalue would make sense only if we used something else.

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Feb 8, 2018

Author Member

i mean in case of SGO its more or less doesnt'matter but in this very case SGV/M is being copied afaik. thankfully not the whole content... problem? or dontcare?

This comment has been minimized.

Copy link
@lisitsyn

lisitsyn Feb 8, 2018

Member

I'd say it is negligible to finding something in map :)

This comment has been minimized.

Copy link
@lisitsyn

lisitsyn Feb 8, 2018

Member

And not sure if rvalues and SWIG are even a thing so I'd say it is premature (yet)

This comment has been minimized.

{
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 2f488e4

Please sign in to comment.