Skip to content

Commit

Permalink
Refactor|Thinker: Private data can think
Browse files Browse the repository at this point in the history
A thinker's private data now has a virtual think() method that gets
called after the public thinker function.

By default, libdoomsday's Thinker sets a NOP function as the public
thinker function.

Modified ClPlaneMover and ClPolyMover so that all their thinking is
done privately.
  • Loading branch information
skyjake committed Jul 27, 2014
1 parent 7cea75d commit 26ed217
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 20 deletions.
3 changes: 0 additions & 3 deletions doomsday/client/include/client/clplanemover.h
Expand Up @@ -51,9 +51,6 @@ class ClPlaneMover : public ThinkerData
* @return The mover thinker. Ownership retained by the Plane's Map.
*/
static thinker_s *newThinker(Plane &plane, coord_t dest, float speed);

protected:
static void thinkerFunc(thinker_s *mover);
};

#endif // DENG_CLIENT_PLANEMOVER_H
3 changes: 0 additions & 3 deletions doomsday/client/include/client/clpolymover.h
Expand Up @@ -42,9 +42,6 @@ class ClPolyMover : public ThinkerData
void think();

static thinker_s *newThinker(Polyobj &polyobj, bool moving, bool rotating);

protected:
static void thinkerFunc(thinker_s *mover);
};

#endif // DENG_CLIENT_POLYMOVER_H
1 change: 1 addition & 0 deletions doomsday/client/include/world/polyobjdata.h
Expand Up @@ -47,6 +47,7 @@ class PolyobjData : public Thinker::IData
~PolyobjData();

void setThinker(thinker_s *thinker);
void think();
IData *duplicate() const;

#ifdef __CLIENT__
Expand Down
6 changes: 0 additions & 6 deletions doomsday/client/src/client/clplanemover.cpp
Expand Up @@ -33,7 +33,6 @@ using namespace de;
thinker_s *ClPlaneMover::newThinker(Plane &plane, coord_t dest, float speed) // static
{
Thinker th(Thinker::AllocateMemoryZone);
th.function = (thinkfunc_t) thinkerFunc;
th.setData(new ClPlaneMover(plane, dest, speed));

// Add to the map.
Expand Down Expand Up @@ -142,8 +141,3 @@ void ClPlaneMover::think()
}
}
}

void ClPlaneMover::thinkerFunc(thinker_s *mover) // static
{
THINKER_DATA(*mover, ClPlaneMover).think();
}
6 changes: 0 additions & 6 deletions doomsday/client/src/client/clpolymover.cpp
Expand Up @@ -41,7 +41,6 @@ thinker_s *ClPolyMover::newThinker(Polyobj &polyobj, bool moving, bool rotating)
}

Thinker th(Thinker::AllocateMemoryZone);
th.function = (thinkfunc_t) thinkerFunc;
th.setData(new ClPolyMover(polyobj, moving, rotating));

thinker_s *ptr = th.take();
Expand Down Expand Up @@ -125,8 +124,3 @@ void ClPolyMover::think()
_polyobj->map().thinkers().remove(thinker()); // we get deleted
}
}

void ClPolyMover::thinkerFunc(thinker_s *mover)
{
THINKER_DATA(*mover, ClPolyMover).think();
}
5 changes: 5 additions & 0 deletions doomsday/client/src/world/polyobjdata.cpp
Expand Up @@ -43,6 +43,11 @@ void PolyobjData::setThinker(thinker_s *thinker)
_polyobj = (Polyobj *) thinker;
}

void PolyobjData::think()
{
// nothing to do; public thinker does all
}

Thinker::IData *PolyobjData::duplicate() const
{
return new PolyobjData(*this);
Expand Down
4 changes: 4 additions & 0 deletions doomsday/client/src/world/thinkers.cpp
Expand Up @@ -443,7 +443,11 @@ static int runThinker(thinker_t *th, void * /*context*/)
initPrivateData(th);
}

// Public thinker callback.
th->function(th);

// Private thinking.
if(th->d) THINKER_DATA(*th, Thinker::IData).think();
}

return false; // Continue iteration.
Expand Down
7 changes: 7 additions & 0 deletions doomsday/libdoomsday/include/doomsday/world/thinker.h
Expand Up @@ -67,6 +67,12 @@ LIBDOOMSDAY_PUBLIC dd_bool Thinker_InStasis(thinker_t const *thinker);
*/
LIBDOOMSDAY_PUBLIC void Thinker_SetStasis(thinker_t *thinker, dd_bool on);

/**
* Generic thinker function that does nothing. This can be used if the private
* data does all the thinking.
*/
LIBDOOMSDAY_PUBLIC void Thinker_NoOperation(void *thinker);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down Expand Up @@ -123,6 +129,7 @@ class LIBDOOMSDAY_PUBLIC Thinker
public:
virtual ~IData() {}
virtual void setThinker(thinker_s *thinker) = 0;
virtual void think() = 0;
virtual IData *duplicate() const = 0;

DENG2_AS_IS_METHODS()
Expand Down
1 change: 1 addition & 0 deletions doomsday/libdoomsday/include/doomsday/world/thinkerdata.h
Expand Up @@ -40,6 +40,7 @@ class LIBDOOMSDAY_PUBLIC ThinkerData : public Thinker::IData
ThinkerData(ThinkerData const &other);

void setThinker(thinker_s *thinker);
void think();
IData *duplicate() const;

thinker_s &thinker();
Expand Down
14 changes: 12 additions & 2 deletions doomsday/libdoomsday/src/world/thinker.cpp
Expand Up @@ -111,12 +111,18 @@ DENG2_PIMPL_NOREF(Thinker)
Thinker::Thinker(dsize sizeInBytes, IData *data)
: d(new Instance(AllocateStandard, sizeInBytes, data))
, STRUCT_MEMBER_ACCESSORS()
{}
{
// Default to no public thinker callback.
function = Thinker_NoOperation;
}

Thinker::Thinker(AllocMethod alloc, dsize sizeInBytes, Thinker::IData *data)
: d(new Instance(alloc, sizeInBytes, data))
, STRUCT_MEMBER_ACCESSORS()
{}
{
// Default to no public thinker callback.
function = Thinker_NoOperation;
}

Thinker::Thinker(Thinker const &other)
: d(new Instance(*other.d))
Expand Down Expand Up @@ -275,3 +281,7 @@ void Thinker_SetStasis(thinker_t *thinker, dd_bool on)
}
}

void Thinker_NoOperation(void *)
{
// do nothing
}
5 changes: 5 additions & 0 deletions doomsday/libdoomsday/src/world/thinkerdata.cpp
Expand Up @@ -58,6 +58,11 @@ void ThinkerData::setThinker(thinker_s *thinker)
d->think = thinker;
}

void ThinkerData::think()
{
/// @todo If there is a think function in the Record, call it now. -jk
}

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

0 comments on commit 26ed217

Please sign in to comment.