summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-10-03 14:23:21 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2016-10-03 14:23:21 +0100
commit3dc9f9d66ee412d2b3476a9b9d50fa04acb0499d (patch)
tree76946b080f75ad5f7e7d7d8ce5a81a63a1404bde
parentApply custom model parts at the root level (diff)
downloadslicer-3dc9f9d66ee412d2b3476a9b9d50fa04acb0499d.tar.bz2
slicer-3dc9f9d66ee412d2b3476a9b9d50fa04acb0499d.tar.xz
slicer-3dc9f9d66ee412d2b3476a9b9d50fa04acb0499d.zip
Adds support for reading/writing in ICE native format
-rw-r--r--slicer/Jamfile.jam3
-rw-r--r--slicer/ice/Jamfile.jam47
-rw-r--r--slicer/ice/serializer.cpp72
-rw-r--r--slicer/ice/serializer.h65
-rw-r--r--slicer/ice/testSpecifics.cpp118
-rw-r--r--slicer/slicer/modelParts.h3
-rw-r--r--slicer/slicer/modelPartsTypes.h2
-rw-r--r--slicer/slicer/modelPartsTypes.impl.h60
8 files changed, 369 insertions, 1 deletions
diff --git a/slicer/Jamfile.jam b/slicer/Jamfile.jam
index 3d5a62b..3108454 100644
--- a/slicer/Jamfile.jam
+++ b/slicer/Jamfile.jam
@@ -5,13 +5,14 @@ build-project slicer ;
build-project xml ;
build-project json ;
build-project db ;
+build-project ice ;
build-project test ;
explicit install ;
explicit install-xml ;
explicit install-json ;
explicit install-db ;
-alias install : slicer//install tool//install ;
+alias install : slicer//install tool//install ice//install ;
alias install-xml : xml//install ;
alias install-json : json//install ;
alias install-db : db//install ;
diff --git a/slicer/ice/Jamfile.jam b/slicer/ice/Jamfile.jam
new file mode 100644
index 0000000..d4c6685
--- /dev/null
+++ b/slicer/ice/Jamfile.jam
@@ -0,0 +1,47 @@
+import testing ;
+import package ;
+
+lib pthread ;
+lib Ice ;
+lib boost_system ;
+lib boost_filesystem ;
+lib boost_utf : : <name>boost_unit_test_framework ;
+lib IceUtil ;
+lib adhocutil : : : : <include>/usr/include/adhocutil ;
+
+lib slicer-ice :
+ [ glob *.cpp : test*.cpp ]
+ :
+ <include>..
+ <library>boost_system
+ <library>boost_filesystem
+ <library>pthread
+ <library>Ice
+ <library>IceUtil
+ <library>adhocutil
+ <library>../slicer//slicer
+ <implicit-dependency>../slicer//slicer
+ : :
+ ;
+
+run testSpecifics.cpp
+ : : :
+ <define>BOOST_TEST_DYN_LINK
+ <library>slicer-ice
+ <library>boost_utf
+ <library>../test//types
+ <library>../test//common
+ <library>../slicer//slicer
+ <include>..
+ <dependency>../test//compilation
+ :
+ testSpecifics
+ ;
+
+alias install : install-lib install-slice ;
+explicit install ;
+explicit install-lib ;
+explicit install-slice ;
+package.install install-lib : <install-header-subdir>slicer/ice : : slicer-ice : [ glob-tree *.h ] ;
+package.install-data install-slice : slicer/ice : [ glob *.ice ] ;
+
diff --git a/slicer/ice/serializer.cpp b/slicer/ice/serializer.cpp
new file mode 100644
index 0000000..981223f
--- /dev/null
+++ b/slicer/ice/serializer.cpp
@@ -0,0 +1,72 @@
+#include "serializer.h"
+#include "Ice/Initialize.h"
+#include "Ice/Stream.h"
+#include "Ice/Communicator.h"
+
+NAMEDFACTORY("application/ice", Slicer::IceStreamSerializer, Slicer::StreamSerializerFactory);
+NAMEDFACTORY("application/ice", Slicer::IceStreamDeserializer, Slicer::StreamDeserializerFactory);
+
+namespace Slicer {
+ Ice::StringSeq empty;
+
+ IceBase::IceBase() :
+ ic(Ice::initialize(empty))
+ {
+ }
+
+ IceBase::~IceBase()
+ {
+ ic->destroy();
+ }
+ IceBlobSerializer::IceBlobSerializer(Ice::ByteSeq & b) :
+ blob(b)
+ {
+ }
+
+ void
+ IceBlobSerializer::Serialize(ModelPartForRootPtr mp)
+ {
+ auto s = Ice::createOutputStream(ic);
+ mp->Write(s);
+ s->finished(blob);
+ }
+
+ IceStreamSerializer::IceStreamSerializer(std::ostream & os) :
+ IceBlobSerializer(blob),
+ strm(os)
+ {
+ }
+
+ void
+ IceStreamSerializer::Serialize(ModelPartForRootPtr mp)
+ {
+ IceBlobSerializer::Serialize(mp);
+ strm.write((const char *)&blob.front(), blob.size());
+ }
+
+ IceBlobDeserializer::IceBlobDeserializer(const Ice::ByteSeq & b) :
+ blob(b)
+ {
+ }
+
+ void
+ IceBlobDeserializer::Deserialize(ModelPartForRootPtr mp)
+ {
+ auto s = Ice::createInputStream(ic, blob);
+ mp->Read(s);
+ }
+
+ IceStreamDeserializer::IceStreamDeserializer(std::istream & is) :
+ IceBlobDeserializer(blob),
+ strm(is)
+ {
+ }
+
+ void
+ IceStreamDeserializer::Deserialize(ModelPartForRootPtr mp)
+ {
+ blob.assign(std::istreambuf_iterator<char>(strm), std::istreambuf_iterator<char>());
+ IceBlobDeserializer::Deserialize(mp);
+ }
+}
+
diff --git a/slicer/ice/serializer.h b/slicer/ice/serializer.h
new file mode 100644
index 0000000..d534b3e
--- /dev/null
+++ b/slicer/ice/serializer.h
@@ -0,0 +1,65 @@
+#ifndef SLICER_ICE_H
+#define SLICER_ICE_H
+
+#include <slicer/serializer.h>
+#include <visibility.h>
+#include <Ice/CommunicatorF.h>
+#include <Ice/BuiltinSequences.h>
+
+namespace Slicer {
+ class IceBase {
+ public:
+ virtual ~IceBase();
+
+ protected:
+ IceBase();
+
+ Ice::CommunicatorPtr ic;
+ };
+
+ class DLL_PUBLIC IceBlobSerializer : public Serializer, protected IceBase {
+ public:
+ IceBlobSerializer(Ice::ByteSeq &);
+
+ virtual void Serialize(ModelPartForRootPtr) override;
+
+ private:
+ Ice::ByteSeq & blob;
+ };
+
+ class DLL_PUBLIC IceStreamSerializer : public IceBlobSerializer {
+ public:
+ IceStreamSerializer(std::ostream &);
+
+ virtual void Serialize(ModelPartForRootPtr) override;
+
+ protected:
+ std::ostream & strm;
+ Ice::ByteSeq blob;
+ };
+
+ class DLL_PUBLIC IceBlobDeserializer : public Deserializer, protected IceBase {
+ public:
+ IceBlobDeserializer(const Ice::ByteSeq &);
+
+ virtual void Deserialize(ModelPartForRootPtr) override;
+
+ protected:
+ const Ice::ByteSeq & blob;
+ };
+
+ class DLL_PUBLIC IceStreamDeserializer : public IceBlobDeserializer {
+ public:
+ IceStreamDeserializer(std::istream &);
+
+ virtual void Deserialize(ModelPartForRootPtr) override;
+
+ protected:
+ std::istream & strm;
+ Ice::ByteSeq blob;
+ };
+}
+
+#endif
+
+
diff --git a/slicer/ice/testSpecifics.cpp b/slicer/ice/testSpecifics.cpp
new file mode 100644
index 0000000..8a625cd
--- /dev/null
+++ b/slicer/ice/testSpecifics.cpp
@@ -0,0 +1,118 @@
+#define BOOST_TEST_MODULE ice_specifics
+#include <boost/test/unit_test.hpp>
+#include <slicer.h>
+#include "serializer.h"
+#include <types.h>
+
+// LCOV_EXCL_START
+BOOST_TEST_DONT_PRINT_LOG_VALUE(TestModule::IsoDate);
+// LCOV_EXCL_END
+
+template<typename X>
+void
+testCompare(const X & x)
+{
+ BOOST_TEST_CHECKPOINT(typeid(X).name());
+ std::stringstream strm;
+ Slicer::SerializeAny<Slicer::IceStreamSerializer>(x, strm);
+ auto x2 = Slicer::DeserializeAny<Slicer::IceStreamDeserializer, X>(strm);
+ BOOST_REQUIRE_EQUAL(x, x2);
+}
+
+template<typename X>
+void
+testCompare(const X & x, const boost::function<bool(const X &, const X &)> & cmp)
+{
+ BOOST_TEST_CHECKPOINT(typeid(X).name());
+ std::stringstream strm;
+ Slicer::SerializeAny<Slicer::IceStreamSerializer>(x, strm);
+ auto x2 = Slicer::DeserializeAny<Slicer::IceStreamDeserializer, X>(strm);
+ BOOST_REQUIRE(cmp(x, x2));
+}
+
+BOOST_AUTO_TEST_CASE( builtins )
+{
+ testCompare<std::string>("some string value");
+ testCompare<bool>(true);
+ testCompare<bool>(false);
+ testCompare<Ice::Byte>(10);
+ testCompare<Ice::Byte>(0);
+ testCompare<Ice::Short>(-5);
+ testCompare<Ice::Short>(534);
+ testCompare<Ice::Int>(-433);
+ testCompare<Ice::Int>(3434090);
+ testCompare<Ice::Long>(-4000033);
+ testCompare<Ice::Long>(343409898900);
+ testCompare<Ice::Float>(-3.14);
+ testCompare<Ice::Float>(3.14);
+ testCompare<Ice::Double>(-3.14159);
+ testCompare<Ice::Double>(3.14159);
+}
+
+template<typename X>
+void
+testCompareOptional(const X & d)
+{
+ BOOST_TEST_CHECKPOINT(typeid(X).name());
+ std::stringstream strm;
+ IceUtil::Optional<X> x;
+ Slicer::SerializeAny<Slicer::IceStreamSerializer>(x, strm);
+ auto x2 = Slicer::DeserializeAny<Slicer::IceStreamDeserializer, IceUtil::Optional<X>>(strm);
+ BOOST_REQUIRE(!x2);
+ x = d;
+ Slicer::SerializeAny<Slicer::IceStreamSerializer>(x, strm);
+ auto x3 = Slicer::DeserializeAny<Slicer::IceStreamDeserializer, IceUtil::Optional<X>>(strm);
+ BOOST_REQUIRE_EQUAL(d, *x3);
+}
+
+BOOST_AUTO_TEST_CASE( optionalBuiltins )
+{
+ testCompareOptional<std::string>("some string value");
+ testCompareOptional<bool>(true);
+ testCompareOptional<bool>(false);
+ testCompareOptional<Ice::Byte>(10);
+ testCompareOptional<Ice::Byte>(0);
+ testCompareOptional<Ice::Short>(-5);
+ testCompareOptional<Ice::Short>(534);
+ testCompareOptional<Ice::Int>(-433);
+ testCompareOptional<Ice::Int>(3434090);
+ testCompareOptional<Ice::Long>(-4000033);
+ testCompareOptional<Ice::Long>(343409898900);
+ testCompareOptional<Ice::Float>(-3.14);
+ testCompareOptional<Ice::Float>(3.14);
+ testCompareOptional<Ice::Double>(-3.14159);
+ testCompareOptional<Ice::Double>(3.14159);
+}
+
+
+
+BOOST_AUTO_TEST_CASE( classes )
+{
+ TestModule::BuiltInsPtr x = new TestModule::BuiltIns();
+ x->mbool = true;
+ x->mbyte = 14;
+ x->mshort = 31434;
+ x->mint = 324324234;
+ x->mlong = 343242342343243;
+ x->mfloat = 3434.32432;
+ x->mdouble = 3423423423.42342342343;
+ x->mstring = "sdfsf432423";
+ testCompare<TestModule::BuiltInsPtr>(x, [](const auto & a, const auto & b) {
+ return a->mbool == b->mbool
+ && a->mbyte == b->mbyte
+ && a->mshort == b->mshort
+ && a->mint == b->mint
+ && a->mlong == b->mlong
+ && a->mfloat == b->mfloat
+ && a->mdouble == b->mdouble
+ && a->mstring == b->mstring;
+ });
+}
+
+BOOST_AUTO_TEST_CASE( structes )
+{
+ TestModule::IsoDate date({ 2016, 10, 3 });
+ testCompare(date);
+ testCompareOptional(date);
+}
+
diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h
index 8cf3bc5..59f12c7 100644
--- a/slicer/slicer/modelParts.h
+++ b/slicer/slicer/modelParts.h
@@ -6,6 +6,7 @@
#include <IceUtil/Handle.h>
#include <IceUtil/Optional.h>
#include <Ice/Handle.h>
+#include <Ice/StreamF.h>
#include <Slice/Parser.h>
#include <stdexcept>
#include <boost/function.hpp>
@@ -189,6 +190,8 @@ namespace Slicer {
virtual void OnEachChild(const ChildHandler & ch) override;
virtual ModelPartType GetType() const override;
virtual bool IsOptional() const override;
+ virtual void Write(::Ice::OutputStreamPtr &) const = 0;
+ virtual void Read(::Ice::InputStreamPtr &) = 0;
ModelPartPtr mp;
};
diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h
index 88ee0bd..5dae7bf 100644
--- a/slicer/slicer/modelPartsTypes.h
+++ b/slicer/slicer/modelPartsTypes.h
@@ -11,6 +11,8 @@ namespace Slicer {
const std::string & GetRootName() const override;
virtual bool HasValue() const override;
+ void Write(::Ice::OutputStreamPtr &) const override;
+ void Read(::Ice::InputStreamPtr &) override;
static const std::string rootName;
diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h
index 46c028b..5788c3f 100644
--- a/slicer/slicer/modelPartsTypes.impl.h
+++ b/slicer/slicer/modelPartsTypes.impl.h
@@ -3,6 +3,10 @@
#include "modelPartsTypes.h"
#include "common.h"
+#include <Ice/Stream.h>
+#include <Ice/StreamHelpers.h>
+#include <Ice/BasicStream.h>
+#include <IceUtil/Optional.h>
#define CUSTOMMODELPARTFOR(Type, BaseModelPart, ModelPartType) \
template class BaseModelPart; \
@@ -35,6 +39,62 @@ namespace Slicer {
return ModelObject && mp->HasValue();
}
+ template<typename T>
+ void
+ typeWrite(::Ice::OutputStreamPtr & s, const ::IceUtil::Optional<T> & m)
+ {
+ typedef Ice::StreamableTraits<T> traits;
+ s->startEncapsulation();
+ if (m && s->writeOptional(0,
+ Ice::StreamOptionalHelper<T, traits::helper, traits::fixedLength>::optionalFormat)) {
+ Ice::StreamOptionalHelper<T, traits::helper, traits::fixedLength>::write(s.get(), *m);
+ }
+ s->endEncapsulation();
+ }
+
+ template<typename T>
+ void
+ typeWrite(::Ice::OutputStreamPtr & s, const T & m)
+ {
+ s->write(m);
+ }
+
+ template<typename T>
+ void
+ typeRead(::Ice::InputStreamPtr & s, ::IceUtil::Optional<T> & m)
+ {
+ typedef Ice::StreamableTraits<T> traits;
+ s->startEncapsulation();
+ if (s->readOptional(0,
+ Ice::StreamOptionalHelper<T, traits::helper, traits::fixedLength>::optionalFormat)) {
+ m.__setIsSet();
+ Ice::StreamOptionalHelper<T, traits::helper, traits::fixedLength>::read(s.get(), *m);
+ }
+ else {
+ m = IceUtil::None;
+ }
+ s->endEncapsulation();
+ }
+
+ template<typename T>
+ void
+ typeRead(::Ice::InputStreamPtr & s, T & m)
+ {
+ s->read(m);
+ }
+
+ template<typename T>
+ void ModelPartForRoot<T>::Write(::Ice::OutputStreamPtr & s) const
+ {
+ typeWrite(s, *ModelObject);
+ }
+
+ template<typename T>
+ void ModelPartForRoot<T>::Read(::Ice::InputStreamPtr & s)
+ {
+ typeRead(s, *ModelObject);
+ }
+
// ModelPartForSimple
template<typename T>
ModelPartForSimple<T>::ModelPartForSimple(T & h) :