Added name to all BaseDefinition and toString representation

Also fixed deletion of children in the definition tree,
to handle dynamic and static destructor calls.
This commit is contained in:
Michaël Lemaire 2015-08-12 17:02:27 +02:00
parent 7d79b6f772
commit 26fb112da1
20 changed files with 90 additions and 37 deletions

View file

@ -4,7 +4,7 @@
#include "RandomGenerator.h" #include "RandomGenerator.h"
AtmosphereDefinition::AtmosphereDefinition(BaseDefinition* parent): AtmosphereDefinition::AtmosphereDefinition(BaseDefinition* parent):
BaseDefinition(parent) BaseDefinition(parent, "atmosphere")
{ {
} }

View file

@ -2,12 +2,13 @@
#include "PackStream.h" #include "PackStream.h"
BaseDefinition::BaseDefinition(BaseDefinition* parent): BaseDefinition::BaseDefinition(BaseDefinition* parent, const std::string &name):
parent(parent) parent(parent), name(name)
{ {
if (parent) if (parent)
{ {
root = parent->root; root = parent->root;
parent->addChild(this);
} }
else else
{ {
@ -17,9 +18,18 @@ BaseDefinition::BaseDefinition(BaseDefinition* parent):
BaseDefinition::~BaseDefinition() BaseDefinition::~BaseDefinition()
{ {
if (parent)
{
parent->removeChild(this);
parent = NULL;
}
for (auto child:children) for (auto child:children)
{ {
delete child; if (child->getParent() == this)
{
delete child;
}
} }
} }
@ -40,6 +50,24 @@ Scenery* BaseDefinition::getScenery()
} }
} }
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;
}
void BaseDefinition::save(PackStream* stream) const void BaseDefinition::save(PackStream* stream) const
{ {
stream->write(name); stream->write(name);
@ -87,6 +115,7 @@ void BaseDefinition::removeChild(BaseDefinition* child)
std::vector<BaseDefinition*>::iterator it = std::find(children.begin(), children.end(), child); std::vector<BaseDefinition*>::iterator it = std::find(children.begin(), children.end(), child);
if (it != children.end()) if (it != children.end())
{ {
child->parent = NULL;
children.erase(it); children.erase(it);
} }
else else

View file

@ -12,7 +12,7 @@ namespace definition {
class DEFINITIONSHARED_EXPORT BaseDefinition class DEFINITIONSHARED_EXPORT BaseDefinition
{ {
public: public:
BaseDefinition(BaseDefinition* parent); BaseDefinition(BaseDefinition* parent, const std::string &name);
virtual ~BaseDefinition(); virtual ~BaseDefinition();
virtual void save(PackStream* stream) const; virtual void save(PackStream* stream) const;
@ -29,6 +29,11 @@ public:
inline const BaseDefinition* getParent() const {return parent;} inline const BaseDefinition* getParent() const {return parent;}
inline const BaseDefinition* getRoot() const {return root;} inline const BaseDefinition* getRoot() const {return root;}
/**
* Return a string representation of the tree (mainly for debugging purposes).
*/
virtual std::string toString(int indent = 0) const;
protected: protected:
void addChild(BaseDefinition* child); void addChild(BaseDefinition* child);
void removeChild(BaseDefinition* child); void removeChild(BaseDefinition* child);

View file

@ -4,8 +4,8 @@
#include "PackStream.h" #include "PackStream.h"
#include "BoundingBox.h" #include "BoundingBox.h"
CameraDefinition::CameraDefinition(): CameraDefinition::CameraDefinition(BaseDefinition *parent):
BaseDefinition(NULL) BaseDefinition(parent, "camera")
{ {
location.x = 0.0; location.x = 0.0;
location.y = 0.0; location.y = 0.0;

View file

@ -22,7 +22,7 @@ typedef struct
class DEFINITIONSHARED_EXPORT CameraDefinition: public BaseDefinition class DEFINITIONSHARED_EXPORT CameraDefinition: public BaseDefinition
{ {
public: public:
CameraDefinition(); CameraDefinition(BaseDefinition *parent = NULL);
virtual void save(PackStream* pack) const override; virtual void save(PackStream* pack) const override;
virtual void load(PackStream* pack) override; virtual void load(PackStream* pack) override;

View file

@ -6,7 +6,7 @@
#include "PackStream.h" #include "PackStream.h"
CloudLayerDefinition::CloudLayerDefinition(BaseDefinition* parent): CloudLayerDefinition::CloudLayerDefinition(BaseDefinition* parent):
BaseDefinition(parent) BaseDefinition(parent, "layer")
{ {
type = CIRRUS; type = CIRRUS;
altitude = 0.5; altitude = 0.5;

View file

@ -8,9 +8,8 @@ static BaseDefinition* _layerConstructor(Layers* parent)
} }
CloudsDefinition::CloudsDefinition(BaseDefinition* parent): CloudsDefinition::CloudsDefinition(BaseDefinition* parent):
Layers(parent, _layerConstructor) Layers(parent, "clouds", _layerConstructor)
{ {
setName("clouds");
} }
void CloudsDefinition::applyPreset(CloudsPreset preset) void CloudsDefinition::applyPreset(CloudsPreset preset)

View file

@ -1,7 +1,9 @@
#include "Layers.h" #include "Layers.h"
Layers::Layers(BaseDefinition* parent, LayerConstructor layer_constructor): #include "Logs.h"
BaseDefinition(parent), layer_constructor(layer_constructor)
Layers::Layers(BaseDefinition* parent, const std::string &name, LayerConstructor layer_constructor):
BaseDefinition(parent, name), layer_constructor(layer_constructor)
{ {
max_layer_count = 100; max_layer_count = 100;
null_layer = layer_constructor(this); null_layer = layer_constructor(this);
@ -35,7 +37,7 @@ void Layers::copy(BaseDefinition* destination_) const
Layers* Layers::newCopy() const Layers* Layers::newCopy() const
{ {
Layers* result = new Layers(NULL, layer_constructor); Layers* result = new Layers(NULL, getName(), layer_constructor);
copy(result); copy(result);
return result; return result;
} }
@ -59,7 +61,7 @@ BaseDefinition* Layers::getLayer(int position) const
} }
else else
{ {
qWarning("Asked for a undefined layer %d on a total of %d", position, (int)layers.size()); logWarning("Asked for a undefined layer %d on a total of %d", position, (int)layers.size());
return null_layer; return null_layer;
} }
} }
@ -75,7 +77,7 @@ int Layers::findLayer(BaseDefinition* layer) const
} }
i++; i++;
} }
qWarning("Layer %p (%s) not found, on a total of %d", layer, layer->getName().c_str(), (int)layers.size()); logWarning("Layer %p (%s) not found, on a total of %d", layer, layer->getName().c_str(), (int)layers.size());
return -1; return -1;
} }
@ -89,7 +91,7 @@ int Layers::addLayer(BaseDefinition* layer)
} }
else else
{ {
qWarning("Add layer ignored because limit of %d reached", max_layer_count); logWarning("Add layer ignored because limit of %d reached", max_layer_count);
return -1; return -1;
} }
} }
@ -110,7 +112,7 @@ void Layers::removeLayer(int position)
} }
else else
{ {
qWarning("Removing unknown layer %d on %d from '%s'", position, (int)layers.size(), getName().c_str()); logWarning("Removing unknown layer %d on %d from '%s'", position, (int)layers.size(), getName().c_str());
} }
} }

View file

@ -16,7 +16,7 @@ typedef BaseDefinition* (*LayerConstructor)(Layers* parent);
class DEFINITIONSHARED_EXPORT Layers:public BaseDefinition class DEFINITIONSHARED_EXPORT Layers:public BaseDefinition
{ {
public: public:
Layers(BaseDefinition* parent, LayerConstructor layer_constructor); Layers(BaseDefinition* parent, const std::string &name, LayerConstructor layer_constructor);
virtual ~Layers(); virtual ~Layers();
virtual void copy(BaseDefinition* destination) const override; virtual void copy(BaseDefinition* destination) const override;

View file

@ -7,7 +7,7 @@
#include "PaintedGridBrush.h" #include "PaintedGridBrush.h"
PaintedGrid::PaintedGrid(BaseDefinition *parent): PaintedGrid::PaintedGrid(BaseDefinition *parent):
BaseDefinition(parent) BaseDefinition(parent, "grid")
{ {
merged_data = new PaintedGridData; merged_data = new PaintedGridData;
brush_data = new PaintedGridData; brush_data = new PaintedGridData;

View file

@ -15,7 +15,7 @@ static const double APP_HEADER = 19866544632.125;
static const int DATA_VERSION = 1; static const int DATA_VERSION = 1;
Scenery::Scenery(): Scenery::Scenery():
BaseDefinition(NULL) BaseDefinition(NULL, "scenery")
{ {
addChild(atmosphere = new AtmosphereDefinition(this)); addChild(atmosphere = new AtmosphereDefinition(this));
addChild(camera = new CameraDefinition); addChild(camera = new CameraDefinition);

View file

@ -5,7 +5,7 @@
#include "PackStream.h" #include "PackStream.h"
TerrainDefinition::TerrainDefinition(BaseDefinition* parent): TerrainDefinition::TerrainDefinition(BaseDefinition* parent):
BaseDefinition(parent) BaseDefinition(parent, "terrain")
{ {
height = 1.0; height = 1.0;
scaling = 1.0; scaling = 1.0;

View file

@ -8,7 +8,7 @@
#include "TerrainDefinition.h" #include "TerrainDefinition.h"
TextureLayerDefinition::TextureLayerDefinition(BaseDefinition* parent): TextureLayerDefinition::TextureLayerDefinition(BaseDefinition* parent):
BaseDefinition(parent) BaseDefinition(parent, "texture")
{ {
terrain_zone = new Zone; terrain_zone = new Zone;
_displacement_noise = new NoiseGenerator; _displacement_noise = new NoiseGenerator;

View file

@ -7,8 +7,8 @@ static BaseDefinition* _layer_constructor(Layers* parent)
return new TextureLayerDefinition(parent); return new TextureLayerDefinition(parent);
} }
TexturesDefinition::TexturesDefinition(BaseDefinition *parent) TexturesDefinition::TexturesDefinition(BaseDefinition *parent):
: Layers(parent, _layer_constructor) Layers(parent, "textures", _layer_constructor)
{ {
} }

View file

@ -6,7 +6,7 @@
#include "SurfaceMaterial.h" #include "SurfaceMaterial.h"
WaterDefinition::WaterDefinition(BaseDefinition* parent): WaterDefinition::WaterDefinition(BaseDefinition* parent):
BaseDefinition(parent) BaseDefinition(parent, "water")
{ {
material = new SurfaceMaterial; material = new SurfaceMaterial;
depth_color = new Color; depth_color = new Color;

View file

@ -6,7 +6,7 @@
#include "Vector3.h" #include "Vector3.h"
Zone::Zone(BaseDefinition *parent): Zone::Zone(BaseDefinition *parent):
BaseDefinition(parent) BaseDefinition(parent, "zone")
{ {
value_by_height = new Curve; value_by_height = new Curve;
absolute_height = 1; absolute_height = 1;

View file

@ -1,5 +1,6 @@
#include "MainModelerWindow.h" #include "MainModelerWindow.h"
#include "Logs.h"
#include "OpenGLView.h" #include "OpenGLView.h"
#include "Scenery.h" #include "Scenery.h"
#include "OpenGLRenderer.h" #include "OpenGLRenderer.h"
@ -17,6 +18,9 @@ MainModelerWindow::MainModelerWindow()
{ {
scenery = new Scenery(); scenery = new Scenery();
scenery->autoPreset(); scenery->autoPreset();
logDebug("Initialized scenery:\n%s", QString::fromStdString(scenery->toString()).toLatin1().data());
renderer = new OpenGLRenderer(scenery); renderer = new OpenGLRenderer(scenery);
render_preview_provider = new RenderPreviewProvider(); render_preview_provider = new RenderPreviewProvider();

View file

@ -0,0 +1,14 @@
#include "BaseTestCase.h"
#include "BaseDefinition.h"
TEST(BaseDefinition, toString)
{
BaseDefinition root(NULL, "root");
BaseDefinition branch(&root, "branch");
BaseDefinition leaf1(&branch, "leaf1");
BaseDefinition leaf2(&branch, "leaf2");
EXPECT_EQ("root\n branch\n leaf1\n leaf2", root.toString());
EXPECT_EQ("branch\n leaf1\n leaf2", branch.toString());
}

View file

@ -5,25 +5,24 @@
BaseDefinition* _construc1(Layers*) BaseDefinition* _construc1(Layers*)
{ {
return new BaseDefinition(NULL); return new BaseDefinition(NULL, "test");
} }
BaseDefinition* _construc2(Layers* parent) BaseDefinition* _construc2(Layers* parent)
{ {
BaseDefinition* result = new BaseDefinition(parent); BaseDefinition* result = new BaseDefinition(parent, "test");
result->setName("test");
return result; return result;
} }
TEST(Layers, Constructor) TEST(Layers, Constructor)
{ {
Layers layers1(NULL, _construc1); Layers layers1(NULL, "test", _construc1);
EXPECT_EQ(0, layers1.count()); EXPECT_EQ(0, layers1.count());
} }
TEST(Layers, NullLayer) TEST(Layers, NullLayer)
{ {
Layers layers1(NULL, _construc1); Layers layers1(NULL, "test", _construc1);
for (int i = -2; i < 5; i++) for (int i = -2; i < 5; i++)
{ {
@ -33,7 +32,7 @@ TEST(Layers, NullLayer)
EXPECT_EQ(NULL, layer->getParent()); EXPECT_EQ(NULL, layer->getParent());
EXPECT_EQ(layer, layer->getRoot()); EXPECT_EQ(layer, layer->getRoot());
EXPECT_EQ("", layer->getName()); EXPECT_EQ("test", layer->getName());
} }
EXPECT_EQ(0, layers1.count()); EXPECT_EQ(0, layers1.count());
@ -41,12 +40,12 @@ TEST(Layers, NullLayer)
TEST(Layers, copy) TEST(Layers, copy)
{ {
Layers layers1(NULL, _construc2); Layers layers1(NULL, "test", _construc2);
layers1.addLayer(); layers1.addLayer();
EXPECT_EQ(1, layers1.count()); EXPECT_EQ(1, layers1.count());
// Copy with the same constructor // Copy with the same constructor
Layers layers2(NULL, _construc2); Layers layers2(NULL, "test", _construc2);
EXPECT_EQ(0, layers2.count()); EXPECT_EQ(0, layers2.count());
layers1.copy(&layers2); layers1.copy(&layers2);
EXPECT_EQ(1, layers2.count()); EXPECT_EQ(1, layers2.count());
@ -59,7 +58,7 @@ TEST(Layers, copy)
TEST(Layers, maxLayerCount) TEST(Layers, maxLayerCount)
{ {
Layers layers1(NULL, _construc1); Layers layers1(NULL, "test", _construc1);
layers1.setMaxLayerCount(2); layers1.setMaxLayerCount(2);
EXPECT_EQ(0, layers1.count()); EXPECT_EQ(0, layers1.count());

View file

@ -22,7 +22,8 @@ SOURCES += main.cpp \
CanvasPortion_Test.cpp \ CanvasPortion_Test.cpp \
CanvasPreview_Test.cpp \ CanvasPreview_Test.cpp \
AtmosphereDefinition_Test.cpp \ AtmosphereDefinition_Test.cpp \
Scenery_Test.cpp Scenery_Test.cpp \
BaseDefinition_Test.cpp
HEADERS += \ HEADERS += \
BaseTestCase.h BaseTestCase.h