Skip to content

Commit

Permalink
Support get/put in SWIG for some of types
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jan 1, 2018
1 parent 99e083f commit 89fe4b6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 62 deletions.
25 changes: 22 additions & 3 deletions src/interfaces/swig/SGBase.i
Expand Up @@ -127,6 +127,7 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav
#endif

using namespace shogun;

%}

#if defined (SWIGPERL) && defined(HAVE_PDL)
Expand Down Expand Up @@ -342,8 +343,8 @@ namespace shogun

PyObject* __reduce_ex__(int proto)
{
pickle_ascii= (proto==0) ? 1 : 0;
return NULL;
pickle_ascii = (proto==0) ? 1 : 0;
Py_RETURN_NONE;
}

PyObject* __getstate__()
Expand Down Expand Up @@ -504,5 +505,23 @@ copy_reg._reduce_ex=_sg_reduce_ex
copy_reg._reconstructor=_sg_reconstructor
%}


#endif /* SWIGPYTHON */

%include <shogun/lib/basetag.h>
%include <shogun/lib/tag.h>
%include <shogun/lib/ShogunException.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)
14 changes: 7 additions & 7 deletions src/shogun/base/SGObject.h
Expand Up @@ -318,8 +318,8 @@ class CSGObject
* @param name name of the parameter
* @return true if the parameter exists with the input name and type
*/
template <typename T, typename U=void>
bool has(const std::string& name) const
template <typename T, typename U = void>
bool has(const std::string& name) const throw(ShogunException)
{
BaseTag tag(name);
if (!has_parameter(tag))
Expand All @@ -335,7 +335,7 @@ class CSGObject
* @param value value of the parameter
*/
template <typename T>
void put(const Tag<T>& _tag, const T& value)
void put(const Tag<T>& _tag, const T& value) throw(ShogunException)
{
if (has_parameter(_tag))
{
Expand All @@ -361,7 +361,7 @@ class CSGObject
* @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)
void put(const std::string& name, const T& value) throw(ShogunException)
{
Tag<T> tag(name);
put(tag, value);
Expand All @@ -374,7 +374,7 @@ class CSGObject
* @return value of the parameter identified by the input tag
*/
template <typename T>
T get(const Tag<T>& _tag) const
T get(const Tag<T>& _tag) const throw(ShogunException)
{
const Any value = get_parameter(_tag).get_value();
try
Expand All @@ -396,8 +396,8 @@ class CSGObject
* @param name name of the parameter
* @return value of the parameter corresponding to the input name and type
*/
template <typename T, typename U=void>
T get(const std::string& name) const
template <typename T, typename U = void>
T get(const std::string& name) const throw(ShogunException)
{
Tag<T> tag(name);
return get(tag);
Expand Down
113 changes: 61 additions & 52 deletions src/shogun/lib/basetag.h
Expand Up @@ -74,61 +74,69 @@ namespace shogun
m_hash = other.m_hash;
return *this;
}

/** @return name of Tag */
inline std::string name() const
{
return m_name;
}

/** @return hash of Tag */
inline std::size_t hash() const
{
return m_hash;
}

/** Equality operator
* @param first first BaseTag
* @param second secondBaseTag
*/
friend inline bool operator==(const BaseTag& first, const BaseTag& second);

/** Inequality operator
* @param first first BaseTag
* @param second secondBaseTag
*/
friend inline bool operator!=(const BaseTag& first, const BaseTag& second);

/** Comparison operator
* @param first first BaseTag
* @param second secondBaseTag
*/
friend inline bool operator<(const BaseTag& first, const BaseTag& second);

private:
/** name for object */
std::string m_name;
/** hash is stored for quick access from hash-map */
size_t m_hash;
};

inline bool operator==(const BaseTag& first, const BaseTag& second)
{
return first.m_hash == second.m_hash ? first.m_name == second.m_name : false;
}

inline bool operator!=(const BaseTag& first, const BaseTag& second)
{
return !(first == second);
}

inline bool operator<(const BaseTag& first, const BaseTag& second)
{
return first.m_name < second.m_name;
}

/** @return name of Tag */
inline std::string name() const
{
return m_name;
}

/** @return hash of Tag */
inline std::size_t hash() const
{
return m_hash;
}

#ifndef SWIG
/** Equality operator
* @param first first BaseTag
* @param second secondBaseTag
*/
friend inline bool
operator==(const BaseTag& first, const BaseTag& second);

/** Inequality operator
* @param first first BaseTag
* @param second secondBaseTag
*/
friend inline bool
operator!=(const BaseTag& first, const BaseTag& second);

/** Comparison operator
* @param first first BaseTag
* @param second secondBaseTag
*/
friend inline bool
operator<(const BaseTag& first, const BaseTag& second);
#endif /* SWIG */

private:
/** name for object */
std::string m_name;
/** hash is stored for quick access from hash-map */
size_t m_hash;
};

#ifndef SWIG
inline bool operator==(const BaseTag& first, const BaseTag& second)
{
return first.m_hash == second.m_hash ? first.m_name == second.m_name
: false;
}

inline bool operator!=(const BaseTag& first, const BaseTag& second)
{
return !(first == second);
}

inline bool operator<(const BaseTag& first, const BaseTag& second)
{
return first.m_name < second.m_name;
}
#endif /* SWIG */
}

#ifndef SWIG
namespace std
{
/** Overload hash for BaseTag */
Expand All @@ -142,5 +150,6 @@ namespace std
};

}
#endif /* SWIG */

#endif // _BASETAG_H_

0 comments on commit 89fe4b6

Please sign in to comment.