Skip to content

Commit

Permalink
CSGObject::put SWIG attempt via explicit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed Feb 6, 2018
1 parent 56ae5e1 commit e0dc2b6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
15 changes: 6 additions & 9 deletions examples/meta/src/meta_api/kwargs.sg
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
# This also fails (keywords not allowed outside initialisation variables)
#kernel_factory("GaussianKernel", a=glob_fun(ordinary_argument))

# doesnt work in ruby (TODO, don't allow in meta grammar)
# KernelMachine svm2 = kernel_machine("LibSVM", kernel=kernel("GaussianKernel"))



# Real example
# ------------

# The following lines fail in Octave because of a float literal issue
#KernelMachine svm = kernel_machine("LibSVM", C1=2.0)
#GaussianKernel k(log_width=2.0)
#k.put("log_width", 4.0)

# The following line fails in Python because of a bool literal issue
#GaussianKernel k2(log_width=14.0, lhs_equals_rhs=True)
Kernel k = kernel("GaussianKernel")
KernelMachine svm = kernel_machine("LibSVM", kernel=k)

# The following line currently gives a type error
#svm.put("kernel", k)
19 changes: 0 additions & 19 deletions src/interfaces/swig/SGBase.i
Original file line number Diff line number Diff line change
Expand Up @@ -508,22 +508,3 @@ copy_reg._reconstructor=_sg_reconstructor
%}

#endif /* SWIGPYTHON */

%include <shogun/lib/basetag.h>
%include <shogun/lib/tag.h>
%include <shogun/base/SGObject.h>

%define SUPPORT_TAG(camel_type, short_type, type)
%template(Tag ## camel_type) shogun::Tag<type>;
%template(put) shogun::CSGObject::put<type>;
%template(put) shogun::CSGObject::put<type, void>;
%template(put) shogun::CSGObject::put<type>;
%template(get_ ## short_type) shogun::CSGObject::get<type, void>;
%template(has) shogun::CSGObject::has<type>;
%template(has_ ## short_type) shogun::CSGObject::has<type, void>;
%enddef

SUPPORT_TAG(String, string, std::string)
SUPPORT_TAG(Float64, float, float64_t)
SUPPORT_TAG(Int64, int, int64_t)
SUPPORT_TAG(Object, object, CSGObject*)
17 changes: 17 additions & 0 deletions src/shogun/base/SGObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,3 +1012,20 @@ 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(int32_t)
SGOBJECT_PUT_DEFINE(float64_t)
SGOBJECT_PUT_DEFINE(SGVector<int32_t>)
SGOBJECT_PUT_DEFINE(SGVector<float64_t>)
SGOBJECT_PUT_DEFINE(CSGObject*)

};
27 changes: 15 additions & 12 deletions src/shogun/base/SGObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,21 @@ class 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);
}

#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(float64_t)
SGOBJECT_PUT_DECLARE(SGVector<int32_t>)
SGOBJECT_PUT_DECLARE(SGVector<float64_t>)
SGOBJECT_PUT_DECLARE(CSGObject*)

/** 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 e0dc2b6

Please sign in to comment.