From 4639ba674ec1da190ff467b3568a0412fade3974 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 22 Jul 2017 14:31:03 +0100 Subject: Move some generated conversion code into impl.h --- slicer/slicer/modelPartsTypes.h | 10 ++++++++ slicer/slicer/modelPartsTypes.impl.h | 46 ++++++++++++++++++++++++++++++++++++ slicer/tool/parser.cpp | 27 +++++---------------- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 8082e07..2038350 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -54,6 +54,16 @@ namespace Slicer { virtual bool HasValue() const override; virtual ModelPartType GetType() const override; static const ModelPartType type; + + protected: + template + inline static bool tryConvertFrom(const ValueSourcePtr & vsp, MT * model, const Conv & conv); + template + inline static bool tryConvertFrom(const ValueSourcePtr & vsp, MT * model); + template + inline static bool tryConvertTo(const ValueTargetPtr & vsp, MT * model, const Conv & conv); + template + inline static bool tryConvertTo(const ValueTargetPtr & vsp, MT * model); }; template diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index ef69309..2121d5d 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -159,6 +159,52 @@ namespace Slicer { { } + template + inline + bool ModelPartForConvertedBase::tryConvertFrom(const ValueSourcePtr & vsp, MT * model, const Conv & conv) + { + if (auto vspt = dynamic_cast *>(vsp.get())) { + ET tmp; + vspt->set(tmp); + *model = conv(tmp); + return true; + } + return false; + } + + template + inline + bool ModelPartForConvertedBase::tryConvertFrom(const ValueSourcePtr & vsp, MT * model) + { + if (auto vspt = dynamic_cast *>(vsp.get())) { + vspt->set(*model); + return true; + } + return false; + } + + template + inline + bool ModelPartForConvertedBase::tryConvertTo(const ValueTargetPtr & vsp, MT * model, const Conv & conv) + { + if (auto vspt = dynamic_cast *>(vsp.get())) { + vspt->get(conv(*model)); + return true; + } + return false; + } + + template + inline + bool ModelPartForConvertedBase::tryConvertTo(const ValueTargetPtr & vsp, MT * model) + { + if (auto vspt = dynamic_cast *>(vsp.get())) { + vspt->get(*model); + return true; + } + return false; + } + // ModelPartForOptional template ModelPartForOptional::ModelPartForOptional(IceUtil::Optional< typename T::element_type > * h) : diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index d9a7a2b..8e37dfc 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -56,23 +56,14 @@ namespace Slicer { fprintbf(cpp, "\tBOOST_ASSERT(Model);\n"); for (const auto & conversion : conversions) { - fprintbf(cpp, "\tif (auto vspt = dynamic_cast *>(vsp.get())) {\n", - conversion.ExchangeType); - fprintbf(cpp, "\t\t%s tmp;\n", - conversion.ExchangeType); - fprintbf(cpp, "\t\tvspt->set(tmp);\n"); - fprintbf(cpp, "\t\t*Model = %s(tmp);\n", + fprintbf(cpp, "\tif (tryConvertFrom< %s >(vsp, Model, %s)) return;\n", + conversion.ExchangeType, conversion.ConvertToModelFunc); - fprintbf(cpp, "\t\treturn;\n"); - fprintbf(cpp, "\t}\n"); } // Default conversion if (!dm->hasMetaData("slicer:nodefaultconversion")) { - fprintbf(cpp, "\tif (auto vspt = dynamic_cast *>(vsp.get())) {\n", + fprintbf(cpp, "\tif (tryConvertFrom< %s >(vsp, Model)) return;\n", Slice::typeToString(type)); - fprintbf(cpp, "\t\tvspt->set(*Model);\n"); - fprintbf(cpp, "\t\treturn;\n"); - fprintbf(cpp, "\t}\n"); } // Failed to convert fprintbf(cpp, "\tthrow NoConversionFound(\"%s\");\n", @@ -85,20 +76,14 @@ namespace Slicer { fprintbf(cpp, "\tBOOST_ASSERT(Model);\n"); for (const auto & conversion : conversions) { - fprintbf(cpp, "\tif (auto vtpt = dynamic_cast *>(vtp.get())) {\n", - conversion.ExchangeType); - fprintbf(cpp, "\t\tvtpt->get(%s(*Model));\n", + fprintbf(cpp, "\tif (tryConvertTo< %s >(vtp, Model, %s)) return;\n", + conversion.ExchangeType, conversion.ConvertToExchangeFunc); - fprintbf(cpp, "\t\treturn;\n"); - fprintbf(cpp, "\t}\n"); } // Default conversion if (!dm->hasMetaData("slicer:nodefaultconversion")) { - fprintbf(cpp, "\tif (auto vtpt = dynamic_cast *>(vtp.get())) {\n", + fprintbf(cpp, "\tif (tryConvertTo< %s >(vtp, Model)) return;\n", Slice::typeToString(type)); - fprintbf(cpp, "\t\tvtpt->get(*Model);\n"); - fprintbf(cpp, "\t\treturn;\n"); - fprintbf(cpp, "\t}\n"); } // Failed to convert fprintbf(cpp, "\tthrow NoConversionFound(\"%s\");\n", -- cgit v1.2.3