[CVS Testing] Adding entity-base files.

This was SVN commit r147.
This commit is contained in:
MarkT 2004-01-20 01:51:06 +00:00
parent c66485e08f
commit 8536a35c8c
4 changed files with 449 additions and 2 deletions

View File

@ -40,8 +40,8 @@ TDD at http://forums.wildfiregames.com/0ad
*/
#ifndef Config_H
#define Config_H
#ifndef CONFIG_INCLUDED
#define CONFIG_INCLUDED
//--------------------------------------------------------
// Includes / Compiler directives

301
source/ps/Entity.cpp Executable file
View File

@ -0,0 +1,301 @@
// Last modified: 19 January 2004 (Mark Thompson)
#include "entity.h"
//--------------------------------------------------------
// CEntity: Entity class
//--------------------------------------------------------
//--------------------------------------------------------
// CEntity::CEntity()
//--------------------------------------------------------
CEntity::CEntity()
{
}
//--------------------------------------------------------
// CEntity::~CEntity()
//--------------------------------------------------------
CEntity::~CEntity()
{
}
//--------------------------------------------------------
// CEntity::Dispatch( CEvent* ev )
//
// Handles the event ev, sent by the scheduler
//--------------------------------------------------------
void CEntity::Dispatch( CEvent* ev )
{
}
//--------------------------------------------------------
// CHandle: Stores entity refcounts, etc.
//--------------------------------------------------------
//--------------------------------------------------------
// CHandle::CHandle()
//--------------------------------------------------------
CHandle::CHandle()
{
ptr = NULL;
refcount = 0;
}
//--------------------------------------------------------
// CHandle::~CHandle()
//--------------------------------------------------------
CHandle::~CHandle()
{
if( refcount )
{
// WARNING: RELEASING LIVE HANDLE!
// TODO: Link this into the new logfile system.
}
delete ptr;
}
//--------------------------------------------------------
// CHandlePool: Tracks entity handles.
//--------------------------------------------------------
//--------------------------------------------------------
// CHandlePool::CHandlePool()
//--------------------------------------------------------
CHandlePool::CHandlePool()
{
for( unsigned short t = 0; t < MAX_HANDLES; t++ )
{
Pool[t].nextfree = t + 1;
}
FreeHandle = 0;
}
//--------------------------------------------------------
// CHandlePool::~CHandlePool()
//--------------------------------------------------------
CHandlePool::~CHandlePool()
{
//Terminating, clearing remaining handles...
}
//--------------------------------------------------------
// CHandlePool::Assign( CEntity* ptr )
//--------------------------------------------------------
HEntity CHandlePool::Assign( CEntity* ptr )
{
assert( FreeHandle != BAD_HANDLE );
unsigned short i = FreeHandle;
ptr->Handle = HEntityWeak( i );
FreeHandle = Pool[i].nextfree;
Pool[i].ptr = ptr;
return HEntity( i );
}
//--------------------------------------------------------
// CHandlePool::Destroy( unsigned short index )
//--------------------------------------------------------
void CHandlePool::Destroy( unsigned short index )
{
//Garbage collection
delete Pool[index].ptr;
Pool[index].nextfree = FreeHandle;
Pool[index].refcount = 0;
FreeHandle = index;
}
//--------------------------------------------------------
// HEntity: Refcounted handle to entities.
//--------------------------------------------------------
//--------------------------------------------------------
// HEntity::HEntity()
//--------------------------------------------------------
HEntity::HEntity()
{
index = BAD_HANDLE;
}
//--------------------------------------------------------
// HEntity::HEntity( unsigned short index )
//--------------------------------------------------------
HEntity::HEntity( unsigned short _index )
{
index = _index;
AddRef();
}
//--------------------------------------------------------
// HEntity::HEntity( const HEntity& copy )
//--------------------------------------------------------
HEntity::HEntity( const HEntity& copy )
{
index = copy.index;
AddRef();
}
//--------------------------------------------------------
// HEntity::HEntity( const HEntityWeak& copy )
//--------------------------------------------------------
HEntity::HEntity( const HEntityWeak& copy )
{
index = copy.index;
AddRef();
}
//--------------------------------------------------------
// HEntity::AddRef()
//
// Increments reference count
//--------------------------------------------------------
void HEntity::AddRef()
{
if( index == BAD_HANDLE ) return;
g_HandlePool.Pool[index].refcount++;
}
//--------------------------------------------------------
// HEntity::DecRef()
//
// Decrements reference count
//--------------------------------------------------------
void HEntity::DecRef()
{
if( index == BAD_HANDLE ) return;
if( !g_HandlePool.Pool[index].refcount ) return;
if( --g_HandlePool.Pool[index].refcount == 0 )
g_HandlePool.Destroy( index );
}
//--------------------------------------------------------
// HEntity::operator=( const HEntity& copy )
//--------------------------------------------------------
void HEntity::operator=( const HEntity& copy )
{
DecRef();
index = copy.index;
AddRef();
}
//--------------------------------------------------------
// HEntity::operator=( const HEntityWeak& copy )
//--------------------------------------------------------
void HEntity::operator=( const HEntityWeak& copy )
{
DecRef();
index = copy.index;
AddRef();
}
//--------------------------------------------------------
// HEntity::operator*()
//--------------------------------------------------------
CEntity& HEntity::operator*()
{
assert( index != BAD_HANDLE );
assert( g_HandlePool.Pool[index].ptr );
return( *g_HandlePool.Pool[index].ptr );
}
//--------------------------------------------------------
// HEntity: operator->()
//--------------------------------------------------------
CEntity* HEntity::operator->()
{
assert( index != BAD_HANDLE );
assert( g_HandlePool.Pool[index].ptr );
return( g_HandlePool.Pool[index].ptr );
}
//--------------------------------------------------------
// HEntity::~HEntity()
//--------------------------------------------------------
HEntity::~HEntity()
{
DecRef();
}
//--------------------------------------------------------
// HEntityWeak: Like HEntity, but it isn't refcounted.
// used mainly to hold references-to-self.
// It's slightly faster, and automatically
// converts to a safe HEntity when needed.
//--------------------------------------------------------
//--------------------------------------------------------
// HEntityWeak::HEntityWeak()
//--------------------------------------------------------
HEntityWeak::HEntityWeak()
{
index = BAD_HANDLE;
}
//--------------------------------------------------------
// HEntityWeak::HEntityWeak( unsigned short _index )
//--------------------------------------------------------
HEntityWeak::HEntityWeak( unsigned short _index )
{
index = _index;
}
//--------------------------------------------------------
// HEntityWeak::operator=( const HEntityWeak& copy )
//--------------------------------------------------------
void HEntityWeak::operator=( const HEntityWeak& copy )
{
index = copy.index;
}
//--------------------------------------------------------
// HEntityWeak::operator*()
//--------------------------------------------------------
CEntity& HEntityWeak::operator*()
{
assert( index != BAD_HANDLE );
assert( g_HandlePool.Pool[index].ptr );
return( *g_HandlePool.Pool[index].ptr );
}
//--------------------------------------------------------
// HEntityWeak::operator->()
//--------------------------------------------------------
CEntity* HEntityWeak::operator->()
{
assert( index != BAD_HANDLE );
assert( g_HandlePool.Pool[index].ptr );
return( *g_HandlePool.Pool[index].ptr );
}
//--------------------------------------------------------
// HEntityWeak::operator HEntity()
//--------------------------------------------------------
HEntityWeak::operator HEntity()
{
return *( new HEntity( index ) );
}

130
source/ps/Entity.h Executable file
View File

@ -0,0 +1,130 @@
/*
Entity.h
Entities and associated classes
Mark Thompson (markt@0ad.wildfiregames.com)
Last modified: 19 January 2004 (Mark Thompson)
--Overview--
Contains the class definitions for entities, netsafe entity handles, handle manager
--Usage--
TODO
--Examples--
TODO
--More info--
TDD at http://forums.wildfiregames.com/0ad
*/
#ifndef ENTITY_INCLUDED
#define ENTITY_INCLUDED
#include "assert.h"
#include "stdio.h"
#include "ps/singleton.h"
// Handle defines
#define MAX_HANDLES 8192
#define BAD_HANDLE MAX_HANDLES
#define NULL 0
typedef unsigned long DWORD;
// Forward-declarations
class CHandlePool;
class CEntity;
class HEntity;
class HEntityWeak;
// CEntity: Entity class
class CEntity
{
friend CHandlePool;
private:
HEntityWeak Handle; // Reference to self, to include in messages, etc.
// Weak handle isn't refcounted, but is as soon
// as it gets passed out of this class.
public:
CEntity();
~CEntity();
void Dispatch( CEvent* ev );
}
// CHandle: Entity handles, used by manager class
class CHandle
{
friend CHandlePool;
private:
CEntity* ptr;
unsigned short refcount;
unsigned short nextfree; //Linked list for free handles
CHandle();
~CHandle();
};
// CHandlePool: Tracks entity handles
#define g_HandlePool CHandlePool::GetSingleton()
class CHandlePool : public Singleton<CHandlePool>
{
friend HEntity;
private:
unsigned short FreeHandle;
CHandle Pool[MAX_HANDLES];
public:
CHandlePool();
~CHandlePool();
HEntity Assign( CEntity* ptr );
HEntity Create() { return Assign( new CEntity ); };
void Destroy( unsigned short index );
};
// HEntity: Entity smart pointer
class HEntity
{
friend CHandlePool;
private:
unsigned short index;
HEntity( unsigned short index );
void AddRef();
void DecRef();
public:
CEntity& operator*();
CEntity* operator->();
HEntity();
HEntity( const HEntity& copy );
HEntity( const HEntityWeak& copy );
void operator=( const HEntity& copy );
void operator=( const HEntityWeak& copy );
~HEntity();
};
// HEntityWeak: Weak (non-refcounted) smart pointer
class HEntityWeak
{
friend CHandlePool;
private:
unsigned short index;
HEntityWeak( unsigned short index );
public:
CEntity& operator*();
CEntity* operator->();
};
#endif // !defined( ENTITY_INCLUDED )

View File

@ -583,10 +583,26 @@ SOURCE=..\ps\Encryption.h
# End Source File
# Begin Source File
SOURCE=.\Entity.cpp
# End Source File
# Begin Source File
SOURCE=..\ps\Entity.h
# End Source File
# Begin Source File
SOURCE=..\ps\Error.h
# End Source File
# Begin Source File
SOURCE=..\ps\Event.cpp
# End Source File
# Begin Source File
SOURCE=..\ps\Event.h
# End Source File
# Begin Source File
SOURCE=..\ps\LogFile.cpp
# End Source File
# Begin Source File