Skip to content

Commit

Permalink
General changes to Stream classes
Browse files Browse the repository at this point in the history
* Move trivial code into header
* Use member variable initialisers instead of setting defaults in constructor
* Change int to unsigned/size_t as appropriate
* Change appropriate methods to const (except where there's a risk of breaking other code)
* Amend comments to clarify operation
* Mark inherited methods with virtual (if not already)
* Add ReadWriteStream::write(uint8_t) implementation to call write(const char*, size_t) method by default
  • Loading branch information
mikee47 committed Oct 18, 2018
1 parent 2937125 commit 4d88b92
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 166 deletions.
25 changes: 0 additions & 25 deletions Sming/SmingCore/Data/CircularBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@

#include "CircularBuffer.h"

CircularBuffer::CircularBuffer(int size) : buffer(new char[size]), readPos(buffer), writePos(buffer), size(size)
{
}

CircularBuffer::~CircularBuffer()
{
delete[] buffer;
}

StreamType CircularBuffer::getStreamType()
{
return StreamType::eSST_Memory;
}

uint16_t CircularBuffer::readMemoryBlock(char* data, int bufSize)
{
size_t bytesAvailable = available();
Expand Down Expand Up @@ -62,11 +48,6 @@ bool CircularBuffer::seek(int len)
return true;
}

bool CircularBuffer::isFinished()
{
return (available() < 1);
}

int CircularBuffer::available()
{
if(writePos >= readPos) {
Expand All @@ -83,12 +64,6 @@ size_t CircularBuffer::room() const
return readPos - writePos - 1;
}

String CircularBuffer::id()
{
// TODO: check if that is printing the address of the buffer...
return String((char*)&buffer);
}

size_t CircularBuffer::write(uint8_t charToWrite)
{
if(!room()) {
Expand Down
39 changes: 28 additions & 11 deletions Sming/SmingCore/Data/CircularBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@
class CircularBuffer : public ReadWriteStream
{
public:
CircularBuffer(int size);
CircularBuffer(int size) : buffer(new char[size]), readPos(buffer), writePos(buffer), size(size)
{
}

virtual ~CircularBuffer();
virtual ~CircularBuffer()
{
delete[] buffer;
}

/** @brief Get the stream type
* @retval StreamType The stream type.
* @todo Return value of IDataSourceStream:getStreamType base class function should be of type StreamType, e.g. eSST_User
*/
virtual StreamType getStreamType();
virtual StreamType getStreamType()
{
return StreamType::eSST_Memory;
}

/** @brief Read a block of memory
* @param data Pointer to the data to be read
Expand All @@ -52,9 +60,12 @@ class CircularBuffer : public ReadWriteStream
virtual bool seek(int len);

/** @brief Check if stream is finished
* @retval bool True on success.
*/
virtual bool isFinished();
* @retval bool true on success.
*/
virtual bool isFinished()
{
return available() < 1;
}

/**
* @brief Return the total length of the stream
Expand All @@ -66,7 +77,10 @@ class CircularBuffer : public ReadWriteStream
* @brief Returns unique id of the resource.
* @retval String the unique id of the stream.
*/
virtual String id();
virtual String id()
{
return String(reinterpret_cast<uint32_t>(&buffer), HEX);
}

virtual size_t write(uint8_t charToWrite);

Expand All @@ -75,8 +89,11 @@ class CircularBuffer : public ReadWriteStream
* @param size Quantity of chars to writen
* @retval size_t Quantity of chars written to stream
*/
virtual size_t write(const uint8_t* buffer, size_t size);
virtual size_t write(const uint8_t* data, size_t size);

/** @brief Get the maximum number of bytes for which write() will succeed
@retval size_t
*/
size_t room() const;

inline void flush()
Expand All @@ -92,9 +109,9 @@ class CircularBuffer : public ReadWriteStream
}

private:
char* buffer = NULL;
char* readPos = NULL;
char* writePos = NULL;
char* buffer = nullptr;
char* readPos = nullptr;
char* writePos = nullptr;
int size = 0;
};

Expand Down
10 changes: 9 additions & 1 deletion Sming/SmingCore/Data/Stream/DataSourceStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ class IDataSourceStream : public Stream
return -1;
}

/*
* From Stream class: We don't write using this stream
*/
virtual size_t write(uint8_t charToWrite)
{
return 0;
}

/**
* @brief Return the total length of the stream
* @retval int -1 is returned when the size cannot be determined
Expand All @@ -110,7 +118,7 @@ class IDataSourceStream : public Stream
*/
virtual String id()
{
return String();
return nullptr;
}
};

Expand Down
41 changes: 3 additions & 38 deletions Sming/SmingCore/Data/Stream/EndlessMemoryStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,26 @@

#include "EndlessMemoryStream.h"

EndlessMemoryStream::~EndlessMemoryStream()
{
delete stream;
stream = NULL;
}

StreamType EndlessMemoryStream::getStreamType()
{
return eSST_Memory;
}

uint16_t EndlessMemoryStream::readMemoryBlock(char* data, int bufSize)
{
if(stream == NULL) {
return 0;
}

return stream->readMemoryBlock(data, bufSize);
}

//Use base class documentation
bool EndlessMemoryStream::seek(int len)
{
if(stream == NULL) {
if(stream == nullptr) {
return false;
}

int res = stream->seek(len);
if(stream->isFinished()) {
delete stream;
stream = NULL;
stream = nullptr;
}

return res;
}

size_t EndlessMemoryStream::write(uint8_t charToWrite)
{
if(stream == NULL) {
stream = new MemoryDataStream();
}

return stream->write(charToWrite);
}

size_t EndlessMemoryStream::write(const uint8_t* buffer, size_t size)
{
if(stream == NULL) {
if(stream == nullptr) {
stream = new MemoryDataStream();
}

return stream->write(buffer, size);
}

bool EndlessMemoryStream::isFinished()
{
return false;
}
33 changes: 20 additions & 13 deletions Sming/SmingCore/Data/Stream/EndlessMemoryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,40 @@
class EndlessMemoryStream : public ReadWriteStream
{
public:
virtual ~EndlessMemoryStream();
virtual ~EndlessMemoryStream()
{
delete stream;
}

//Use base class documentation
virtual StreamType getStreamType();
virtual StreamType getStreamType()
{
return eSST_Memory;
}

virtual uint16_t readMemoryBlock(char* data, int bufSize);
virtual uint16_t readMemoryBlock(char* data, int bufSize)
{
return stream ? stream->readMemoryBlock(data, bufSize) : 0;
}

//Use base class documentation
virtual bool seek(int len);

/** @brief Write a single char to stream
* @param charToWrite Char to write to the stream
* @retval size_t Quantity of chars written to stream (always 1)
*/
virtual size_t write(uint8_t charToWrite);

/** @brief Write chars to stream
* @param buffer Pointer to buffer to write to the stream
* @param size Quantity of chars to write
* @retval size_t Quantity of chars written to stream
*/
virtual size_t write(const uint8_t* buffer, size_t size);

virtual bool isFinished();
/** @todo is this behaviour consistent with DataSourceStream ?
* It might be desirable to return true when _stream is null.
*/
virtual bool isFinished()
{
return false;
}

private:
MemoryDataStream* stream = NULL;
MemoryDataStream* stream = nullptr;
};

/** @} */
Expand Down
24 changes: 1 addition & 23 deletions Sming/SmingCore/Data/Stream/JsonObjectStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,12 @@

#include "JsonObjectStream.h"

JsonObjectStream::JsonObjectStream() : rootNode(buffer.createObject()), send(true)
{
}

JsonObjectStream::~JsonObjectStream()
{
}

JsonObject& JsonObjectStream::getRoot()
{
return rootNode;
}

uint16_t JsonObjectStream::readMemoryBlock(char* data, int bufSize)
{
if(rootNode != JsonObject::invalid() && send) {
if(send && rootNode.success()) {
rootNode.printTo(*this);
send = false;
}

return MemoryDataStream::readMemoryBlock(data, bufSize);
}

int JsonObjectStream::available()
{
if(rootNode == JsonObject::invalid()) {
return 0;
}

return rootNode.measureLength();
}
21 changes: 16 additions & 5 deletions Sming/SmingCore/Data/Stream/JsonObjectStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ class JsonObjectStream : public MemoryDataStream
public:
/** @brief Create a JSON object stream
*/
JsonObjectStream();
virtual ~JsonObjectStream();
JsonObjectStream() : rootNode(buffer.createObject())
{
}

virtual ~JsonObjectStream()
{
}

//Use base class documentation
virtual StreamType getStreamType()
Expand All @@ -34,7 +39,10 @@ class JsonObjectStream : public MemoryDataStream
/** @brief Get the JSON root node
* @retval JsonObject Reference to the root node
*/
JsonObject& getRoot();
JsonObject& getRoot()
{
return rootNode;
}

//Use base class documentation
virtual uint16_t readMemoryBlock(char* data, int bufSize);
Expand All @@ -43,12 +51,15 @@ class JsonObjectStream : public MemoryDataStream
* @brief Return the total length of the stream
* @retval int -1 is returned when the size cannot be determined
*/
int available();
int available()
{
return rootNode.success() ? rootNode.measureLength() : 0;
}

private:
DynamicJsonBuffer buffer;
JsonObject& rootNode;
bool send;
bool send = true;
};

/** @} */
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/Stream/LimitedMemoryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class LimitedMemoryStream : public ReadWriteStream
virtual bool isFinished();

private:
uint8_t* buffer = NULL;
uint8_t* buffer = nullptr;
size_t writePos = 0;
size_t readPos = 0;
size_t length = 0;
Expand Down
Loading

0 comments on commit 4d88b92

Please sign in to comment.