Pyrogenesis
13997
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
source
simulation2
system
CmpPtr.h
Go to the documentation of this file.
1
/* Copyright (C) 2012 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
21
#include "
simulation2/system/Entity.h
"
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
// "Safe Bool Idiom" based on http://www.artima.com/cppsource/safebool.html
61
// to allow "if (cmp)" and "if (!cmp)" etc, without also allowing "int i = cmp"
62
// or "if (cmp1 == cmp2)" etc.
63
typedef
void (
CmpPtr
::*
bool_type
)()
const
;
64
void
this_type_does_not_support_comparisons
()
const
{}
65
66
public
:
67
CmpPtr
(
const
CSimContext
& context,
entity_id_t
ent)
68
{
69
m
=
static_cast<
T
*
>
(
QueryInterface
(context, ent, T::GetInterfaceId()));
70
}
71
72
CmpPtr
(
const
CSimulation2
& simulation,
entity_id_t
ent)
73
{
74
m
=
static_cast<
T
*
>
(
QueryInterface
(simulation, ent, T::GetInterfaceId()));
75
}
76
77
CmpPtr
(
CEntityHandle
ent)
78
{
79
SEntityComponentCache
* cache = ent.
GetComponentCache
();
80
if
(cache != NULL && T::GetInterfaceId() < (
int
)cache->
numInterfaces
)
81
m
=
static_cast<
T
*
>
(cache->
interfaces
[T::GetInterfaceId()]);
82
else
83
m
= NULL;
84
}
85
86
T
*
operator->
() {
return
m
; }
87
88
operator
bool_type
()
const
89
{
90
return
(
m
!= NULL) ? &
CmpPtr::this_type_does_not_support_comparisons
: 0;
91
}
92
};
93
94
template
<
typename
T,
typename
U>
95
bool
operator!=
(
const
CmpPtr<T>
& lhs,
const
U
&
UNUSED
(rhs))
96
{
97
lhs.
this_type_does_not_support_comparisons
();
98
return
false
;
99
}
100
101
template
<
typename
T,
typename
U>
102
bool
operator==
(
const
CmpPtr<T>
& lhs,
const
U
&
UNUSED
(rhs))
103
{
104
lhs.
this_type_does_not_support_comparisons
();
105
return
false
;
106
}
107
108
#endif // INCLUDED_CMPPTR
SEntityComponentCache::interfaces
IComponent * interfaces[1]
Definition:
Entity.h:66
IComponent
Definition:
IComponent.h:33
SEntityComponentCache::numInterfaces
size_t numInterfaces
Definition:
Entity.h:65
Entity.h
UNUSED
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition:
code_annotation.h:38
CmpPtr::bool_type
void(CmpPtr::* bool_type)() const
Definition:
CmpPtr.h:63
CmpPtr::CmpPtr
CmpPtr(const CSimulation2 &simulation, entity_id_t ent)
Definition:
CmpPtr.h:72
operator==
bool operator==(const FCDJointWeightPair &a, const FCDJointWeightPair &b)
Definition:
GeomReindex.cpp:59
CEntityHandle
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition:
Entity.h:80
CSimContext
Contains pointers to various 'global' objects that are needed by the simulation code, to allow easy access without using real (evil) global variables.
Definition:
SimContext.h:32
CSimulation2
Public API for simulation system.
Definition:
Simulation2.h:46
CmpPtr::CmpPtr
CmpPtr(CEntityHandle ent)
Definition:
CmpPtr.h:77
QueryInterface
IComponent * QueryInterface(const CSimContext &context, entity_id_t ent, int iid)
Definition:
CmpPtr.cpp:26
T
#define T(string_literal)
Definition:
secure_crt.cpp:70
CmpPtr
A simplified syntax for accessing entity components.
Definition:
CmpPtr.h:55
SEntityComponentCache
Definition:
Entity.h:63
CmpPtr::CmpPtr
CmpPtr(const CSimContext &context, entity_id_t ent)
Definition:
CmpPtr.h:67
operator!=
NOTHROW_DEFINE bool operator!=(const AlignedAllocator< T1 > &, const AlignedAllocator< T2 > &)
Definition:
aligned_allocator.h:143
CmpPtr::operator->
T * operator->()
Definition:
CmpPtr.h:86
CmpPtr::m
T * m
Definition:
CmpPtr.h:58
CmpPtr::this_type_does_not_support_comparisons
void this_type_does_not_support_comparisons() const
Definition:
CmpPtr.h:64
x86_x64::CPUID2::U
Definition:
cache.cpp:318
entity_id_t
u32 entity_id_t
Entity ID type.
Definition:
Entity.h:24
CEntityHandle::GetComponentCache
SEntityComponentCache * GetComponentCache() const
Definition:
Entity.h:90
Generated on Mon Oct 14 2013 00:58:09 for Pyrogenesis by
1.8.5