Fixed problems with new c++ layers

This commit is contained in:
Michaël Lemaire 2013-11-01 00:09:51 +01:00
parent ca0f87b419
commit d2620ae2e3
8 changed files with 121 additions and 9 deletions

View file

@ -49,7 +49,8 @@ void BaseDefinition::load(PackStream* pack)
void BaseDefinition::copy(BaseDefinition* destination) void BaseDefinition::copy(BaseDefinition* destination)
{ {
// TODO destination->setName(name);
// can't copy children as we don't know their types...
} }
void BaseDefinition::validate() void BaseDefinition::validate()

View file

@ -26,7 +26,7 @@ public:
virtual void validate(); virtual void validate();
inline const QString& getName() {return name;} inline const QString& getName() {return name;}
void setName(QString name); virtual void setName(QString name);
inline const BaseDefinition* getParent() {return parent;} inline const BaseDefinition* getParent() {return parent;}
inline const BaseDefinition* getRoot() {return root;} inline const BaseDefinition* getRoot() {return root;}

View file

@ -17,8 +17,10 @@ Layers::~Layers()
delete null_layer; delete null_layer;
} }
void Layers::copy(Layers* destination) void Layers::copy(BaseDefinition* destination_)
{ {
Layers* destination = (Layers*)destination_;
// don't call overridden method, it will copy again the children // don't call overridden method, it will copy again the children
// FIXME ... but the definition name (and other future attributes) is not copied // FIXME ... but the definition name (and other future attributes) is not copied
@ -109,7 +111,10 @@ void Layers::removeLayer(BaseDefinition* layer)
void Layers::moveLayer(int old_position, int new_position) void Layers::moveLayer(int old_position, int new_position)
{ {
// TODO if (old_position >= 0 and old_position < layers.size() and new_position >= 0 and new_position < layers.size())
{
layers.move(old_position, new_position);
}
} }
void Layers::moveLayer(BaseDefinition* layer, int new_position) void Layers::moveLayer(BaseDefinition* layer, int new_position)
@ -173,7 +178,7 @@ void layersLoad(PackStream* stream, Layers* layers)
const char* layersGetName(Layers* layers, int layer) const char* layersGetName(Layers* layers, int layer)
{ {
return "TODO"; return ((LegacyLayer*)(layers->getLayer(layer)))->getLegacyName();
} }
void layersSetName(Layers* layers, int layer, const char* name) void layersSetName(Layers* layers, int layer, const char* name)

View file

@ -26,7 +26,7 @@ public:
Layers(BaseDefinition* parent, LayerConstructor layer_constructor, LayerType* legacy_type=0); Layers(BaseDefinition* parent, LayerConstructor layer_constructor, LayerType* legacy_type=0);
virtual ~Layers(); virtual ~Layers();
virtual void copy(Layers* destination); virtual void copy(BaseDefinition* destination);
void setMaxLayerCount(int max_layer_count); void setMaxLayerCount(int max_layer_count);

View file

@ -4,6 +4,7 @@ LegacyLayer::LegacyLayer(BaseDefinition* parent, LayerType* type):
BaseDefinition(parent), type(*type) BaseDefinition(parent), type(*type)
{ {
legacy = type->callback_create(); legacy = type->callback_create();
setName(getName());
} }
LegacyLayer::~LegacyLayer() LegacyLayer::~LegacyLayer()
@ -13,20 +14,30 @@ LegacyLayer::~LegacyLayer()
void LegacyLayer::save(PackStream* pack) void LegacyLayer::save(PackStream* pack)
{ {
BaseDefinition::save(pack);
type.callback_save(pack, legacy); type.callback_save(pack, legacy);
} }
void LegacyLayer::load(PackStream* pack) void LegacyLayer::load(PackStream* pack)
{ {
BaseDefinition::load(pack);
type.callback_load(pack, legacy); type.callback_load(pack, legacy);
} }
void LegacyLayer::copy(LegacyLayer* destination) void LegacyLayer::copy(BaseDefinition* destination)
{ {
type.callback_copy(legacy, destination->legacy); BaseDefinition::copy(destination);
type.callback_copy(legacy, ((LegacyLayer*)destination)->legacy);
} }
void LegacyLayer::validate() void LegacyLayer::validate()
{ {
BaseDefinition::validate();
type.callback_validate(legacy); type.callback_validate(legacy);
} }
void LegacyLayer::setName(QString name)
{
BaseDefinition::setName(name);
qstrncpy(legacy_name, name.toUtf8().constData(), 99);
}

View file

@ -35,14 +35,18 @@ public:
virtual void save(PackStream* pack); virtual void save(PackStream* pack);
virtual void load(PackStream* pack); virtual void load(PackStream* pack);
virtual void copy(LegacyLayer* destination); virtual void copy(BaseDefinition* destination);
virtual void validate(); virtual void validate();
virtual void setName(QString name);
inline void* getLegacyDefinition() { return legacy; } inline void* getLegacyDefinition() { return legacy; }
inline char* getLegacyName() {return legacy_name;}
private: private:
LayerType type; LayerType type;
void* legacy; void* legacy;
char legacy_name[100];
}; };
#endif #endif

View file

@ -78,4 +78,93 @@ void Layers_Test::testMaxLayerCount()
CPPUNIT_ASSERT_EQUAL(2, layers1.count()); CPPUNIT_ASSERT_EQUAL(2, layers1.count());
} }
static int _legacy_instance_count = 0;
typedef struct
{
int a;
} LegacyData;
static void* _legacy_create()
{
LegacyData* data = new LegacyData;
data->a = 2;
_legacy_instance_count++;
return data;
}
static void _legacy_delete(void* data)
{
delete (LegacyData*)data;
_legacy_instance_count--;
}
static void _legacy_save(PackStream* stream, void* data)
{
stream->write(&((LegacyData*)data)->a);
}
static void _legacy_load(PackStream* stream, void* data)
{
stream->read(&((LegacyData*)data)->a);
}
static void _legacy_validate(void* data)
{
LegacyData* ldata = (LegacyData*)data;
if (ldata->a > 5)
{
ldata->a = 5;
}
}
static void _legacy_copy(void* source, void* destination)
{
((LegacyData*)destination)->a = ((LegacyData*)source)->a;
}
void Layers_Test::testLegacyLayers()
{
LayerType type;
type.callback_create = _legacy_create;
type.callback_delete = _legacy_delete;
type.callback_save = _legacy_save;
type.callback_load = _legacy_load;
type.callback_validate = _legacy_validate;
type.callback_copy = _legacy_copy;
Layers* layers1 = layersCreate(type, 3);
LegacyData* data1;
// Test the null layer
CPPUNIT_ASSERT_EQUAL(0, layersCount(layers1));
CPPUNIT_ASSERT_EQUAL(1, _legacy_instance_count);
data1 = (LegacyData*)layersGetLayer(layers1, 0);
CPPUNIT_ASSERT_EQUAL(2, data1->a);
data1->a = 3;
data1 = (LegacyData*)layersGetLayer(layers1, 0);
CPPUNIT_ASSERT_EQUAL(3, data1->a);
// Add an empty layer
layersAddLayer(layers1, NULL);
CPPUNIT_ASSERT_EQUAL(1, layersCount(layers1));
data1 = (LegacyData*)layersGetLayer(layers1, 0);
CPPUNIT_ASSERT_EQUAL(2, data1->a);
CPPUNIT_ASSERT_EQUAL(2, _legacy_instance_count);
// Validation
data1->a = 6;
layersValidate(layers1);
CPPUNIT_ASSERT_EQUAL(5, data1->a);
// Naming
layersSetName(layers1, 0, "test1");
CPPUNIT_ASSERT(QString("test1") == QString(layersGetName(layers1, 0)));
// Deletion
delete layers1;
CPPUNIT_ASSERT_EQUAL(0, _legacy_instance_count);
}
CPPUNIT_TEST_SUITE_REGISTRATION(Layers_Test); CPPUNIT_TEST_SUITE_REGISTRATION(Layers_Test);

View file

@ -11,12 +11,14 @@ public:
CPPUNIT_TEST(testNullLayer); CPPUNIT_TEST(testNullLayer);
CPPUNIT_TEST(testCopy); CPPUNIT_TEST(testCopy);
CPPUNIT_TEST(testMaxLayerCount); CPPUNIT_TEST(testMaxLayerCount);
CPPUNIT_TEST(testLegacyLayers);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
void testConstructor(); void testConstructor();
void testNullLayer(); void testNullLayer();
void testCopy(); void testCopy();
void testMaxLayerCount(); void testMaxLayerCount();
void testLegacyLayers();
}; };
#endif // LAYERS_TEST_H #endif // LAYERS_TEST_H