From cb192826861a4963030d443ec5d85d27566c49cb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Oct 2016 16:53:36 +0100 Subject: Allow working with local types (except classes, need ice_id functionality) --- slicer/slicer/common.ice | 1 + slicer/slicer/modelPartsTypes.h | 5 +++++ slicer/slicer/modelPartsTypes.impl.h | 41 ++++++++++++++++++++++++++++++++---- slicer/test/collections.ice | 4 ++-- slicer/test/locals.ice | 16 ++++++++++++++ slicer/tool/parser.cpp | 18 ++++++++++------ slicer/tool/parser.h | 2 +- 7 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 slicer/test/locals.ice diff --git a/slicer/slicer/common.ice b/slicer/slicer/common.ice index 5d87d0a..96d3a85 100644 --- a/slicer/slicer/common.ice +++ b/slicer/slicer/common.ice @@ -12,6 +12,7 @@ module Slicer { string name; }; exception UnsupportedModelType extends RuntimeError { }; + exception LocalTypeException extends RuntimeError { }; exception NoConversionFound extends RuntimeError { string type; }; diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 5297865..93b4bb5 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -4,6 +4,11 @@ #include "modelParts.h" namespace Slicer { + template + struct isLocal { + static constexpr bool value = false; + }; + template class DLL_PUBLIC ModelPartForRoot : public ModelPartForRootBase { public: diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index d04dd9c..b19f937 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -41,8 +41,13 @@ namespace Slicer { return ModelObject && mp->HasValue(); } +#define IfLocal(T) \ + typename std::enable_if::value>::type +#define IfNotLocal(T) \ + typename std::enable_if::value>::type + template - void + IfNotLocal(T) typeWrite(::Ice::OutputStreamPtr & s, const ::IceUtil::Optional & m) { typedef Ice::StreamableTraits traits; @@ -55,14 +60,28 @@ namespace Slicer { } template - void + IfLocal(T) + typeWrite(::Ice::OutputStreamPtr &, const ::IceUtil::Optional &) + { + throw LocalTypeException(); + } + + template + IfNotLocal(T) typeWrite(::Ice::OutputStreamPtr & s, const T & m) { s->write(m); } template - void + IfLocal(T) + typeWrite(::Ice::OutputStreamPtr &, const T &) + { + throw LocalTypeException(); + } + + template + IfNotLocal(T) typeRead(::Ice::InputStreamPtr & s, ::IceUtil::Optional & m) { typedef Ice::StreamableTraits traits; @@ -79,12 +98,26 @@ namespace Slicer { } template - void + IfLocal(T) + typeRead(::Ice::InputStreamPtr &, ::IceUtil::Optional &) + { + throw LocalTypeException(); + } + + template + IfNotLocal(T) typeRead(::Ice::InputStreamPtr & s, T & m) { s->read(m); } + template + IfLocal(T) + typeRead(::Ice::InputStreamPtr &, T &) + { + throw LocalTypeException(); + } + template void ModelPartForRoot::Write(::Ice::OutputStreamPtr & s) const { diff --git a/slicer/test/collections.ice b/slicer/test/collections.ice index 5ff54bd..c9845e0 100644 --- a/slicer/test/collections.ice +++ b/slicer/test/collections.ice @@ -8,9 +8,9 @@ module TestModule { sequence SimpleSeq; sequence BuiltInSeq; sequence Classes; - sequence Structs; + local sequence Structs; dictionary ClassMap; - dictionary StructMap; + local dictionary StructMap; }; #endif diff --git a/slicer/test/locals.ice b/slicer/test/locals.ice new file mode 100644 index 0000000..eb914dd --- /dev/null +++ b/slicer/test/locals.ice @@ -0,0 +1,16 @@ +#ifndef SLICER_TEST_LOCALS +#define SLICER_TEST_LOCALS + +module Locals { + local struct S { + int a; + }; + local sequence Ss; + local dictionary Sd; + local enum E { + a, b + }; +}; + +#endif + diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 23a804b..c2c98d0 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -159,10 +159,14 @@ namespace Slicer { return true; } - void - Slicer::defineRootName(const std::string & type, const std::string & name) const + Slicer::defineRoot(const std::string & type, const std::string & name, const Slice::TypePtr & stype) const { + if (stype->isLocal()) { + fprintbf(cpp, "template<>\n"); + fprintbf(cpp, "struct isLocal< %s > { static constexpr bool value = true; };\n\n", + type); + } fprintbf(cpp, "template<> DLL_PUBLIC\n"); fprintbf(cpp, "const std::string ModelPartForRoot< %s >::rootName(\"%s\");\n\n", type, name); @@ -192,7 +196,7 @@ namespace Slicer { typeId ? *typeId : "slicer-typeid"); auto name = metaDataValue("slicer:root:", c->getMetaData()); - defineRootName(typeToString(decl), name ? *name : c->name()); + defineRoot(typeToString(decl), name ? *name : c->name(), decl); auto typeName = metaDataValue("slicer:typename:", c->getMetaData()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); @@ -242,7 +246,7 @@ namespace Slicer { visitComplexDataMembers(c, c->dataMembers()); auto name = metaDataValue("slicer:root:", c->getMetaData()); - defineRootName(c->scoped(), name ? *name : c->name()); + defineRoot(c->scoped(), name ? *name : c->name(), c); fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForComplex< %s >::metadata ", c->scoped()); @@ -339,7 +343,7 @@ namespace Slicer { e->scoped()); auto name = metaDataValue("slicer:root:", e->getMetaData()); - defineRootName(e->scoped(), name ? *name : e->name()); + defineRoot(e->scoped(), name ? *name : e->name(), e); defineMODELPART(e->scoped(), e, e->getMetaData()); } @@ -381,7 +385,7 @@ namespace Slicer { ename ? *ename : "element"); auto name = metaDataValue("slicer:root:", s->getMetaData()); - defineRootName(s->scoped(), name ? *name : s->name()); + defineRoot(s->scoped(), name ? *name : s->name(), s); fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForSequence< %s >::metadata ", s->scoped()); @@ -435,7 +439,7 @@ namespace Slicer { fprintbf(cpp, "\n"); auto name = metaDataValue("slicer:root:", d->getMetaData()); - defineRootName(d->scoped(), name ? *name : d->name()); + defineRoot(d->scoped(), name ? *name : d->name(), d); fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForDictionary< %s >::metadata ", d->scoped()); diff --git a/slicer/tool/parser.h b/slicer/tool/parser.h index ffc771d..9495607 100644 --- a/slicer/tool/parser.h +++ b/slicer/tool/parser.h @@ -62,7 +62,7 @@ namespace Slicer { void visitComplexDataMembers(Slice::ConstructedPtr t, const Slice::DataMemberList &) const; void defineConversions(Slice::DataMemberPtr dm) const; - void defineRootName(const std::string & type, const std::string & name) const; + void defineRoot(const std::string & type, const std::string & name, const Slice::TypePtr & stype) const; bool hasMetadata(const std::list & metadata) const; void copyMetadata(const std::list & metadata) const; -- cgit v1.2.3