diff options
-rw-r--r-- | slicer/slicer/modelPartsTypes.h | 10 | ||||
-rw-r--r-- | slicer/slicer/modelPartsTypes.impl.h | 46 | ||||
-rw-r--r-- | 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<typename ET, typename MT, typename Conv> + inline static bool tryConvertFrom(const ValueSourcePtr & vsp, MT * model, const Conv & conv); + template<typename ET, typename MT> + inline static bool tryConvertFrom(const ValueSourcePtr & vsp, MT * model); + template<typename ET, typename MT, typename Conv> + inline static bool tryConvertTo(const ValueTargetPtr & vsp, MT * model, const Conv & conv); + template<typename ET, typename MT> + inline static bool tryConvertTo(const ValueTargetPtr & vsp, MT * model); }; template<typename T, typename MT, typename M, MT M::* MV> 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<typename ET, typename MT, typename Conv> + inline + bool ModelPartForConvertedBase::tryConvertFrom(const ValueSourcePtr & vsp, MT * model, const Conv & conv) + { + if (auto vspt = dynamic_cast<TValueSource<ET> *>(vsp.get())) { + ET tmp; + vspt->set(tmp); + *model = conv(tmp); + return true; + } + return false; + } + + template<typename ET, typename MT> + inline + bool ModelPartForConvertedBase::tryConvertFrom(const ValueSourcePtr & vsp, MT * model) + { + if (auto vspt = dynamic_cast<TValueSource<ET> *>(vsp.get())) { + vspt->set(*model); + return true; + } + return false; + } + + template<typename ET, typename MT, typename Conv> + inline + bool ModelPartForConvertedBase::tryConvertTo(const ValueTargetPtr & vsp, MT * model, const Conv & conv) + { + if (auto vspt = dynamic_cast<TValueTarget<ET> *>(vsp.get())) { + vspt->get(conv(*model)); + return true; + } + return false; + } + + template<typename ET, typename MT> + inline + bool ModelPartForConvertedBase::tryConvertTo(const ValueTargetPtr & vsp, MT * model) + { + if (auto vspt = dynamic_cast<TValueTarget<ET> *>(vsp.get())) { + vspt->get(*model); + return true; + } + return false; + } + // ModelPartForOptional template<typename T> ModelPartForOptional<T>::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<TValueSource< %s > *>(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<TValueSource< %s > *>(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<TValueTarget< %s > *>(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<TValueTarget< %s > *>(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", |