Skip to content

Commit

Permalink
Refactor|Cleanup|libdoomsday: Added base class for definition records
Browse files Browse the repository at this point in the history
Various types of definitions have a number of common methods, such as
order(), so having a base class for the accessors is appropriate.
  • Loading branch information
skyjake committed Aug 7, 2014
1 parent d8c2361 commit 93f954f
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 86 deletions.
61 changes: 61 additions & 0 deletions doomsday/libdoomsday/include/doomsday/defs/definition.h
@@ -0,0 +1,61 @@
/** @file defs/definition.h
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDOOMSDAY_DEFN_DEFINITION_H
#define LIBDOOMSDAY_DEFN_DEFINITION_H

#include "../libdoomsday.h"
#include <de/RecordAccessor>

namespace defn {

/**
* Base class for definition records.
*/
class LIBDOOMSDAY_PUBLIC Definition : public de::RecordAccessor
{
public:
Definition() : RecordAccessor(0) {}
Definition(Definition const &other) : RecordAccessor(other) {}
Definition(de::Record &d) : RecordAccessor(d) {}
Definition(de::Record const &d) : RecordAccessor(d) {}

de::Record &def();
de::Record const &def() const;

/**
* Determines if the definition point to a record.
*/
operator bool() const;

/**
* Returns the ordinal of the definition. Ordinals are automatically set by
* DEDRegister as definitions are created.
*/
int order() const;

/**
* Inserts the default members into the definition. All definitions are required to
* implement this, as it is automatically called for all newly created definitions.
*/
virtual void resetToDefaults() = 0;
};

} // namespace defn

#endif // LIBDOOMSDAY_DEFN_DEFINITION_H
23 changes: 6 additions & 17 deletions doomsday/libdoomsday/include/doomsday/defs/model.h
Expand Up @@ -19,42 +19,31 @@
#ifndef LIBDOOMSDAY_DEFN_MODEL_H
#define LIBDOOMSDAY_DEFN_MODEL_H

#include "../libdoomsday.h"
#include "definition.h"
#include <de/RecordAccessor>

namespace defn {

/**
* Utility for handling model definitions.
*/
class LIBDOOMSDAY_PUBLIC Model : public de::RecordAccessor
class LIBDOOMSDAY_PUBLIC Model : public Definition
{
public:
Model() : RecordAccessor(0), _def(0) {}
Model(Model const &other)
: RecordAccessor(other)
, _def(other._def) {}
Model(de::Record &d) : RecordAccessor(d), _def(&d) {}
Model(de::Record const &d) : RecordAccessor(d), _def(0) {}
Model() : Definition() {}
Model(Model const &other) : Definition(other) {}
Model(de::Record &d) : Definition(d) {}
Model(de::Record const &d) : Definition(d) {}

void resetToDefaults();

Model &operator = (de::Record *d);

operator bool() const;
int order() const;

de::Record &addSub();

int subCount() const;
bool hasSub(int index) const;
de::Record &sub(int index);
de::Record const &sub(int index) const;

void cleanupAfterParsing(de::Record const &prev);

private:
de::Record *_def; ///< Modifiable access.
};

} // namespace defn
Expand Down
2 changes: 2 additions & 0 deletions doomsday/libdoomsday/libdoomsday.pro
Expand Up @@ -67,6 +67,7 @@ HEADERS += \
include/doomsday/defs/ded.h \
include/doomsday/defs/dedarray.h \
include/doomsday/defs/dedfile.h \
include/doomsday/defs/definition.h \
include/doomsday/defs/dedparser.h \
include/doomsday/defs/dedregister.h \
include/doomsday/defs/dedtypes.h \
Expand Down Expand Up @@ -108,6 +109,7 @@ SOURCES += \
src/console/var.cpp \
src/defs/ded.cpp \
src/defs/dedfile.cpp \
src/defs/definition.cpp \
src/defs/dedparser.cpp \
src/defs/dedregister.cpp \
src/defs/model.cpp \
Expand Down
46 changes: 46 additions & 0 deletions doomsday/libdoomsday/src/defs/definition.cpp
@@ -0,0 +1,46 @@
/** @file defs/definition.cpp
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "doomsday/defs/definition.h"

using namespace de;

namespace defn {

Record &Definition::def()
{
return const_cast<Record &>(accessedRecord());
}

Record const &Definition::def() const
{
return accessedRecord();
}

int Definition::order() const
{
if(!accessedRecordPtr()) return -1;
return geti("__order__");
}

Definition::operator bool() const
{
return accessedRecordPtr() != 0;
}

} // namespace defn
113 changes: 44 additions & 69 deletions doomsday/libdoomsday/src/defs/model.cpp
Expand Up @@ -28,78 +28,56 @@ namespace defn {

void Model::resetToDefaults()
{
DENG2_ASSERT(_def);

// Add all expected fields with their default values.
_def->addText ("id", "");
_def->addText ("state", "");
_def->addNumber("off", 0);
_def->addText ("sprite", "");
_def->addNumber("spriteFrame", 0);
_def->addNumber("group", 0);
_def->addNumber("selector", 0);
_def->addNumber("flags", 0);
_def->addNumber("interMark", 0);
_def->addArray ("interRange", new ArrayValue(Vector2i(0, 1)));
_def->addNumber("skinTics", 0);
_def->addArray ("scale", new ArrayValue(Vector3i(1, 1, 1)));
_def->addNumber("resize", 0);
_def->addArray ("offset", new ArrayValue(Vector3f()));
_def->addNumber("shadowRadius", 0);
_def->addArray ("sub", new ArrayValue);
}

Model &Model::operator = (Record *d)
{
setAccessedRecord(*d);
_def = d;
return *this;
}

int Model::order() const
{
if(!accessedRecordPtr()) return -1;
return geti("__order__");
}

Model::operator bool() const
{
return accessedRecordPtr() != 0;
def().addText ("id", "");
def().addText ("state", "");
def().addNumber("off", 0);
def().addText ("sprite", "");
def().addNumber("spriteFrame", 0);
def().addNumber("group", 0);
def().addNumber("selector", 0);
def().addNumber("flags", 0);
def().addNumber("interMark", 0);
def().addArray ("interRange", new ArrayValue(Vector2i(0, 1)));
def().addNumber("skinTics", 0);
def().addArray ("scale", new ArrayValue(Vector3i(1, 1, 1)));
def().addNumber("resize", 0);
def().addArray ("offset", new ArrayValue(Vector3f()));
def().addNumber("shadowRadius", 0);
def().addArray ("sub", new ArrayValue);
}

Record &Model::addSub()
{
DENG2_ASSERT(_def);

Record *def = new Record;

def->addText ("filename", "");
def->addText ("skinFilename", "");
def->addText ("frame", "");
def->addNumber("frameRange", 0);
def->addNumber("flags", 0);
def->addNumber("skin", 0);
def->addNumber("skinRange", 0);
def->addArray ("offset", new ArrayValue(Vector3f()));
def->addNumber("alpha", 0);
def->addNumber("parm", 0);
def->addNumber("selSkinMask", 0);
def->addNumber("selSkinShift", 0);
Record *sub = new Record;

sub->addText ("filename", "");
sub->addText ("skinFilename", "");
sub->addText ("frame", "");
sub->addNumber("frameRange", 0);
sub->addNumber("flags", 0);
sub->addNumber("skin", 0);
sub->addNumber("skinRange", 0);
sub->addArray ("offset", new ArrayValue(Vector3f()));
sub->addNumber("alpha", 0);
sub->addNumber("parm", 0);
sub->addNumber("selSkinMask", 0);
sub->addNumber("selSkinShift", 0);

ArrayValue *skins = new ArrayValue;
for(int i = 0; i < 8; ++i) *skins << NumberValue(0);
def->addArray ("selSkins", skins);
sub->addArray ("selSkins", skins);

def->addText ("shinySkin", "");
def->addNumber("shiny", 0);
def->addArray ("shinyColor", new ArrayValue(Vector3f(1, 1, 1)));
def->addNumber("shinyReact", 1);
def->addNumber("blendMode", BM_NORMAL);
sub->addText ("shinySkin", "");
sub->addNumber("shiny", 0);
sub->addArray ("shinyColor", new ArrayValue(Vector3f(1, 1, 1)));
sub->addNumber("shinyReact", 1);
sub->addNumber("blendMode", BM_NORMAL);

(*_def)["sub"].value<ArrayValue>()
.add(new RecordValue(def, RecordValue::OwnsRecord));
def()["sub"].value<ArrayValue>()
.add(new RecordValue(sub, RecordValue::OwnsRecord));

return *def;
return *sub;
}

int Model::subCount() const
Expand All @@ -114,8 +92,7 @@ bool Model::hasSub(int index) const

Record &Model::sub(int index)
{
DENG2_ASSERT(_def);
return *_def->geta("sub")[index].as<RecordValue>().record();
return *def().geta("sub")[index].as<RecordValue>().record();
}

Record const &Model::sub(int index) const
Expand All @@ -125,15 +102,13 @@ Record const &Model::sub(int index) const

void Model::cleanupAfterParsing(Record const &prev)
{
DENG2_ASSERT(_def);

if(_def->gets("state") == "-")
if(gets("state") == "-")
{
_def->set("state", prev.gets("state"));
def().set("state", prev.gets("state"));
}
if(_def->gets("sprite") == "-")
if(gets("sprite") == "-")
{
_def->set("sprite", prev.gets("sprite"));
def().set("sprite", prev.gets("sprite"));
}

for(int i = 0; i < subCount(); ++i)
Expand Down

0 comments on commit 93f954f

Please sign in to comment.