Skip to content

Commit 0ffe2f4

Browse files
authored
Added peekstr and peektyp methods
These methods peeks a part and return a copy of its value as a string or the specified type.
1 parent 0feae8c commit 0ffe2f4

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

zmq_addon.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2016 VOCA AS / Harald Nøkland
2+
Copyright (c) 2016 VOCA AS / Harald Nøkland
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to
@@ -36,7 +36,7 @@ namespace zmq {
3636
/*
3737
This class handles multipart messaging. It is the C++ equivalent of zmsg.h,
3838
which is part of CZMQ (the high-level C binding). Furthermore, it is a major
39-
improvement compared to zmsg.hpp, which is part of the examples in the ØMQ
39+
improvement compared to zmsg.hpp, which is part of the examples in the ØMQ
4040
Guide. Unnecessary copying is avoided by using move semantics to efficiently
4141
add/remove parts.
4242
*/
@@ -327,6 +327,23 @@ class multipart_t
327327
{
328328
return &m_parts[index];
329329
}
330+
331+
// Get a string copy of a specific message part
332+
std::string peekstr(size_t index) const
333+
{
334+
std::string string(m_parts[index].data<char>(), m_parts[index].size());
335+
return string;
336+
}
337+
338+
// Peek type (fixed-size) from front
339+
template<typename T>
340+
T peektyp(size_t index)
341+
{
342+
static_assert(!std::is_same<T, std::string>::value, "Use peekstr() instead of peektyp<std::string>()");
343+
if(sizeof(T) != m_parts.front().size())
344+
throw std::runtime_error("Invalid type, size does not match the message size");
345+
T type = *m_parts[index].data<T>();
346+
}
330347

331348
// Create multipart from type (fixed-size)
332349
template<typename T>

0 commit comments

Comments
 (0)