Odamex
Setting the Standard in Multiplayer Doom
common/dthinker.h
Go to the documentation of this file.
00001 // Emacs style mode select   -*- C++ -*- 
00002 //-----------------------------------------------------------------------------
00003 //
00004 // $Id: dthinker.h 1788 2010-08-24 04:42:57Z russellrice $
00005 //
00006 // Copyright (C) 1998-2006 by Randy Heit (ZDoom).
00007 // Copyright (C) 2006-2010 by The Odamex Team.
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // DESCRIPTION:
00020 //      MapObj data. Map Objects or mobjs are actors, entities,
00021 //      thinker, take-your-pick... anything that moves, acts, or
00022 //      suffers state changes of more or less violent nature.
00023 //
00024 //-----------------------------------------------------------------------------
00025 
00026 
00027 #ifndef __DTHINKER_H__
00028 #define __DTHINKER_H__
00029 
00030 #include <stdlib.h>
00031 #include "dobject.h"
00032 
00033 class AActor;
00034 class player_s;
00035 struct pspdef_s;
00036 
00037 typedef void (*actionf_v)();
00038 typedef void (*actionf_p1)( AActor* );
00039 typedef void (*actionf_p2)( player_s*, pspdef_s* );
00040 
00041 typedef union
00042 {
00043         void *acvoid;
00044         actionf_p1      acp1;
00045         actionf_v       acv;
00046         actionf_p2      acp2;
00047 } actionf_t;
00048 
00049 // Historically, "think_t" is yet another
00050 // function pointer to a routine to handle an actor
00051 typedef actionf_t  think_t;
00052 
00053 class FThinkerIterator;
00054 
00055 // Doubly linked list of thinkers
00056 class DThinker : public DObject
00057 {
00058         DECLARE_SERIAL (DThinker, DObject)
00059 
00060 public:
00061         DThinker ();
00062         virtual void Destroy ();
00063         virtual ~DThinker ();
00064         virtual void RunThink () {}
00065 
00066         void *operator new (size_t size);
00067         void operator delete (void *block);
00068 
00069         // Both the head and tail of the thinker list.
00070         static DThinker *FirstThinker;
00071         static DThinker *LastThinker;
00072         static void RunThinkers ();
00073         static void DestroyAllThinkers ();
00074         static void DestroyMostThinkers ();
00075         static void SerializeAll (FArchive &arc, bool keepPlayers);
00076 
00077         bool WasDestroyed();
00078 
00079         size_t refCount;
00080         
00081 private:
00082         DThinker *m_Next, *m_Prev;
00083         bool destroyed;
00084 
00085         friend class FThinkerIterator;
00086 };
00087 
00088 class FThinkerIterator
00089 {
00090 private:
00091         TypeInfo *m_ParentType;
00092         DThinker *m_CurrThinker;
00093 
00094 public:
00095         FThinkerIterator (TypeInfo *type)
00096         {
00097                 m_ParentType = type;
00098                 m_CurrThinker = DThinker::FirstThinker;
00099         }
00100         DThinker *Next ()
00101         {
00102                 while (m_CurrThinker)
00103                 {
00104                         if (m_CurrThinker->IsKindOf (m_ParentType))
00105                         {
00106                                 DThinker *res = m_CurrThinker;
00107                                 m_CurrThinker = m_CurrThinker->m_Next;
00108                                 return res;
00109                         }
00110                         m_CurrThinker = m_CurrThinker->m_Next;
00111                 }
00112                 m_CurrThinker = DThinker::FirstThinker;
00113                 return NULL;
00114         }
00115 };
00116 
00117 template <class T> class TThinkerIterator : public FThinkerIterator
00118 {
00119 public:
00120         TThinkerIterator () : FThinkerIterator (RUNTIME_CLASS(T))
00121         {
00122         }
00123         T *Next ()
00124         {
00125                 return static_cast<T *>(FThinkerIterator::Next ());
00126         }
00127 };
00128 
00129 #endif //__DTHINKER_H__
00130 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends