Skip to content

Commit

Permalink
fix broken unit tests for close/equals/serialization via re-factoring…
Browse files Browse the repository at this point in the history
… wrapped objects
  • Loading branch information
karlnapf committed Mar 1, 2016
1 parent 62eeb3f commit 8bbf4de
Show file tree
Hide file tree
Showing 7 changed files with 423 additions and 153 deletions.
6 changes: 3 additions & 3 deletions examples/meta/generator/translate.py
Expand Up @@ -55,8 +55,8 @@ def translateVarsStoring(self, programName):
storageFilename = {"Expr": {"StringLiteral": "{}.txt".format(programName)}}
# 'w'
storageFilemode = {"Expr": {"NumberLiteral": "119"}}

storageInit = {"Init": [{"ObjectType": "DynamicObjectArray"},
storageComment = {"Comment": {"StringLiteral": "Serialize output for integration testing (automatically generated)"}}
storageInit = {"Init": [{"ObjectType": "WrappedObjectArray"},
{"Identifier": storage},
{"ArgumentList": []}]}
storageFileInit = {"Init": [{"ObjectType": "SerializableAsciiFile"},
Expand All @@ -75,7 +75,7 @@ def translateVarsStoring(self, programName):
varnameIdentifierExpr = {"Expr": {"Identifier": varname}}

methodCall = {"MethodCall": [{"Identifier": storage},
{"Identifier": "append_element_wrapped"},
{"Identifier": "append_wrapped"},
[varnameIdentifierExpr, varnameExpr]]}
expression = {"Expr": methodCall}
result += self.translateStatement(expression)
Expand Down
14 changes: 0 additions & 14 deletions src/shogun/lib/DynamicObjectArray.h
Expand Up @@ -17,7 +17,6 @@
#include <shogun/base/SGObject.h>
#include <shogun/base/DynArray.h>
#include <shogun/base/Parameter.h>
#include <shogun/lib/SGObjectWrapper.h>

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

/** Works as ::append_element, but accepts non CSGObject input types,
* which are wrapped through CSGObjectWrapper.
*
* @param data Data element to append, after being wrapped.
* @param data_name Name of wrapped data element.
*/
template<class T> bool append_element_wrapped(T data,
const char* data_name="")
{
return this->append_element(new CSGObjectWrapper<T>(data,
data_name));
}

/** append array element to the end of array
*
* @param e element to append
Expand Down
136 changes: 0 additions & 136 deletions src/shogun/lib/SGObjectWrapper.h

This file was deleted.

108 changes: 108 additions & 0 deletions src/shogun/lib/WrappedBasic.h
@@ -0,0 +1,108 @@
/*
* -*- coding: utf-8 -*-
* vim: set fileencoding=utf-8
*
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Heiko Strathmann
*/
#ifndef WRAPPED_BASIC_H__
#define WRAPPED_BASIC_H__

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


namespace shogun
{

/** @brief Simple wrapper class that allows to store any Shogun basic parameter
* (i.e. float64_t, int64_t, char, etc) 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<class T> class CWrappedBasic: public CSGObject
{
public:
/** Default constructor. Do not use. */
CWrappedBasic() : CSGObject()
{
m_value = 0;
m_value_name = "Unnamed";
set_generic<T>();
register_params();
}

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

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

private:
void register_params()
{
SG_ADD(&m_value, m_value_name, "Wrapped value", MS_NOT_AVAILABLE);
}

protected:
/** Wrapped value. */
T m_value;

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

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

};
#endif // WRAPPED_BASIC_H__
98 changes: 98 additions & 0 deletions src/shogun/lib/WrappedObjectArray.h
@@ -0,0 +1,98 @@
/*
* -*- coding: utf-8 -*-
* vim: set fileencoding=utf-8
*
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Heiko Strathmann
*/
#ifndef WRAPPED_OBJECT_ARRAY_H__
#define WRAPPED_OBJECT_ARRAY_H__

#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/lib/WrappedBasic.h>
#include <shogun/lib/WrappedSGVector.h>
#include <shogun/lib/WrappedSGMatrix.h>


namespace shogun
{

/** @brief Specialization of CDynamicObjectArray that adds methods to append
* wrapped elements to make them serializable. Objects are wrapped through the
* classes CWrappedBasic, CWrappedSGVector, CWrappedSGMatrix.
*/
class CWrappedObjectArray : public CDynamicObjectArray
{
public:
CWrappedObjectArray(): CDynamicObjectArray() { }

/** Works as ::append_element, but accepts basic Shogun types,
* which are wrapped through CWrappedBasicObject.
*
* @param data Data element to append, after being wrapped.
* @param data_name Name of wrapped data element.
*/
template<class T> bool append_wrapped(T data, const char* data_name="")
{
return this->append_element(new CWrappedBasic<T>(data, data_name));
}

/** Works as ::append_element, but accepts SGVector,
* which are wrapped through CWrappedSGVectorObject.
*
* @param data Data element to append, after being wrapped.
* @param data_name Name of wrapped data element.
*/
template<class T> bool append_wrapped(SGVector<T> data, const char* data_name="")
{
return this->append_element(new CWrappedSGVector<T>(data,
data_name));
}

/** Works as ::append_element, but accepts SGMatrix,
* which are wrapped through CWrappedSGVectorObject.
*
* @param data Data element to append, after being wrapped.
* @param data_name Name of wrapped data element.
*/
template<class T> bool append_wrapped(SGMatrix<T> data, const char* data_name="")
{
return this->append_element(new CWrappedSGMatrix<T>(data,
data_name));
}

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


};
}
#endif // WRAPPED_OBJECT_ARRAY_H__

0 comments on commit 8bbf4de

Please sign in to comment.