OGLplus  (0.59.0) a C++ wrapper for rendering APIs

vararray.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_VARARRAY_HPP
10 #define EAGINE_VARARRAY_HPP
11 
12 #include <cstdint>
13 #include <type_traits>
14 
15 namespace eagine {
16 
17 // basic_vararray
18 template <typename T, typename S>
19 class basic_vararray {
20 public:
21  using value_type = T;
22  using size_type = S;
23 
24  using alloc_unit_t = std::conditional_t<alignof(T) >= alignof(S), T, S>;
25 
26  static std::size_t alloc_unit_c(std::size_t n) noexcept {
27  if(alignof(T) >= alignof(S)) {
28  return n + (sizeof(S) / sizeof(T)) +
29  ((sizeof(S) % sizeof(T)) ? 1 : 0);
30  } else {
31  return 1 + ((n * sizeof(T)) / sizeof(S)) +
32  (((n * sizeof(T)) % sizeof(S)) ? 1 : 0);
33  }
34  }
35 
36 private:
37  const size_type _size = 0;
38  value_type _data[1] = {};
39 
40 public:
41  constexpr basic_vararray() noexcept = default;
42 
43  static std::size_t instance_size(size_type n) noexcept {
44  return alloc_unit_c(n) * sizeof(alloc_unit_t);
45  }
46 
47  std::size_t instance_size() const noexcept {
48  return instance_size(_size);
49  }
50 
51  size_type size() const noexcept {
52  return _size;
53  }
54 
55  using iterator = T*;
56  using const_iterator = const T*;
57 
58  iterator begin() noexcept {
59  return _data;
60  }
61 
62  iterator end() noexcept {
63  return begin() + size();
64  }
65 
66  const_iterator begin() const noexcept {
67  return _data;
68  }
69 
70  const_iterator end() const noexcept {
71  return begin() + size();
72  }
73 };
74 
75 template <typename T>
76 using vararray = basic_vararray<T, std::size_t>;
77 
78 // basic_vararray_store
79 template <typename T, typename S, S N>
80 class basic_vararray_store {
81 private:
82  const S _size = N;
83  T _data[N] = {};
84 
85 public:
86  constexpr basic_vararray_store() noexcept = default;
87 
88  operator basic_vararray<T, S> &() {
89  return *reinterpret_cast<basic_vararray<T, S>*>(this);
90  }
91 
92  operator const basic_vararray<T, S> &() const {
93  return *reinterpret_cast<const basic_vararray<T, S>*>(this);
94  }
95 };
96 
97 template <typename T, std::size_t N>
98 using vararray_store = basic_vararray_store<T, std::size_t, N>;
99 
100 } // namespace eagine
101 
102 #endif // EAGINE_VARARRAY_HPP
value_type
Value tree value element data type enumeration.
Definition: interface.hpp:27
Common code is placed in this namespace.
Definition: eagine.hpp:21

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).