2013-10-30 11:46:18 +00:00
|
|
|
#include "BaseDefinition.h"
|
|
|
|
|
2013-11-03 12:00:31 +00:00
|
|
|
#include "PackStream.h"
|
|
|
|
|
2015-08-12 15:02:27 +00:00
|
|
|
BaseDefinition::BaseDefinition(BaseDefinition* parent, const std::string &name):
|
|
|
|
parent(parent), name(name)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
2013-10-30 14:39:56 +00:00
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
root = parent->root;
|
2015-08-12 15:02:27 +00:00
|
|
|
parent->addChild(this);
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
root = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseDefinition::~BaseDefinition()
|
|
|
|
{
|
2015-08-12 15:02:27 +00:00
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
parent->removeChild(this);
|
|
|
|
parent = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-12 17:29:28 +00:00
|
|
|
// Work on a copy, because the child destructor will modify the array by removing itself using removeChild
|
|
|
|
std::vector<BaseDefinition*> children_copy = children;
|
|
|
|
for (auto child:children_copy)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2015-08-12 15:02:27 +00:00
|
|
|
if (child->getParent() == this)
|
|
|
|
{
|
|
|
|
delete child;
|
|
|
|
}
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
void BaseDefinition::setName(const std::string &name)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2013-10-31 16:59:18 +00:00
|
|
|
this->name = name;
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 21:36:18 +00:00
|
|
|
Scenery* BaseDefinition::getScenery()
|
|
|
|
{
|
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
return parent->getScenery();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 15:02:27 +00:00
|
|
|
std::string BaseDefinition::toString(int indent) const
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
for (int i = 0; i < indent; i++)
|
|
|
|
{
|
|
|
|
result += " ";
|
|
|
|
}
|
|
|
|
result += name;
|
|
|
|
if (not children.empty())
|
|
|
|
{
|
|
|
|
for (auto &child: children)
|
|
|
|
{
|
|
|
|
result += "\n" + child->toString(indent + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:27:40 +00:00
|
|
|
void BaseDefinition::save(PackStream* stream) const
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2013-11-15 23:27:40 +00:00
|
|
|
stream->write(name);
|
2013-12-10 21:32:58 +00:00
|
|
|
for (auto child: children)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
child->save(stream);
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:27:40 +00:00
|
|
|
void BaseDefinition::load(PackStream* stream)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2013-11-15 23:27:40 +00:00
|
|
|
name = stream->readString();
|
2013-12-10 21:32:58 +00:00
|
|
|
for (auto child: children)
|
2013-10-30 14:39:56 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
child->load(stream);
|
2013-10-30 14:39:56 +00:00
|
|
|
}
|
2013-10-30 11:46:18 +00:00
|
|
|
}
|
2013-10-31 16:59:18 +00:00
|
|
|
|
2013-11-13 19:07:35 +00:00
|
|
|
void BaseDefinition::copy(BaseDefinition* destination) const
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-10-31 23:09:51 +00:00
|
|
|
destination->setName(name);
|
|
|
|
// can't copy children as we don't know their types...
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseDefinition::validate()
|
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
for (auto child: children)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
child->validate();
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseDefinition::addChild(BaseDefinition* child)
|
|
|
|
{
|
2013-12-15 14:33:54 +00:00
|
|
|
if (std::find(children.begin(), children.end(), child) == children.end())
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
children.push_back(child);
|
2013-10-31 16:59:18 +00:00
|
|
|
child->parent = this;
|
|
|
|
child->root = this->root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseDefinition::removeChild(BaseDefinition* child)
|
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
std::vector<BaseDefinition*>::iterator it = std::find(children.begin(), children.end(), child);
|
|
|
|
if (it != children.end())
|
|
|
|
{
|
2015-08-12 15:02:27 +00:00
|
|
|
child->parent = NULL;
|
2013-12-10 21:32:58 +00:00
|
|
|
children.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-15 14:33:54 +00:00
|
|
|
qWarning("Trying to remove not found child '%s' from '%s'", child->name.c_str(), name.c_str());
|
2013-12-10 21:32:58 +00:00
|
|
|
}
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|