Files
allwpilib/networktables/cpp/lib/share/networktables2/connection/DataIOStream.cpp
Tyler Veness f96b61ef11 artf4106: ISO C++ forbids variable-size array
C++14 changed the definition of VLAs, so the previous usage was no longer valid ISO C++. GCC 5.1.0 actually enforces this definition, so this commit fixes the build under GCC 5.1.0. This also builds under GCC 4.9.1.

Change-Id: Ib5ae2c49b4c4c21455b722b6633d7841066b4872
2015-08-13 12:26:46 -07:00

77 lines
1.7 KiB
C++

#include "networktables2/connection/DataIOStream.h"
#include <memory>
///This is used in case NULL is passed so all logic calls to IOstream can continue to assume there is never a null pointer
class InertStream : public IOStream
{
protected:
virtual int read(void* ptr, int numbytes) {return numbytes;} //return success
virtual int write(const void* ptr, int numbytes) {return numbytes;}
virtual void flush() {}
virtual void close() {}
};
static InertStream s_InertStream;
DataIOStream::DataIOStream(IOStream* _iostream) :
iostream(_iostream)
{
}
DataIOStream::~DataIOStream()
{
close();
if (iostream!=&s_InertStream)
delete iostream;
}
void DataIOStream::SetIOStream(IOStream* stream)
{
IOStream *temp=iostream;
iostream=stream ? stream : &s_InertStream; //We'll never assign NULL
if (temp!=&s_InertStream)
delete temp;
}
void DataIOStream::close()
{
iostream->close();
}
void DataIOStream::writeByte(uint8_t b)
{
iostream->write(&b, 1);
}
void DataIOStream::write2BytesBE(uint16_t s)
{
writeByte((uint8_t)(s >> 8));
writeByte((uint8_t)s);
}
void DataIOStream::writeString(std::string& str)
{
write2BytesBE(str.length());
iostream->write(str.c_str(), str.length());
}
void DataIOStream::flush()
{
iostream->flush();
}
uint8_t DataIOStream::readByte()
{
uint8_t value;
iostream->read(&value, 1);
return value;
}
uint16_t DataIOStream::read2BytesBE()
{
uint16_t value;
value = readByte()<<8 | readByte();
return value;
}
std::string* DataIOStream::readString()
{
unsigned int byteLength = read2BytesBE();
auto bytes = std::make_unique<char[]>(byteLength);
iostream->read(bytes.get(), byteLength);
return new std::string(bytes.get());//FIXME implement UTF-8 aware version
}