Pyrogenesis  trunk
CmpPtr.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_CMPPTR
19 #define INCLUDED_CMPPTR
20 
22 
23 class CSimContext;
24 class CSimulation2;
25 class IComponent;
26 
27 // Helper functions for CmpPtr
28 IComponent* QueryInterface(const CSimContext& context, entity_id_t ent, int iid);
29 IComponent* QueryInterface(const CSimulation2& simulation, entity_id_t ent, int iid);
30 
31 /**
32  * A simplified syntax for accessing entity components.
33  * E.g. to get the @c Position component, write:
34  *
35  * @code
36  * CmpPtr<ICmpPosition> cmpPosition(context, ent);
37  * if (!cmpPosition)
38  * // do something (maybe just silently abort; you should never crash if the
39  * // component is missing, even if you're sure it should never be missing)
40  * @endcode
41  *
42  * where @c context is (if you're writing component code) a CSimContext object, or
43  * (if you're writing external engine code that makes use of the simulation system)
44  * a CSimulation2 object; and @c ent is the entity ID.
45  *
46  * @c ent can be CComponentManager::SYSTEM_ENTITY (if you're writing a component), or
47  * CSimulation2::SYSTEM_ENTITY (for external code), if you want to access the global
48  * singleton system components.
49  *
50  * You should never hold onto a component pointer outside of the method in which you acquire
51  * it, because it might get deleted and invalidate your pointer. (Components will never be
52  * deleted while inside a simulation method.)
53  */
54 template<typename T>
55 class CmpPtr
56 {
57 private:
58  T* m;
59 
60 public:
61  CmpPtr(const CSimContext& context, entity_id_t ent)
62  {
63  m = static_cast<T*>(QueryInterface(context, ent, T::GetInterfaceId()));
64  }
65 
66  CmpPtr(const CSimulation2& simulation, entity_id_t ent)
67  {
68  m = static_cast<T*>(QueryInterface(simulation, ent, T::GetInterfaceId()));
69  }
70 
72  {
74  if (cache != NULL && T::GetInterfaceId() < (int)cache->numInterfaces)
75  m = static_cast<T*>(cache->interfaces[T::GetInterfaceId()]);
76  else
77  m = NULL;
78  }
79 
80  T* operator->() { return m; }
81 
82  explicit operator bool() const
83  {
84  return m != NULL;
85  }
86 };
87 
88 #endif // INCLUDED_CMPPTR
IComponent * interfaces[1]
Definition: Entity.h:65
Definition: IComponent.h:32
size_t numInterfaces
Definition: Entity.h:64
CmpPtr(const CSimulation2 &simulation, entity_id_t ent)
Definition: CmpPtr.h:66
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition: Entity.h:79
Contains pointers to various &#39;global&#39; objects that are needed by the simulation code, to allow easy access without using real (evil) global variables.
Definition: SimContext.h:32
Public API for simulation system.
Definition: Simulation2.h:46
CmpPtr(CEntityHandle ent)
Definition: CmpPtr.h:71
#define T(string_literal)
Definition: secure_crt.cpp:77
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:55
SEntityComponentCache * GetComponentCache() const
Definition: Entity.h:89
Definition: Entity.h:62
CmpPtr(const CSimContext &context, entity_id_t ent)
Definition: CmpPtr.h:61
IComponent * QueryInterface(const CSimContext &context, entity_id_t ent, int iid)
Definition: CmpPtr.cpp:26
T * operator->()
Definition: CmpPtr.h:80
T * m
Definition: CmpPtr.h:58
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23