emlabcpp
modern opinionated embedded C++ library
static_storage.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include <cstddef>
27 #include <memory>
28 #include <utility>
29 
30 namespace emlabcpp
31 {
32 
35 template < typename T, std::size_t N >
37 {
38  static constexpr std::size_t capacity = N;
39 
40  using value_type = T;
41  using reference = T&;
42  using const_reference = T const&;
43  using pointer = T*;
44  using const_pointer = T const*;
45  using size_type = std::size_t;
46 
48  [[nodiscard]] constexpr pointer data() noexcept
49  {
50  return reinterpret_cast< pointer >( data_ );
51  }
52 
54  [[nodiscard]] constexpr const_pointer data() const noexcept
55  {
56  return reinterpret_cast< const_pointer >( data_ );
57  }
58 
60  template < typename... Args >
61  constexpr T& emplace_item( size_type const i, Args&&... args ) noexcept(
62  std::is_nothrow_constructible_v< T, Args... > )
63  {
64  return *std::construct_at( data() + i, std::forward< Args >( args )... );
65  }
66 
68  constexpr void
69  delete_item( size_type const i ) noexcept( std::is_nothrow_destructible_v< T > )
70  {
71  std::destroy_at( data() + i );
72  }
73 
75  [[nodiscard]] constexpr reference operator[]( size_type const i ) noexcept
76  {
77  return *( data() + i );
78  }
79 
81  [[nodiscard]] constexpr const_reference operator[]( size_type const i ) const noexcept
82  {
83  return *( data() + i );
84  }
85 
86 private:
87  alignas( T ) std::byte data_[N * sizeof( T )];
88 };
89 
90 template < typename T >
92  std::is_trivially_default_constructible_v< T > && std::is_trivially_destructible_v< T >;
93 
94 template < typename T, std::size_t N >
95 requires( trivial_for_static_storage< T > )
96 struct static_storage< T, N >
97 {
98  static constexpr std::size_t capacity = N;
99 
100  using value_type = T;
101  using reference = T&;
102  using const_reference = T const&;
103  using pointer = T*;
104  using const_pointer = T const*;
105  using size_type = std::size_t;
106 
108  [[nodiscard]] constexpr pointer data() noexcept
109  {
110  return data_;
111  }
112 
114  [[nodiscard]] constexpr const_pointer data() const noexcept
115  {
116  return data_;
117  }
118 
120  template < typename... Args >
121  constexpr T& emplace_item( size_type const i, Args&&... args ) noexcept(
122  std::is_nothrow_constructible_v< T, Args... > )
123  {
124  data_[i] = T{ std::forward< Args >( args )... };
125  return data_[i];
126  }
127 
129  constexpr void
130  delete_item( size_type const i ) noexcept( std::is_nothrow_destructible_v< T > )
131  {
132  data_[i] = T{};
133  }
134 
136  [[nodiscard]] constexpr reference operator[]( size_type const i ) noexcept
137  {
138  return data_[i];
139  }
140 
142  [[nodiscard]] constexpr const_reference operator[]( size_type const i ) const noexcept
143  {
144  return data_[i];
145  }
146 
147 private:
148  T data_[N];
149 };
150 } // namespace emlabcpp
MIT License.
Definition: impl.h:31
std::size_t size_type
Definition: static_storage.h:105
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:108
T const * const_pointer
Definition: static_storage.h:104
T & reference
Definition: static_storage.h:101
constexpr reference operator[](size_type const i) noexcept
Provides a reference to item at position i.
Definition: static_storage.h:136
concept trivial_for_static_storage
Definition: static_storage.h:91
Args const & args
Definition: min_max.h:83
T * pointer
Definition: static_storage.h:103
T value_type
Definition: static_storage.h:100
constexpr T & emplace_item(size_type const i, Args &&... args) noexcept(std::is_nothrow_constructible_v< T, Args... >)
Constructs an item at position i with arguments args...
Definition: static_storage.h:121
requires(!range_container< Container >) const expr std
Returns index of an element in tuple 't', for which call to predicate f(x) holds true,...
Definition: algorithm.h:127
constexpr void delete_item(size_type const i) noexcept(std::is_nothrow_destructible_v< T >)
Deconstructs an item at position i.
Definition: static_storage.h:130
T const & const_reference
Definition: static_storage.h:102
N
Definition: static_storage.h:97
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118
Continuous data container that can contain N of uninitialized elements.
Definition: static_storage.h:37
T const & const_reference
Definition: static_storage.h:42
constexpr reference operator[](size_type const i) noexcept
Provides a reference to item at position i.
Definition: static_storage.h:75
static constexpr std::size_t capacity
Definition: static_storage.h:38
T * pointer
Definition: static_storage.h:43
T const * const_pointer
Definition: static_storage.h:44
std::size_t size_type
Definition: static_storage.h:45
constexpr T & emplace_item(size_type const i, Args &&... args) noexcept(std::is_nothrow_constructible_v< T, Args... >)
Constructs an item at position i with arguments args...
Definition: static_storage.h:61
constexpr const_pointer data() const noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:54
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:48
constexpr const_reference operator[](size_type const i) const noexcept
Provides a reference to item at position i.
Definition: static_storage.h:81
T & reference
Definition: static_storage.h:41
T value_type
Definition: static_storage.h:40
constexpr void delete_item(size_type const i) noexcept(std::is_nothrow_destructible_v< T >)
Deconstructs an item at position i.
Definition: static_storage.h:69