Skip to content

Commit

Permalink
Add Serializable object and drop Wrapped classes
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Jan 19, 2018
1 parent 33d68fb commit 1b55e38
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 433 deletions.
24 changes: 14 additions & 10 deletions examples/meta/generator/translate.py
Expand Up @@ -185,8 +185,8 @@ def injectVarsStoring(self, statementList, programName, varsToStore):
""" Injects statements at the end of the program that perform variable
storing
"""
storage = "__sg_storage"
storageFile = "__sg_storage_file"
storage = "sg_storage"
storageFile = "sg_storage_file"

# TODO: handle directories
storageFilename = {
Expand All @@ -195,7 +195,7 @@ def injectVarsStoring(self, statementList, programName, varsToStore):
# 'w'
storageFilemode = {"Expr": {"CharLiteral": 'w'}}
storageComment = {"Comment": " Serialize output for integration testing (automatically generated)"}
storageInit = {"Init": [{"ObjectType": "WrappedObjectArray"},
storageInit = {"Init": [{"ObjectType": "DynamicObjectArray"},
{"Identifier": storage},
{"ArgumentList": []}]}
storageFileInit = {
Expand All @@ -221,16 +221,20 @@ def injectVarsStoring(self, statementList, programName, varsToStore):
# i.e. one key
# python2/3 compatible key accessing
sgType = vartypeAST[list(vartypeAST.keys())[0]]

assert sgType in getBasicTypesToStore() or sgType in getSGTypesToStore()
if sgType in getBasicTypesToStore():
methodNameSuffix = sgType
elif sgType in getSGTypesToStore():
methodNameSuffix = getSGTypeToStoreMethodName(sgType)


if "cpp" in self.targetDict["FileExtension"]:
methodNameSuffix = "<%s>" % self.targetDict["Type"][sgType]
else:
if sgType in getBasicTypesToStore():
methodNameSuffix = sgType
elif sgType in getSGTypesToStore():
methodNameSuffix = getSGTypeToStoreMethodName(sgType)
methodNameSuffix = "_%s" % methodNameSuffix

methodCall = {
"MethodCall": [{"Identifier": storage},
{"Identifier": "append_wrapped_%s" % methodNameSuffix},
{"Identifier": "append_element%s" % methodNameSuffix},
{"ArgumentList": [varnameIdentifierExpr,
varnameExpr]}]
}
Expand Down
30 changes: 28 additions & 2 deletions src/interfaces/swig/Library.i
Expand Up @@ -22,7 +22,6 @@
%rename(Hash) CHash;
%rename(StructuredData) CStructuredData;
%rename(DynamicObjectArray) CDynamicObjectArray;
%rename(WrappedObjectArray) CWrappedObjectArray;
%rename(Tokenizer) CTokenizer;
%rename(DelimiterTokenizer) CDelimiterTokenizer;
%rename(NGramTokenizer) CNGramTokenizer;
Expand Down Expand Up @@ -399,7 +398,34 @@ namespace shogun
%include <shogun/lib/StructuredDataTypes.h>
%include <shogun/lib/StructuredData.h>
%include <shogun/lib/DynamicObjectArray.h>
%include <shogun/lib/WrappedObjectArray.h>
namespace shogun
{
/* Specialize DynamicObjectArray::append_element function */
#ifdef USE_FLOAT64
%template(append_element_real) CDynamicObjectArray::append_element<float64_t, float64_t>;
%template(append_element_real_vector) CDynamicObjectArray::append_element<SGVector<float64_t>, SGVector<float64_t>>;
%template(append_element_real_matrix) CDynamicObjectArray::append_element<SGMatrix<float64_t>, SGMatrix<float64_t>>;
#ifdef SWIGOCTAVE
/* (Octave converts single element arrays to scalars and our typemaps take that for real) */
%extend CDynamicObjectArray {
bool append_element_real_vector(float64_t v, const char* name="")
{
SGVector<float64_t> wrap(1);
wrap[0] = v;
return $self->append_element(wrap, name);
}
}
#endif
#endif
#ifdef USE_FLOAT32
%template(append_element_float) CDynamicObjectArray::append_element<float32_t, float32_t>;
%template(append_element_float_vector) CDynamicObjectArray::append_element<SGVector<float32_t>, SGVector<float32_t>>;
%template(append_element_float_matrix) CDynamicObjectArray::append_element<SGMatrix<float32_t>, SGMatrix<float32_t>>;
#endif
#ifdef USE_INT32
%template(append_element_int) CDynamicObjectArray::append_element<int32_t, int32_t>;
#endif
}
%include <shogun/lib/IndexBlock.h>
%include <shogun/lib/IndexBlockRelation.h>
%include <shogun/lib/IndexBlockGroup.h>
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/swig/Library_includes.i
Expand Up @@ -21,7 +21,6 @@
#include <shogun/lib/StructuredDataTypes.h>
#include <shogun/lib/StructuredData.h>
#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/lib/WrappedObjectArray.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockRelation.h>
#include <shogun/lib/IndexBlockGroup.h>
Expand Down
80 changes: 35 additions & 45 deletions src/shogun/lib/WrappedSGMatrix.h → src/shogun/io/Serializable.h
Expand Up @@ -2,7 +2,7 @@
* -*- coding: utf-8 -*-
* vim: set fileencoding=utf-8
*
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -31,81 +31,71 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Heiko Strathmann
*/
#ifdef USE_META_INTEGRATION_TESTS
#ifndef WRAPPED_SGMATRIX_H__
#define WRAPPED_SGMATRIX_H__
#ifndef SHOGUN_SERIALIZABLE_H__
#define SHOGUN_SERIALIZABLE_H__

#include <shogun/base/SGObject.h>
#include <shogun/lib/SGString.h>
#include <shogun/lib/SGMatrix.h>


namespace shogun
{

/** @brief Simple wrapper class that allows to store any Shogun SGMatrix<T>
* in a CSGObject, and therefore to make it serializable. Using a template
* argument that is not a Shogun parameter will cause a compile error when
* trying to register the passed value as a parameter in the constructors.
template<typename T>
struct extract_value_type
{
typedef T value_type;
};

template<template<typename, typename ...> class X, typename T, typename ...Args>
struct extract_value_type<X<T, Args...>>
{
typedef T value_type;
};

/** @brief A trait that makes a none SGObject SG-serializable
* This only works with classes derived of SGReferencedData (SGVector, SGMatrix etc)
* and fundamental types (std::is_arithmetic)
*/
template<class T> class CWrappedSGMatrix: public CSGObject
template<class T> class CSerializable: public CSGObject
{
public:
/** Default constructor. Do not use. */
CWrappedSGMatrix() : CSGObject()
CSerializable() : CSGObject()
{
set_generic<T>();
register_params();
init();
}

/** Constructor.
* @param value Value to wrap as CSGObject.
* @param value Value to serialize as CSGObject.
* @param value_name Name under which value is registered.
*/
CWrappedSGMatrix(SGMatrix<T> value, const char* value_name="")
CSerializable(T value, const char* value_name="")
{
set_generic<T>();
register_params();
init();
m_value = value;
m_value_name = value_name;
}

/** @return name of the CSGObject, without C prefix */
virtual const char* get_name() const { return "WrappedSGMatrix"; }
virtual const char* get_name() const { return "Serializable"; }

private:
void register_params()
void init()
{
m_value_name = "Unnamed";
m_value = SGMatrix<T>();

SG_ADD(&m_value, "value", "Wrapped value", MS_NOT_AVAILABLE);
set_generic<typename extract_value_type<T>::value_type>();
m_value = 0;
m_value_name = "Unnamed";

SG_ADD(&m_value, "value", "Serialized value", MS_NOT_AVAILABLE);
}

protected:
/** Wrapped value. */
SGMatrix<T> m_value;
/** Serialized value. */
T m_value;

/** Name of wrapped value */
/** Name of serialized value */
const char* m_value_name;
};

template class CWrappedSGMatrix<bool>;
template class CWrappedSGMatrix<char>;
template class CWrappedSGMatrix<int8_t>;
template class CWrappedSGMatrix<uint8_t>;
template class CWrappedSGMatrix<int16_t>;
template class CWrappedSGMatrix<uint16_t>;
template class CWrappedSGMatrix<int32_t>;
template class CWrappedSGMatrix<uint32_t>;
template class CWrappedSGMatrix<int64_t>;
template class CWrappedSGMatrix<uint64_t>;
template class CWrappedSGMatrix<float32_t>;
template class CWrappedSGMatrix<float64_t>;
template class CWrappedSGMatrix<floatmax_t>;

};
#endif // WRAPPED_SGMATRIX_H__
#endif // USE_META_INTEGRATION_TESTS
#endif // SHOGUN_SERIALIZABLE_H_
18 changes: 18 additions & 0 deletions src/shogun/lib/DynamicObjectArray.h
Expand Up @@ -12,11 +12,14 @@
#ifndef _DYNAMIC_OBJECT_ARRAY_H_
#define _DYNAMIC_OBJECT_ARRAY_H_

#include <type_traits>

#include <shogun/lib/config.h>

#include <shogun/base/SGObject.h>
#include <shogun/base/DynArray.h>
#include <shogun/base/Parameter.h>
#include <shogun/io/Serializable.h>

namespace shogun
{
Expand Down Expand Up @@ -277,6 +280,17 @@ class CDynamicObjectArray : public CSGObject
return success;
}

template <typename T, typename T2 = typename std::enable_if<!std::is_base_of<CSGObject, typename std::remove_pointer<T>::type>::value, T>::type>
inline bool append_element(T e, const char* name="")
{
auto serialized_element = new CSerializable<T>(e, name);
bool success = m_array.append_element(serialized_element);
if (success)
SG_REF(serialized_element);

return success;
}

/** append array element to the end of array
*
* @param e element to append
Expand Down Expand Up @@ -397,6 +411,10 @@ class CDynamicObjectArray : public CSGObject
virtual const char* get_name() const
{ return "DynamicObjectArray"; }

// without this definition R interface is missing these inherited functions
using CSGObject::save_serializable;
using CSGObject::load_serializable;

/** Can (optionally) be overridden to pre-initialize some member
* variables which are not PARAMETER::ADD'ed. Make sure that at
* first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_PRE
Expand Down
125 changes: 0 additions & 125 deletions src/shogun/lib/WrappedBasic.h

This file was deleted.

0 comments on commit 1b55e38

Please sign in to comment.