gusimplewhiteboard
gugenericwhiteboardobject.cpp
Go to the documentation of this file.
1/*
2 * gugenericwhiteboardobject.cpp
3 *
4 * Created by Carl Lusty in 2013.
5 * Copyright (c) 2013 Carl Lusty
6 * All rights reserved.
7 */
8
10#include <gu_util.h>
11
16template <>
18{
19 int t = type_offset;
20
21#ifndef NO_SAFETY
22 assert(GU_SIMPLE_WHITEBOARD_BUFSIZE > msg.length());
23#endif
24 if (atomic) gsw_procure(_wbd->sem, GSW_SEM_PUTMSG);
25
26 gu_simple_whiteboard *wb = _wbd->wb;
28
29 gu_strlcpy(m->string, msg.c_str(), msg.length()+1);
30
31 gsw_increment(wb, t);
33 if (atomic) gsw_vacate(_wbd->sem, GSW_SEM_PUTMSG);
34 if (notify_subscribers && wb->subscribed) gsw_signal_subscribers(wb);
35}
36
41template <>
42void generic_whiteboard_object<std::vector<int> >::set(const std::vector<int> &msg)
43{
44 int t = type_offset;
45
46#ifndef NO_SAFETY
47 assert(GU_SIMPLE_WHITEBOARD_BUFSIZE >= (msg.size()*sizeof(int)));
48#endif
49 if (atomic) gsw_procure(_wbd->sem, GSW_SEM_PUTMSG);
50
51 gu_simple_whiteboard *wb = _wbd->wb;
53
54 memcpy(m->ivec, &msg[0], msg.size()*sizeof(int));
55
56 gsw_increment(wb, t);
58 if (atomic) gsw_vacate(_wbd->sem, GSW_SEM_PUTMSG);
59 if (notify_subscribers && wb->subscribed) gsw_signal_subscribers(wb);
60}
61
66template <>
67void generic_whiteboard_object<std::vector<bool> >::set(const std::vector<bool> &msg)
68{
69 int t = type_offset;
70
71#ifndef NO_SAFETY
72 assert(GU_SIMPLE_WHITEBOARD_BUFSIZE >= (msg.size()*sizeof(bool)));
73#endif
74 if (atomic) gsw_procure(_wbd->sem, GSW_SEM_PUTMSG);
75
76 gu_simple_whiteboard *wb = _wbd->wb;
78
79 //The STL vector specialisation for bool may not store an array of bools, it may be bits.
80 //http://www.cplusplus.com/reference/vector/vector-bool/
81 //That's why this loops instead of doing a memcpy
82 for(size_t i = 0; i < msg.size(); i++)
83 m->bvec[i] = msg.at(i);
84
85 gsw_increment(wb, t);
87 if (atomic) gsw_vacate(_wbd->sem, GSW_SEM_PUTMSG);
88 if (notify_subscribers && wb->subscribed) gsw_signal_subscribers(wb);
89}
90
96template<>
98{
99 return msg->string;
100}
101
107template<>
109{
110 return std::vector<int> (msg->ivec, msg->ivec + sizeof msg->ivec / sizeof msg->ivec[0]);
111}
112
118template<>
120{
121 return std::vector<bool> (msg->bvec, msg->bvec + sizeof msg->bvec / sizeof msg->bvec[0]);
122}
This class allows you to set and get data directly into or out of the local whiteboard in shared memo...
void set(const object_type &msg)
designated setter for posting whiteboard messages
object_type get_from(gu_simple_message *msg)
access method to get data from an existing, low-level message
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
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
@ 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 subscribed
subscribed processes
union type that is used to store data in shared memory
char string[128]
string type
bool bvec[128/sizeof(bool)]
bool array
int ivec[128/sizeof(int)]
int array