PityTest11 is a very flexible C++11 peer-to-peer test framework supporting hierarchically structured test suites, multi-processing, logging, IPC, synchronization and more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.9 KiB

// This file is under GNU General Public License 3.0
// see LICENSE.txt
//#include "../../../src/pEpLog.hh"
#include <string>
#include <map>
#include <memory>
#include <functional>
#include <type_traits>
#include <vector>
namespace pEp {
namespace PityTest11 {
template<class T>
class PityTree {
// static_assert(std::is_base_of<PityTree<T>, T>::value, "PityTree<T> must be a base of T");
using ChildObj = std::shared_ptr<T>;
using ChildObjs = std::vector<ChildObj>;
using ChildRef = std::pair<const std::string, T&>;
using ChildRefs = std::map<const std::string, T&>;
// Constructors
explicit PityTree(T& self, const std::string& name);
explicit PityTree(T& self, const std::string& name, T& parent);
explicit PityTree(const PityTree& rhs, T& owner);
// copy-assign
// PityTree& operator=(const PityTree<T>& rhs);
// clone
virtual PityTree* clone() = 0;
// Append
// creates a new instance of CT, add the new instance as child and returns a ref to it
template<typename CT, typename... Args>
CT* addNew(Args&&... args);
// Creates a copy, adds the copy as child and returns a ref to it
template<typename CT>
CT* addCopy(const CT&& child, const std::string& new_name = "");
template<typename CT>
CT* addCopy(const CT& child, const std::string& new_name = "");
// Just adds child as a non-owned reference.
T* addRef(T& child);
// Query
virtual T* getSelf() = 0;
T* getParent() const;
ChildRefs getChildRefs() const;
int getChildCount() const;
T* getChild(const std::string& name);
T* getRoot();
void setName(const std::string& name);
std::string getName() const;
std::string getPath() const;
bool isRoot() const; // true if has no parent
std::string to_string(bool recursive = true, int indent = 0);
//TODO HACK in PityTransport, this should be private
static std::string _normalizeName(std::string name);
virtual ~PityTree() = default;
void setParent(T* const parent);
void _copyChildRefs(const PityTree<T>& rhs);
// Fields
std::string _nodename;
T& _self;
T* _parent; //nullptr if RootUnit
ChildRefs _childrefs; // map to guarantee uniqueness of sibling-names
ChildObjs _childobjs;
}; // namespace PityTest11
}; // namespace pEp
#include "PityTree.hxx"