From 89fe4b621312a9b0410ce5c379561b9d8c0e4d8a Mon Sep 17 00:00:00 2001 From: Sergey Lisitsyn Date: Mon, 1 Jan 2018 21:06:01 +0100 Subject: [PATCH] Support get/put in SWIG for some of types --- src/interfaces/swig/SGBase.i | 25 +++++++- src/shogun/base/SGObject.h | 14 ++--- src/shogun/lib/basetag.h | 113 +++++++++++++++++++---------------- 3 files changed, 90 insertions(+), 62 deletions(-) diff --git a/src/interfaces/swig/SGBase.i b/src/interfaces/swig/SGBase.i index baba2c8aedd..7272b19844b 100644 --- a/src/interfaces/swig/SGBase.i +++ b/src/interfaces/swig/SGBase.i @@ -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) @@ -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__() @@ -504,5 +505,23 @@ copy_reg._reduce_ex=_sg_reduce_ex copy_reg._reconstructor=_sg_reconstructor %} - #endif /* SWIGPYTHON */ + +%include +%include +%include +%include + +%define SUPPORT_TAG(camel_type, short_type, type) + %template(Tag ## camel_type) shogun::Tag; + %template(put) shogun::CSGObject::put; + %template(put) shogun::CSGObject::put; + %template(put) shogun::CSGObject::put; + %template(get_ ## short_type) shogun::CSGObject::get; + %template(has) shogun::CSGObject::has; + %template(has_ ## short_type) shogun::CSGObject::has; +%enddef + +SUPPORT_TAG(String, string, std::string) +SUPPORT_TAG(Float64, float, float64_t) +SUPPORT_TAG(Int64, int, int64_t) diff --git a/src/shogun/base/SGObject.h b/src/shogun/base/SGObject.h index b6bc550137d..f4fe2da6b87 100644 --- a/src/shogun/base/SGObject.h +++ b/src/shogun/base/SGObject.h @@ -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 - bool has(const std::string& name) const + template + bool has(const std::string& name) const throw(ShogunException) { BaseTag tag(name); if (!has_parameter(tag)) @@ -335,7 +335,7 @@ class CSGObject * @param value value of the parameter */ template - void put(const Tag& _tag, const T& value) + void put(const Tag& _tag, const T& value) throw(ShogunException) { if (has_parameter(_tag)) { @@ -361,7 +361,7 @@ class CSGObject * @param value value of the parameter along with type information */ template - void put(const std::string& name, const T& value) + void put(const std::string& name, const T& value) throw(ShogunException) { Tag tag(name); put(tag, value); @@ -374,7 +374,7 @@ class CSGObject * @return value of the parameter identified by the input tag */ template - T get(const Tag& _tag) const + T get(const Tag& _tag) const throw(ShogunException) { const Any value = get_parameter(_tag).get_value(); try @@ -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 - T get(const std::string& name) const + template + T get(const std::string& name) const throw(ShogunException) { Tag tag(name); return get(tag); diff --git a/src/shogun/lib/basetag.h b/src/shogun/lib/basetag.h index 0c45434ed68..e1ac73e9adf 100644 --- a/src/shogun/lib/basetag.h +++ b/src/shogun/lib/basetag.h @@ -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 */ @@ -142,5 +150,6 @@ namespace std }; } +#endif /* SWIG */ #endif // _BASETAG_H_