Browse Source

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..
master
heck 4 years ago
parent
commit
aedf73f772
  1. 4
      src/AbstractPityUnit.cc
  2. 6
      src/PitySwarm.cc
  3. 14
      src/PityTree.hh
  4. 26
      src/PityTree.hxx
  5. 2
      src/PityUnit.hh
  6. 4
      src/PityUnit.hxx

4
src/AbstractPityUnit.cc

@ -122,7 +122,7 @@ namespace pEp {
// Every RootUnit has its own dir // Every RootUnit has its own dir
std::string AbstractPityUnit::getRootUnitDir() std::string AbstractPityUnit::getRootUnitDir()
{ {
return getGlobalRootDir() + getRoot().getName() + "/"; return getGlobalRootDir() + getRoot()->getName() + "/";
} }
// Every process has its own dir inside its rootUnitDir // Every process has its own dir inside its rootUnitDir
@ -286,7 +286,7 @@ namespace pEp {
if (isRoot()) { if (isRoot()) {
return _transport_endpoints; return _transport_endpoints;
} else { } else {
return getRoot().transportEndpoints(); return getRoot()->transportEndpoints();
} }
} }

6
src/PitySwarm.cc

@ -23,7 +23,7 @@ namespace pEp {
pEpLogClass("called"); pEpLogClass("called");
for (auto n : _model.nodes()) { for (auto n : _model.nodes()) {
TestUnit* tmp = &_swarmUnit.addNew<TestUnit>( TestUnit* tmp = _swarmUnit.addNew<TestUnit>(
n->getName(), n->getName(),
std::bind( std::bind(
&PitySwarm::_init_process, &PitySwarm::_init_process,
@ -48,7 +48,7 @@ namespace pEp {
_swarmUnit.setExecMode(PityUnit<>::ExecutionMode::PROCESS_SEQUENTIAL); _swarmUnit.setExecMode(PityUnit<>::ExecutionMode::PROCESS_SEQUENTIAL);
_swarmUnit.setName(new_name); _swarmUnit.setName(new_name);
for (auto n : rhs._nodeUnits) { for (auto n : rhs._nodeUnits) {
TestUnit* tmp = &_swarmUnit.addCopy(TestUnit(*n.second)); TestUnit* tmp = _swarmUnit.addCopy(TestUnit(*n.second));
_nodeUnits.insert(std::pair<int, TestUnit*>(n.first, tmp)); _nodeUnits.insert(std::pair<int, TestUnit*>(n.first, tmp));
} }
} }
@ -78,7 +78,7 @@ namespace pEp {
PitySwarm::TestUnit& PitySwarm::addTestUnit(int nodeNr, const TestUnit& unit) 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; return ret;
} }

14
src/PityTree.hh

@ -38,25 +38,25 @@ namespace pEp {
// Append // Append
// creates a new instance of CT, add the new instance as child and returns a ref to it // creates a new instance of CT, add the new instance as child and returns a ref to it
template<typename CT, typename... Args> template<typename CT, typename... Args>
CT& addNew(Args&&... args); CT* addNew(Args&&... args);
// Creates a copy, adds the copy as child and returns a ref to it // Creates a copy, adds the copy as child and returns a ref to it
template<typename CT> template<typename CT>
CT& addCopy(const CT&& child, const std::string& new_name = ""); CT* addCopy(const CT&& child, const std::string& new_name = "");
template<typename CT> template<typename CT>
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. // Just adds child as a non-owned reference.
T& addRef(T& child); T* addRef(T& child);
// Query // Query
virtual T& getSelf() = 0; virtual T* getSelf() = 0;
T* getParent() const; T* getParent() const;
ChildRefs getChildRefs() const; ChildRefs getChildRefs() const;
int getChildCount() const; int getChildCount() const;
T& getChild(const std::string& name); T* getChild(const std::string& name);
T& getRoot(); T* getRoot();
void setName(const std::string& name); void setName(const std::string& name);
std::string getName() const; std::string getName() const;

26
src/PityTree.hxx

@ -49,18 +49,18 @@ namespace pEp {
// //
template<typename T> template<typename T>
template<typename CT, typename... Args> template<typename CT, typename... Args>
CT& PityTree<T>::addNew(Args&&... args) CT* PityTree<T>::addNew(Args&&... args)
{ {
static_assert(std::is_base_of<T, CT>::value, "T must be base of CT"); static_assert(std::is_base_of<T, CT>::value, "T must be base of CT");
std::shared_ptr<CT> tmp = std::make_shared<CT>(std::forward<Args>(args)...); std::shared_ptr<CT> tmp = std::make_shared<CT>(std::forward<Args>(args)...);
_childobjs.push_back(tmp); _childobjs.emplace_back(tmp);
addRef(*tmp.get()); addRef(*tmp);
return *tmp.get(); return tmp.get();
} }
template<typename T> template<typename T>
template<typename CT> template<typename CT>
CT& PityTree<T>::addCopy(const CT&& child, const std::string& new_name) CT* PityTree<T>::addCopy(const CT&& child, const std::string& new_name)
{ {
static_assert(std::is_base_of<T, CT>::value, "PityTree<T> must be a base of T"); static_assert(std::is_base_of<T, CT>::value, "PityTree<T> must be a base of T");
CT* tmpraw = new CT(child); CT* tmpraw = new CT(child);
@ -69,22 +69,22 @@ namespace pEp {
tmpraw->setName(new_name); tmpraw->setName(new_name);
} }
addRef(*tmpraw); addRef(*tmpraw);
return *tmpraw; return tmpraw;
} }
template<typename T> template<typename T>
template<typename CT> template<typename CT>
CT& PityTree<T>::addCopy(const CT& child, const std::string& new_name) CT* PityTree<T>::addCopy(const CT& child, const std::string& new_name)
{ {
return addCopy(std::move(child)); return addCopy(std::move(child));
} }
template<class T> template<class T>
T& PityTree<T>::addRef(T& child) T* PityTree<T>::addRef(T& child)
{ {
child.setParent(&_self); child.setParent(&_self);
_childrefs.insert(ChildRef(child.getName(), child)); _childrefs.insert(ChildRef(child.getName(), child));
return child; return &child;
} }
template<class T> template<class T>
@ -157,12 +157,12 @@ namespace pEp {
} }
template<class T> template<class T>
T& PityTree<T>::getRoot() T* PityTree<T>::getRoot()
{ {
if (!isRoot()) { if (!isRoot()) {
return _parent->getRoot(); return _parent->getRoot();
} else { } else {
return _self; return &_self;
} }
} }
@ -173,7 +173,7 @@ namespace pEp {
} }
template<class T> template<class T>
T& PityTree<T>::getChild(const std::string& name) T* PityTree<T>::getChild(const std::string& name)
{ {
T* ret = nullptr; T* ret = nullptr;
try { try {
@ -181,7 +181,7 @@ namespace pEp {
} catch (const std::exception& e) { } catch (const std::exception& e) {
throw std::invalid_argument("PityNode not found: '" + name + "'"); throw std::invalid_argument("PityNode not found: '" + name + "'");
} }
return *ret; return ret;
} }
// name is alphanumeric only (everything else will be replaced by an underscore) // name is alphanumeric only (everything else will be replaced by an underscore)

2
src/PityUnit.hh

@ -47,7 +47,7 @@ namespace pEp {
// copy-assign // copy-assign
PityUnit<TestContext>& operator=(const PityUnit<TestContext>& rhs); PityUnit<TestContext>& operator=(const PityUnit<TestContext>& rhs);
PityUnit<TestContext>& getSelf() override; PityUnit<TestContext>* getSelf() override;
// clone // clone
PityUnit<TestContext>* clone() override; PityUnit<TestContext>* clone() override;

4
src/PityUnit.hxx

@ -61,9 +61,9 @@ namespace pEp {
} }
template<class TestContext> template<class TestContext>
PityUnit<TestContext> &PityUnit<TestContext>::getSelf() PityUnit<TestContext> *PityUnit<TestContext>::getSelf()
{ {
return *this; return this;
} }
template<class TestContext> template<class TestContext>

Loading…
Cancel
Save