From 3cc1221f7643e749c6076046727a33096171a1ac Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 7 Jul 2020 11:14:38 +0100 Subject: Apply previous fixes to enums too Includes change to method of finding in-use types. Allows removal of dirty clang link fix. --- slicer/slicer/modelPartsTypes.impl.h | 8 +---- slicer/tool/parser.cpp | 64 +++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index ca562a6..b821d06 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -13,12 +13,6 @@ #include #include -#ifdef __clang__ -# define FINALVISMODELPARTS DLL_PUBLIC -#else -# define FINALVISMODELPARTS -#endif - #define CUSTOMMODELPARTFOR(Type, BaseModelPart, ModelPartType) \ template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor() \ { \ @@ -56,7 +50,7 @@ { \ return CreateRootFor(const_cast &>(s)); \ } \ - template class FINALVISMODELPARTS BaseModelPart; \ + template class BaseModelPart; \ template class ModelPartForRoot; \ template class ModelPartForRoot>; diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 3d0505a..33f003b 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -14,16 +14,59 @@ namespace fs = std::filesystem; namespace Slicer { + class Count : public Slice::ParserVisitor { + public: + bool + visitClassDefStart(const Slice::ClassDefPtr &) override + { + classes += 1; + return true; + } + bool + visitStructStart(const Slice::StructPtr &) override + { + structs += 1; + return true; + } + void + visitEnum(const Slice::EnumPtr &) override + { + enums += 1; + } + + unsigned int classes {0}; + unsigned int structs {0}; + unsigned int enums {0}; + }; + class ForwardDeclare : public Slice::ParserVisitor { public: - ForwardDeclare(FILE * c) : cpp(c) { } + ForwardDeclare(FILE * c, const Count & cnt) : cpp(c), count(cnt) { } + bool - visitModuleStart(const Slice::ModulePtr & m) override + visitUnitStart(const Slice::UnitPtr &) override { - fprintbf(cpp, "namespace %s {\n", m->name()); + if (count.enums) { + fprintbf(cpp, "#ifdef ICE_CPP11_MAPPING // C++11 mapping\n"); + fprintbf( + cpp, "#define FORWARD_ENUM(name) enum class ICE_CLASS(JAM_DLL_PUBLIC) name : unsigned char;\n"); + fprintbf(cpp, "#else // C++98 mapping\n"); + fprintbf(cpp, "#define FORWARD_ENUM(name) enum ICE_CLASS(JAM_DLL_PUBLIC) name;\n"); + fprintbf(cpp, "#endif\n\n"); + } return true; } + bool + visitModuleStart(const Slice::ModulePtr & m) override + { + if (count.classes || count.structs || count.enums) { + fprintbf(cpp, "namespace %s {\n", m->name()); + return true; + } + return false; + } + bool visitClassDefStart(const Slice::ClassDefPtr & c) override { @@ -38,13 +81,22 @@ namespace Slicer { return false; }; + void + visitEnum(const Slice::EnumPtr & e) override + { + fprintbf(cpp, "FORWARD_ENUM(%s);\n", e->name()); + }; + void visitModuleEnd(const Slice::ModulePtr & m) override { - fprintbf(cpp, "} // %s\n\n", m->name()); + if (count.classes || count.structs || count.enums) { + fprintbf(cpp, "} // %s\n\n", m->name()); + } } FILE * cpp; + const Count & count; }; template @@ -136,7 +188,9 @@ namespace Slicer { fprintbf(cpp, "// Begin Slicer code\n\n"); fprintbf(cpp, "#include <%s>\n\n", (headerPrefix / "modelPartsTypes.impl.h").string()); - ForwardDeclare fd {cpp}; + Count count; + u->visit(&count, true); + ForwardDeclare fd {cpp, count}; u->visit(&fd, true); fprintbf(cpp, "#include <%s>\n", fs::path(topLevelFile.filename()).replace_extension(".h").string()); -- cgit v1.2.3