Skip to content

Commit

Permalink
WIP: add any class to mrpt
Browse files Browse the repository at this point in the history
  • Loading branch information
jolting committed Oct 11, 2016
1 parent 0dbaa3c commit 653021a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
4 changes: 2 additions & 2 deletions libs/base/include/mrpt/math/matrix_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ namespace mrpt
/** Read operator from a CStream. The format is compatible with that of CMatrix & CMatrixD */
template <size_t NROWS,size_t NCOLS>
mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in, CMatrixFixedNumeric<float,NROWS,NCOLS> & M) {
CMatrix &aux = boost::get<CMatrix>(in.ReadObject());
CMatrix &aux = mrpt::utils::any_cast<CMatrix>(in.ReadObject());
ASSERTMSG_(M.cols()==aux.cols() && M.rows()==aux.rows(), format("Size mismatch: deserialized is %ux%u, expected is %ux%u",(unsigned)aux.getRowCount(),(unsigned)aux.getColCount(),(unsigned)NROWS,(unsigned)NCOLS))
M = aux;
return in;
}
/** Read operator from a CStream. The format is compatible with that of CMatrix & CMatrixD */
template <size_t NROWS,size_t NCOLS>
mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in, CMatrixFixedNumeric<double,NROWS,NCOLS> & M) {
const CMatrixD &aux = boost::get<CMatrixD>(in.ReadObject());
const CMatrixD &aux = mrpt::utils::any_cast<CMatrixD>(in.ReadObject());
ASSERTMSG_(M.cols()==aux.cols() && M.rows()==aux.rows(), format("Size mismatch: deserialized is %ux%u, expected is %ux%u",(unsigned)aux.getRowCount(),(unsigned)aux.getColCount(),(unsigned)NROWS,(unsigned)NCOLS))
M = aux;
return in;
Expand Down
1 change: 1 addition & 0 deletions libs/base/include/mrpt/poses/CPosePDFGaussian.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace mrpt
{
namespace poses
{
class CPoint2DPDFGaussian;
/** Declares a class that represents a Probability Density function (PDF) of a 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
*
* This class implements that PDF using a mono-modal Gaussian distribution. See mrpt::poses::CPosePDF for more details.
Expand Down
83 changes: 65 additions & 18 deletions libs/base/include/mrpt/utils/CSerializable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,79 @@
#ifndef CSERIALIZABLE_H
#define CSERIALIZABLE_H

#include <mrpt/utils/CObject.h>
#include <mrpt/utils/TTypeName.h>
#include <mrpt/utils/types_simple.h>
#include <boost/variant.hpp>
#include <mrpt/math/CMatrix.h>
#include <mrpt/poses/CPoint2DPDFGaussian.h>
#include <mrpt/poses/CPoint2D.h>
#include <mrpt/poses/CPoses2DSequence.h>

#if MRPT_HAS_MATLAB
typedef struct mxArray_tag mxArray; //!< Forward declaration for mxArray (avoid #including as much as possible to speed up compiling)
#endif
#include <typeinfo>
#include <memory>

namespace mrpt
{
namespace utils
{
typedef boost::variant<
mrpt::math::CMatrix,
mrpt::poses::CPoint2DPDFGaussian,
mrpt::poses::CPoint2D,
mrpt::math::TPolygon2D,
mrpt::math::TPolygon3D,
mrpt::poses::CPoses2DSequence,
mrpt::poses::CPose2D
> CSerializableOption;
class Any
{
class StorageBase
{
public:
virtual ~StorageBase(){}
};

template<class T>
class Storage : public StorageBase
{
public:
Storage(const T& t) :t(t) {}
Storage(T&& t) :t(t) {}
~Storage() override{}
T t;
};

public:
Any() : valid(false), m_typeInfo(&typeid(nullptr)){}
~Any(){}
template<class V>
Any(V&& v)
{
valid = true;
m_ptr.reset(new Storage<V>(v));
m_typeInfo = &typeid(V);
}
template<class V>
Any(const V& v)
{
valid = true;
m_ptr.reset(new Storage<V>(v));
m_typeInfo = &typeid(V);
}

const std::type_info& type() const
{
return *m_typeInfo;
}

template<class T>
T& get_unsafe()
{
return dynamic_cast<Storage<T>*>(m_ptr.get())->t;
}
template<class T>
const T& get_unsafe() const
{
return dynamic_cast<Storage<T>*>(m_ptr.get())->t;
}

private:
bool valid;
std::unique_ptr<StorageBase> m_ptr;
const std::type_info *m_typeInfo;
};
template<class V>V any_cast(const Any& operand)
{
//if(!valid || operand.type() != typeid(V))
// throw
return operand.get_unsafe<V>();
}
}
} // End of namespace

Expand Down
6 changes: 3 additions & 3 deletions libs/base/include/mrpt/utils/CStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ namespace mrpt

/** Writes an object to the stream.
*/
void WriteObject( const CSerializableOption &o );
void WriteObject( const Any &o );

/** Reads an object from stream, its class determined at runtime, and returns a smart pointer to the object.
* \exception std::exception On I/O error or undefined class.
* \exception mrpt::utils::CExceptionEOF On an End-Of-File condition found at a correct place: an EOF that abruptly finishes in the middle of one object raises a plain std::exception instead.
*/
CSerializableOption ReadObject();
Any ReadObject();

/** Write an object to a stream in the binary MRPT format. */
template <class T>
Expand All @@ -175,7 +175,7 @@ namespace mrpt
template <class T>
CStream& operator >> (T &obj)
{
obj = boost::get<T>(ReadObject());
obj = any_cast<T>(ReadObject());
return *this;
}

Expand Down

0 comments on commit 653021a

Please sign in to comment.