glbinding  2.1.1.000000000000
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#include <utility>
5#include <functional>
6#include <memory>
7
8#include <glbinding/logging.h>
9#include <glbinding/Value.h>
10
11
12namespace
13{
14
15template <typename ReturnType, typename... Arguments>
16struct BasicCallHelper
17{
18
19 static ReturnType call(const glbinding::Function<ReturnType, Arguments...> * function, Arguments&&... arguments)
20 {
21 return reinterpret_cast<typename glbinding::Function<ReturnType, Arguments...>::Signature>(function->address())(std::forward<Arguments>(arguments)...);
22 }
23};
24
25
26// Special case for GLboolean because of MSVC differing behavior
27
28template <typename... Arguments>
29struct BasicCallHelper<gl::GLboolean, Arguments...>
30{
31 static gl::GLboolean call(const glbinding::Function<gl::GLboolean, Arguments...> * function, Arguments&&... arguments)
32 {
33 return reinterpret_cast<typename glbinding::Function<gl::GLboolean::underlying_type, Arguments...>::Signature>(function->address())(std::forward<Arguments>(arguments)...);
34 }
35};
36
37
38template <typename ReturnType, typename... Arguments>
39struct FunctionHelper
40{
41 ReturnType call(const glbinding::Function<ReturnType, Arguments...> * function, Arguments&&... arguments) const
42 {
43 std::unique_ptr<glbinding::FunctionCall> functionCall{new glbinding::FunctionCall(function)};
44
46 {
47 functionCall->parameters = glbinding::createValues(std::forward<Arguments>(arguments)...);
48 }
49
51 {
52 function->before(*functionCall);
53
54 if (function->beforeCallback())
55 {
56 function->beforeCallback()(std::forward<Arguments>(arguments)...);
57 }
58 }
59
60 auto value = BasicCallHelper<ReturnType, Arguments ...>::call(function, std::forward<Arguments>(arguments)...);
61
63 {
64 functionCall->returnValue = glbinding::createValue(value);
65 }
66
68 {
69 function->after(*functionCall);
70
71 if (function->afterCallback())
72 {
73 function->afterCallback()(value, std::forward<Arguments>(arguments)...);
74 }
75 }
76
78 {
79 glbinding::logging::log(functionCall.release());
80 }
81
82 return value;
83 }
84};
85
86
87template <typename... Arguments>
88struct FunctionHelper<void, Arguments...>
89{
90 void call(const glbinding::Function<void, Arguments...> * function, Arguments&&... arguments) const
91 {
92 std::unique_ptr<glbinding::FunctionCall> functionCall(new glbinding::FunctionCall(function));
93
95 {
96 functionCall->parameters = glbinding::createValues(std::forward<Arguments>(arguments)...);
97 }
98
100 {
101 function->before(*functionCall);
102
103 if (function->beforeCallback())
104 {
105 function->beforeCallback()(std::forward<Arguments>(arguments)...);
107 }
108
109 BasicCallHelper<void, Arguments ...>::call(function, std::forward<Arguments>(arguments)...);
110
112 {
113 function->after(*functionCall);
114
115 if (function->afterCallback())
116 {
117 function->afterCallback()(std::forward<Arguments>(arguments)...);
118 }
119 }
122 {
123 glbinding::logging::log(functionCall.release());
124 }
125 }
126
127};
128
129
130} // namespace
132
133namespace glbinding
134{
135
136
137template <typename ReturnType, typename... Arguments>
139 : AbstractFunction{_name}
140, m_beforeCallback{nullptr}
141, m_afterCallback{nullptr}
142{
143}
144
145template <typename ReturnType, typename... Arguments>
146ReturnType Function<ReturnType, Arguments...>::operator()(Arguments&... arguments) const
147{
148 return call(arguments...);
149}
150
151template <typename ReturnType, typename... Arguments>
152ReturnType Function<ReturnType, Arguments...>::call(Arguments&... arguments) const
153{
154 const auto myAddress = address();
155
156 if (myAddress == nullptr)
157 {
158 if (isEnabled(CallbackMask::Unresolved))
159 {
160 unresolved();
161 }
162
163 return ReturnType();
164 }
165
167 {
168 return FunctionHelper<ReturnType, Arguments...>().call(this, std::forward<Arguments>(arguments)...);
169 }
170 else
171 {
172 return BasicCallHelper<ReturnType, Arguments...>::call(this, std::forward<Arguments>(arguments)...);
173 }
174}
175
176template <typename ReturnType, typename... Arguments>
177ReturnType Function<ReturnType, Arguments...>::directCall(Arguments... arguments) const
178{
179 return BasicCallHelper<ReturnType, Arguments...>::call(this, std::forward<Arguments>(arguments)...);
180}
181
182template <typename ReturnType, typename... Arguments>
184{
185 m_beforeCallback = std::move(callback);
186}
187
188template <typename ReturnType, typename... Arguments>
190{
191 m_beforeCallback = nullptr;
192}
193
194template <typename ReturnType, typename... Arguments>
196{
197 m_afterCallback = std::move(callback);
198}
199
200template <typename ReturnType, typename... Arguments>
202{
203 m_afterCallback = nullptr;
204}
205
206template <typename ReturnType, typename... Arguments>
207typename Function<ReturnType, Arguments...>::BeforeCallback Function<ReturnType, Arguments...>::beforeCallback() const
208{
209 return m_beforeCallback;
210}
211
212template <typename ReturnType, typename... Arguments>
213typename Function<ReturnType, Arguments...>::AfterCallback Function<ReturnType, Arguments...>::afterCallback() const
214{
215 return m_afterCallback;
216}
217
218
219} // namespace glbinding
The AbstractFunction represents an OpenGL API function.
Definition AbstractFunction.h:24
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
Function pointer accessor.
void before(const FunctionCall &call) const
Triggers a call of the before callback, passing the parameters.
bool isEnabled(CallbackMask mask) const
Check if all bits of the parameter are set in the currently configured callback mask of the current s...
void after(const FunctionCall &call) const
Triggers a call of the after callback, passing the parameters and return value.
The Function represents an OpenGL API function with additional features, including:
Definition Function.h:63
void setAfterCallback(AfterCallback callback)
Register a callback that is triggered after a function call to the OpenGL driver.
Definition Function.inl:195
AfterCallback afterCallback() const
The accessor for the afterCallback.
Definition Function.inl:213
ReturnType operator()(Arguments &... arguments) const
Executes a function call on the resolved function pointer and passes the arguments.
Definition Function.inl:146
ReturnType call(Arguments &... arguments) const
Executes a function call on the resolved function pointer and passes the arguments.
Definition Function.inl:152
ReturnType directCall(Arguments... arguments) const
Executes a function call on the resolved function pointer and passes the arguments.
Definition Function.inl:177
typename CallbackType< ReturnType, Arguments... >::type AfterCallback
The callback type for the after callback.
Definition Function.h:68
Function(const char *name)
Constructor.
Definition Function.inl:138
void setBeforeCallback(BeforeCallback callback)
Register a callback that is triggered before a function call to the OpenGL driver.
Definition Function.inl:183
void clearBeforeCallback()
Clears any previously registered before callback.
Definition Function.inl:189
void clearAfterCallback()
Clears any previously registered after callback.
Definition Function.inl:201
BeforeCallback beforeCallback() const
The accessor for the beforeCallback.
Definition Function.inl:207
typename CallbackType< void, Arguments... >::type BeforeCallback
The callback type for the before callback.
Definition Function.h:67
Definition ContextInfo.h:11
GLBINDING_API void log(LogEntry call)
Add a function call to the log.
Contains all the classes of glbinding.
std::vector< AbstractValue * > createValues(Arguments &&... arguments)
A wrapper around the creation of a vector of arguments.
Definition Value.inl:66
AbstractValue * createValue(const Argument &argument)
A wrapper around the type deduction and memory allocation of a specific argument.
Definition Value.inl:60
@ 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 to file.
@ Before
Enables the before callbacks.
@ ReturnValue
Enables the provision of a return value in the after callback.
A FunctionCall represents a function call of an OpenGL API function, including the parameter and retu...
Definition FunctionCall.h:23
std::vector< AbstractValue * > parameters
The list of parameter values; doesn't have to be filled.
Definition FunctionCall.h:83