GLSL Struct 1.4.0
glslstruct
Loading...
Searching...
No Matches
scalar_data.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_SCALAR_DATA_HPP_
12 #define _GLSL_STRUCT_SCALAR_DATA_HPP_
13
14 #include <glslstruct/config.hpp>
15
17_GLSL_STRUCT_ERROR("This is only available for c++17 and greater!");
18 #else
19
20 #include <glslstruct/type/traits/concepts/scalar_traits_concept.hpp>
21
22namespace glslstruct {
23 /**
24 * @brief glsl scalar data container
25 * @ingroup glslstruct
26 */
27 class scalar_data {
28 private:
29 /// @brief returns value of type T pure data bytes
30 template<class T>
31 [[nodiscard]] static _GLSL_STRUCT_CONSTEXPR20 std::vector<std::byte> _get_value_data(const T& value) {
32 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable to be treated as raw bytes!!");
33
34 const auto* valueDataPtr = reinterpret_cast<const std::byte*>(std::addressof(value));
35 return { valueDataPtr, valueDataPtr + sizeof(T) };
36 }
37
38 /// @brief vector of data bytes
39 std::vector<std::byte> _data;
40
41 public:
42 /// @brief constructor which accepts raw data (not recommended to use for end user)
43 explicit scalar_data(const std::vector<std::byte>& data);
44 /// @brief constructor for bool value
45 explicit scalar_data(bool value);
46 /// @brief constructor for int value
47 explicit scalar_data(int value);
48 /// @brief constructor for unsigned int value
49 explicit scalar_data(unsigned int value);
50 /// @brief constructor for float value
51 explicit scalar_data(float value);
52 /// @brief constructor for double value
53 explicit scalar_data(double value);
54
55 /// @brief default copy constructor
56 scalar_data(const scalar_data& other);
57 /// @brief default move constructor
58 scalar_data(scalar_data&& other) noexcept;
59
60 /// @brief default destructor
61 ~scalar_data();
62
63 /// @brief default copy assign operator
64 scalar_data& operator=(const scalar_data& other);
65 /// @brief default move assign operator
66 scalar_data& operator=(scalar_data&& other) noexcept;
67
68 /// @brief returns value from this scalar_data container using scalar_traits conversion
70 template<utils::glsl_scalar T>
71 #else
72 template<class T, std::enable_if_t<utils::is_glsl_scalar_v<T>, bool> = true>
73 #endif
74 T get() const {
75 return scalar_traits<T>::get_value(*this);
76 }
77
78 /// @brief returns value data in bytes
79 [[nodiscard]] const std::vector<std::byte>& data() const noexcept;
80 };
81} // namespace glslstruct
82
83 #endif
84#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_CXX17
check if compiler has c++ version greater or equal to c++17
Definition config.hpp:130