OGLplus (0.52.0) a C++ wrapper for OpenGL

typecheck.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_PROG_VAR_TYPECHECK_1405052241_HPP
14 #define OGLPLUS_PROG_VAR_TYPECHECK_1405052241_HPP
15 
16 #include <oglplus/glfunc.hpp>
17 #include <oglplus/config/basic.hpp>
18 #include <oglplus/config/compiler.hpp>
20 #include <oglplus/detail/enum_class.hpp>
21 
22 #include <cassert>
23 
24 namespace oglplus {
25 
26 template <typename T>
27 struct GLSLtoCppTypeMatcher;
28 
29 #if !OGLPLUS_NO_UNIFORM_TYPECHECK
30 
31 OGLPLUS_ENUM_CLASS_FWD(SLDataType, GLenum)
32 
33 template <oglplus::SLDataType>
34 struct SLtoCpp;
35 
36 template <typename T>
37 struct GLSLtoCppTypeMatcher
38 {
39  static bool _matches(GLenum /*sl_type*/)
40  {
41  return false;
42  }
43 };
44 
45 template <oglplus::SLDataType SLType>
46 struct GLSLtoCppTypeMatcher<oglplus::SLtoCpp<SLType> >
47 {
48  static bool _matches(GLenum sl_type)
49  {
50  return sl_type == GLenum(SLType);
51  }
52 };
53 
54 template <>
55 struct GLSLtoCppTypeMatcher<bool>
56 {
57  static bool _matches(GLenum sl_type)
58  {
59  return sl_type == GL_BOOL;
60  }
61 };
62 
63 template <>
64 struct GLSLtoCppTypeMatcher<GLint>
65 {
66  static bool _matches(GLenum sl_type);
67 };
68 
69 template <>
70 struct GLSLtoCppTypeMatcher<GLuint>
71 {
72  static bool _matches(GLenum sl_type);
73 };
74 
75 template <>
76 struct GLSLtoCppTypeMatcher<GLfloat>
77 {
78  static bool _matches(GLenum sl_type)
79  {
80  return sl_type == GL_FLOAT;
81  }
82 };
83 
84 #if defined(GL_DOUBLE)
85 template <>
86 struct GLSLtoCppTypeMatcher<GLdouble>
87 {
88  static bool _matches(GLenum sl_type)
89  {
90  return sl_type == GL_DOUBLE;
91  }
92 };
93 #endif
94 
95 #if defined(GL_UNSIGNED_INT64_ARB)
96 template <>
97 struct GLSLtoCppTypeMatcher<GLuint64>
98 {
99  static bool _matches(GLenum sl_type)
100  {
101  return sl_type == GL_UNSIGNED_INT64_ARB;
102  }
103 };
104 #endif
105 
106 struct GLSLtoCppTypeMatcher_Vec
107 {
108  static std::size_t _type_idx(bool*) { return 0; }
109  static std::size_t _type_idx(GLint*) { return 1; }
110  static std::size_t _type_idx(GLuint*) { return 2; }
111  static std::size_t _type_idx(GLfloat*) { return 3; }
112 #if defined(GL_DOUBLE)
113  static std::size_t _type_idx(GLdouble*) { return 4; }
114 #endif
115 
116  static bool _does_match(
117  GLenum sl_type,
118  std::size_t type_idx,
119  std::size_t dim
120  );
121 };
122 
123 template <typename T, std::size_t N>
124 struct GLSLtoCppTypeMatcher<oglplus::Vector<T, N> >
125  : public GLSLtoCppTypeMatcher_Vec
126 {
127  static_assert(N <= 4, "Invalid vector size");
128 
129  static T* _type_sel(void) { return nullptr; }
130 
131  static bool _matches(GLenum sl_type)
132  {
133  return GLSLtoCppTypeMatcher_Vec::_does_match(
134  sl_type,
135  GLSLtoCppTypeMatcher_Vec::_type_idx(_type_sel()),
136  N
137  );
138  }
139 };
140 
141 struct GLSLtoCppTypeMatcher_Mat
142 {
143  static std::size_t _type_idx(GLfloat*) { return 0; }
144 #ifdef GL_DOUBLE
145  static std::size_t _type_idx(GLdouble*) { return 1; }
146 #endif
147 
148  static bool _does_match(
149  GLenum sl_type,
150  std::size_t type_idx,
151  std::size_t rows,
152  std::size_t cols
153  );
154 };
155 
156 template <typename T, std::size_t Rows, std::size_t Cols>
157 struct GLSLtoCppTypeMatcher<oglplus::Matrix<T, Rows, Cols> >
158  : public GLSLtoCppTypeMatcher_Mat
159 {
160  static_assert(Rows >= 2, "Invalid matrix size");
161  static_assert(Cols >= 2, "Invalid matrix size");
162 
163  static_assert(Rows <= 4, "Invalid matrix size");
164  static_assert(Cols <= 4, "Invalid matrix size");
165 
166  static T* _type_sel(void) { return nullptr; }
167 
168  static bool _matches(GLenum sl_type)
169  {
170  return GLSLtoCppTypeMatcher_Mat::_does_match(
171  sl_type,
172  GLSLtoCppTypeMatcher_Mat::_type_idx(_type_sel()),
173  Rows,
174  Cols
175  );
176  }
177 };
178 
179 template <>
180 class ProgVarTypecheck<tag::Typecheck, tag::Uniform>
181  : public ProgVarTypeOps<tag::Uniform>
182 {
183 private:
184  bool (*_type_matches)(GLenum);
185 
186  static void _do_check(
187  bool (*)(GLenum),
188  GLenum var_type,
189  ProgramName program,
190  GLint location,
191  StrCRef identifier
192  );
193 public:
194  template <typename TypeSel>
195  ProgVarTypecheck(TypeSel*)
196  : _type_matches(&GLSLtoCppTypeMatcher<TypeSel>::_matches)
197  { }
198 
199  void CheckType(
200  ProgramName program,
201  GLint location,
202  StrCRef identifier
203  ) const
204  {
205  _do_check(
206  _type_matches,
207  ProgVarTypeOps<tag::Uniform>::GetType(
208  program,
209  location,
210  identifier
211  ),
212  program,
213  location,
214  identifier
215  );
216  }
217 };
218 #endif // !OGLPLUS_NO_UNIFORM_TYPECHECK
219 
220 // Default/No typechecking
221 template <typename ChkTag, typename VarTag>
222 class ProgVarTypecheck
223 {
224 public:
225  ProgVarTypecheck(void*)
226  OGLPLUS_NOEXCEPT(true)
227  { }
228 
229  void CheckType(
230  ProgramName program,
231  GLint location,
232  StrCRef identifier
233  ) const
234  {
235  (void)program;
236  (void)location;
237  (void)identifier;
238  }
239 };
240 
241 } // namespace oglplus
242 
243 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
244 #include <oglplus/prog_var/typecheck.ipp>
245 #endif // OGLPLUS_LINK_LIB
246 
247 #endif // include guard
String const reference wrapper template.
Definition: ref_tpl.hpp:72
Helper macro for optional checking of availability of GL function.
Program variable type operations.
SLDataType
OpenGL Shading Language data type enumeration.
Definition: data_type.hpp:123

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).