8 #ifndef __SHM_PS_VECTOR_LIB_H__
9 #define __SHM_PS_VECTOR_LIB_H__
48 Publisher(std::string name =
"",
int buffer_num = 3,
PERM perm = DEFAULT_PERM);
51 void publish(
const std::vector<T>& data);
52 void _publish(
const std::vector<T> data);
76 const std::vector<T>
subscribe(
bool *is_success);
77 bool waitFor(uint64_t timeout_usec);
78 void setDataExpiryTime_us(uint64_t time_us);
84 int current_reading_buffer;
85 uint64_t data_expiry_time_us;
102 template <
typename T>
105 , shm_buf_num(buffer_num)
107 , shared_memory(nullptr)
108 , ring_buffer(nullptr)
111 if (!std::is_standard_layout<T>::value)
113 throw std::runtime_error(
"shm::Publisher: Be setted not POD class in vector!");
117 shared_memory->connect(RingBuffer::getSize(vector_size, shm_buf_num));
118 if (shared_memory->isDisconnected())
120 throw std::runtime_error(
"shm::Publisher: Cannot get memory!");
123 ring_buffer =
new RingBuffer(shared_memory->getPtr(), vector_size, shm_buf_num);
131 template <
typename T>
134 if (ring_buffer !=
nullptr)
138 if (shared_memory !=
nullptr)
140 delete shared_memory;
150 template <
typename T>
154 if (data.size() > vector_size)
156 vector_size = data.size();
158 shared_memory->disconnect();
159 shared_memory->connect(RingBuffer::getSize(
sizeof(T)*vector_size, shm_buf_num));
160 ring_buffer =
new RingBuffer(shared_memory->getPtr(),
sizeof(T)*vector_size, shm_buf_num);
163 int oldest_buffer = ring_buffer->getOldestBufferNum();
165 T * first_ptr = &( (
reinterpret_cast<T *
>(ring_buffer->getDataList()))[oldest_buffer*vector_size] );
166 memcpy(first_ptr, data.data(),
sizeof(T) * vector_size);
169 clock_gettime(CLOCK_MONOTONIC_RAW, &t);
170 ring_buffer->setTimestamp_us(((uint64_t) t.tv_sec * 1000000L) + ((uint64_t) t.tv_nsec / 1000L), oldest_buffer);
172 ring_buffer->signal();
180 template <
typename T>
192 template <
typename T>
195 , shared_memory(nullptr)
196 , ring_buffer(nullptr)
197 , current_reading_buffer(0)
198 , data_expiry_time_us(2000000)
200 if (!std::is_standard_layout<T>::value)
202 throw std::runtime_error(
"shm::Subscriber: Be setted not POD class!");
209 template <
typename T>
212 if (ring_buffer !=
nullptr)
216 if (shared_memory !=
nullptr)
218 delete shared_memory;
228 template <
typename T>
232 if (shared_memory ==
nullptr || shared_memory->isDisconnected())
234 shared_memory->connect();
235 if (shared_memory->isDisconnected())
238 return std::vector<T>(0);
240 ring_buffer =
new RingBuffer(shared_memory->getPtr());
241 size_t element_size = ring_buffer->getElementSize();
242 vector_size = element_size /
sizeof(T);
244 int newest_buffer = ring_buffer->getNewestBufferNum();
245 if (newest_buffer < 0)
248 T * first_ptr = &((
reinterpret_cast<T*
>(ring_buffer->getDataList()))[current_reading_buffer*vector_size]);
249 T * last_ptr = &((
reinterpret_cast<T*
>(ring_buffer->getDataList()))[current_reading_buffer*vector_size + vector_size]);
250 return std::vector<T>(first_ptr, last_ptr);
254 current_reading_buffer = newest_buffer;
255 T * first_ptr = &((
reinterpret_cast<T*
>(ring_buffer->getDataList()))[newest_buffer*vector_size]);
256 T * last_ptr = &((
reinterpret_cast<T*
>(ring_buffer->getDataList()))[newest_buffer*vector_size + vector_size - 1]);
257 return std::vector<T>(first_ptr, last_ptr);
261 template <
typename T>
265 if (shared_memory->isDisconnected())
267 shared_memory->connect();
268 if (shared_memory->isDisconnected())
272 ring_buffer =
new RingBuffer(shared_memory->getPtr());
275 return ring_buffer->waitFor(timeout_usec);
279 template <
typename T>
281 Subscriber<std::vector<T>>::setDataExpiryTime_us(uint64_t time_us)
283 data_expiry_time_us = time_us;
284 if (ring_buffer !=
nullptr)
286 ring_buffer->setDataExpiryTime_us(data_expiry_time_us);