18template<
typename T,
class allocator>
47 , m_type(other.m_type)
48 , m_size(other.m_size)
49 , m_capacity(other.m_capacity)
50 , m_data(other.m_data)
51 , m_allocator(other.m_allocator)
53 other.m_data =
nullptr;
56 assert(
typeId() == m_type);
65 int type()
const {
return m_type; }
69 assert(m_size <= m_capacity);
71 std::cerr <<
"shm_array: INCONSISTENCY: refcount() < 0" << std::endl;
74 if (m_size > m_capacity) {
75 std::cerr <<
"shm_array: INCONSISTENCY: m_size > m_capacity" << std::endl;
78 if (m_dim[0] != 0 && m_size != m_dim[0] * m_dim[1] * m_dim[2]) {
79 std::cerr <<
"shm_array: INCONSISTENCY: dimensions" << std::endl;
85 typedef typename allocator::pointer
pointer;
91 T *
data()
const {
return &*m_data; }
94 T &
operator[](
const size_t idx)
const {
return m_data[idx]; }
95 T &
at(
const size_t idx)
98 throw(std::out_of_range(
"shm_array"));
101 T &
at(
const size_t idx)
const
104 throw(std::out_of_range(
"shm_array"));
110 if (m_size >= m_capacity)
111 reserve(m_capacity == 0 ? 1 : m_capacity * 2);
112 assert(m_size < m_capacity);
113 new (&m_data[m_size]) T(v);
117 template<
class... Args>
120 if (m_size >= m_capacity)
121 reserve(m_capacity == 0 ? 1 : m_capacity * 2);
122 assert(m_size < m_capacity);
123 new (&m_data[m_size]) T(std::forward<Args>(args)...);
127 T &
back() {
return m_data[m_size - 1]; }
130 bool empty()
const {
return m_size == 0; }
133 size_t size()
const {
return m_size; }
137 if (!std::is_trivially_copyable<T>::value) {
138 for (
size_t i = m_size; i <
size; ++i)
139 new (&m_data[i]) T();
142 assert(m_size <= m_capacity);
148 for (
size_t i = m_size; i <
size; ++i)
149 new (&m_data[i]) T(value);
151 assert(m_size <= m_capacity);
158 m_dim[1] = m_dim[2] = 1;
162 assert(m_size == sx * sy * sz);
179 if (m_data && new_data) {
180 if (std::is_trivially_copyable<T>::value) {
181 ::memcpy(&*new_data, &*m_data,
sizeof(T) * n);
183 for (
size_t i = 0; i < n; ++i) {
184 new (&new_data[i]) T(std::move(m_data[i]));
189 if (!std::is_trivially_copyable<T>::value) {
190 for (
size_t i = n; i < m_size; ++i) {
194 m_allocator.deallocate(m_data, m_capacity);
202 assert(m_capacity == m_size);
206 const uint32_t m_type;
208 size_t m_dim[3] = {0, 1, 1};
209 size_t m_capacity = 0;
210 bool m_exact = std::is_integral<T>::value;
215 template<
class Archive>
216 void save(Archive &ar)
const;
217 template<
class Archive>
218 void load(Archive &ar);
228#ifndef SHM_ARRAY_IMPL_H
229#define SHM_ARRAY_IMPL_H
233template<
typename T,
class allocator>
234template<
class Archive>
237 ar &
V_NAME(ar,
"type", m_type);
239 ar &
V_NAME(ar,
"exact", m_exact);
242 if (m_dim[0] * m_dim[1] * m_dim[2] == m_size)
243 ar &
V_NAME(ar,
"elements", detail::wrap_array<Archive>(&m_data[0], m_exact, m_dim[0], m_dim[1], m_dim[2]));
245 ar &
V_NAME(ar,
"elements", detail::wrap_array<Archive>(&m_data[0], m_exact, m_size));
249template<
typename T,
class allocator>
250template<
class Archive>
254 ar &
V_NAME(ar,
"type", type);
255 assert(m_type == type);
257 ar &
V_NAME(ar,
"size", size);
259 ar &
V_NAME(ar,
"exact", m_exact);
261 if (m_dim[0] * m_dim[1] * m_dim[2] == m_size)
262 ar &
V_NAME(ar,
"elements", detail::wrap_array<Archive>(&m_data[0], m_exact, m_dim[0], m_dim[1], m_dim[2]));
264 ar &
V_NAME(ar,
"elements", detail::wrap_array<Archive>(&m_data[0], m_exact, m_size));
#define V_NAME(ar, name, obj)
Definition: archives_config.h:441
#define ARCHIVE_ACCESS_SPLIT
Definition: archives_config.h:502
Definition: allocator.h:12
Definition: shm_array.h:19
bool check() const
Definition: shm_array.h:66
void push_back(const T &v)
Definition: shm_array.h:108
T & at(const size_t idx)
Definition: shm_array.h:95
T & back()
Definition: shm_array.h:127
void setDimensionHint(const size_t sx, const size_t sy=1, const size_t sz=1)
Definition: shm_array.h:160
size_t capacity() const
Definition: shm_array.h:169
bool empty() const
Definition: shm_array.h:130
void reserve(const size_t new_capacity)
Definition: shm_array.h:170
T value_type
Definition: shm_array.h:21
shm_array(const size_t size, const allocator &alloc=allocator())
Definition: shm_array.h:33
const value_type & const_reference
Definition: shm_array.h:23
void shrink_to_fit()
Definition: shm_array.h:199
void resize(const size_t size, const T &value)
Definition: shm_array.h:145
T & operator[](const size_t idx) const
Definition: shm_array.h:94
void clear()
Definition: shm_array.h:131
T & front()
Definition: shm_array.h:128
void clearDimensionHint()
Definition: shm_array.h:155
shm_array(const allocator &alloc=allocator())
Definition: shm_array.h:27
size_t size() const
Definition: shm_array.h:133
void resize(const size_t size)
Definition: shm_array.h:134
allocator::pointer pointer
Definition: shm_array.h:85
static int typeId()
Definition: shm_impl.h:24
shm_array(const size_t size, const T &value, const allocator &alloc=allocator())
Definition: shm_array.h:39
int type() const
Definition: shm_array.h:65
T * data() const
Definition: shm_array.h:91
uint64_t size_type
Definition: shm_array.h:22
void emplace_back(Args &&...args)
Definition: shm_array.h:118
shm_array(shm_array &&other)
Definition: shm_array.h:45
void reserve_or_shrink(const size_t capacity)
Definition: shm_array.h:175
T * iterator
Definition: shm_array.h:86
iterator end() const
Definition: shm_array.h:90
T & at(const size_t idx) const
Definition: shm_array.h:101
const T * const_iterator
Definition: shm_array.h:87
~shm_array()
Definition: shm_array.h:59
T & operator[](const size_t idx)
Definition: shm_array.h:93
void setExact(bool exact)
Definition: shm_array.h:167
iterator begin() const
Definition: shm_array.h:89
Definition: allobjects.cpp:30
default_init_allocator< T > allocator
Definition: allocator.h:36
@ ARRAY
Definition: shmdata.h:16
int refcount() const
Definition: shmdata.h:35