diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2020-10-25 16:47:52 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2020-10-25 16:47:52 +0000 |
commit | f42d9dc3ca8a09478a25ead5a46040b2fa2d9acb (patch) | |
tree | f425c858dfbe2f5b9c176ed736d14fbd8f00abb0 | |
parent | First cut constexpr hooks and metadata (diff) | |
download | slicer-f42d9dc3ca8a09478a25ead5a46040b2fa2d9acb.tar.bz2 slicer-f42d9dc3ca8a09478a25ead5a46040b2fa2d9acb.tar.xz slicer-f42d9dc3ca8a09478a25ead5a46040b2fa2d9acb.zip |
Combine Hook and HookMetaData
-rw-r--r-- | slicer/slicer/hook-test.cpp | 8 | ||||
-rw-r--r-- | slicer/slicer/modelPartsTypes.h | 4 | ||||
-rw-r--r-- | slicer/slicer/modelPartsTypes.impl.h | 41 | ||||
-rw-r--r-- | slicer/tool/parser.cpp | 8 |
4 files changed, 19 insertions, 42 deletions
diff --git a/slicer/slicer/hook-test.cpp b/slicer/slicer/hook-test.cpp index 11641c2..d478beb 100644 --- a/slicer/slicer/hook-test.cpp +++ b/slicer/slicer/hook-test.cpp @@ -13,11 +13,11 @@ struct S { const std::string aa {"aa"}, aA {"aA"}, Aa {"Aa"}, AA {"AA"}, b {"b"}; using C = Slicer::ModelPartForComplex<S>; -constexpr C::Hook<int, Slicer::ModelPartForSimple<int>> haa {&S::aa, "aa", "aa", &aa}; -constexpr C::Hook<int, Slicer::ModelPartForSimple<int>> haA {&S::aA, "aA", "aa", &aA}; -constexpr C::Hook<int, Slicer::ModelPartForSimple<int>> hAa {&S::Aa, "Aa", "aa", &Aa}; +constexpr C::Hook<int, Slicer::ModelPartForSimple<int>, 0> haa {&S::aa, "aa", "aa", &aa}; +constexpr C::Hook<int, Slicer::ModelPartForSimple<int>, 1> haA {&S::aA, "aA", "aa", &aA, "md1"}; +constexpr C::Hook<int, Slicer::ModelPartForSimple<int>, 3> hAa {&S::Aa, "Aa", "aa", &Aa, "md2", "md3", "md4"}; constexpr C::Hook<int, Slicer::ModelPartForSimple<int>> hAA {&S::AA, "AA", "aa", &AA}; -constexpr C::Hook<std::string, Slicer::ModelPartForSimple<std::string>> hb {&S::b, "b", "b", &b}; +constexpr C::Hook<std::string, Slicer::ModelPartForSimple<std::string>, 0> hb {&S::b, "b", "b", &b}; constexpr Slicer::HooksImpl<S, 5> h {{{&haa, &haA, &hAa, &hAA, &hb}}}; static_assert(h.arr.size() == 5); diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index eff56b7..087c318 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -144,9 +144,7 @@ namespace Slicer { public: class DLL_PRIVATE HookBase; - template<typename MT, typename MP> class DLL_PRIVATE Hook; - - template<typename MT, typename MP, std::size_t> class DLL_PRIVATE HookMetadata; + template<typename MT, typename MP, std::size_t = 0> class DLL_PRIVATE Hook; void OnEachChild(const ChildHandler & ch) override; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index f8e2a65..87094e5 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -449,42 +449,15 @@ namespace Slicer { using HookCommon::HookCommon; virtual ModelPartPtr Get(T * t) const = 0; - [[nodiscard]] const Metadata & - GetMetadata() const override - { - return emptyMetadata; - } - }; - - template<typename T> - template<typename MT, typename MP> - class DLL_PRIVATE ModelPartForComplex<T>::Hook : public ModelPartForComplex<T>::HookBase { - public: - constexpr Hook(MT T::*m, std::string_view n, std::string_view nl, const std::string * ns) : - HookBase(n, nl, ns), member(m) - { - } - - ModelPartPtr - Get(T * t) const override - { - return std::make_shared<MP>( - t ? const_cast<typename std::remove_const<MT>::type *>(&(t->*member)) : nullptr); - } - - private: - const MT T::*member; }; template<typename T> template<typename MT, typename MP, std::size_t N> - class DLL_PRIVATE ModelPartForComplex<T>::HookMetadata : public ModelPartForComplex<T>::template Hook<MT, MP> { + class DLL_PRIVATE ModelPartForComplex<T>::Hook : public ModelPartForComplex<T>::HookBase { public: template<typename... MD> - constexpr HookMetadata( - MT T::*member, std::string_view n, std::string_view nl, const std::string * ns, MD &&... md) : - Hook<MT, MP>(member, n, nl, ns), - hookMetadata {{std::forward<MD>(md)...}} + constexpr Hook(MT T::*m, std::string_view n, std::string_view nl, const std::string * ns, MD &&... md) : + HookBase(n, nl, ns), member(m), hookMetadata {{std::forward<MD>(md)...}} { static_assert(sizeof...(MD) == N, "Wrong amount of metadata"); } @@ -495,7 +468,15 @@ namespace Slicer { return hookMetadata; } + ModelPartPtr + Get(T * t) const override + { + return std::make_shared<MP>( + t ? const_cast<typename std::remove_const<MT>::type *>(&(t->*member)) : nullptr); + } + private: + const MT T::*member; const MetaDataImpl<N> hookMetadata; }; diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index ef9139d..ba776b4 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -407,12 +407,10 @@ namespace Slicer { t = Slice::ClassDefPtr::dynamicCast(dm->container())->declaration(); } auto type = dm->type(); - fprintbf(cpp, "\tconstexpr C%d::%s<", components, md.countSlicerMetaData() ? "HookMetadata" : "Hook"); + fprintbf(cpp, "\tconstexpr C%d::Hook<", components); fprintbf(cpp, " %s, ", Slice::typeToString(type, dm->optional())); createNewModelPartPtrFor(type, dm, md); - if (auto n = md.countSlicerMetaData()) { - fprintbf(cpp, ", %d", n); - } + fprintbf(cpp, ", %d", md.countSlicerMetaData()); fprintbf(cpp, " > hook%d_%d {&%s, \"%s\", \"%s\", &hstr%d_%d", components, en, dm->scoped(), name, lname, components, en); if (md.hasSlicerMetaData()) { @@ -558,7 +556,7 @@ namespace Slicer { fprintbf(cpp, "\tconst std::string hstr%d_%d { \"%s\" };\n", components, element, name); fprintbf(cpp, "\tconstexpr C%d::Hook< const %s, ", components, Slice::typeToString(t)); createNewModelPartPtrFor(t); - fprintbf(cpp, " > hook%d_%s {&%s::value_type::%s, \"%s\", \"%s\", &hstr%d_%s};\n", components, element, + fprintbf(cpp, ", 0 > hook%d_%s {&%s::value_type::%s, \"%s\", \"%s\", &hstr%d_%s};\n", components, element, d->scoped(), element, name, lname, components, element); }; addHook(md.value("slicer:key:").value_or("key"), "first", d->keyType()); |