Skip to content

Commit

Permalink
libdoomsday: Added ThinkerData and MobjThinkerData
Browse files Browse the repository at this point in the history
Base classes to be used for all private data of thinkers. The lowest
base class provides a Doomsday Script namespace, while the mobj-specific
one contains common internal functionality shared by the server and
the client.
  • Loading branch information
skyjake committed Jul 26, 2014
1 parent e8a25a5 commit 009f5b1
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 11 deletions.
47 changes: 47 additions & 0 deletions doomsday/libdoomsday/include/doomsday/world/mobjthinkerdata.h
@@ -0,0 +1,47 @@
/** @file mobjthinkerdata.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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDOOMSDAY_MOBJTHINKERDATA_H
#define LIBDOOMSDAY_MOBJTHINKERDATA_H

#include "thinkerdata.h"
#include "mobj.h"

/**
* Private mobj data common to both client and server.
*
* @todo Game-side IData should be here; eventually the games don't need to add any
* custom members to mobj_s, just to their own private data instance. -jk
*/
class MobjThinkerData : public ThinkerData
{
public:
MobjThinkerData(mobj_t *mobj);
MobjThinkerData(MobjThinkerData const &other);

IData *duplicate() const;

mobj_t *mobj();
mobj_t const *mobj() const;

private:
DENG2_PRIVATE(d)
};

#endif // LIBDOOMSDAY_MOBJTHINKERDATA_H
21 changes: 12 additions & 9 deletions doomsday/libdoomsday/include/doomsday/world/thinker.h
Expand Up @@ -112,7 +112,9 @@ class LIBDOOMSDAY_PUBLIC Thinker
int _offset;
};

/// Base class for the private data of a thinker.
/**
* Base class for the private data of a thinker.
*/
class LIBDOOMSDAY_PUBLIC IData
{
public:
Expand Down Expand Up @@ -207,6 +209,13 @@ class LIBDOOMSDAY_PUBLIC Thinker
IData &data();
IData const &data() const;

/**
* Sets the private data for the thinker.
*
* @param data Private data object. Ownership taken.
*/
void setData(IData *data);

public:
/**
* Destroys a POD thinker that has been acquired using take(). All the memory owned
Expand All @@ -220,14 +229,6 @@ class LIBDOOMSDAY_PUBLIC Thinker

static void zap(thinker_s &thinkerBase, de::dsize sizeInBytes);

protected:
/**
* Sets the private data for the thinker.
*
* @param data Private data object. Ownership taken.
*/
void setData(IData *data);

private:
DENG2_PRIVATE(d)

Expand Down Expand Up @@ -279,6 +280,8 @@ class ThinkerT : public Thinker

void putInto(Type &dest) { Thinker::putInto(reinterpret_cast<thinker_s &>(dest)); }

static void destroy(Type *thinker) { Thinker::destroy(reinterpret_cast<thinker_s *>(thinker)); }

static void zap(Type &thinker, de::dsize sizeInBytes = sizeof(Type)) {
Thinker::zap(reinterpret_cast<thinker_s &>(thinker), sizeInBytes);
}
Expand Down
68 changes: 68 additions & 0 deletions doomsday/libdoomsday/include/doomsday/world/thinkerdata.h
@@ -0,0 +1,68 @@
/** @file thinkerdata.h Base class for thinker private data.
*
* @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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDOOMSDAY_THINKERDATA_H
#define LIBDOOMSDAY_THINKERDATA_H

#include "thinker.h"

#include <de/Record>
#include <de/Id>

/**
* Base class for thinker private data.
*
* Contains internal functionality common to all thinkers regardless of their type.
*/
class LIBDOOMSDAY_PUBLIC ThinkerData : public Thinker::IData
{
public:
ThinkerData(thinker_s *self);
ThinkerData(ThinkerData const &other);

IData *duplicate() const;

thinker_s &thinker();
thinker_s const &thinker() const;

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

private:
DENG2_PRIVATE(d)

#ifdef DENG2_DEBUG
public:
struct DebugCounter {
de::Id id;
static duint32 total;

DebugCounter() { total++; }
~DebugCounter() { total--; }
};
DebugCounter _debugCounter;

struct DebugValidator {
DebugValidator() { DENG2_ASSERT(DebugCounter::total == 0); }
~DebugValidator() { DENG2_ASSERT(DebugCounter::total == 0); }
};
#endif
};

#endif // LIBDOOMSDAY_THINKERDATA_H
8 changes: 6 additions & 2 deletions doomsday/libdoomsday/libdoomsday.pro
Expand Up @@ -91,7 +91,9 @@ HEADERS += \
include/doomsday/resource/wav.h \
include/doomsday/uri.h \
include/doomsday/world/mobj.h \
include/doomsday/world/thinker.h
include/doomsday/world/mobjthinkerdata.h \
include/doomsday/world/thinker.h \
include/doomsday/world/thinkerdata.h

win32: HEADERS += \
include/doomsday/filesys/fs_windows.h
Expand Down Expand Up @@ -128,7 +130,9 @@ SOURCES += \
src/resource/resourceclass.cpp \
src/resource/wav.cpp \
src/uri.cpp \
src/world/thinker.cpp
src/world/mobjthinkerdata.cpp \
src/world/thinker.cpp \
src/world/thinkerdata.cpp

win32: SOURCES += \
src/filesys/fs_windows.cpp
Expand Down
50 changes: 50 additions & 0 deletions doomsday/libdoomsday/src/world/mobjthinkerdata.cpp
@@ -0,0 +1,50 @@
/** @file mobjthinkerdata.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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#include "doomsday/world/mobjthinkerdata.h"

using namespace de;

DENG2_PIMPL_NOREF(MobjThinkerData)
{};

MobjThinkerData::MobjThinkerData(mobj_t *mobj)
: ThinkerData(&mobj->thinker)
, d(new Instance)
{}

MobjThinkerData::MobjThinkerData(MobjThinkerData const &other)
: ThinkerData(other)
, d(new Instance)
{}

Thinker::IData *MobjThinkerData::duplicate() const
{
return new MobjThinkerData(*this);
}

mobj_t *MobjThinkerData::mobj()
{
return reinterpret_cast<mobj_t *>(&thinker());
}

mobj_t const *MobjThinkerData::mobj() const
{
return reinterpret_cast<mobj_t const *>(&thinker());
}
74 changes: 74 additions & 0 deletions doomsday/libdoomsday/src/world/thinkerdata.cpp
@@ -0,0 +1,74 @@
/** @file thinkerdata.cpp Base class for thinker private data.
*
* @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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#include "doomsday/world/thinkerdata.h"

using namespace de;

DENG2_PIMPL(ThinkerData)
{
thinker_s *think;
Record info;

Instance(Public *i) : Base(i), think(0) {}

Instance(Public *i, Instance const &other)
: Base(i)
, think(other.think)
, info(other.info)
{}
};

ThinkerData::ThinkerData(thinker_s *self) : d(new Instance(this))
{
d->think = self;
}

ThinkerData::ThinkerData(ThinkerData const &other) : d(new Instance(this, *other.d))
{}

Thinker::IData *ThinkerData::duplicate() const
{
return new ThinkerData(*this);
}

thinker_s &ThinkerData::thinker()
{
return *d->think;
}

thinker_s const &ThinkerData::thinker() const
{
return *d->think;
}

Record &ThinkerData::info()
{
return d->info;
}

Record const &ThinkerData::info() const
{
return d->info;
}

#ifdef DENG2_DEBUG
duint32 ThinkerData::DebugCounter::total = 0;
ThinkerData::DebugValidator ensureAllPrivateDataIsReleased;
#endif

0 comments on commit 009f5b1

Please sign in to comment.