GLSL Struct 1.4.8
glslstruct
Loading...
Searching...
No Matches
base_type.hpp
1/*
2 * glslstruct - a C++ library designed to easily represent GLSL's Uniform Buffer Objects (UBOs) and Shader Storage Buffer Objects (SSBOs) in C++.
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/glslstruct/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _GLSL_STRUCT_BASE_TYPE_HPP_
12 #define _GLSL_STRUCT_BASE_TYPE_HPP_
13
14 #include <glslstruct/config.hpp>
15
17_GLSL_STRUCT_ERROR(
18 "This is only available for c++17 and greater and when types are not disabled with defined GLSL_STRUCT_DISABLE_TYPES!"
19);
20 #else
21
22 #include <glslstruct/type/visitors/type_visitor_concept.hpp>
23
24/**
25 * @defgroup glsl_types Glsl type containers
26 * @ingroup glslstruct
27 * @brief type containers are for people that want to know of which type is value stored in struct
28 */
29
30namespace glslstruct {
31 /**
32 * @brief base class for all glsl type containers
33 * @ingroup glsl_types
34 */
35 class _GLSL_STRUCT_EXPORT base_type {
36 private:
37 friend struct std::hash<base_type>;
38
39 /// @brief size value of type
40 size_t _size;
41
42 protected:
43 /// @brief accept function for type visitors
44 virtual void _accept(mstd::function_view<void(const scalar_type&)> scalarVisit,
45 mstd::function_view<void(const vec_type&)> vecVisit, mstd::function_view<void(const mat_type&)> matVisit,
46 mstd::function_view<void(const struct_type&)> structVisit,
47 mstd::function_view<void(const array_type&)> arrayVisit) const = 0;
48
49 public:
50 /// @brief constructor with type size
51 explicit base_type(size_t size) noexcept;
52 /// @brief default copy constructor
53 base_type(const base_type& other) noexcept;
54 /// @brief default move constructor
55 base_type(base_type&& other) noexcept;
56 /// @brief default virtual destructor
57 virtual ~base_type() noexcept;
58
59 /// @brief default copy assignment operator
60 base_type& operator=(const base_type& other) noexcept;
61 /// @brief default move assignment operator
62 base_type& operator=(base_type&& other) noexcept;
63
64 /// @brief accept function for type_visitors
66 template<type_visitor V>
67 #else
68 template<class V, std::enable_if_t<is_type_visitor_v<V>, bool> = true>
69 #endif
70 void accept(V&& visitor) const {
71 _accept([&visitor](const scalar_type& s) { visitor(s); }, [&visitor](const vec_type& v) { visitor(v); },
72 [&visitor](const mat_type& m) { visitor(m); }, [&visitor](const struct_type& s) { visitor(s); },
73 [&visitor](const array_type& a) { visitor(a); });
74 }
75
76 /// @brief virtual equality operator
77 [[nodiscard]] virtual bool operator==(const base_type& other) const noexcept;
78 /// @brief default not equal operator
79 [[nodiscard]] bool operator!=(const base_type& other) const noexcept;
81 template<glsl_type T>
82 #else
83 template<class T, std::enable_if_t<is_glsl_type_v<T>, bool> >
84 #endif
85 friend _GLSL_STRUCT_CONSTEXPR20 bool operator==(const base_type& lhs, const T& rhs) noexcept;
87 template<glsl_type T>
88 #else
89 template<class T, std::enable_if_t<is_glsl_type_v<T>, bool> >
90 #endif
91 friend _GLSL_STRUCT_CONSTEXPR20 bool operator!=(const base_type& lhs, const T& rhs) noexcept;
92
93 /// @brief returns size of type
94 [[nodiscard]] size_t get_size() const noexcept;
95
96 /// @brief converts type to string
97 [[nodiscard]] virtual std::string to_string() const noexcept = 0;
98 };
99
100 /**
101 * @brief returns size of given type
102 * @ingroup glsl_types
103 */
104 [[nodiscard]] _GLSL_STRUCT_EXPORT size_t sizeof_type(const base_type_handle& type) noexcept;
105
106 /**
107 * @brief returns size of given type
108 * @ingroup glsl_types
109 */
111 template<glsl_type T>
112 #else
113 template<class T, std::enable_if_t<is_glsl_type_v<T>, bool> = true>
114 #endif
115 [[nodiscard]] _GLSL_STRUCT_CONSTEXPR17 size_t sizeof_type(const std::shared_ptr<T>& type) noexcept {
116 return type->get_size();
117 }
118
119 /**
120 * @brief converts type to string
121 * @ingroup glsl_types
122 */
123 [[nodiscard]] _GLSL_STRUCT_EXPORT std::string to_string(const base_type_handle& type) noexcept;
124
125 /**
126 * @brief converts type to string
127 * @ingroup glsl_types
128 */
130 template<glsl_type T>
131 #else
132 template<class T, std::enable_if_t<is_glsl_type_v<T>, bool> = true>
133 #endif
134 [[nodiscard]] inline std::string to_string(const std::shared_ptr<T>& type) noexcept {
135 return type->to_string();
136 }
137
138 #pragma region VISIT
139 /**
140 * @brief visits type
141 * @ingroup glsl_types
142 */
144 template<type_visitor V>
145 #else
146 template<class V, std::enable_if_t<is_type_visitor_v<V>, bool> = true>
147 #endif
148 inline void visit(V&& visitor, const base_type_handle& type) {
149 type->accept(visitor);
150 }
151
152 /**
153 * @brief visits type
154 * @ingroup glsl_types
155 */
157 template<glsl_type T, type_visitor V>
158 #else
159 template<class T, class V, std::enable_if_t<is_glsl_type_v<T> && is_type_visitor_v<V>, bool> = true>
160 #endif
161 inline void visit(V&& visitor, const std::shared_ptr<T>& type) {
162 type->accept(visitor);
163 }
164
165 /**
166 * @brief visits type
167 * @ingroup glsl_types
168 */
170 template<type_visitor V>
171 #else
172 template<class V, std::enable_if_t<is_type_visitor_v<V>, bool> = true>
173 #endif
174 inline void visit(V&& visitor, const base_type& type) {
175 type.accept(visitor);
176 }
177
178 /**
179 * @brief visits type
180 * @ingroup glsl_types
181 */
183 template<glsl_type T, type_visitor V>
184 #else
185 template<class T, class V, std::enable_if_t<is_glsl_type_v<T> && is_type_visitor_v<V>, bool> = true>
186 #endif
187 inline void visit(V&& visitor, const T& type) {
188 type.accept(visitor);
189 }
190
191 #pragma endregion
192
193 /**
194 * @brief checks if types are equal
195 * @ingroup glsl_types
196 */
198 template<glsl_type T>
199 #else
200 template<class T, std::enable_if_t<is_glsl_type_v<T>, bool> = true>
201 #endif
202 [[nodiscard]] _GLSL_STRUCT_INLINE17 _GLSL_STRUCT_CONSTEXPR20 bool operator==(const base_type& lhs, const T& rhs) noexcept {
203 return rhs == lhs;
204 }
205
206 /**
207 * @brief checks if types are not equal
208 * @ingroup glsl_types
209 */
211 template<glsl_type T>
212 #else
213 template<class T, std::enable_if_t<is_glsl_type_v<T>, bool> = true>
214 #endif
215 [[nodiscard]] _GLSL_STRUCT_INLINE17 _GLSL_STRUCT_CONSTEXPR20 bool operator!=(const base_type& lhs, const T& rhs) noexcept {
216 return rhs != lhs;
217 }
218} // namespace glslstruct
219
220/**
221 * @brief std::hash overload for base_type
222 * @ingroup glsl_types
223 */
224template<>
225struct _GLSL_STRUCT_EXPORT std::hash<glslstruct::base_type> {
226 [[nodiscard]] size_t operator()(const glslstruct::base_type& type) const noexcept;
227};
228 #endif
229#endif
#define _GLSL_STRUCT_INLINE17
Definition config.hpp:215
#define _GLSL_STRUCT_EXPORT
This is for exporting symbols in shared library setup.
Definition config.hpp:251
#define _GLSL_STRUCT_CONSTEXPR20
constexpr keyword for c++20 and higher
Definition config.hpp:213
#define _GLSL_STRUCT_HAS_CXX20
check if compiler has c++ version greater or equal to c++20 and if user enabled c++20 features using ...
Definition config.hpp:142
#define _GLSL_STRUCT_HAS_TYPES
check if user not disabled type containers using GLSL_STRUCT_DISABLE_TYPES
Definition config.hpp:162
#define _GLSL_STRUCT_CONSTEXPR17
constexpr for c++17 and higher
Definition config.hpp:196