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
23class CSimContext;
24class CSimulation2;
25class IComponent;
26
27// Helper functions for CmpPtr
28IComponent* QueryInterface(const CSimContext& context, entity_id_t ent, int iid);
29IComponent* 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 */
54template<typename T>
55class CmpPtr
56{
57private:
58 T* m;
59
60public:
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 * QueryInterface(const CSimContext &context, entity_id_t ent, int iid)
Definition: CmpPtr.cpp:26
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition: Entity.h:80
SEntityComponentCache * GetComponentCache() const
Definition: Entity.h:89
Contains pointers to various 'global' objects that are needed by the simulation code,...
Definition: SimContext.h:33
Public API for simulation system.
Definition: Simulation2.h:47
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:56
CmpPtr(const CSimulation2 &simulation, entity_id_t ent)
Definition: CmpPtr.h:66
T * operator->()
Definition: CmpPtr.h:80
CmpPtr(const CSimContext &context, entity_id_t ent)
Definition: CmpPtr.h:61
T * m
Definition: CmpPtr.h:58
CmpPtr(CEntityHandle ent)
Definition: CmpPtr.h:71
Definition: IComponent.h:33
#define T(string_literal)
Definition: secure_crt.cpp:77
u32 entity_id_t
Entity ID type.
Definition: Entity.h:29
Definition: Entity.h:63
size_t numInterfaces
Definition: Entity.h:64
IComponent * interfaces[1]
Definition: Entity.h:65