zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
message.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * This file is part of zmqpp.
7  * Copyright (c) 2011-2015 Contributors as noted in the AUTHORS file.
8  */
9 
17 #ifndef ZMQPP_MESSAGE_HPP_
18 #define ZMQPP_MESSAGE_HPP_
19 
20 #include <cassert>
21 #include <functional>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 #include <utility>
26 
27 #include <zmq.h>
28 
29 #include "compatibility.hpp"
30 #include "frame.hpp"
31 #include "signal.hpp"
32 
33 namespace zmqpp
34 {
35 
43 class message
44 {
45 public:
56  typedef std::function<void (void*)> release_function;
57 
58  message();
59  ~message();
60 
61  template <typename T, typename ...Args>
62  message(T const &part, Args &&...args)
63  : message()
64  {
65  add(part, std::forward<Args>(args)...);
66  }
67 
68  size_t parts() const;
69  size_t size(size_t const part) const;
70  std::string get(size_t const part) const;
71 
72  void get(int8_t& integer, size_t const part) const;
73  void get(int16_t& integer, size_t const part) const;
74  void get(int32_t& integer, size_t const part) const;
75  void get(int64_t& integer, size_t const part) const;
76  void get(signal& sig, size_t const part) const;
77 
78  void get(uint8_t& unsigned_integer, size_t const part) const;
79  void get(uint16_t& unsigned_integer, size_t const part) const;
80  void get(uint32_t& unsigned_integer, size_t const part) const;
81  void get(uint64_t& unsigned_integer, size_t const part) const;
82 
83  void get(float& floating_point, size_t const part) const;
84  void get(double& double_precision, size_t const part) const;
85  void get(bool& boolean, size_t const part) const;
86 
87  void get(std::string& string, size_t const part) const;
88 
89  // Warn: If a pointer type is requested the message (well zmq) still 'owns'
90  // the data and will release it when the message object is freed.
91  template<typename Type>
92  Type get(size_t const part)
93  {
94  Type value;
95  get(value, part);
96  return value;
97  }
98 
99  template<int part=0, typename T, typename ...Args>
100  void extract(T &nextpart, Args &...args)
101  {
102  assert(part < parts());
103  get(nextpart,part);
104  extract<part+1>(args...);
105  }
106 
107  template<int part=0, typename T>
108  void extract(T &nextpart)
109  {
110  assert(part < parts());
111  get(nextpart,part);
112  }
113 
114  // Raw get data operations, useful with data structures more than anything else
115  // Warn: The message (well zmq) still 'owns' the data and will release it
116  // when the message object is freed.
117  template<typename Type>
118  void get(Type*& value, size_t const part) const
119  {
120  value = static_cast<Type*>(raw_data(part));
121  }
122 
123  // Warn: The message (well zmq) still 'owns' the data and will release it
124  // when the message object is freed.
125  template<typename Type>
126  void get(Type** value, size_t const part) const
127  {
128  *value = static_cast<Type*>(raw_data(part));
129  }
130 
131  // Move operators will take ownership of message parts without copying
132  void move(void* part, size_t const size, release_function const& release);
133 
134  // Raw move data operation, useful with data structures more than anything else
135  template<typename Object>
136  void move(Object *part)
137  {
138  move(part, sizeof(Object), &deleter_callback<Object>);
139  }
140 
141  // Copy operators will take copies of any data
142  template<typename Type, typename ...Args>
143  void add(Type const& part, Args &&...args)
144  {
145  *this << part;
146  add(std::forward<Args>(args)...);
147  }
148 
149  template<typename Type>
150  void add(Type const part)
151  {
152  *this << part;
153  }
154 
155  // Copy operators will take copies of any data with a given size
156  template<typename Type>
157  void add_raw(Type *part, size_t const data_size)
158  {
159  _parts.push_back( frame( part, data_size ) );
160  }
161 
162  // Use exact data past, neither zmqpp nor 0mq will copy, alter or delete
163  // this data. It must remain as valid for at least the lifetime of the
164  // 0mq message, recommended only with const data.
165  template<typename Type>
166  void add_const(Type *part, size_t const data_size)
167  {
168  _parts.push_back( frame( part, data_size, nullptr, nullptr ) );
169  }
170 
171  // Stream reader style
172  void reset_read_cursor();
173 
174  template<typename Type>
175  message& operator>>(Type& value)
176  {
177  get(value, _read_cursor++);
178  return *this;
179  }
180 
181  // Stream writer style - these all use copy styles
182  message& operator<<(int8_t const integer);
183  message& operator<<(int16_t const integer);
184  message& operator<<(int32_t const integer);
185  message& operator<<(int64_t const integer);
186  message& operator<<(signal const sig);
187 
188  message& operator<<(uint8_t const unsigned_integer);
189  message& operator<<(uint16_t const unsigned_integer);
190  message& operator<<(uint32_t const unsigned_integer);
191  message& operator<<(uint64_t const unsigned_integer);
192 
193  message& operator<<(float const floating_point);
194  message& operator<<(double const double_precision);
195  message& operator<<(bool const boolean);
196 
197  message& operator<<(char const* c_string);
198  message& operator<<(std::string const& string);
199 
200  // Queue manipulation
201  void push_front(void const* part, size_t const size);
202 
203  // TODO: unify conversion of types with the stream operators
204  void push_front(int8_t const integer);
205  void push_front(int16_t const integer);
206  void push_front(int32_t const integer);
207  void push_front(int64_t const integer);
208  void push_front(signal const sig);
209 
210  void push_front(uint8_t const unsigned_integer);
211  void push_front(uint16_t const unsigned_integer);
212  void push_front(uint32_t const unsigned_integer);
213  void push_front(uint64_t const unsigned_integer);
214 
215  void push_front(float const floating_point);
216  void push_front(double const double_precision);
217  void push_front(bool const boolean);
218 
219  void push_front(char const* c_string);
220  void push_front(std::string const& string);
221 
222  void pop_front();
223 
224  void push_back(void const* part, size_t const data_size)
225  {
226  add_raw( part, data_size );
227  }
228 
229  template<typename Type>
230  void push_back(Type const part)
231  {
232  *this << part;
233  }
234 
235  void pop_back();
236 
237  void remove(size_t const part);
238 
239  // Move supporting
240  message(message&& source) NOEXCEPT;
241  message& operator=(message&& source) NOEXCEPT;
242 
243  // Copy support
244  message copy() const;
245  void copy(message const& source);
246 
247  // Used for internal tracking
248  void sent(size_t const part);
249 
250  // Access to raw zmq details
251  void const* raw_data(size_t const part = 0) const;
252  zmq_msg_t& raw_msg(size_t const part = 0);
253  zmq_msg_t& raw_new_msg();
254  zmq_msg_t& raw_new_msg(size_t const reserve_data_size);
255 
262  bool is_signal() const;
263 
267  size_t read_cursor() const NOEXCEPT { return _read_cursor; }
268 
272  size_t remaining() const NOEXCEPT { return _parts.size() - _read_cursor; }
273 
278  size_t next() NOEXCEPT { return ++_read_cursor; }
279 
280 
281 #if (ZMQ_VERSION_MAJOR == 4 && ZMQ_VERSION_MINOR >= 1)
282 
288  bool get_property(const std::string &property, std::string &out);
289 #endif
290 
291 private:
292  typedef std::vector<frame> parts_type;
293  parts_type _parts;
294  size_t _read_cursor;
295 
296  // Disable implicit copy support, code must request a copy to clone
298  message& operator=(message const&) NOEXCEPT ZMQPP_EXPLICITLY_DELETED;
299 
300  static void release_callback(void* data, void* hint);
301 
302  template<typename Object>
303  static void deleter_callback(void* data)
304  {
305  delete static_cast<Object*>(data);
306  }
307 };
308 
309 }
310 
311 #endif /* ZMQPP_MESSAGE_HPP_ */
message copy() const
Definition: message.cpp:444
void add(Type const &part, Args &&...args)
Definition: message.hpp:143
bool is_signal() const
Check if the message is a signal.
Definition: message.cpp:483
void extract(T &nextpart)
Definition: message.hpp:108
void extract(T &nextpart, Args &...args)
Definition: message.hpp:100
void move(void *part, size_t const size, release_function const &release)
Definition: message.cpp:107
void const * raw_data(size_t const part=0) const
Definition: message.cpp:66
#define ZMQPP_EXPLICITLY_DELETED
Definition: compatibility.hpp:100
C++ wrapper around zmq.
Definition: actor.cpp:29
message & operator>>(Type &value)
Definition: message.hpp:175
size_t next() NOEXCEPT
Moves the read cursor to the next element.
Definition: message.hpp:278
message(T const &part, Args &&...args)
Definition: message.hpp:62
message & operator<<(int8_t const integer)
Definition: message.cpp:225
std::vector< frame > parts_type
Definition: message.hpp:292
size_t remaining() const NOEXCEPT
Gets the remaining number of parts in the message.
Definition: message.hpp:272
void reset_read_cursor()
Definition: message.cpp:116
zmq_msg_t & raw_msg(size_t const part=0)
Definition: message.cpp:76
static void release_callback(void *data, void *hint)
Definition: message.cpp:475
void push_back(Type const part)
Definition: message.hpp:230
void move(Object *part)
Definition: message.hpp:136
void add_raw(Type *part, size_t const data_size)
Definition: message.hpp:157
void sent(size_t const part)
Definition: message.cpp:465
#define NOEXCEPT
Definition: compatibility.hpp:104
void push_back(void const *part, size_t const data_size)
Definition: message.hpp:224
~message()
Definition: message.cpp:40
size_t read_cursor() const NOEXCEPT
Gets the read cursor.
Definition: message.hpp:267
size_t size(size_t const part) const
Definition: message.cpp:56
std::function< void(void *)> release_function
callback to release user allocated data.
Definition: message.hpp:56
void push_front(void const *part, size_t const size)
Definition: message.cpp:330
void pop_back()
Definition: message.cpp:423
size_t _read_cursor
Definition: message.hpp:294
message()
Definition: message.cpp:34
size_t parts() const
Definition: message.cpp:45
parts_type _parts
Definition: message.hpp:293
void add(Type const part)
Definition: message.hpp:150
a zmq message with optional multipart support
Definition: message.hpp:43
void add_const(Type *part, size_t const data_size)
Definition: message.hpp:166
static void deleter_callback(void *data)
Definition: message.hpp:303
signal
Signal is a 8 bytes integer.
Definition: signal.hpp:23
zmq_msg_t & raw_new_msg()
Definition: message.cpp:86
an internal frame wrapper for a single zmq message
Definition: frame.hpp:33
void pop_front()
Definition: message.cpp:418