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:
Vincent Tellier 2017-06-07 09:54:37 +02:00 committed by GitHub
parent 0feae8c3e9
commit 0ffe2f4974

View File

@ -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
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,
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
add/remove parts.
*/
@ -327,6 +327,23 @@ public:
{
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)
template<typename T>