GLSL Struct 1.4.0
glslstruct
Loading...
Searching...
No Matches
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_GLSL_TYPE_HPP_
12 #define _GLSL_STRUCT_GLSL_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/containers/base_type.hpp>
23 #include <glslstruct/type/visitors/eq_type_visitor.hpp>
24
25namespace glslstruct {
26 /**
27 * @brief intermediate glsl type containers
28 * @ingroup glsl_types
29 * @tparam Derived derived type
30 */
31 template<class Derived>
32 class type : public base_type {
33 private:
34 /// @brief standard cast from this to derived type
35 _GLSL_STRUCT_CONSTEXPR17 const Derived& _get_derived() const noexcept { return *static_cast<const Derived*>(this); }
36
37 protected:
38 /// @brief accept function overload
39 void _accept(const mstd::function_view<void(const scalar_type&)> scalarVisit,
40 const mstd::function_view<void(const vec_type&)> vecVisit, const mstd::function_view<void(const mat_type&)> matVisit,
41 const mstd::function_view<void(const struct_type&)> structVisit,
42 const mstd::function_view<void(const array_type&)> arrayVisit) const override {
43 if _MSTD_CONSTEXPR17 (std::is_same_v<Derived, scalar_type>) { scalarVisit(_get_derived()); }
44 else if _MSTD_CONSTEXPR17 (std::is_same_v<Derived, vec_type>) { vecVisit(_get_derived()); }
45 else if _MSTD_CONSTEXPR17 (std::is_same_v<Derived, mat_type>) { matVisit(_get_derived()); }
46 else if _MSTD_CONSTEXPR17 (std::is_same_v<Derived, struct_type>) { structVisit(_get_derived()); }
47 else if _MSTD_CONSTEXPR17 (std::is_same_v<Derived, array_type>) { arrayVisit(_get_derived()); }
48 }
49
50 public:
51 using base_type::operator!=;
52
53 /// @brief constructor with type size
54 explicit _GLSL_STRUCT_CONSTEXPR20 type(const size_t size) noexcept : base_type(size) {}
55
56 /// @brief default copy constructor
57 _GLSL_STRUCT_CONSTEXPR20 type(const type& other) noexcept = default;
58 /// @brief default move constructor
59 _GLSL_STRUCT_CONSTEXPR20 type(type&& other) noexcept = default;
60 /// @brief default destructor
61 virtual _GLSL_STRUCT_CONSTEXPR20 ~type() noexcept override = default;
62
63 /// @brief default copy assign operator
64 _GLSL_STRUCT_CONSTEXPR17 type& operator=(const type& other) noexcept = default;
65 /// @brief default move assign operator
66 _GLSL_STRUCT_CONSTEXPR17 type& operator=(type&& other) noexcept = default;
67
68 /// @brief accept function for type visitors
70 template<type_visitor T>
71 #else
72 template<class T, std::enable_if_t<is_type_visitor_v<T>, bool> = true>
73 #endif
74 void accept(T& visitor) const {
75 visitor.visit(_get_derived());
76 }
77
78 /// @brief equal operator
79 [[nodiscard]] bool operator==(const base_type& other) const noexcept override {
80 if (!base_type::operator==(other)) { return false; }
81
82 eq_type_visitor<Derived> visitor(&_get_derived());
83 other.accept(visitor);
84 return visitor.result();
85 }
86
87 template<class DerivedA, class DerivedB>
88 friend _GLSL_STRUCT_CONSTEXPR17 bool operator==(const type<DerivedA>& lhs, const type<DerivedB>& rhs) noexcept;
89 template<class DerivedA, class DerivedB>
90 friend _GLSL_STRUCT_CONSTEXPR17 bool operator!=(const type<DerivedA>& lhs, const type<DerivedB>& rhs) noexcept;
91 };
92
93 /**
94 * @brief equal operator for two intermediate types
95 * @ingroup glsl_types
96 */
97 template<class DerivedA, class DerivedB>
98 [[nodiscard]] _GLSL_STRUCT_CONSTEXPR17 bool operator==(const type<DerivedA>& lhs, const type<DerivedB>& rhs) noexcept {
100 else { return false; }
101 }
102
103 /**
104 * @brief not equal operator for two intermediate types
105 * @ingroup glsl_types
106 */
107 template<class DerivedA, class DerivedB>
108 [[nodiscard]] _GLSL_STRUCT_CONSTEXPR17 bool operator!=(const type<DerivedA>& lhs, const type<DerivedB>& rhs) noexcept {
109 return !(lhs == rhs);
110 }
111} // namespace glslstruct
112 #endif
113#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
#define _GLSL_STRUCT_CONSTEXPR17
constexpr for c++17 and higher
Definition config.hpp:196
Main namespace of glslstruct library.
Definition scalar_layout_traits.hpp:23