1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Base class for objects that are kept in synch between client and server
//
//=============================================================================
#ifndef SHAREDOBJECTCACHE_H
#define SHAREDOBJECTCACHE_H
#ifdef _WIN32
#pragma once
#endif
#include "sharedobject.h"
namespace GCSDK
{
//----------------------------------------------------------------------------
// Purpose: The part of a shared object cache that handles all objects of a
// single type.
//----------------------------------------------------------------------------
class CSharedObjectTypeCache
{
public:
CSharedObjectTypeCache( int nTypeID );
virtual ~CSharedObjectTypeCache();
int GetTypeID() const { return m_nTypeID; }
uint32 GetCount() const { return m_vecObjects.Count(); }
CSharedObject *GetObject( uint32 nObj ) { return m_vecObjects[nObj]; }
const CSharedObject *GetObject( uint32 nObj ) const { return m_vecObjects[nObj]; }
virtual bool AddObject( CSharedObject *pObject );
virtual bool AddObjectClean( CSharedObject *pObject );
virtual CSharedObject *RemoveObject( const CSharedObject & soIndex );
CSharedObject *RemoveObjectByIndex( uint32 nObj );
void DestroyAllObjects();
void RemoveAllObjectsWithoutDeleting();
virtual void EnsureCapacity( uint32 nItems );
CSharedObject *FindSharedObject( const CSharedObject & soIndex );
virtual void Dump() const;
#ifdef DBGFLAG_VALIDATE
virtual void Validate( CValidator &validator, const char *pchName );
#endif
private:
int FindSharedObjectIndex( const CSharedObject & soIndex ) const;
void AddObjectInternal( CSharedObject *pObject );
CSharedObjectVec m_vecObjects;
int m_nTypeID;
};
//----------------------------------------------------------------------------
// Purpose: A cache of a bunch of shared objects of different types. This class
// is shared between clients, gameservers, and the GC and is
// responsible for sending messages from the GC to cause object
// creation/destruction/updating on the clients/gameservers.
//----------------------------------------------------------------------------
class CSharedObjectCache
{
public:
CSharedObjectCache();
virtual ~CSharedObjectCache();
virtual const CSteamID & GetOwner() const = 0;
bool AddObject( CSharedObject *pSharedObject );
bool BDestroyObject( const CSharedObject & soIndex, bool bRemoveFromDatabase );
CSharedObject *RemoveObject( const CSharedObject & soIndex );
void RemoveAllObjectsWithoutDeleting();
//called to find the type cache for the specified class ID. This will return NULL if one does not exist
CSharedObjectTypeCache *FindBaseTypeCache( int nClassID ) const;
//called to create the specified class ID. If one exists, this is the same as find, otherwise one will be constructed
CSharedObjectTypeCache *CreateBaseTypeCache( int nClassID );
CSharedObject *FindSharedObject( const CSharedObject & soIndex );
template < class T >
T *FindTypedSharedObject( const CSharedObject &soIndex )
{
return assert_cast<T *>( FindSharedObject( soIndex ) );
}
// returns various singleton objects
template< typename SOClass_t >
SOClass_t *GetSingleton() const
{
CSharedObjectTypeCache *pTypeCache = FindBaseTypeCache( SOClass_t::k_nTypeID );
if ( pTypeCache )
{
AssertMsg2( pTypeCache->GetCount() == 0 || pTypeCache->GetCount() == 1, "GetSingleton() called on type %u that has invalid number of items %u.", SOClass_t::k_nTypeID, pTypeCache->GetCount() );
if ( pTypeCache->GetCount() == 1 )
{
return (SOClass_t *)pTypeCache->GetObject( 0 );
}
}
return NULL;
}
void SetVersion( uint64 ulVersion ) { m_ulVersion = ulVersion; }
uint64 GetVersion() const { return m_ulVersion; }
virtual void MarkDirty() {}
virtual void Dump() const;
#ifdef DBGFLAG_VALIDATE
virtual void Validate( CValidator &validator, const char *pchName );
#endif
protected:
virtual CSharedObjectTypeCache *AllocateTypeCache( int nClassID ) const = 0;
CSharedObjectTypeCache *GetTypeCacheByIndex( int nIndex ) { return m_mapObjects.IsValidIndex( nIndex ) ? m_mapObjects.Element( nIndex ) : NULL; }
int GetTypeCacheCount() const { return m_mapObjects.MaxElement(); }
int FirstTypeCacheIndex() { return m_mapObjects.FirstInorder(); }
int NextTypeCacheIndex( int iCurrent ) { return m_mapObjects.NextInorder( iCurrent ); }
int InvalidTypeCacheIndex() { return m_mapObjects.InvalidIndex(); }
uint64 m_ulVersion;
private:
CUtlMap<int, CSharedObjectTypeCache *> m_mapObjects;
};
} // namespace GCSDK
#endif //SHAREDOBJECTCACHE_H
|