From 1ac16c9fbde1650dcee3729638d973359af2ab38 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 1 Nov 2020 17:14:45 +0000 Subject: Improve conversion helpers constness, constexpr, noexcept, local vars --- slicer/slicer/modelPartsTraits.h | 103 ++++++++++++++++++++++++++++++ slicer/slicer/modelPartsTypes.h | 8 +-- slicer/slicer/modelPartsTypes.impl.h | 118 +++++------------------------------ 3 files changed, 124 insertions(+), 105 deletions(-) create mode 100644 slicer/slicer/modelPartsTraits.h diff --git a/slicer/slicer/modelPartsTraits.h b/slicer/slicer/modelPartsTraits.h new file mode 100644 index 0000000..6213c9c --- /dev/null +++ b/slicer/slicer/modelPartsTraits.h @@ -0,0 +1,103 @@ +#ifndef SLICER_MODELPARTSTRAITS_H +#define SLICER_MODELPARTSTRAITS_H + +#include +#include + +namespace Slicer { + // Function traits helpers + template struct function_traits; + + template struct function_traits> { + template struct arg { + using type = std::tuple_element_t>; + }; + }; + + template using callable_traits = function_traits>>; + + template using callable_param = typename callable_traits::template arg::type; + + // Converters that remove "optionalness". + template struct Coerce { + using T = std::decay_t; + + [[nodiscard]] inline constexpr T & + operator()(T & x) const noexcept + { + return x; + } + + [[nodiscard]] inline constexpr const T & + operator()(const T & x) const noexcept + { + return x; + } + + template + [[nodiscard]] inline constexpr T & + operator()(Ice::optional & x) const noexcept + { + if (!x) { + x = Y(); + } + return *x; + } + + template + [[nodiscard]] inline constexpr const T & + operator()(const Ice::optional & x) const + { + return *x; + } + + [[nodiscard]] inline constexpr static bool + valueExists(const T &) noexcept + { + return true; + } + + [[nodiscard]] inline constexpr static bool + valueExists(const Ice::optional & y) noexcept + { + return y.has_value(); + } + }; + + template struct Coerce> { + using T = std::decay_t; + + [[nodiscard]] inline constexpr Ice::optional & + operator()(Ice::optional & x) const noexcept + { + return x; + } + + [[nodiscard]] inline constexpr const Ice::optional & + operator()(const Ice::optional & x) const noexcept + { + return x; + } + + template + [[nodiscard]] inline constexpr Ice::optional + operator()(Y & y) const noexcept + { + return y; + } + + [[nodiscard]] inline constexpr static bool + valueExists(const T &) noexcept + { + return true; + } + + [[nodiscard]] inline constexpr static bool + valueExists(const Ice::optional &) noexcept + { + return true; + } + }; +} + +#endif diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index ecc965a..e1e96d6 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -60,12 +60,12 @@ namespace Slicer { protected: [[noreturn]] void conversion_fail(std::string_view typeName); template - inline static bool tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv); - template inline static bool tryConvertFrom(ValueSource & vsp, MT * model); + inline static bool tryConvertFrom(const ValueSource & vsp, MT * model, const Conv & conv); + template inline static bool tryConvertFrom(const ValueSource & vsp, MT * model); template - inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv); + inline static TryConvertResult tryConvertTo(const ValueTarget & vsp, const MT * model, const Conv & conv); template - inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model); + inline static TryConvertResult tryConvertTo(const ValueTarget & vsp, const MT * model); }; template diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index af3f980..b332c08 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -4,6 +4,7 @@ #include "common.h" #include "enumMap.h" #include "hookMap.h" +#include "modelPartsTraits.h" #include "modelPartsTypes.h" #include #include @@ -206,99 +207,16 @@ namespace Slicer { return (bool)*this->Model; } - // Function traits helpers - template struct function_traits; - template struct function_traits> { - template struct arg { - using type = typename std::tuple_element>::type; - }; - }; - template - struct callable_traits : public function_traits::type>> { - }; - - // Converters that remove "optionalness". - template struct Coerce { - using T = typename std::remove_const::type>::type; - - T & - operator()(T & x) const - { - return x; - } - const T & - operator()(const T & x) const - { - return x; - } - template - T & - operator()(Ice::optional & x) const - { - if (!x) { - x = Y(); - } - return *x; - } - template - const T & - operator()(const Ice::optional & x) const - { - return *x; - } - static bool - valueExists(const T &) - { - return true; - } - static bool - valueExists(const Ice::optional & y) - { - return y.has_value(); - } - }; - template struct Coerce> { - using T = typename std::remove_const::type>::type; - - Ice::optional & - operator()(Ice::optional & x) const - { - return x; - } - const Ice::optional & - operator()(const Ice::optional & x) const - { - return x; - } - template - Ice::optional - operator()(Y & y) const - { - return y; - } - static bool - valueExists(const T &) - { - return true; - } - static bool - valueExists(const Ice::optional &) - { - return true; - } - }; - template inline bool - ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv) + ModelPartForConvertedBase::tryConvertFrom(const ValueSource & vsp, MT * model, const Conv & conv) { - if (auto vspt = dynamic_cast *>(&vsp)) { - using CA = typename callable_traits::template arg<0>::type; + if (auto vspt = dynamic_cast *>(&vsp)) { + using CA = callable_param; ET tmp; vspt->set(tmp); - auto converted = conv(Coerce()(tmp)); - if (Coerce::valueExists(converted)) { - *model = Coerce()(converted); + if (auto converted = conv(Coerce()(tmp)); Coerce::valueExists(converted)) { + *model = Coerce()(std::move(converted)); } return true; } @@ -307,9 +225,9 @@ namespace Slicer { template inline bool - ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model) + ModelPartForConvertedBase::tryConvertFrom(const ValueSource & vsp, MT * model) { - if (auto vspt = dynamic_cast *>(&vsp)) { + if (auto vspt = dynamic_cast *>(&vsp)) { if (Coerce::valueExists(*model)) { vspt->set(Coerce()(*model)); } @@ -320,15 +238,13 @@ namespace Slicer { template inline TryConvertResult - ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv) - { - if (auto vspt = dynamic_cast *>(&vsp)) { - using CA = typename callable_traits::template arg<0>::type; - using CAR = typename std::remove_const::type>::type; - if (Coerce::valueExists(*model)) { - auto converted = conv(Coerce()(*model)); - if (Coerce::valueExists(converted)) { - vspt->get(Coerce()(converted)); + ModelPartForConvertedBase::tryConvertTo(const ValueTarget & vsp, const MT * model, const Conv & conv) + { + if (auto vspt = dynamic_cast *>(&vsp)) { + using CA = callable_param; + if (Coerce>::valueExists(*model)) { + if (auto converted = conv(Coerce()(*model)); Coerce::valueExists(converted)) { + vspt->get(Coerce()(std::move(converted))); return TryConvertResult::Value; } } @@ -339,9 +255,9 @@ namespace Slicer { template inline TryConvertResult - ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model) + ModelPartForConvertedBase::tryConvertTo(const ValueTarget & vsp, const MT * model) { - if (auto vspt = dynamic_cast *>(&vsp)) { + if (auto vspt = dynamic_cast *>(&vsp)) { if (Coerce::valueExists(*model)) { vspt->get(Coerce()(*model)); return TryConvertResult::Value; -- cgit v1.2.3