pdustream output operators
This commit is contained in:
parent
555122bd3b
commit
7e58ce5562
128
acn/pdu.cpp
128
acn/pdu.cpp
|
@ -28,6 +28,107 @@
|
|||
namespace ACN {
|
||||
namespace PDU {
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator >>
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator>> (uint8_t& val)
|
||||
{
|
||||
val = readType<uint8_t>();
|
||||
return *this;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator >>
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator>> (uint16_t& val)
|
||||
{
|
||||
val = readType<uint16_t>();
|
||||
return *this;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator >>
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator>> (uint32_t& val)
|
||||
{
|
||||
val = readType<uint32_t>();
|
||||
return *this;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator >>
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator>> (uint64_t& val)
|
||||
{
|
||||
val = readType<uint64_t>();
|
||||
return *this;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator <<
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator<< (const uint8_t& val)
|
||||
{
|
||||
writeType<uint8_t>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator <<
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator<< (const uint16_t& val)
|
||||
{
|
||||
writeType<uint16_t>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator <<
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator<< (const uint32_t& val)
|
||||
{
|
||||
writeType<uint32_t>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_stream::operator <<
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
pdu_stream& pdu_stream::operator<< (const uint64_t& val)
|
||||
{
|
||||
writeType<uint64_t>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Pdu::Pdu
|
||||
* @param stream
|
||||
* @param vector_size
|
||||
*/
|
||||
Pdu::Pdu(Stream stream, size_t vector_size)
|
||||
: flags_(stream->peek())
|
||||
{
|
||||
|
@ -76,12 +177,19 @@ Pdu::Pdu(Stream stream, size_t vector_size)
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Pdu::~Pdu
|
||||
*/
|
||||
Pdu::~Pdu() {
|
||||
if (header_) delete header_;
|
||||
if (data_) delete data_;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Pdu::vector
|
||||
* @return
|
||||
*/
|
||||
const uint32_t Pdu::vector() {
|
||||
if (flags_.hasVector)
|
||||
return vector_;
|
||||
|
@ -91,6 +199,10 @@ const uint32_t Pdu::vector() {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Pdu::header
|
||||
* @return
|
||||
*/
|
||||
pdu_header * Pdu::header() {
|
||||
if (flags_.hasHeader)
|
||||
return header_;
|
||||
|
@ -100,6 +212,10 @@ pdu_header * Pdu::header() {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Pdu::data
|
||||
* @return
|
||||
*/
|
||||
pdu_data * Pdu::data() {
|
||||
if (flags_.hasData)
|
||||
return data_;
|
||||
|
@ -109,6 +225,10 @@ pdu_data * Pdu::data() {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Pdu::readLength
|
||||
* @param stream
|
||||
*/
|
||||
void Pdu::readLength(Stream stream) {
|
||||
length_ = stream->readType<uint16_t>() & 0x0fff; // high 4 bytes are flags
|
||||
if (flags_.hasLength)
|
||||
|
@ -116,6 +236,10 @@ void Pdu::readLength(Stream stream) {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Pdu::readVector
|
||||
* @param vector_size
|
||||
*/
|
||||
void Pdu::readVector(uint8_t vector_size) {
|
||||
vector_ = 0;
|
||||
for (int o = vector_size - 1; o >= 0; o--)
|
||||
|
@ -123,6 +247,10 @@ void Pdu::readVector(uint8_t vector_size) {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief pdu_flags::pdu_flags
|
||||
* @param val
|
||||
*/
|
||||
pdu_flags::pdu_flags(uint8_t val) {
|
||||
hasLength = (val >> 7) & 0b1;
|
||||
hasVector = (val >> 6) & 0b1;
|
||||
|
|
25
acn/pdu.h
25
acn/pdu.h
|
@ -72,18 +72,14 @@ public:
|
|||
}
|
||||
uint32_t available() { return in_avail(); }
|
||||
uint8_t * data() { return gptr(); };
|
||||
pdu_stream& operator>> (uint8_t& val) {
|
||||
val = readType<uint8_t>();
|
||||
return *this;
|
||||
};
|
||||
pdu_stream& operator>> (uint16_t& val) {
|
||||
val = readType<uint16_t>();
|
||||
return *this;
|
||||
};
|
||||
pdu_stream& operator>> (uint32_t& val) {
|
||||
val = readType<uint32_t>();
|
||||
return *this;
|
||||
};
|
||||
pdu_stream& operator>> (uint8_t& val);
|
||||
pdu_stream& operator>> (uint16_t& val);
|
||||
pdu_stream& operator>> (uint32_t& val);
|
||||
pdu_stream& operator>> (uint64_t& val);
|
||||
pdu_stream& operator<< (const uint8_t& val);
|
||||
pdu_stream& operator<< (const uint16_t& val);
|
||||
pdu_stream& operator<< (const uint32_t& val);
|
||||
pdu_stream& operator<< (const uint64_t& val);
|
||||
template<typename T> T readType() {
|
||||
if (in_avail() < sizeof(T)) {
|
||||
setstate(std::ios_base::failbit);
|
||||
|
@ -97,6 +93,11 @@ public:
|
|||
setstate(std::ios_base::eofbit);
|
||||
return ret;
|
||||
}
|
||||
template<typename T> void writeType (const T& val) {
|
||||
auto data = reinterpret_cast<const uint8_t*>(&val);
|
||||
for (int i = sizeof(T); --i >= 0; )
|
||||
put(data[i]);
|
||||
}
|
||||
};
|
||||
using Stream = shared_ptr<pdu_stream>;
|
||||
|
||||
|
|
Loading…
Reference in New Issue