From d5c892afa93503bfc9a942d5f8e9e29607e00b90 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 29 Sep 2017 21:32:12 +0100 Subject: Remove the requirement to create value targets and sources as a smart pointered object on the heap, replacing instead with a rvalue reference that is consumed by the get/set function call --- slicer/db/sqlInsertSerializer.cpp | 4 ++-- slicer/db/sqlSelectDeserializer.cpp | 11 ++++------- slicer/db/sqlUpdateSerializer.cpp | 4 ++-- slicer/json/serializer.cpp | 10 +++++----- slicer/slicer/modelParts.cpp | 4 ++-- slicer/slicer/modelParts.h | 6 ++---- slicer/slicer/modelPartsTypes.cpp | 4 ++-- slicer/slicer/modelPartsTypes.h | 28 ++++++++++++++-------------- slicer/slicer/modelPartsTypes.impl.h | 36 ++++++++++++++++++------------------ slicer/tool/parser.cpp | 4 ++-- slicer/xml/serializer.cpp | 26 +++++++++++++------------- 11 files changed, 66 insertions(+), 71 deletions(-) diff --git a/slicer/db/sqlInsertSerializer.cpp b/slicer/db/sqlInsertSerializer.cpp index cd10366..0033c7c 100644 --- a/slicer/db/sqlInsertSerializer.cpp +++ b/slicer/db/sqlInsertSerializer.cpp @@ -87,7 +87,7 @@ namespace Slicer { SqlAutoIdInsertSerializer::bindObjectAndExecute(cmp, ins); cmp->OnEachChild([&ins, this](const std::string &, ModelPartPtr cmp, HookCommonPtr h) { if (isAuto(h)) { - cmp->SetValue(new IdSave(connection)); + cmp->SetValue(IdSave(connection)); } }); } @@ -96,7 +96,7 @@ namespace Slicer { SqlInsertSerializer::bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand * ins, Slicer::ModelPartPtr cmp, HookCommonPtr h) const { if (isBind(h)) { - if (!cmp->GetValue(new SqlBinder(*ins, paramNo))) { + if (!cmp->GetValue(SqlBinder(*ins, paramNo))) { ins->bindNull(paramNo); } paramNo++; diff --git a/slicer/db/sqlSelectDeserializer.cpp b/slicer/db/sqlSelectDeserializer.cpp index b8e00b1..a2d5619 100644 --- a/slicer/db/sqlSelectDeserializer.cpp +++ b/slicer/db/sqlSelectDeserializer.cpp @@ -43,11 +43,10 @@ namespace Slicer { } return; } - SqlSourcePtr h = new SqlSource(cmd[0]); - if (!h->isNull()) { + if (!cmd[0].isNull()) { auto fmp = mp->GetAnonChild(); fmp->Create(); - fmp->SetValue(h); + fmp->SetValue(SqlSource(cmd[0])); fmp->Complete(); } if (cmd.fetch()) { @@ -101,10 +100,9 @@ namespace Slicer { return boost::iequals(c.name, h->name); }); if (fmpr) { - SqlSourcePtr h = new SqlSource(c); auto fmp = fmpr->Child(); fmp->Create(); - fmp->SetValue(h); + fmp->SetValue(SqlSource(c)); fmp->Complete(); } } @@ -117,8 +115,7 @@ namespace Slicer { rmp->Create(); const DB::Column & c = cmd[0]; if (!c.isNull()) { - SqlSourcePtr h = new SqlSource(c); - rmp->SetValue(h); + rmp->SetValue(SqlSource(c)); } rmp->Complete(); } diff --git a/slicer/db/sqlUpdateSerializer.cpp b/slicer/db/sqlUpdateSerializer.cpp index 394c660..ca9a50c 100644 --- a/slicer/db/sqlUpdateSerializer.cpp +++ b/slicer/db/sqlUpdateSerializer.cpp @@ -51,7 +51,7 @@ namespace Slicer { int paramNo = 0; cmp->OnEachChild([&upd, ¶mNo](const std::string &, ModelPartPtr cmp, HookCommonPtr h) { if (isValue(h)) { - if (!cmp->GetValue(new SqlBinder(*upd, paramNo))) { + if (!cmp->GetValue(SqlBinder(*upd, paramNo))) { upd->bindNull(paramNo); } paramNo++; @@ -59,7 +59,7 @@ namespace Slicer { }); cmp->OnEachChild([&upd, ¶mNo](const std::string &, ModelPartPtr cmp, HookCommonPtr h) { if (isPKey(h)) { - cmp->GetValue(new SqlBinder(*upd, paramNo++)); + cmp->GetValue(SqlBinder(*upd, paramNo++)); } }); if (upd->execute() == 0) { diff --git a/slicer/json/serializer.cpp b/slicer/json/serializer.cpp index 9429b9e..2145d17 100644 --- a/slicer/json/serializer.cpp +++ b/slicer/json/serializer.cpp @@ -133,7 +133,7 @@ namespace Slicer { void operator()(const SimpleT & v) const { modelPart->Create(); - modelPart->SetValue(new JsonValueSource(v)); + modelPart->SetValue(JsonValueSource(v)); modelPart->Complete(); } void operator()(const json::Null &) const @@ -156,7 +156,7 @@ namespace Slicer { auto key = emp->GetChild(keyName); auto value = emp->GetChild(valueName); key->Create(); - key->SetValue(new JsonValueSource(element.first)); + key->SetValue(JsonValueSource(element.first)); key->Complete(); boost::apply_visitor(DocumentTreeIterate(value), *element.second); emp->Complete(); @@ -208,7 +208,7 @@ namespace Slicer { json::Object::key_type k; auto v = json::ValuePtr(new json::Value()); json::Value kv; - mp->GetChild(keyName)->GetValue(new JsonValueTarget(kv)); + mp->GetChild(keyName)->GetValue(JsonValueTarget(kv)); JsonValueSource s(kv); s.set(k); ModelTreeIterateRoot(v.get(), mp->GetChild(valueName)); @@ -228,7 +228,7 @@ namespace Slicer { case mpt_Simple: { json::Value v; - if (mp->GetValue(new JsonValueTarget(v))) { + if (mp->GetValue(JsonValueTarget(v))) { boost::get(*n).insert({ name, json::ValuePtr(new json::Value(v)) }); } break; @@ -272,7 +272,7 @@ namespace Slicer { *n = json::Null(); return; case mpt_Simple: - mp->GetValue(new JsonValueTarget(*n)); + mp->GetValue(JsonValueTarget(*n)); break; case mpt_Complex: *n = json::Object(); diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 6b26fe7..cc0ee13 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -83,12 +83,12 @@ namespace Slicer { } void - ModelPart::SetValue(ValueSourcePtr) + ModelPart::SetValue(ValueSource &&) { } bool - ModelPart::GetValue(ValueTargetPtr) + ModelPart::GetValue(ValueTarget &&) { return false; } diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 611d370..0aebf8f 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -47,7 +47,6 @@ namespace Slicer { using TValueTarget::get; using TValueTarget::get; }; - typedef IceUtil::Handle ValueTargetPtr; template class TValueSource { @@ -73,7 +72,6 @@ namespace Slicer { using TValueSource::set; using TValueSource::set; }; - typedef IceUtil::Handle ValueSourcePtr; class ModelPart; class ModelPartForRootBase; @@ -174,8 +172,8 @@ namespace Slicer { virtual ModelPartType GetType() const = 0; virtual void Create(); virtual void Complete(); - virtual void SetValue(ValueSourcePtr); - virtual bool GetValue(ValueTargetPtr); + virtual void SetValue(ValueSource &&); + virtual bool GetValue(ValueTarget &&); virtual bool HasValue() const = 0; virtual const Metadata & GetMetadata() const; virtual bool IsOptional() const; diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp index 7d6edaf..f2b6b67 100644 --- a/slicer/slicer/modelPartsTypes.cpp +++ b/slicer/slicer/modelPartsTypes.cpp @@ -152,10 +152,10 @@ namespace Slicer { return NULL; } - void ModelPartForOptionalBase::SetValue(ValueSourcePtr s) + void ModelPartForOptionalBase::SetValue(ValueSource && s) { if (this->hasModel()) { - modelPart->SetValue(s); + modelPart->SetValue(std::move(s)); } } diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 62280cc..7392bbe 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -43,8 +43,8 @@ namespace Slicer { ModelPartForSimple(T * h); - virtual void SetValue(ValueSourcePtr s) override; - virtual bool GetValue(ValueTargetPtr s) override; + virtual void SetValue(ValueSource && s) override; + virtual bool GetValue(ValueTarget && s) override; }; class DLL_PUBLIC ModelPartForConvertedBase : public ModelPart { @@ -58,13 +58,13 @@ namespace Slicer { protected: template - inline static bool tryConvertFrom(const ValueSourcePtr & vsp, MT * model, const Conv & conv); + inline static bool tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv); template - inline static bool tryConvertFrom(const ValueSourcePtr & vsp, MT * model); + inline static bool tryConvertFrom(ValueSource & vsp, MT * model); template - inline static TryConvertResult tryConvertTo(const ValueTargetPtr & vsp, const MT * model, const Conv & conv); + inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv); template - inline static TryConvertResult tryConvertTo(const ValueTargetPtr & vsp, const MT * model); + inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model); }; template @@ -74,8 +74,8 @@ namespace Slicer { ModelPartForConverted(T * h); - virtual void SetValue(ValueSourcePtr s) override; - virtual bool GetValue(ValueTargetPtr s) override; + virtual void SetValue(ValueSource && s) override; + virtual bool GetValue(ValueTarget && s) override; }; template M::* MV> @@ -85,8 +85,8 @@ namespace Slicer { ModelPartForConverted(IceUtil::Optional * h); - virtual void SetValue(ValueSourcePtr s) override; - virtual bool GetValue(ValueTargetPtr s) override; + virtual void SetValue(ValueSource && s) override; + virtual bool GetValue(ValueTarget && s) override; virtual bool HasValue() const override; }; @@ -96,7 +96,7 @@ namespace Slicer { virtual void Complete() override; virtual ChildRefPtr GetAnonChildRef(const HookFilter & flt) override; virtual ChildRefPtr GetChildRef(const std::string & name, const HookFilter & flt) override; - virtual void SetValue(ValueSourcePtr s) override; + virtual void SetValue(ValueSource && s) override; virtual bool HasValue() const override; virtual bool IsOptional() const override; virtual const Metadata & GetMetadata() const override; @@ -111,7 +111,7 @@ namespace Slicer { public: ModelPartForOptional(IceUtil::Optional< typename T::element_type > * h); virtual void Create() override; - virtual bool GetValue(ValueTargetPtr s) override; + virtual bool GetValue(ValueTarget && s) override; virtual ModelPartType GetType() const override; protected: @@ -246,9 +246,9 @@ namespace Slicer { virtual const Metadata & GetMetadata() const override; - virtual void SetValue(ValueSourcePtr s) override; + virtual void SetValue(ValueSource && s) override; - virtual bool GetValue(ValueTargetPtr s) override; + virtual bool GetValue(ValueTarget && s) override; static const Metadata metadata; static const Enumerations enumerations; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 38fdf2c..6a45618 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -149,17 +149,17 @@ namespace Slicer { } template - void ModelPartForSimple::SetValue(ValueSourcePtr s) + void ModelPartForSimple::SetValue(ValueSource && s) { BOOST_ASSERT(this->Model); - s->set(*this->Model); + s.set(*this->Model); } template - bool ModelPartForSimple::GetValue(ValueTargetPtr s) + bool ModelPartForSimple::GetValue(ValueTarget && s) { BOOST_ASSERT(this->Model); - s->get(*this->Model); + s.get(*this->Model); return true; } @@ -220,9 +220,9 @@ namespace Slicer { template inline - bool ModelPartForConvertedBase::tryConvertFrom(const ValueSourcePtr & vsp, MT * model, const Conv & conv) + bool ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv) { - if (auto vspt = dynamic_cast *>(vsp.get())) { + if (auto vspt = dynamic_cast *>(&vsp)) { typedef typename callable_traits::template arg<0>::type CA; ET tmp; vspt->set(tmp); @@ -237,9 +237,9 @@ namespace Slicer { template inline - bool ModelPartForConvertedBase::tryConvertFrom(const ValueSourcePtr & vsp, MT * model) + bool ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model) { - if (auto vspt = dynamic_cast *>(vsp.get())) { + if (auto vspt = dynamic_cast *>(&vsp)) { if (Coerce::valueExists(*model)) { vspt->set(Coerce()(*model)); } @@ -250,9 +250,9 @@ namespace Slicer { template inline - TryConvertResult ModelPartForConvertedBase::tryConvertTo(const ValueTargetPtr & vsp, const MT * model, const Conv & conv) + TryConvertResult ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv) { - if (auto vspt = dynamic_cast *>(vsp.get())) { + if (auto vspt = dynamic_cast *>(&vsp)) { typedef typename callable_traits::template arg<0>::type CA; typedef typename std::remove_const::type>::type CAR; if (Coerce::valueExists(*model)) { @@ -269,9 +269,9 @@ namespace Slicer { template inline - TryConvertResult ModelPartForConvertedBase::tryConvertTo(const ValueTargetPtr & vsp, const MT * model) + TryConvertResult ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model) { - if (auto vspt = dynamic_cast *>(vsp.get())) { + if (auto vspt = dynamic_cast *>(&vsp)) { if (Coerce::valueExists(*model)) { vspt->get(Coerce()(*model)); return tcr_Value; @@ -310,11 +310,11 @@ namespace Slicer { } template - bool ModelPartForOptional::GetValue(ValueTargetPtr s) + bool ModelPartForOptional::GetValue(ValueTarget && s) { BOOST_ASSERT(this->Model); if (*this->Model) { - return modelPart->GetValue(s); + return modelPart->GetValue(std::move(s)); } return false; } @@ -546,19 +546,19 @@ namespace Slicer { } template - void ModelPartForEnum::SetValue(ValueSourcePtr s) + void ModelPartForEnum::SetValue(ValueSource && s) { BOOST_ASSERT(this->Model); std::string val; - s->set(val); + s.set(val); *this->Model = lookup(val); } template - bool ModelPartForEnum::GetValue(ValueTargetPtr s) + bool ModelPartForEnum::GetValue(ValueTarget && s) { BOOST_ASSERT(this->Model); - s->get(lookup(*this->Model)); + s.get(lookup(*this->Model)); return true; } diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index fbe86d4..2e27ca1 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -52,7 +52,7 @@ namespace Slicer { if (!conversions.empty()) { fprintbf(cpp, "template<> DLL_PUBLIC\nvoid\n"); createModelPartForConverted(type, c->scoped(), dm); - fprintbf(cpp, "::SetValue(ValueSourcePtr vsp)\n{\n"); + fprintbf(cpp, "::SetValue(ValueSource && vsp)\n{\n"); fprintbf(cpp, "\tBOOST_ASSERT(Model);\n"); for (const auto & conversion : conversions) { @@ -72,7 +72,7 @@ namespace Slicer { fprintbf(cpp, "template<> DLL_PUBLIC\nbool\n"); createModelPartForConverted(type, c->scoped(), dm); - fprintbf(cpp, "::GetValue(ValueTargetPtr vtp)\n{\n"); + fprintbf(cpp, "::GetValue(ValueTarget && vtp)\n{\n"); fprintbf(cpp, "\tBOOST_ASSERT(Model);\n"); for (const auto & conversion : conversions) { diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index 8aa9552..66e8b71 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -187,9 +187,9 @@ namespace Slicer { emp->Create(); auto key = emp->GetChild(keyName); auto value = emp->GetChild(valueName); - key->SetValue(new XmlValueSource(attr->get_name())); + key->SetValue(XmlValueSource(attr->get_name())); key->Complete(); - value->SetValue(new XmlValueSource(attr->get_value())); + value->SetValue(XmlValueSource(attr->get_value())); value->Complete(); emp->Complete(); } @@ -205,7 +205,7 @@ namespace Slicer { emp->Create(); auto key = emp->GetChild(keyName); auto value = emp->GetChildRef(valueName); - key->SetValue(new XmlValueSource(element->get_name())); + key->SetValue(XmlValueSource(element->get_name())); key->Complete(); DocumentTreeIterateElement(element, value->Child(), value); emp->Complete(); @@ -242,7 +242,7 @@ namespace Slicer { DocumentTreeIterate(firstChild, smp); } else { - smp->SetValue(new XmlContentValueSource()); + smp->SetValue(XmlContentValueSource()); } } smp->Complete(); @@ -270,7 +270,7 @@ namespace Slicer { boost::bind(metaDataFlagSet, boost::bind(&Slicer::HookCommon::GetMetadata, _1), md_attribute)); if (smp) { smp->Create(); - smp->SetValue(new XmlAttributeValueSource(attribute)); + smp->SetValue(XmlAttributeValueSource(attribute)); smp->Complete(); } } @@ -280,10 +280,10 @@ namespace Slicer { smp = mp->GetAnonChild(boost::bind(metaDataFlagSet, boost::bind(&Slicer::HookCommon::GetMetadata, _1), md_text)); } if (smp) { - smp->SetValue(new XmlContentValueSource(content)); + smp->SetValue(XmlContentValueSource(content)); } else { - mp->SetValue(new XmlContentValueSource(content)); + mp->SetValue(XmlContentValueSource(content)); } } node = node->get_next_sibling(); @@ -303,10 +303,10 @@ namespace Slicer { return; } if (hp && metaDataFlagSet(hp->GetMetadata(), md_attribute)) { - mp->GetValue(new XmlAttributeValueTarget(n, name)); + mp->GetValue(XmlAttributeValueTarget(n, name)); } else if (hp && metaDataFlagSet(hp->GetMetadata(), md_text)) { - mp->GetValue(new XmlContentValueTarget(n)); + mp->GetValue(XmlContentValueTarget(n)); } else if (hp && metaDataFlagSet(hp->GetMetadata(), md_attributes)) { ModelTreeIterateDictAttrs(n->add_child_element(name), mp); @@ -330,8 +330,8 @@ namespace Slicer { { dict->OnEachChild([element](const auto &, const auto & mp, const auto &) { if (mp->HasValue()) { - mp->GetChild(keyName)->GetValue(new XmlValueTarget([&mp,element](const auto & name) { - mp->GetChild(valueName)->GetValue(new XmlAttributeValueTarget(element, name)); + mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp,element](const auto & name) { + mp->GetChild(valueName)->GetValue(XmlAttributeValueTarget(element, name)); })); } }); @@ -342,7 +342,7 @@ namespace Slicer { { dict->OnEachChild([element](const auto &, const auto & mp, const auto &) { if (mp->HasValue()) { - mp->GetChild(keyName)->GetValue(new XmlValueTarget([&mp,element](const auto & name) { + mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp,element](const auto & name) { CurrentElementCreator cec([&element, &name]() { return element->add_child_element(name); }); ModelTreeProcessElement(cec, mp->GetChild(valueName), defaultElementCreator); })); @@ -354,7 +354,7 @@ namespace Slicer { XmlSerializer::ModelTreeProcessElement(const CurrentElementCreator & cec, ModelPartPtr mp, const ElementCreator & ec) { if (mp->GetType() == mpt_Simple) { - mp->GetValue(new XmlContentValueTarget(cec)); + mp->GetValue(XmlContentValueTarget(cec)); } else if (mp->HasValue()) { auto typeIdPropName = mp->GetTypeIdProperty(); -- cgit v1.2.3