mirror of
https://github.com/zeromq/cppzmq.git
synced 2024-12-14 02:57:48 +01:00
Added peekstr and peektyp methods
These methods peeks a part and return a copy of its value as a string or the specified type.
This commit is contained in:
parent
0feae8c3e9
commit
0ffe2f4974
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright (c) 2016 VOCA AS / Harald Nøkland
|
Copyright (c) 2016 VOCA AS / Harald Nøkland
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to
|
of this software and associated documentation files (the "Software"), to
|
||||||
@ -36,7 +36,7 @@ namespace zmq {
|
|||||||
/*
|
/*
|
||||||
This class handles multipart messaging. It is the C++ equivalent of zmsg.h,
|
This class handles multipart messaging. It is the C++ equivalent of zmsg.h,
|
||||||
which is part of CZMQ (the high-level C binding). Furthermore, it is a major
|
which is part of CZMQ (the high-level C binding). Furthermore, it is a major
|
||||||
improvement compared to zmsg.hpp, which is part of the examples in the ØMQ
|
improvement compared to zmsg.hpp, which is part of the examples in the ØMQ
|
||||||
Guide. Unnecessary copying is avoided by using move semantics to efficiently
|
Guide. Unnecessary copying is avoided by using move semantics to efficiently
|
||||||
add/remove parts.
|
add/remove parts.
|
||||||
*/
|
*/
|
||||||
@ -328,6 +328,23 @@ public:
|
|||||||
return &m_parts[index];
|
return &m_parts[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get a string copy of a specific message part
|
||||||
|
std::string peekstr(size_t index) const
|
||||||
|
{
|
||||||
|
std::string string(m_parts[index].data<char>(), m_parts[index].size());
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Peek type (fixed-size) from front
|
||||||
|
template<typename T>
|
||||||
|
T peektyp(size_t index)
|
||||||
|
{
|
||||||
|
static_assert(!std::is_same<T, std::string>::value, "Use peekstr() instead of peektyp<std::string>()");
|
||||||
|
if(sizeof(T) != m_parts.front().size())
|
||||||
|
throw std::runtime_error("Invalid type, size does not match the message size");
|
||||||
|
T type = *m_parts[index].data<T>();
|
||||||
|
}
|
||||||
|
|
||||||
// Create multipart from type (fixed-size)
|
// Create multipart from type (fixed-size)
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static multipart_t create(const T& type)
|
static multipart_t create(const T& type)
|
||||||
|
Loading…
Reference in New Issue
Block a user