GLSL Struct 1.4.0
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 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 T>
67 #else
68 template<class T, std::enable_if_t<is_type_visitor_v<T>, bool> = true>
69 #endif
70 void accept(T& visitor) const {
71 _accept([&visitor](const scalar_type& s) { visitor.visit(s); }, [&visitor](const vec_type& v) { visitor.visit(v); },
72 [&visitor](const mat_type& m) { visitor.visit(m); }, [&visitor](const struct_type& s) { visitor.visit(s); },
73 [&visitor](const array_type& a) { visitor.visit(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<utils::glsl_type T>
82 #else
83 template<class T, std::enable_if_t<utils::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<utils::glsl_type T>
88 #else
89 template<class T, std::enable_if_t<utils::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]] 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<utils::glsl_type T>
112 #else
113 template<class T, std::enable_if_t<utils::is_glsl_type_v<T>, bool> = true>
114 #endif
115 [[nodiscard]] 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]] 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<utils::glsl_type T>
131 #else
132 template<class T, std::enable_if_t<utils::is_glsl_type_v<T>, bool> = true>
133 #endif
134 [[nodiscard]] std::string to_string(const std::shared_ptr<T>& type) noexcept {
135 return type->to_string();
136 }
137
138 /**
139 * @brief checks if types are equal
140 * @ingroup glsl_types
141 */
143 template<utils::glsl_type T>
144 #else
145 template<class T, std::enable_if_t<utils::is_glsl_type_v<T>, bool> = true>
146 #endif
147 [[nodiscard]] _GLSL_STRUCT_CONSTEXPR20 bool operator==(const base_type& lhs, const T& rhs) noexcept {
148 return rhs == lhs;
149 }
150
151 /**
152 * @brief checks if types are not equal
153 * @ingroup glsl_types
154 */
156 template<utils::glsl_type T>
157 #else
158 template<class T, std::enable_if_t<utils::is_glsl_type_v<T>, bool> = true>
159 #endif
160 [[nodiscard]] _GLSL_STRUCT_CONSTEXPR20 bool operator!=(const base_type& lhs, const T& rhs) noexcept {
161 return rhs != lhs;
162 }
163} // namespace glslstruct
164
165/**
166 * @brief std::hash overload for base_type
167 * @ingroup glsl_types
168 */
169template<>
170struct std::hash<glslstruct::base_type> {
171 [[nodiscard]] size_t operator()(const glslstruct::base_type& type) const noexcept;
172};
173 #endif
174#endif
#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