POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
Loading...
Searching...
No Matches
shm_span.h
Go to the documentation of this file.
1#pragma once
2#include <type_traits>
3
10template<typename T, typename ShmType>
11class shm_span {
12protected:
13 ShmType& shm;
14 size_t offset;
15 size_t num_elem;
16
17public:
18 shm_span(ShmType& shm, size_t off, size_t count)
19 : shm(shm), offset(off), num_elem(count) {}
20
21 T* data() {
22 return reinterpret_cast<T*>(
23 static_cast<char*>(shm.get_base_addr()) + offset
24 );
25 }
26
27 const T* data() const {
28 return reinterpret_cast<const T*>(
29 static_cast<const char*>(shm.get_base_addr()) + offset
30 );
31 }
32
33 size_t size() const { return num_elem; }
34
35 T& operator[](size_t index) { return data()[index]; }
36 const T& operator[](size_t index) const { return data()[index]; }
37};
Base class for shared memory data structures that span a region.
Definition shm_span.h:11
ShmType & shm
Definition shm_span.h:13
const T * data() const
Definition shm_span.h:27
size_t offset
Definition shm_span.h:14
size_t size() const
Definition shm_span.h:33
shm_span(ShmType &shm, size_t off, size_t count)
Definition shm_span.h:18
size_t num_elem
Definition shm_span.h:15
T & operator[](size_t index)
Definition shm_span.h:35
const T & operator[](size_t index) const
Definition shm_span.h:36
T * data()
Definition shm_span.h:21