gusimplewhiteboard
gugenericwhiteboardobject.h
Go to the documentation of this file.
1/*
2 * gugenericwhiteboardobject.h
3 *
4 * Created by Carl Lusty in 2013.
5 * Copyright (c) 2013 Carl Lusty
6 * All rights reserved.
7 */
8
9#ifndef GENERIC_WB_OBJ_H
10#define GENERIC_WB_OBJ_H
11
12#include "gusimplewhiteboard.h"
13
14#pragma clang diagnostic push
15#pragma clang diagnostic ignored "-Wweak-vtables"
16#pragma clang diagnostic ignored "-Wpadded"
17#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
18#pragma clang diagnostic ignored "-Wdeprecated"
19#pragma clang diagnostic ignored "-Wreserved-id-macro"
20
21#include <cstddef>
22#include <cassert>
23#include <iostream>
24#include <string>
25#include <vector>
26
27#include <gu_util.h>
28
29#ifdef bool
30#undef bool
31#endif
32
33#ifdef true
34#undef true
35#undef false
36#endif
37
39
78template <class object_type> class generic_whiteboard_object
79{
80protected:
82 uint16_t type_offset;
83 bool atomic;
86 bool isFirst = true;
87
88public:
92 generic_whiteboard_object(gu_simple_whiteboard_descriptor *wbd, uint16_t toffs, bool want_atomic = true, bool do_notify_subscribers = true) //Constructor
93 {
94 init(toffs, wbd, want_atomic, do_notify_subscribers);
95 }
96
101 {
102 init(source.type_offset, source._wbd, source.atomic, source.notify_subscribers);
103 }
104
108 generic_whiteboard_object(const object_type &value, uint16_t toffs, gu_simple_whiteboard_descriptor *wbd = NULLPTR, bool want_atomic = true)
109 {
110 init(toffs, wbd, want_atomic);
111 set(value);
112 }
113
117 void init(uint16_t toffs, gu_simple_whiteboard_descriptor *wbd = NULLPTR, bool want_atomic = true, bool do_notify_subscribers = true)
118 {
119 if(!wbd)
120 {
122 }
123 type_offset = toffs;
124 atomic = want_atomic;
125 notify_subscribers = do_notify_subscribers;
126 _wbd = wbd;
127 }
128
132 void set(const object_type &msg);
133
137 object_type get()
138 {
139// return *(object_type *)gsw_current_message(_wbd->wb, type_offset);
141 }
142
146 object_type get_from(gu_simple_message *msg);
147
151 void post(const object_type &msg) { set(msg); }
152
156 const object_type &operator<<(const object_type &value)
157 {
158 set(value);
159
160 return value;
161 }
162
167 {
168 value = get();
169
170 return *this;
171 }
172
176 const object_type &operator=(const object_type &value)
177 {
178 set(value);
179
180 return value;
181 }
182
186 object_type operator=(object_type value)
187 {
188 set(value);
189
190 return value;
191 }
192
196 operator object_type()
197 {
198 return get();
199 }
200
204 object_type operator()()
205 {
206 return get();
207 }
208
212 void operator()(object_type value)
213 {
214 set(value);
215 }
216
218 const u_int16_t newCount = this->_wbd->wb->event_counters[this->type_offset];
219 if (this->isFirst) {
220 this->isFirst = false;
221 this->last_count = newCount;
222 return true;
223 }
224 const bool isNew = newCount != this->last_count;
225 this->last_count = newCount;
226 return isNew;
227 }
228};
229
241template<> std::vector<int> generic_whiteboard_object<std::vector<int> >::get_from(gu_simple_message *msg);
247template<> std::vector<bool> generic_whiteboard_object<std::vector<bool> >::get_from(gu_simple_message *msg);
248
254template <typename object_type>
256{
257 return *reinterpret_cast<object_type *>(msg);
258}
259
264template<> void generic_whiteboard_object<std::string>::set(const std::string &msg);
269template<> void generic_whiteboard_object<std::vector<int> >::set(const std::vector<int> &msg);
274template<> void generic_whiteboard_object<std::vector<bool> >::set(const std::vector<bool> &msg);
275
280template <class object_type>
282{
283 int t = type_offset;
284
285#ifndef NO_SAFETY
286 assert(GU_SIMPLE_WHITEBOARD_BUFSIZE >= sizeof(object_type));
287#endif
288 if (atomic) gsw_procure(_wbd->sem, GSW_SEM_PUTMSG);
289
290 gu_simple_whiteboard *wb = _wbd->wb;
292 object_type *wbobj = reinterpret_cast<object_type*>(m);
293 *wbobj = msg;
294
295 gsw_increment(wb, t);
297 if (atomic) gsw_vacate(_wbd->sem, GSW_SEM_PUTMSG);
298 if (notify_subscribers && wb->subscribed) gsw_signal_subscribers(wb);
299}
300
301#pragma clang diagnostic pop
302
303
304#endif //GENERIC_WB_OBJ_H
This class allows you to set and get data directly into or out of the local whiteboard in shared memo...
void post(const object_type &msg)
post method (calls set())
generic_whiteboard_object< object_type > & operator>>(object_type &value)
shift right operator (calls get())
gu_simple_whiteboard_descriptor * _wbd
generic_whiteboard_object(gu_simple_whiteboard_descriptor *wbd, uint16_t toffs, bool want_atomic=true, bool do_notify_subscribers=true)
designated constructor
void init(uint16_t toffs, gu_simple_whiteboard_descriptor *wbd=NULLPTR, bool want_atomic=true, bool do_notify_subscribers=true)
intialiser (called from constructors)
void set(const object_type &msg)
designated setter for posting whiteboard messages
object_type operator=(object_type value)
assignment copy operator (calls set())
generic_whiteboard_object(const generic_whiteboard_object< object_type > &source)
copy constructor
void operator()(object_type value)
function operator with object_type copy parameter (calls set())
object_type get()
designated getter for getting a whiteboard message
object_type operator()()
empty function operator (calls get())
const object_type & operator<<(const object_type &value)
shift left operator (calls set())
const object_type & operator=(const object_type &value)
assignment operator (calls set())
object_type get_from(gu_simple_message *msg)
access method to get data from an existing, low-level message
generic_whiteboard_object(const object_type &value, uint16_t toffs, gu_simple_whiteboard_descriptor *wbd=NULLPTR, bool want_atomic=true)
value conversion reference constructor (needs to be overridden by subclasses to set toffs to be usefu...
gu_simple_whiteboard_descriptor * local_whiteboard_descriptor
gu_simple_message * gsw_next_message(gu_simple_whiteboard *wb, int i)
get the next shared memory location for the given whiteboard message type i
void gsw_increment(gu_simple_whiteboard *wb, int i)
get the next shared memory location for the given whiteboard message type i
void gsw_increment_event_counter(gu_simple_whiteboard *wb, int i)
add to a messages event counter on the wb
gu_simple_whiteboard_descriptor * get_local_singleton_whiteboard(void)
create a simple whiteboard for the local singleton wb pointer
gu_simple_message * gsw_current_message(gu_simple_whiteboard *wb, int i)
get the current shared memory location for the given whiteboard message type i
int gsw_procure(gsw_sema_t sem, enum gsw_semaphores s)
grab a whiteboard semaphore
int gsw_vacate(gsw_sema_t sem, enum gsw_semaphores s)
release a whiteboard semaphore
void gsw_signal_subscribers(gu_simple_whiteboard *wb)
signal all subscribing processes
#define u_int16_t
@ GSW_SEM_PUTMSG
semaphore for adding to the whiteboard
#define GU_SIMPLE_WHITEBOARD_BUFSIZE
message len (max)
the actual whiteboard in shared mem
uint16_t event_counters[512]
event counter loops
uint16_t subscribed
subscribed processes
the underlying whiteboard object
gu_simple_whiteboard * wb
the actual whiteboard in shared mem
union type that is used to store data in shared memory