Repository navigation
Expand file tree
/
Copy pathTypeInfo.h
More file actions
133 lines (114 loc) · 3.41 KB
/
Copy pathTypeInfo.h
File metadata and controls
133 lines (114 loc) · 3.41 KB
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
/*
* TypeInfo.h
*
* Copyright (c) 2015 Lukas Hermanns. All rights reserved.
* Licensed under the terms of the BSD 3-Clause license (see LICENSE.txt).
*/
#ifndef LLGL_TYPE_INFO_H
#define LLGL_TYPE_INFO_H
#include <LLGL/Interface.h>
namespace LLGL
{
/**
\brief Returns whether the specified object is an instance of the interface specified by template parameter <T>.
\remarks The typename \c T must be a sub class of Interface and implement the following function:
\code
static InterfaceID GetInterfaceID();
\endcode
\see Interface::IsInstanceOf
*/
template <typename T>
inline bool IsInstanceOf(const Interface* obj)
{
return (obj != nullptr && obj->IsInstanceOf(T::GetInterfaceID()));
}
/**
\brief Const-reference version of IsInstanceOf.
\see IsInstanceOf(const Interface*)
*/
template <typename T>
inline bool IsInstanceOf(const Interface& obj)
{
return obj.IsInstanceOf(T::GetInterfaceID());
}
/**
\brief Returns a constant pointer to this instance of type <code>const T*</code> if it implements the specified interface.
\remarks The typename \c T must be a sub class of Interface and implement the following function:
\code
static InterfaceID GetInterfaceID();
\endcode
\remarks This template can be used like in the following example:
\code
const LLGL::Surface* mySurface = ...
if (const LLGL::Window* myWindow = LLGL::CastTo<LLGL::Window>(mySurface))
{
...
}
\endcode
*/
template <typename T>
inline const T* CastTo(const Interface* obj)
{
return (IsInstanceOf<T>(obj) ? static_cast<const T*>(obj) : nullptr);
}
/**
\brief Returns a pointer to this instance of type <code>T*</code> if it implements the specified interface.
\remarks The typename \c T must be a sub class of Interface and implement the following function:
\code
static InterfaceID GetInterfaceID();
\endcode
\remarks This template can be used like in the following example:
\code
LLGL::Surface* mySurface = ...
if (LLGL::Window* myWindow = LLGL::CastTo<LLGL::Window>(mySurface))
{
...
}
\endcode
*/
template <typename T>
inline T* CastTo(Interface* obj)
{
return (IsInstanceOf<T>(obj) ? static_cast<T*>(obj) : nullptr);
}
/**
\brief Returns a constant reference to this instance of type <code>const T&</code> if it implements the specified interface.
\remarks The typename \c T must be a sub class of Interface and implement the following function:
\code
static InterfaceID GetInterfaceID();
\endcode
\remarks This template can be used like in the following example:
\code
const LLGL::Surface& mySurface = ...
const LLGL::Window& myWindow = LLGL::CastTo<LLGL::Window>(mySurface);
\endcode
*/
template <typename T>
inline const T& CastTo(const Interface& obj)
{
if (!IsInstanceOf<T>(obj))
throw std::bad_cast();
return static_cast<const T&>(obj);
}
/**
\brief Returns a reference to this instance of type <code>T&</code> if it implements the specified interface.
\remarks The typename \c T must be a sub class of Interface and implement the following function:
\code
static InterfaceID GetInterfaceID();
\endcode
\remarks This template can be used like in the following example:
\code
LLGL::Surface& mySurface = ...
LLGL::Window& myWindow = LLGL::CastTo<LLGL::Window&>(mySurface);
\endcode
*/
template <typename T>
inline T& CastTo(Interface& obj)
{
if (!IsInstanceOf<T>(obj))
throw std::bad_cast();
return static_cast<T&>(obj);
}
} // /namespace LLGL
#endif
// ================================================================================