60template <
typename T,
typename TableType = shm_table>
61 requires std::is_trivially_copyable_v<T>
65 const typename TableType::entry* _table_entry{
nullptr};
96 template<
typename ShmType>
100 static_assert(std::is_same_v<typename ShmType::table_type, TableType>,
101 "SharedMemory table type must match array table type");
116 auto *table =
static_cast<TableType *
>(this->
shm.get_base_addr());
119 char name_buf[TableType::MAX_NAME_SIZE]{};
120 size_t copy_len = std::min(
name.size(),
sizeof(name_buf) - 1);
121 std::copy_n(
name.begin(), copy_len, name_buf);
123 auto *entry = table->find(name_buf);
128 if (count != 0 && entry->num_elem != count)
130 throw std::runtime_error(
"Mismatch in array size");
132 this->
offset = entry->offset;
134 _table_entry = entry;
139 size_t required_size = count *
sizeof(T);
140 size_t current_used = table->get_total_allocated_size();
142 size_t available_size = this->
shm.get_total_size() - current_used;
144 if (required_size > available_size)
146 throw std::runtime_error(
"Not enough space in shared memory");
150 size_t byte_offset =
sizeof(TableType) + current_used;
151 this->
offset = byte_offset;
154 if (!table->add(name_buf, byte_offset, required_size,
sizeof(T), count))
156 throw std::runtime_error(
"Failed to add array to table");
159 _table_entry = table->find(name_buf);
163 throw std::runtime_error(
"Array not found and size not specified");
179 [[nodiscard]] T &
at(
size_t pos)
183 throw std::out_of_range(
"Array index out of range");
190 [[nodiscard]]
const T &
at(
size_t pos)
const
194 throw std::out_of_range(
"Array index out of range");
205 [[nodiscard]]
const T &
front()
const {
return *this->
data(); }
262 [[nodiscard]] std::span<const T>
as_span() const noexcept
264 return std::span<const T>(this->
data(), this->
num_elem);
354 return _table_entry ? std::string_view(_table_entry->name.data()) : std::string_view{};
POSIX shared memory wrapper with RAII and reference counting.
Fixed-size array in shared memory with zero-overhead access.
const T & back() const
Access last element (const)
const T & at(size_t pos) const
Const version of at()
T & at(size_t pos)
Bounds-checked element access.
const T & front() const
Access first element (const)
std::string_view name() const noexcept
Get array name from metadata table.
const_reverse_iterator crbegin() const noexcept
Get const reverse iterator to beginning (explicit)
T & reference
Reference type.
std::span< const T > as_span() const noexcept
Convert to const span.
T value_type
Element type.
const_pointer data() const noexcept
Get const pointer to underlying data.
void fill(const T &value)
Fill array with value.
const T * const_iterator
Const iterator.
size_type max_size() const noexcept
Get maximum size (same as size() for fixed arrays)
const_iterator cend() const noexcept
Get const iterator to end (explicit)
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator.
shm_array(ShmType &shared_mem, std::string_view name, size_t count=0)
Create new array or attach to existing array by name.
const_reverse_iterator crend() const noexcept
Get const reverse iterator to end (explicit)
size_t size_type
Size type.
const T * const_pointer
Const pointer.
const_iterator begin() const noexcept
Get const iterator to beginning.
std::ptrdiff_t difference_type
Iterator difference.
const_reverse_iterator rbegin() const noexcept
Get const reverse iterator to beginning.
void initialize(std::string_view name, size_t count=0)
Internal initialization helper.
T & back()
Access last element.
const T & const_reference
Const reference.
std::span< T > as_span() noexcept
Convert to std::span for modern C++ usage.
const_iterator cbegin() const noexcept
Get const iterator to beginning (explicit)
reverse_iterator rbegin() noexcept
Get reverse iterator to beginning.
const_iterator end() const noexcept
Get const iterator to end.
std::reverse_iterator< const_iterator > const_reverse_iterator
Const reverse.
iterator begin() noexcept
Get iterator to beginning.
const_reverse_iterator rend() const noexcept
Get const reverse iterator to end.
bool empty() const noexcept
Check if array is empty.
T & front()
Access first element.
iterator end() noexcept
Get iterator to end.
reverse_iterator rend() noexcept
Get reverse iterator to end.
T * iterator
Iterator type (raw pointer)
size_t size() const noexcept
Get number of elements.
pointer data() noexcept
Get pointer to underlying data.
Base class for shared memory data structures that span a region.
Core POSIX shared memory management with automatic reference counting.