Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / server / src / gpm_service / gpm_utilities.h
blobb821038eddac600feafabe16ef5d739be9de442f
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef RY_GPM_UTILITIES_H
20 #define RY_GPM_UTILITIES_H
22 #include "nel/misc/types_nl.h"
23 #include "nel/misc/time_nl.h"
24 #include "nel/misc/block_memory.h"
25 #include "nel/pacs/u_move_container.h"
26 #include "nel/pacs/u_move_primitive.h"
27 #include "nel/pacs/u_collision_desc.h"
28 #include "nel/pacs/u_global_retriever.h"
29 #include "nel/net/message.h"
31 #include "game_share/ryzom_entity_id.h"
32 #include "game_share/player_vision_delta.h"
34 #include <map>
35 #include <set>
36 #include <vector>
37 #include <list>
39 class CCell;
40 class CWorldEntity;
41 class CWorldPositionManager;
42 class ConstIteratorType;
43 class CPlayerInfos;
47 * Misc. data structures (essentially vision data)
50 /**
51 * Front end vision data to be sent
53 class CFrontEndData
55 public:
56 CFrontEndData() : Message("",false),
57 MessageHeaderSize(0),
58 NumPlayers(0),
59 CurrentVisionsAtTick(0),
60 MaxVisionsPerTick(0),
61 VisionIn(0),
62 VisionOut(0),
63 VisionReplace(0)
67 NLNET::CMessage Message;
68 sint32 MessageHeaderSize;
69 sint32 NumPlayers;
70 sint32 CurrentVisionsAtTick;
71 sint32 MaxVisionsPerTick;
72 sint32 VisionIn;
73 sint32 VisionOut;
74 sint32 VisionReplace;
77 /**
78 * Entities around vision data to be sent
80 struct CServiceData
82 CServiceData() : Message("",false) {} // always an output message
83 NLNET::CMessage Message;
84 sint32 MessageHeaderSize;
87 typedef std::map<NLNET::TServiceId, CFrontEndData> TMapFrontEndData;
88 typedef std::map<NLNET::TServiceId, CServiceData> TMapServiceData;
90 typedef std::list< CPlayerInfos* > TPlayerList;
94 * Utitility classes
97 /**
98 * A list of object that must have Next and Previous pointers
99 * \param T the type of item
100 * \param TPtr a pointer to T to use in list (useful for smartpointer)
102 template<class T, class TPtr = T*>
103 class CObjectList
105 public:
106 TPtr Head;
107 TPtr Tail;
109 CObjectList() : Head(NULL), Tail(NULL) {}
111 void insertAtHead(T *object)
113 nlassert(object->Next == NULL);
114 nlassert(object->Previous == NULL);
116 object->Next = Head;
117 if (object->Next != NULL)
118 object->Next->Previous = object;
119 Head = object;
122 void insertAtTail(T *object)
124 nlassert(object->Next == NULL);
125 nlassert(object->Previous == NULL);
127 object->Previous = Tail;
128 if (object->Previous != NULL)
129 object->Previous->Next = object;
130 Tail = object;
133 void remove(T *object)
135 // if object at head
136 if (object->Previous == NULL)
137 Head = object->Next;
138 else
139 object->Previous->Next = object->Next;
141 // if object at tail
142 if (object->Next == NULL)
143 Tail = object->Previous;
144 else
145 object->Next->Previous = object->Previous;
147 object->Previous = NULL;
148 object->Next = NULL;
151 T *getHead() { return (T*)Head; }
152 T *getTail() { return (T*)Tail; }
156 * A little stack implementation, for really fast push_back/pop_back
158 template<class T, uint stackSize>
159 class CUnsafeConstantSizeStack
161 private:
162 T _Array[stackSize];
163 T *_Top;
165 public:
166 CUnsafeConstantSizeStack() { _Top = _Array; }
168 void push_back(const T &o = T()) { *(_Top++) = o; }
169 void pop_back() { nlassert(_Top > _Array); --_Top; }
170 //T &front() { return _Array[0]; }
171 T &back() { nlassert(_Top > _Array); return *(_Top-1); }
172 uint size() { return (uint)(_Top-_Array); }
173 bool empty() { return _Top == _Array; }
174 void clear() { _Top = _Array; }
178 * The same class, but uses stl vectors instead, and should be saffer
180 template<class T, uint stackSize>
181 class CSafeConstantSizeStack : public std::vector<T>
183 public:
184 CSafeConstantSizeStack() { this->reserve(stackSize); }
191 * Simple smart pointers, doesn't delete object when no reference
192 * Instanciation class T must have a RefCounter attribute
194 template<class T>
195 class CSimpleSmartPointer
197 private:
198 T *_Ptr;
200 public:
201 CSimpleSmartPointer() : _Ptr(NULL) {}
202 CSimpleSmartPointer(T* ptr) : _Ptr(NULL) { *this = ptr; }
203 CSimpleSmartPointer(const CSimpleSmartPointer &ptr) : _Ptr(NULL) { *this = ptr; }
204 ~CSimpleSmartPointer() { *this = NULL; }
206 T * operator = (T *ptr)
208 if (ptr != NULL)
209 ++(ptr->RefCounter);
210 if (_Ptr != NULL)
212 nlassert(_Ptr->RefCounter > 0);
213 --(_Ptr->RefCounter);
215 _Ptr = ptr;
216 return _Ptr;
219 T * operator = (const CSimpleSmartPointer &ptr)
221 *this = ptr._Ptr;
222 return _Ptr;
225 T & operator * () { return *_Ptr; }
226 T * operator -> () { return _Ptr; }
227 const T & operator * () const { return *_Ptr; }
228 const T * operator -> () const { return _Ptr; }
229 bool operator == (const T *ptr) const { return ptr == _Ptr; }
230 bool operator != (const T *ptr) const { return ptr != _Ptr; }
231 bool operator == (const CSimpleSmartPointer &ptr) const { return ptr._Ptr == _Ptr; }
232 bool operator != (const CSimpleSmartPointer &ptr) const { return ptr._Ptr != _Ptr; }
233 operator T* () const { return _Ptr; }
238 #endif // RY_GPM_UTILITIES_H
240 /* End of gpm_utilities.h */