From aedf73f7725abe0e1400c4ec338530e6b15575a0 Mon Sep 17 00:00:00 2001 From: heck Date: Fri, 8 Oct 2021 16:24:30 +0200 Subject: [PATCH] Return pointers instead of refs, because its too dangerous with the bloody auto keyword that takes a copy if you are not careful. This will never happen using pointers. Tongue in cheek situation but hrmpl.. --- src/AbstractPityUnit.cc | 4 ++-- src/PitySwarm.cc | 6 +++--- src/PityTree.hh | 14 +++++++------- src/PityTree.hxx | 26 +++++++++++++------------- src/PityUnit.hh | 2 +- src/PityUnit.hxx | 4 ++-- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/AbstractPityUnit.cc b/src/AbstractPityUnit.cc index 1f18fcd..49137d3 100644 --- a/src/AbstractPityUnit.cc +++ b/src/AbstractPityUnit.cc @@ -122,7 +122,7 @@ namespace pEp { // Every RootUnit has its own dir std::string AbstractPityUnit::getRootUnitDir() { - return getGlobalRootDir() + getRoot().getName() + "/"; + return getGlobalRootDir() + getRoot()->getName() + "/"; } // Every process has its own dir inside its rootUnitDir @@ -286,7 +286,7 @@ namespace pEp { if (isRoot()) { return _transport_endpoints; } else { - return getRoot().transportEndpoints(); + return getRoot()->transportEndpoints(); } } diff --git a/src/PitySwarm.cc b/src/PitySwarm.cc index a32082c..e838a4a 100644 --- a/src/PitySwarm.cc +++ b/src/PitySwarm.cc @@ -23,7 +23,7 @@ namespace pEp { pEpLogClass("called"); for (auto n : _model.nodes()) { - TestUnit* tmp = &_swarmUnit.addNew( + TestUnit* tmp = _swarmUnit.addNew( n->getName(), std::bind( &PitySwarm::_init_process, @@ -48,7 +48,7 @@ namespace pEp { _swarmUnit.setExecMode(PityUnit<>::ExecutionMode::PROCESS_SEQUENTIAL); _swarmUnit.setName(new_name); for (auto n : rhs._nodeUnits) { - TestUnit* tmp = &_swarmUnit.addCopy(TestUnit(*n.second)); + TestUnit* tmp = _swarmUnit.addCopy(TestUnit(*n.second)); _nodeUnits.insert(std::pair(n.first, tmp)); } } @@ -78,7 +78,7 @@ namespace pEp { PitySwarm::TestUnit& PitySwarm::addTestUnit(int nodeNr, const TestUnit& unit) { - TestUnit& ret = getLeafUnit(nodeNr).addCopy(std::move(unit)); + TestUnit& ret = *getLeafUnit(nodeNr).addCopy(std::move(unit)); return ret; } diff --git a/src/PityTree.hh b/src/PityTree.hh index eafa01a..ee00889 100644 --- a/src/PityTree.hh +++ b/src/PityTree.hh @@ -38,25 +38,25 @@ namespace pEp { // Append // creates a new instance of CT, add the new instance as child and returns a ref to it template - CT& addNew(Args&&... args); + CT* addNew(Args&&... args); // Creates a copy, adds the copy as child and returns a ref to it template - CT& addCopy(const CT&& child, const std::string& new_name = ""); + CT* addCopy(const CT&& child, const std::string& new_name = ""); template - CT& addCopy(const CT& child, const std::string& new_name = ""); + CT* addCopy(const CT& child, const std::string& new_name = ""); // Just adds child as a non-owned reference. - T& addRef(T& child); + T* addRef(T& child); // Query - virtual T& getSelf() = 0; + virtual T* getSelf() = 0; T* getParent() const; ChildRefs getChildRefs() const; int getChildCount() const; - T& getChild(const std::string& name); - T& getRoot(); + T* getChild(const std::string& name); + T* getRoot(); void setName(const std::string& name); std::string getName() const; diff --git a/src/PityTree.hxx b/src/PityTree.hxx index 9215175..a920e38 100644 --- a/src/PityTree.hxx +++ b/src/PityTree.hxx @@ -49,18 +49,18 @@ namespace pEp { // template template - CT& PityTree::addNew(Args&&... args) + CT* PityTree::addNew(Args&&... args) { static_assert(std::is_base_of::value, "T must be base of CT"); std::shared_ptr tmp = std::make_shared(std::forward(args)...); - _childobjs.push_back(tmp); - addRef(*tmp.get()); - return *tmp.get(); + _childobjs.emplace_back(tmp); + addRef(*tmp); + return tmp.get(); } template template - CT& PityTree::addCopy(const CT&& child, const std::string& new_name) + CT* PityTree::addCopy(const CT&& child, const std::string& new_name) { static_assert(std::is_base_of::value, "PityTree must be a base of T"); CT* tmpraw = new CT(child); @@ -69,22 +69,22 @@ namespace pEp { tmpraw->setName(new_name); } addRef(*tmpraw); - return *tmpraw; + return tmpraw; } template template - CT& PityTree::addCopy(const CT& child, const std::string& new_name) + CT* PityTree::addCopy(const CT& child, const std::string& new_name) { return addCopy(std::move(child)); } template - T& PityTree::addRef(T& child) + T* PityTree::addRef(T& child) { child.setParent(&_self); _childrefs.insert(ChildRef(child.getName(), child)); - return child; + return &child; } template @@ -157,12 +157,12 @@ namespace pEp { } template - T& PityTree::getRoot() + T* PityTree::getRoot() { if (!isRoot()) { return _parent->getRoot(); } else { - return _self; + return &_self; } } @@ -173,7 +173,7 @@ namespace pEp { } template - T& PityTree::getChild(const std::string& name) + T* PityTree::getChild(const std::string& name) { T* ret = nullptr; try { @@ -181,7 +181,7 @@ namespace pEp { } catch (const std::exception& e) { throw std::invalid_argument("PityNode not found: '" + name + "'"); } - return *ret; + return ret; } // name is alphanumeric only (everything else will be replaced by an underscore) diff --git a/src/PityUnit.hh b/src/PityUnit.hh index 1a6ee17..cdfd4a0 100644 --- a/src/PityUnit.hh +++ b/src/PityUnit.hh @@ -47,7 +47,7 @@ namespace pEp { // copy-assign PityUnit& operator=(const PityUnit& rhs); - PityUnit& getSelf() override; + PityUnit* getSelf() override; // clone PityUnit* clone() override; diff --git a/src/PityUnit.hxx b/src/PityUnit.hxx index 81d9049..136974e 100644 --- a/src/PityUnit.hxx +++ b/src/PityUnit.hxx @@ -61,9 +61,9 @@ namespace pEp { } template - PityUnit &PityUnit::getSelf() + PityUnit *PityUnit::getSelf() { - return *this; + return this; } template