From 388962a5e7b126d33baf88706c9a4738dd775c9e Mon Sep 17 00:00:00 2001 From: andreja6 Date: Tue, 1 May 2018 11:33:24 -0700 Subject: [PATCH] Finished base for new datamodel --- Instance.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++- Instance.h | 7 ++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/Instance.cpp b/Instance.cpp index 4d72d43..1d3fe47 100644 --- a/Instance.cpp +++ b/Instance.cpp @@ -15,7 +15,10 @@ Instance::Instance(void) Instance::~Instance(void) { - name = "Default Game Instance"; + for(size_t i = 0; i < children.size(); i++) + { + delete children.at(i); + } } std::string Instance::getClassName() @@ -23,6 +26,66 @@ std::string Instance::getClassName() return className; } +std::vector Instance::getChildren() +{ + return children; +} +std::vector Instance::getAllChildren() +{ + std::vector totalchildren = children; + for(size_t i = 0; i < children.size(); i++) + { + std::vector subchildren = children.at(i)->getAllChildren(); + totalchildren.insert(totalchildren.end(), subchildren.begin(), subchildren.end()); + } + return totalchildren; +} +void Instance::setParent(Instance* newParent) +{ + if(parent != NULL) + { + parent->removeChild(this); + } + parent = newParent; + if(newParent != NULL) + { + newParent->addChild(this); + } +} +Instance* Instance::getParent() +{ + return parent; +} + +void Instance::addChild(Instance* newChild) +{ + children.push_back(newChild); +} + +void Instance::removeChild(Instance* oldChild) +{ + for(size_t i = 0; i < children.size(); i++) + { + if(children.at(i) == oldChild) + { + children.erase(children.begin() + i); + } + } +} + +Instance* Instance::findFirstChild(std::string name) +{ + Instance* child = NULL; + for(size_t i = 0; i < children.size(); i++) + { + if(children.at(i)->name == name) + { + child = children.at(i); + break; + } + } + return child; +} diff --git a/Instance.h b/Instance.h index b718a3e..be3f943 100644 --- a/Instance.h +++ b/Instance.h @@ -10,6 +10,13 @@ public: Instance* parent; // Another pointer. std::vector children; // All children. std::string getClassName(); + Instance* findFirstChild(std::string); + std::vector getChildren(); + std::vector getAllChildren(); + void setParent(Instance*); + void addChild(Instance*); + void removeChild(Instance*); + Instance* getParent(); protected: std::string className; };