glbinding  3.3.0.0
A C++ binding for the OpenGL API, generated using the gl.xml specification.
Loading...
Searching...
No Matches
Function.inl
Go to the documentation of this file.
1
2#pragma once
3
4
5#include <utility>
6#include <functional>
7#include <cassert>
8
9#include <glbinding/Value.h>
12
13#include <glbinding/Boolean8.h>
14
15
16
17namespace glbinding
18{
19
20
21template <typename ReturnType, typename... Arguments>
22struct BasicCallHelper
23{
24 inline static ReturnType call(const glbinding::Function<ReturnType, Arguments...> * function, Arguments&&... arguments)
25 {
26 return reinterpret_cast<typename glbinding::Function<ReturnType, Arguments...>::Signature>(function->address())(std::forward<Arguments>(arguments)...);
27 }
28};
29
30
31// Special case for booleans because of MSVC differing behavior
32
33
34template <typename... Arguments>
35struct BasicCallHelper<glbinding::Boolean8, Arguments...>
36{
37 inline static glbinding::Boolean8 call(const glbinding::Function<glbinding::Boolean8, Arguments...> * function, Arguments&&... arguments)
38 {
39 return reinterpret_cast<typename glbinding::Function<glbinding::Boolean8::underlying_type, Arguments...>::Signature>(function->address())(std::forward<Arguments>(arguments)...);
40 }
41};
42
43
44
45template <typename ReturnType, typename... Arguments>
47{
48 inline static ReturnType call(const glbinding::Function<ReturnType, Arguments...> * function, Arguments&&... arguments)
49 {
50 glbinding::FunctionCall functionCall(function);
51
53 {
54 functionCall.parameters = glbinding::createValues(std::forward<Arguments>(arguments)...);
55 }
56
58 {
59 AbstractFunction::before(functionCall);
60
61 if (function->beforeCallback())
62 {
63 function->beforeCallback()(std::forward<Arguments>(arguments)...);
64 }
65 }
66
67 auto value = BasicCallHelper<ReturnType, Arguments ...>::call(function, std::forward<Arguments>(arguments)...);
68
70 {
71 functionCall.returnValue = glbinding::createValue(value);
72 }
73
75 {
76 AbstractFunction::after(functionCall);
77
78 if (function->afterCallback())
79 {
80 function->afterCallback()(value, std::forward<Arguments>(arguments)...);
81 }
82 }
83
85 {
86 AbstractFunction::log(std::move(functionCall));
87 }
88
89 return value;
90 }
91};
92
93
94template <typename... Arguments>
95struct FunctionHelper<void, Arguments...>
96{
97 inline static void call(const glbinding::Function<void, Arguments...> * function, Arguments&&... arguments)
98 {
99 glbinding::FunctionCall functionCall(function);
100
102 {
103 functionCall.parameters = glbinding::createValues(std::forward<Arguments>(arguments)...);
104 }
105
107 {
108 AbstractFunction::before(functionCall);
110 if (function->beforeCallback())
111 {
112 function->beforeCallback()(std::forward<Arguments>(arguments)...);
113 }
114 }
115
116 BasicCallHelper<void, Arguments ...>::call(function, std::forward<Arguments>(arguments)...);
117
119 {
120 AbstractFunction::after(functionCall);
121
122 if (function->afterCallback())
123 {
124 function->afterCallback()(std::forward<Arguments>(arguments)...);
125 }
126 }
127
129 {
130 AbstractFunction::log(std::move(functionCall));
131 }
132 }
133};
134
135
136template <typename ReturnType, typename... Arguments>
138: AbstractFunction{_name}
139, m_beforeCallback{nullptr}
140, m_afterCallback{nullptr}
141{
142}
143
144template <typename ReturnType, typename... Arguments>
145ReturnType Function<ReturnType, Arguments...>::operator()(Arguments&... arguments) const
146{
147 return call(arguments...);
148}
149
150template <typename ReturnType, typename... Arguments>
151ReturnType Function<ReturnType, Arguments...>::call(Arguments&... arguments) const
152{
153 const auto myAddress = address();
154
155 if (myAddress == nullptr)
156 {
158 {
160 }
161 else
162 {
163 // Trying to call a function without check if it is resolvable is considered a programming error.
164 // You may try to call AbstractFunction::resolve first and check the address for validity (a pointer
165 // unequal to nullptr is considered valid) or check the exposition of associated extensions.
166 assert(false);
167 }
168
169 return ReturnType();
170 }
171
173 {
174 return FunctionHelper<ReturnType, Arguments...>::call(this, std::forward<Arguments>(arguments)...);
176 else
177 {
178 return BasicCallHelper<ReturnType, Arguments...>::call(this, std::forward<Arguments>(arguments)...);
179 }
180}
181
182template <typename ReturnType, typename... Arguments>
183ReturnType Function<ReturnType, Arguments...>::directCall(Arguments... arguments) const
185 if (address() == nullptr)
186 {
187 return ReturnType();
188 }
189
190 return BasicCallHelper<ReturnType, Arguments...>::call(this, std::forward<Arguments>(arguments)...);
191}
192
193template <typename ReturnType, typename... Arguments>
196 m_beforeCallback = std::move(callback);
197}
199template <typename ReturnType, typename... Arguments>
202 m_beforeCallback = nullptr;
203}
204
205template <typename ReturnType, typename... Arguments>
207{
208 m_afterCallback = std::move(callback);
209}
210
211template <typename ReturnType, typename... Arguments>
213{
214 m_afterCallback = nullptr;
215}
216
217template <typename ReturnType, typename... Arguments>
218typename Function<ReturnType, Arguments...>::BeforeCallback Function<ReturnType, Arguments...>::beforeCallback() const
219{
220 return m_beforeCallback;
221}
222
223template <typename ReturnType, typename... Arguments>
224typename Function<ReturnType, Arguments...>::AfterCallback Function<ReturnType, Arguments...>::afterCallback() const
225{
226 return m_afterCallback;
227}
228
229template <typename ReturnType, typename... Arguments>
234
235template <typename ReturnType, typename... Arguments>
237{
238 return pos > -1 && AbstractFunction::maxPos() <= pos;
239}
240
241template <typename ReturnType, typename... Arguments>
246
247template <typename ReturnType, typename... Arguments>
249{
250 assert(AbstractFunction::maxPos() >= pos);
251 assert(pos > -1);
252
253 return m_states.at(pos);
254}
255
256template <typename ReturnType, typename... Arguments>
258{
259 m_states.resize(static_cast<std::size_t>(count));
260}
261
262
263} // namespace glbinding
The AbstractFunction represents an OpenGL API function by its name and entry point after dynamic addr...
Definition AbstractFunction.h:30
static void unresolved(const AbstractFunction *function)
Call unresolved callback.
bool isAnyEnabled(CallbackMask mask) const
Check if any bit of the parameter is set in the currently configured callback mask of the current sta...
ProcAddress address() const
Get function pointer.
static void before(const FunctionCall &call)
Call before callback.
static int currentPos()
Get index of current state.
bool isEnabled(CallbackMask mask) const
Check if all bits of the parameter are set in the currently configured callback mask of the current s...
static int maxPos()
Get highest state index currently used.
static void log(FunctionCall &&call)
Call log callback.
static void after(const FunctionCall &call)
Call after callback.
The State struct represents the configuration of a single OpenGL function for one thread....
Definition AbstractState.h:21
Boolean type based on an 8-bit integer.
Definition Boolean8.h:20
unsigned char underlying_type
Type used for storing the value.
Definition Boolean8.h:22
A FunctionCall represents a function call of an OpenGL API function, including the parameter and retu...
Definition FunctionCall.h:27
std::vector< std::unique_ptr< AbstractValue > > parameters
The list of parameter values; doesn't have to be filled.
Definition FunctionCall.h:72
std::unique_ptr< AbstractValue > returnValue
The return value; doesn't have to be filled.
Definition FunctionCall.h:73
The Function represents an OpenGL API function with additional features.
Definition Function.h:78
void setAfterCallback(AfterCallback callback)
Register a callback that is triggered after a function call to the OpenGL driver.
Definition Function.inl:206
AfterCallback afterCallback() const
The accessor for the afterCallback.
Definition Function.inl:224
ReturnType operator()(Arguments &... arguments) const
Executes a function call on the resolved function pointer and passes the arguments.
Definition Function.inl:145
ReturnType call(Arguments &... arguments) const
Executes a function call on the resolved function pointer and passes the arguments.
Definition Function.inl:151
ReturnType directCall(Arguments... arguments) const
Executes a function call on the resolved function pointer and passes the arguments.
Definition Function.inl:183
typename CallbackType< ReturnType, Arguments... >::type AfterCallback
The callback type for the after callback.
Definition Function.h:84
virtual AbstractState & state() const override
Get current state.
Definition Function.inl:242
Function(const char *name)
Constructor.
Definition Function.inl:137
void setBeforeCallback(BeforeCallback callback)
Register a callback that is triggered before a function call to the OpenGL driver.
Definition Function.inl:194
void clearBeforeCallback()
Clears any previously registered before callback.
Definition Function.inl:200
void clearAfterCallback()
Clears any previously registered after callback.
Definition Function.inl:212
virtual void resizeStates(int count) override
Resize internal cache of states.
Definition Function.inl:257
BeforeCallback beforeCallback() const
The accessor for the beforeCallback.
Definition Function.inl:218
virtual bool hasState() const override
Checks for existence of the current configured state.
Definition Function.inl:230
typename CallbackType< void, Arguments... >::type BeforeCallback
The callback type for the before callback.
Definition Function.h:83
Contains all the classes of glbinding.
std::vector< std::unique_ptr< AbstractValue > > createValues(Arguments &&... arguments)
A wrapper around the creation of a vector of arguments.
Definition Value.inl:64
@ Parameters
Enables the provision of parameter values in the before and after callbacks.
@ Unresolved
Enables the callback for unresolved function calls.
@ After
Enables the after callbacks.
@ Logging
Enables logging.
@ Before
Enables the before callbacks.
@ ReturnValue
Enables the provision of a return value in the after callback.
std::unique_ptr< AbstractValue > createValue(const Argument &argument)
A wrapper around the type deduction and memory allocation of a specific argument.
Definition Value.inl:58
Helper struct for calling GL functions and registered callbacks.
Definition Function.inl:47
static ReturnType call(const glbinding::Function< ReturnType, Arguments... > *function, Arguments &&... arguments)
Definition Function.inl:48