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:
parent
7d79b6f772
commit
26fb112da1
20 changed files with 90 additions and 37 deletions
|
@ -4,7 +4,7 @@
|
|||
#include "RandomGenerator.h"
|
||||
|
||||
AtmosphereDefinition::AtmosphereDefinition(BaseDefinition* parent):
|
||||
BaseDefinition(parent)
|
||||
BaseDefinition(parent, "atmosphere")
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,13 @@
|
|||
|
||||
#include "PackStream.h"
|
||||
|
||||
BaseDefinition::BaseDefinition(BaseDefinition* parent):
|
||||
parent(parent)
|
||||
BaseDefinition::BaseDefinition(BaseDefinition* parent, const std::string &name):
|
||||
parent(parent), name(name)
|
||||
{
|
||||
if (parent)
|
||||
{
|
||||
root = parent->root;
|
||||
parent->addChild(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -17,10 +18,19 @@ BaseDefinition::BaseDefinition(BaseDefinition* parent):
|
|||
|
||||
BaseDefinition::~BaseDefinition()
|
||||
{
|
||||
if (parent)
|
||||
{
|
||||
parent->removeChild(this);
|
||||
parent = NULL;
|
||||
}
|
||||
|
||||
for (auto child:children)
|
||||
{
|
||||
if (child->getParent() == this)
|
||||
{
|
||||
delete child;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BaseDefinition::setName(const std::string &name)
|
||||
|
@ -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
|
||||
{
|
||||
stream->write(name);
|
||||
|
@ -87,6 +115,7 @@ void BaseDefinition::removeChild(BaseDefinition* child)
|
|||
std::vector<BaseDefinition*>::iterator it = std::find(children.begin(), children.end(), child);
|
||||
if (it != children.end())
|
||||
{
|
||||
child->parent = NULL;
|
||||
children.erase(it);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace definition {
|
|||
class DEFINITIONSHARED_EXPORT BaseDefinition
|
||||
{
|
||||
public:
|
||||
BaseDefinition(BaseDefinition* parent);
|
||||
BaseDefinition(BaseDefinition* parent, const std::string &name);
|
||||
virtual ~BaseDefinition();
|
||||
|
||||
virtual void save(PackStream* stream) const;
|
||||
|
@ -29,6 +29,11 @@ public:
|
|||
inline const BaseDefinition* getParent() const {return parent;}
|
||||
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:
|
||||
void addChild(BaseDefinition* child);
|
||||
void removeChild(BaseDefinition* child);
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
#include "PackStream.h"
|
||||
#include "BoundingBox.h"
|
||||
|
||||
CameraDefinition::CameraDefinition():
|
||||
BaseDefinition(NULL)
|
||||
CameraDefinition::CameraDefinition(BaseDefinition *parent):
|
||||
BaseDefinition(parent, "camera")
|
||||
{
|
||||
location.x = 0.0;
|
||||
location.y = 0.0;
|
||||
|
|
|
@ -22,7 +22,7 @@ typedef struct
|
|||
class DEFINITIONSHARED_EXPORT CameraDefinition: public BaseDefinition
|
||||
{
|
||||
public:
|
||||
CameraDefinition();
|
||||
CameraDefinition(BaseDefinition *parent = NULL);
|
||||
|
||||
virtual void save(PackStream* pack) const override;
|
||||
virtual void load(PackStream* pack) override;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include "PackStream.h"
|
||||
|
||||
CloudLayerDefinition::CloudLayerDefinition(BaseDefinition* parent):
|
||||
BaseDefinition(parent)
|
||||
BaseDefinition(parent, "layer")
|
||||
{
|
||||
type = CIRRUS;
|
||||
altitude = 0.5;
|
||||
|
|
|
@ -8,9 +8,8 @@ static BaseDefinition* _layerConstructor(Layers* parent)
|
|||
}
|
||||
|
||||
CloudsDefinition::CloudsDefinition(BaseDefinition* parent):
|
||||
Layers(parent, _layerConstructor)
|
||||
Layers(parent, "clouds", _layerConstructor)
|
||||
{
|
||||
setName("clouds");
|
||||
}
|
||||
|
||||
void CloudsDefinition::applyPreset(CloudsPreset preset)
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#include "Layers.h"
|
||||
|
||||
Layers::Layers(BaseDefinition* parent, LayerConstructor layer_constructor):
|
||||
BaseDefinition(parent), layer_constructor(layer_constructor)
|
||||
#include "Logs.h"
|
||||
|
||||
Layers::Layers(BaseDefinition* parent, const std::string &name, LayerConstructor layer_constructor):
|
||||
BaseDefinition(parent, name), layer_constructor(layer_constructor)
|
||||
{
|
||||
max_layer_count = 100;
|
||||
null_layer = layer_constructor(this);
|
||||
|
@ -35,7 +37,7 @@ void Layers::copy(BaseDefinition* destination_) const
|
|||
|
||||
Layers* Layers::newCopy() const
|
||||
{
|
||||
Layers* result = new Layers(NULL, layer_constructor);
|
||||
Layers* result = new Layers(NULL, getName(), layer_constructor);
|
||||
copy(result);
|
||||
return result;
|
||||
}
|
||||
|
@ -59,7 +61,7 @@ BaseDefinition* Layers::getLayer(int position) const
|
|||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +77,7 @@ int Layers::findLayer(BaseDefinition* layer) const
|
|||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -89,7 +91,7 @@ int Layers::addLayer(BaseDefinition* layer)
|
|||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +112,7 @@ void Layers::removeLayer(int position)
|
|||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ typedef BaseDefinition* (*LayerConstructor)(Layers* parent);
|
|||
class DEFINITIONSHARED_EXPORT Layers:public BaseDefinition
|
||||
{
|
||||
public:
|
||||
Layers(BaseDefinition* parent, LayerConstructor layer_constructor);
|
||||
Layers(BaseDefinition* parent, const std::string &name, LayerConstructor layer_constructor);
|
||||
virtual ~Layers();
|
||||
|
||||
virtual void copy(BaseDefinition* destination) const override;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "PaintedGridBrush.h"
|
||||
|
||||
PaintedGrid::PaintedGrid(BaseDefinition *parent):
|
||||
BaseDefinition(parent)
|
||||
BaseDefinition(parent, "grid")
|
||||
{
|
||||
merged_data = new PaintedGridData;
|
||||
brush_data = new PaintedGridData;
|
||||
|
|
|
@ -15,7 +15,7 @@ static const double APP_HEADER = 19866544632.125;
|
|||
static const int DATA_VERSION = 1;
|
||||
|
||||
Scenery::Scenery():
|
||||
BaseDefinition(NULL)
|
||||
BaseDefinition(NULL, "scenery")
|
||||
{
|
||||
addChild(atmosphere = new AtmosphereDefinition(this));
|
||||
addChild(camera = new CameraDefinition);
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include "PackStream.h"
|
||||
|
||||
TerrainDefinition::TerrainDefinition(BaseDefinition* parent):
|
||||
BaseDefinition(parent)
|
||||
BaseDefinition(parent, "terrain")
|
||||
{
|
||||
height = 1.0;
|
||||
scaling = 1.0;
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include "TerrainDefinition.h"
|
||||
|
||||
TextureLayerDefinition::TextureLayerDefinition(BaseDefinition* parent):
|
||||
BaseDefinition(parent)
|
||||
BaseDefinition(parent, "texture")
|
||||
{
|
||||
terrain_zone = new Zone;
|
||||
_displacement_noise = new NoiseGenerator;
|
||||
|
|
|
@ -7,8 +7,8 @@ static BaseDefinition* _layer_constructor(Layers* parent)
|
|||
return new TextureLayerDefinition(parent);
|
||||
}
|
||||
|
||||
TexturesDefinition::TexturesDefinition(BaseDefinition *parent)
|
||||
: Layers(parent, _layer_constructor)
|
||||
TexturesDefinition::TexturesDefinition(BaseDefinition *parent):
|
||||
Layers(parent, "textures", _layer_constructor)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include "SurfaceMaterial.h"
|
||||
|
||||
WaterDefinition::WaterDefinition(BaseDefinition* parent):
|
||||
BaseDefinition(parent)
|
||||
BaseDefinition(parent, "water")
|
||||
{
|
||||
material = new SurfaceMaterial;
|
||||
depth_color = new Color;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include "Vector3.h"
|
||||
|
||||
Zone::Zone(BaseDefinition *parent):
|
||||
BaseDefinition(parent)
|
||||
BaseDefinition(parent, "zone")
|
||||
{
|
||||
value_by_height = new Curve;
|
||||
absolute_height = 1;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "MainModelerWindow.h"
|
||||
|
||||
#include "Logs.h"
|
||||
#include "OpenGLView.h"
|
||||
#include "Scenery.h"
|
||||
#include "OpenGLRenderer.h"
|
||||
|
@ -17,6 +18,9 @@ MainModelerWindow::MainModelerWindow()
|
|||
{
|
||||
scenery = new Scenery();
|
||||
scenery->autoPreset();
|
||||
|
||||
logDebug("Initialized scenery:\n%s", QString::fromStdString(scenery->toString()).toLatin1().data());
|
||||
|
||||
renderer = new OpenGLRenderer(scenery);
|
||||
|
||||
render_preview_provider = new RenderPreviewProvider();
|
||||
|
|
14
src/tests/BaseDefinition_Test.cpp
Normal file
14
src/tests/BaseDefinition_Test.cpp
Normal 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());
|
||||
}
|
|
@ -5,25 +5,24 @@
|
|||
|
||||
BaseDefinition* _construc1(Layers*)
|
||||
{
|
||||
return new BaseDefinition(NULL);
|
||||
return new BaseDefinition(NULL, "test");
|
||||
}
|
||||
|
||||
BaseDefinition* _construc2(Layers* parent)
|
||||
{
|
||||
BaseDefinition* result = new BaseDefinition(parent);
|
||||
result->setName("test");
|
||||
BaseDefinition* result = new BaseDefinition(parent, "test");
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(Layers, Constructor)
|
||||
{
|
||||
Layers layers1(NULL, _construc1);
|
||||
Layers layers1(NULL, "test", _construc1);
|
||||
EXPECT_EQ(0, layers1.count());
|
||||
}
|
||||
|
||||
TEST(Layers, NullLayer)
|
||||
{
|
||||
Layers layers1(NULL, _construc1);
|
||||
Layers layers1(NULL, "test", _construc1);
|
||||
|
||||
for (int i = -2; i < 5; i++)
|
||||
{
|
||||
|
@ -33,7 +32,7 @@ TEST(Layers, NullLayer)
|
|||
EXPECT_EQ(NULL, layer->getParent());
|
||||
EXPECT_EQ(layer, layer->getRoot());
|
||||
|
||||
EXPECT_EQ("", layer->getName());
|
||||
EXPECT_EQ("test", layer->getName());
|
||||
}
|
||||
|
||||
EXPECT_EQ(0, layers1.count());
|
||||
|
@ -41,12 +40,12 @@ TEST(Layers, NullLayer)
|
|||
|
||||
TEST(Layers, copy)
|
||||
{
|
||||
Layers layers1(NULL, _construc2);
|
||||
Layers layers1(NULL, "test", _construc2);
|
||||
layers1.addLayer();
|
||||
EXPECT_EQ(1, layers1.count());
|
||||
|
||||
// Copy with the same constructor
|
||||
Layers layers2(NULL, _construc2);
|
||||
Layers layers2(NULL, "test", _construc2);
|
||||
EXPECT_EQ(0, layers2.count());
|
||||
layers1.copy(&layers2);
|
||||
EXPECT_EQ(1, layers2.count());
|
||||
|
@ -59,7 +58,7 @@ TEST(Layers, copy)
|
|||
|
||||
TEST(Layers, maxLayerCount)
|
||||
{
|
||||
Layers layers1(NULL, _construc1);
|
||||
Layers layers1(NULL, "test", _construc1);
|
||||
layers1.setMaxLayerCount(2);
|
||||
|
||||
EXPECT_EQ(0, layers1.count());
|
||||
|
|
|
@ -22,7 +22,8 @@ SOURCES += main.cpp \
|
|||
CanvasPortion_Test.cpp \
|
||||
CanvasPreview_Test.cpp \
|
||||
AtmosphereDefinition_Test.cpp \
|
||||
Scenery_Test.cpp
|
||||
Scenery_Test.cpp \
|
||||
BaseDefinition_Test.cpp
|
||||
|
||||
HEADERS += \
|
||||
BaseTestCase.h
|
||||
|
|
Loading…
Reference in a new issue