From 0e98e45a6fd91019ad765e1e439ad60e7f75432c Mon Sep 17 00:00:00 2001 From: Philip Rauwolf Date: Mon, 28 Jan 2013 15:22:20 +0100 Subject: [PATCH] Integrated and tested the TypeOutputStream with enumerations --- src/CommonAPI/DBus/DBusInputStream.cpp | 42 ++++ src/CommonAPI/DBus/DBusInputStream.h | 10 + src/CommonAPI/DBus/DBusOutputStream.cpp | 266 +++++++++++++++++++++++- src/CommonAPI/DBus/DBusOutputStream.h | 358 ++++++++++---------------------- src/test/DBusTypeStreamTest.cpp | 55 ++++- 5 files changed, 481 insertions(+), 250 deletions(-) diff --git a/src/CommonAPI/DBus/DBusInputStream.cpp b/src/CommonAPI/DBus/DBusInputStream.cpp index 2f39891..1356c31 100644 --- a/src/CommonAPI/DBus/DBusInputStream.cpp +++ b/src/CommonAPI/DBus/DBusInputStream.cpp @@ -197,6 +197,48 @@ void DBusInputStream::beginReadVersionVector() { savedStreamPositions_.push(currentDataPosition_); } +void DBusInputStream::beginReadInt8EnumVector() { + beginReadGenericVector(); + savedStreamPositions_.push(currentDataPosition_); +} + +void DBusInputStream::beginReadInt16EnumVector() { + beginReadGenericVector(); + savedStreamPositions_.push(currentDataPosition_); +} + +void DBusInputStream::beginReadInt32EnumVector() { + beginReadGenericVector(); + savedStreamPositions_.push(currentDataPosition_); +} + +void DBusInputStream::beginReadInt64EnumVector() { + beginReadGenericVector(); + alignToBoundary(8); + savedStreamPositions_.push(currentDataPosition_); +} + +void DBusInputStream::beginReadUInt8EnumVector() { + beginReadGenericVector(); + savedStreamPositions_.push(currentDataPosition_); +} + +void DBusInputStream::beginReadUInt16EnumVector() { + beginReadGenericVector(); + savedStreamPositions_.push(currentDataPosition_); +} + +void DBusInputStream::beginReadUInt32EnumVector() { + beginReadGenericVector(); + savedStreamPositions_.push(currentDataPosition_); +} + +void DBusInputStream::beginReadUInt64EnumVector() { + beginReadGenericVector(); + alignToBoundary(8); + savedStreamPositions_.push(currentDataPosition_); +} + void DBusInputStream::beginReadVectorOfSerializableStructs() { beginReadGenericVector(); alignToBoundary(8); diff --git a/src/CommonAPI/DBus/DBusInputStream.h b/src/CommonAPI/DBus/DBusInputStream.h index 7e580ee..6505923 100644 --- a/src/CommonAPI/DBus/DBusInputStream.h +++ b/src/CommonAPI/DBus/DBusInputStream.h @@ -84,6 +84,16 @@ class DBusInputStream: public InputStream { virtual void beginReadStringVector(); virtual void beginReadByteBufferVector(); virtual void beginReadVersionVector(); + + virtual void beginReadInt8EnumVector(); + virtual void beginReadInt16EnumVector(); + virtual void beginReadInt32EnumVector(); + virtual void beginReadInt64EnumVector(); + virtual void beginReadUInt8EnumVector(); + virtual void beginReadUInt16EnumVector(); + virtual void beginReadUInt32EnumVector(); + virtual void beginReadUInt64EnumVector(); + virtual void beginReadVectorOfSerializableStructs(); virtual void beginReadVectorOfSerializableVariants(); virtual void beginReadVectorOfVectors(); diff --git a/src/CommonAPI/DBus/DBusOutputStream.cpp b/src/CommonAPI/DBus/DBusOutputStream.cpp index 8cec0e1..bdefcd4 100644 --- a/src/CommonAPI/DBus/DBusOutputStream.cpp +++ b/src/CommonAPI/DBus/DBusOutputStream.cpp @@ -16,6 +16,201 @@ DBusOutputStream::DBusOutputStream(DBusMessage dbusMessage) : DBusOutputStream::~DBusOutputStream() { } +OutputStream& DBusOutputStream::writeValue(const bool& boolValue) { return writeBasicTypeValue(boolValue); } + +OutputStream& DBusOutputStream::writeValue(const int8_t& int8Value) { return writeBasicTypeValue(int8Value); } +OutputStream& DBusOutputStream::writeValue(const int16_t& int16Value) { return writeBasicTypeValue(int16Value); } +OutputStream& DBusOutputStream::writeValue(const int32_t& int32Value) { return writeBasicTypeValue(int32Value); } +OutputStream& DBusOutputStream::writeValue(const int64_t& int64Value) { return writeBasicTypeValue(int64Value); } + +OutputStream& DBusOutputStream::writeValue(const uint8_t& uint8Value) { return writeBasicTypeValue(uint8Value); } +OutputStream& DBusOutputStream::writeValue(const uint16_t& uint16Value) { return writeBasicTypeValue(uint16Value); } +OutputStream& DBusOutputStream::writeValue(const uint32_t& uint32Value) { return writeBasicTypeValue(uint32Value); } +OutputStream& DBusOutputStream::writeValue(const uint64_t& uint64Value) { return writeBasicTypeValue(uint64Value); } + +OutputStream& DBusOutputStream::writeValue(const float& floatValue) { return writeBasicTypeValue((double) floatValue); } +OutputStream& DBusOutputStream::writeValue(const double& doubleValue) { return writeBasicTypeValue(doubleValue); } + +OutputStream& DBusOutputStream::writeValue(const std::string& stringValue) { return writeString(stringValue.c_str(), stringValue.length()); } + +OutputStream& DBusOutputStream::writeValue(const ByteBuffer& byteBufferValue) { + return *this; +} + +OutputStream& DBusOutputStream::writeEnumValue(const int8_t& int8BackingTypeValue) { return writeValue(int8BackingTypeValue); } +OutputStream& DBusOutputStream::writeEnumValue(const int16_t& int16BackingTypeValue) { return writeValue(int16BackingTypeValue); } +OutputStream& DBusOutputStream::writeEnumValue(const int32_t& int32BackingTypeValue) { return writeValue(int32BackingTypeValue); } +OutputStream& DBusOutputStream::writeEnumValue(const int64_t& int64BackingTypeValue) { return writeValue(int64BackingTypeValue); } +OutputStream& DBusOutputStream::writeEnumValue(const uint8_t& uint8BackingTypeValue) { return writeValue(uint8BackingTypeValue); } +OutputStream& DBusOutputStream::writeEnumValue(const uint16_t& uint16BackingTypeValue) { return writeValue(uint16BackingTypeValue); } +OutputStream& DBusOutputStream::writeEnumValue(const uint32_t& uint32BackingTypeValue) { return writeValue(uint32BackingTypeValue); } +OutputStream& DBusOutputStream::writeEnumValue(const uint64_t& uint64BackingTypeValue) { return writeValue(uint64BackingTypeValue); } + +void DBusOutputStream::beginWriteBoolVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteInt8Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteInt16Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteInt32Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteInt64Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt8Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt16Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt32Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt64Vector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteFloatVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteDoubleVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteStringVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteByteBufferVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteVersionVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} + +void DBusOutputStream::beginWriteInt8EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteInt16EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteInt32EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteInt64EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt8EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt16EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt32EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteUInt64EnumVector(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} + +void DBusOutputStream::beginWriteVectorOfSerializableStructs(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteVectorOfSerializableVariants(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteVectorOfVectors(uint32_t sizeOfVector) { + beginWriteGenericVector(); + rememberCurrentStreamPosition(); +} +void DBusOutputStream::beginWriteVectorOfMaps(uint32_t sizeOfVector) { + beginWriteGenericVector(); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} + +void DBusOutputStream::endWriteVector() { + uint32_t numOfWrittenBytes = getCurrentStreamPosition() - popRememberedStreamPosition(); + writeBasicTypeValueAtPosition(popRememberedStreamPosition(), numOfWrittenBytes); +} + +OutputStream& DBusOutputStream::writeVersionValue(const Version& versionValue) { + alignToBoundary(8); + writeValue(versionValue.Major); + writeValue(versionValue.Minor); + return *this; +} + +void DBusOutputStream::beginWriteSerializableStruct(const SerializableStruct& serializableStruct) { alignToBoundary(8); } +void DBusOutputStream::endWriteSerializableStruct(const SerializableStruct& serializableStruct) { } + +void DBusOutputStream::beginWriteMap(size_t elementCount) { + alignToBoundary(sizeof(uint32_t)); + rememberCurrentStreamPosition(); + writeBasicTypeValue((uint32_t) 0); + alignToBoundary(8); + rememberCurrentStreamPosition(); +} + +void DBusOutputStream::endWriteMap() { + uint32_t numOfWrittenBytes = getCurrentStreamPosition() - popRememberedStreamPosition(); + writeBasicTypeValueAtPosition(popRememberedStreamPosition(), numOfWrittenBytes); +} + +void DBusOutputStream::beginWriteSerializableVariant(const SerializableVariant& serializableVariant) { + alignToBoundary(8); + writeValue(serializableVariant.getValueType()); + + DBusTypeOutputStream typeOutputStream; + serializableVariant.writeToTypeOutputStream(typeOutputStream); + writeSignature(std::move(typeOutputStream.retrieveSignature())); +} + +void DBusOutputStream::endWriteSerializableVariant(const SerializableVariant& serializableVariant) {} + +bool DBusOutputStream::hasError() const { + return dbusError_; +} + +/** + * Writes the data that was buffered within this #DBusOutputMessageStream to the #DBusMessage that was given to the constructor. Each call to flush() + * will completely override the data that currently is contained in the #DBusMessage. The data that is buffered in this #DBusOutputMessageStream is + * not deleted by calling flush(). + */ void DBusOutputStream::flush() { const int toWrite = payload_.size(); const bool success = dbusMessage_.setBodyLength(toWrite); @@ -24,12 +219,25 @@ void DBusOutputStream::flush() { memcpy(destinationDataPtr, payload_.c_str(), toWrite); } +void DBusOutputStream::setError() {} + +/** + * Reserves the given number of bytes for writing, thereby negating the need to dynamically allocate memory while writing. + * Use this method for optimization: If possible, reserve as many bytes as you need for your data before doing any writing. + * + * @param numOfBytes The number of bytes that should be reserved for writing. + */ void DBusOutputStream::reserveMemory(size_t numOfBytes) { assert(numOfBytes >= 0); - payload_.reserve(numOfBytes); } +/** + * @return current data position where later writing is possible + */ +size_t getCurrentPosition(); + + DBusOutputStream& DBusOutputStream::writeString(const char* cString, const uint32_t& length) { assert(cString != NULL); assert(cString[length] == '\0'); @@ -41,5 +249,61 @@ DBusOutputStream& DBusOutputStream::writeString(const char* cString, const uint3 return *this; } + +//Additional 0-termination, so this is 8 byte of \0 +static const char* eightByteZeroString = "\0\0\0\0\0\0\0"; + +void DBusOutputStream::alignToBoundary(const size_t alignBoundary) { + assert(alignBoundary > 0 && alignBoundary <= 8 && (alignBoundary % 2 == 0 || alignBoundary == 1)); + + size_t alignMask = alignBoundary - 1; + size_t necessaryAlignment = ((alignMask - (payload_.size() & alignMask)) + 1) & alignMask; + + writeRawData(eightByteZeroString, necessaryAlignment); +} + +bool DBusOutputStream::writeRawData(const char* rawDataPtr, const size_t sizeInByte) { + assert(sizeInByte >= 0); + + payload_.append(rawDataPtr, sizeInByte); + + return true; +} + +bool DBusOutputStream::writeRawDataAtPosition(size_t position, const char* rawDataPtr, const size_t sizeInByte) { + assert(sizeInByte >= 0); + + payload_ = payload_.replace(position, sizeInByte, rawDataPtr, sizeInByte); + + return true; +} + +void DBusOutputStream::beginWriteGenericVector() { + alignToBoundary(sizeof(uint32_t)); + rememberCurrentStreamPosition(); + writeBasicTypeValue((uint32_t) 0); +} + +void DBusOutputStream::writeSignature(const std::string& signature) { + uint8_t length = (uint8_t) signature.length(); + assert(length < 256); + *this << length; + writeRawData(signature.c_str(), length + 1); +} + +void DBusOutputStream::rememberCurrentStreamPosition() { + savedStreamPositions_.push(payload_.size()); +} + +size_t DBusOutputStream::popRememberedStreamPosition() { + size_t val = savedStreamPositions_.top(); + savedStreamPositions_.pop(); + return val; +} + +size_t DBusOutputStream::getCurrentStreamPosition() { + return payload_.size(); +} + } // namespace DBus } // namespace CommonAPI diff --git a/src/CommonAPI/DBus/DBusOutputStream.h b/src/CommonAPI/DBus/DBusOutputStream.h index 928fc29..9d3895c 100644 --- a/src/CommonAPI/DBus/DBusOutputStream.h +++ b/src/CommonAPI/DBus/DBusOutputStream.h @@ -31,107 +31,107 @@ class DBusTypeOutputStream: public TypeOutputStream { virtual ~DBusTypeOutputStream() {} - inline virtual void writeBoolType() { + virtual void writeBoolType() { signature_.append("b"); } - inline virtual void writeInt8Type() { + virtual void writeInt8Type() { signature_.append("y"); } - inline virtual void writeInt16Type() { + virtual void writeInt16Type() { signature_.append("n"); } - inline virtual void writeInt32Type() { + virtual void writeInt32Type() { signature_.append("i"); } - inline virtual void writeInt64Type() { + virtual void writeInt64Type() { signature_.append("x"); } - inline virtual void writeUInt8Type() { + virtual void writeUInt8Type() { signature_.append("y"); } - inline virtual void writeUInt16Type() { + virtual void writeUInt16Type() { signature_.append("q"); } - inline virtual void writeUInt32Type() { + virtual void writeUInt32Type() { signature_.append("u"); } - inline virtual void writeUInt64Type() { + virtual void writeUInt64Type() { signature_.append("t"); } - inline virtual void writeInt8EnumType() { + virtual void writeInt8EnumType() { signature_.append("y"); } - inline virtual void writeInt16EnumType() { + virtual void writeInt16EnumType() { signature_.append("n"); } - inline virtual void writeInt32EnumType() { + virtual void writeInt32EnumType() { signature_.append("i"); } - inline virtual void writeInt64EnumType() { + virtual void writeInt64EnumType() { signature_.append("x"); } - inline virtual void writeUInt8EnumType() { + virtual void writeUInt8EnumType() { signature_.append("y"); } - inline virtual void writeUInt16EnumType() { + virtual void writeUInt16EnumType() { signature_.append("n"); } - inline virtual void writeUInt32EnumType() { + virtual void writeUInt32EnumType() { signature_.append("u"); } - inline virtual void writeUInt64EnumType() { + virtual void writeUInt64EnumType() { signature_.append("t"); } - inline virtual void writeFloatType() { + virtual void writeFloatType() { signature_.append("d"); } - inline virtual void writeDoubleType() { + virtual void writeDoubleType() { signature_.append("d"); } - inline virtual void writeStringType() { + virtual void writeStringType() { signature_.append("s"); } - inline virtual void writeByteBufferType() { + virtual void writeByteBufferType() { signature_.append("ay"); } - inline virtual void writeVersionType() { + virtual void writeVersionType() { signature_.append("(uu)"); } - inline virtual void beginWriteStructType() { + virtual void beginWriteStructType() { signature_.append("("); } - inline virtual void endWriteStructType() { + virtual void endWriteStructType() { signature_.append(")"); } - inline virtual void beginWriteMapType() { + virtual void beginWriteMapType() { signature_.append("a{"); } - inline virtual void endWriteMapType() { + virtual void endWriteMapType() { signature_.append("}"); } - inline virtual void beginWriteVectorType() { + virtual void beginWriteVectorType() { signature_.append("a"); } - inline virtual void endWriteVectorType() { + virtual void endWriteVectorType() { } - inline virtual void writeVariantType() { + virtual void writeVariantType() { signature_.append("(yv)"); } - inline virtual std::string retrieveSignature() { + virtual std::string retrieveSignature() { return std::move(signature_); } @@ -156,159 +156,8 @@ typedef uint32_t position_t; */ class DBusOutputStream: public OutputStream { public: - virtual OutputStream& writeValue(const bool& boolValue) { return writeBasicTypeValue(boolValue); } - - virtual OutputStream& writeValue(const int8_t& int8Value) { return writeBasicTypeValue(int8Value); } - virtual OutputStream& writeValue(const int16_t& int16Value) { return writeBasicTypeValue(int16Value); } - virtual OutputStream& writeValue(const int32_t& int32Value) { return writeBasicTypeValue(int32Value); } - virtual OutputStream& writeValue(const int64_t& int64Value) { return writeBasicTypeValue(int64Value); } - - virtual OutputStream& writeValue(const uint8_t& uint8Value) { return writeBasicTypeValue(uint8Value); } - virtual OutputStream& writeValue(const uint16_t& uint16Value) { return writeBasicTypeValue(uint16Value); } - virtual OutputStream& writeValue(const uint32_t& uint32Value) { return writeBasicTypeValue(uint32Value); } - virtual OutputStream& writeValue(const uint64_t& uint64Value) { return writeBasicTypeValue(uint64Value); } - - virtual OutputStream& writeValue(const float& floatValue) { return writeBasicTypeValue((double) floatValue); } - virtual OutputStream& writeValue(const double& doubleValue) { return writeBasicTypeValue(doubleValue); } - virtual OutputStream& writeValue(const std::string& stringValue) { return writeString(stringValue.c_str(), stringValue.length()); } - - inline virtual OutputStream& writeValue(const ByteBuffer& byteBufferValue); - - virtual OutputStream& writeEnumValue(const int8_t& int8BackingTypeValue) { return writeValue(int8BackingTypeValue); } - virtual OutputStream& writeEnumValue(const int16_t& int16BackingTypeValue) { return writeValue(int16BackingTypeValue); } - virtual OutputStream& writeEnumValue(const int32_t& int32BackingTypeValue) { return writeValue(int32BackingTypeValue); } - virtual OutputStream& writeEnumValue(const int64_t& int64BackingTypeValue) { return writeValue(int64BackingTypeValue); } - virtual OutputStream& writeEnumValue(const uint8_t& uint8BackingTypeValue) { return writeValue(uint8BackingTypeValue); } - virtual OutputStream& writeEnumValue(const uint16_t& uint16BackingTypeValue) { return writeValue(uint16BackingTypeValue); } - virtual OutputStream& writeEnumValue(const uint32_t& uint32BackingTypeValue) { return writeValue(uint32BackingTypeValue); } - virtual OutputStream& writeEnumValue(const uint64_t& uint64BackingTypeValue) { return writeValue(uint64BackingTypeValue); } - - virtual void beginWriteBoolVector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteInt8Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteInt16Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteInt32Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteInt64Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - virtual void beginWriteUInt8Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteUInt16Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteUInt32Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteUInt64Vector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - virtual void beginWriteFloatVector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - virtual void beginWriteDoubleVector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - virtual void beginWriteStringVector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteByteBufferVector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteVersionVector(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - virtual void beginWriteVectorOfSerializableStructs(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - virtual void beginWriteVectorOfSerializableVariants(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - virtual void beginWriteVectorOfVectors(uint32_t sizeOfVector) { - beginWriteGenericVector(); - rememberCurrentStreamPosition(); - } - virtual void beginWriteVectorOfMaps(uint32_t sizeOfVector) { - beginWriteGenericVector(); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - - virtual void endWriteVector() { - uint32_t numOfWrittenBytes = getCurrentStreamPosition() - popRememberedStreamPosition(); - writeBasicTypeValueAtPosition(popRememberedStreamPosition(), numOfWrittenBytes); - } - - virtual OutputStream& writeVersionValue(const Version& versionValue) { - alignToBoundary(8); - writeValue(versionValue.Major); - writeValue(versionValue.Minor); - return *this; - } - - virtual void beginWriteSerializableStruct(const SerializableStruct& serializableStruct) { alignToBoundary(8); } - virtual void endWriteSerializableStruct(const SerializableStruct& serializableStruct) { } - - virtual void beginWriteMap(size_t elementCount) { - alignToBoundary(sizeof(uint32_t)); - rememberCurrentStreamPosition(); - writeBasicTypeValue((uint32_t) 0); - alignToBoundary(8); - rememberCurrentStreamPosition(); - } - - virtual void endWriteMap() { - uint32_t numOfWrittenBytes = getCurrentStreamPosition() - popRememberedStreamPosition(); - writeBasicTypeValueAtPosition(popRememberedStreamPosition(), numOfWrittenBytes); - } - - virtual void beginWriteSerializableVariant(const SerializableVariant& serializableVariant) { - alignToBoundary(8); - writeValue(serializableVariant.getValueType()); - - DBusTypeOutputStream typeOutputStream; - serializableVariant.writeToTypeOutputStream(typeOutputStream); - writeSignature(std::move(typeOutputStream.retrieveSignature())); - } - - virtual void endWriteSerializableVariant(const SerializableVariant& serializableVariant) {} - - virtual bool hasError() const { - return dbusError_; - } - - /** + /** * Creates a #DBusOutputMessageStream which can be used to serialize and write data into the given #DBusMessage. Any data written is buffered within the stream. * Remember to call flush() when you are done with writing: Only then the data actually is written to the #DBusMessage. * @@ -323,6 +172,80 @@ class DBusOutputStream: public OutputStream { */ virtual ~DBusOutputStream(); + virtual OutputStream& writeValue(const bool& boolValue); + + virtual OutputStream& writeValue(const int8_t& int8Value); + virtual OutputStream& writeValue(const int16_t& int16Value); + virtual OutputStream& writeValue(const int32_t& int32Value); + virtual OutputStream& writeValue(const int64_t& int64Value); + + virtual OutputStream& writeValue(const uint8_t& uint8Value); + virtual OutputStream& writeValue(const uint16_t& uint16Value); + virtual OutputStream& writeValue(const uint32_t& uint32Value); + virtual OutputStream& writeValue(const uint64_t& uint64Value); + + virtual OutputStream& writeValue(const float& floatValue); + virtual OutputStream& writeValue(const double& doubleValue); + + virtual OutputStream& writeValue(const std::string& stringValue); + + virtual OutputStream& writeValue(const ByteBuffer& byteBufferValue); + + virtual OutputStream& writeEnumValue(const int8_t& int8BackingTypeValue); + virtual OutputStream& writeEnumValue(const int16_t& int16BackingTypeValue); + virtual OutputStream& writeEnumValue(const int32_t& int32BackingTypeValue); + virtual OutputStream& writeEnumValue(const int64_t& int64BackingTypeValue); + virtual OutputStream& writeEnumValue(const uint8_t& uint8BackingTypeValue); + virtual OutputStream& writeEnumValue(const uint16_t& uint16BackingTypeValue); + virtual OutputStream& writeEnumValue(const uint32_t& uint32BackingTypeValue); + virtual OutputStream& writeEnumValue(const uint64_t& uint64BackingTypeValue); + + virtual void beginWriteBoolVector(uint32_t sizeOfVector); + virtual void beginWriteInt8Vector(uint32_t sizeOfVector); + virtual void beginWriteInt16Vector(uint32_t sizeOfVector); + virtual void beginWriteInt32Vector(uint32_t sizeOfVector); + virtual void beginWriteInt64Vector(uint32_t sizeOfVector); + virtual void beginWriteUInt8Vector(uint32_t sizeOfVector); + virtual void beginWriteUInt16Vector(uint32_t sizeOfVector); + virtual void beginWriteUInt32Vector(uint32_t sizeOfVector); + virtual void beginWriteUInt64Vector(uint32_t sizeOfVector); + virtual void beginWriteFloatVector(uint32_t sizeOfVector); + virtual void beginWriteDoubleVector(uint32_t sizeOfVector); + virtual void beginWriteStringVector(uint32_t sizeOfVector); + virtual void beginWriteByteBufferVector(uint32_t sizeOfVector); + virtual void beginWriteVersionVector(uint32_t sizeOfVector); + + virtual void beginWriteInt8EnumVector(uint32_t sizeOfVector); + virtual void beginWriteInt16EnumVector(uint32_t sizeOfVector); + virtual void beginWriteInt32EnumVector(uint32_t sizeOfVector); + virtual void beginWriteInt64EnumVector(uint32_t sizeOfVector); + virtual void beginWriteUInt8EnumVector(uint32_t sizeOfVector); + virtual void beginWriteUInt16EnumVector(uint32_t sizeOfVector); + virtual void beginWriteUInt32EnumVector(uint32_t sizeOfVector); + virtual void beginWriteUInt64EnumVector(uint32_t sizeOfVector); + + virtual void beginWriteVectorOfSerializableStructs(uint32_t sizeOfVector); + virtual void beginWriteVectorOfSerializableVariants(uint32_t sizeOfVector); + virtual void beginWriteVectorOfVectors(uint32_t sizeOfVector); + virtual void beginWriteVectorOfMaps(uint32_t sizeOfVector); + + virtual void endWriteVector(); + + virtual OutputStream& writeVersionValue(const Version& versionValue); + + virtual void beginWriteSerializableStruct(const SerializableStruct& serializableStruct); + virtual void endWriteSerializableStruct(const SerializableStruct& serializableStruct); + + virtual void beginWriteMap(size_t elementCount); + + virtual void endWriteMap(); + + virtual void beginWriteSerializableVariant(const SerializableVariant& serializableVariant); + + virtual void endWriteSerializableVariant(const SerializableVariant& serializableVariant); + + virtual bool hasError() const; + /** * Writes the data that was buffered within this #DBusOutputMessageStream to the #DBusMessage that was given to the constructor. Each call to flush() * will completely override the data that currently is contained in the #DBusMessage. The data that is buffered in this #DBusOutputMessageStream is @@ -330,10 +253,7 @@ class DBusOutputStream: public OutputStream { */ void flush(); - /** - * Marks the stream as erroneous. - */ - inline void setError(); + void setError(); /** * Reserves the given number of bytes for writing, thereby negating the need to dynamically allocate memory while writing. @@ -405,32 +325,15 @@ class DBusOutputStream: public OutputStream { std::string payload_; private: - inline void beginWriteGenericVector() { - alignToBoundary(sizeof(uint32_t)); - rememberCurrentStreamPosition(); - writeBasicTypeValue((uint32_t) 0); - } + void beginWriteGenericVector(); - inline void writeSignature(const std::string& signature) { - uint8_t length = (uint8_t) signature.length(); - assert(length < 256); - *this << length; - writeRawData(signature.c_str(), length + 1); - } + void writeSignature(const std::string& signature); - inline void rememberCurrentStreamPosition() { - savedStreamPositions_.push(payload_.size()); - } + void rememberCurrentStreamPosition(); - inline size_t popRememberedStreamPosition() { - size_t val = savedStreamPositions_.top(); - savedStreamPositions_.pop(); - return val; - } + size_t popRememberedStreamPosition(); - inline size_t getCurrentStreamPosition() { - return payload_.size(); - } + size_t getCurrentStreamPosition(); DBusError dbusError_; DBusMessage dbusMessage_; @@ -439,45 +342,6 @@ class DBusOutputStream: public OutputStream { }; -//Additional 0-termination, so this is 8 byte of \0 -static const char* eightByteZeroString = "\0\0\0\0\0\0\0"; - -inline void DBusOutputStream::alignToBoundary(const size_t alignBoundary) { - assert(alignBoundary > 0 && alignBoundary <= 8 && (alignBoundary % 2 == 0 || alignBoundary == 1)); - - size_t alignMask = alignBoundary - 1; - size_t necessaryAlignment = ((alignMask - (payload_.size() & alignMask)) + 1) & alignMask; - - writeRawData(eightByteZeroString, necessaryAlignment); -} - -inline bool DBusOutputStream::writeRawData(const char* rawDataPtr, const size_t sizeInByte) { - assert(sizeInByte >= 0); - - payload_.append(rawDataPtr, sizeInByte); - - return true; -} - -inline bool DBusOutputStream::writeRawDataAtPosition(size_t position, const char* rawDataPtr, const size_t sizeInByte) { - assert(sizeInByte >= 0); - - payload_ = payload_.replace(position, sizeInByte, rawDataPtr, sizeInByte); - - return true; -} - -inline size_t DBusOutputStream::getCurrentPosition() { - return payload_.size(); -} - - -OutputStream& DBusOutputStream::writeValue(const ByteBuffer& byteBufferValue) { - *this << byteBufferValue; - return *this; -} - - } // namespace DBus } // namespace CommonAPI diff --git a/src/test/DBusTypeStreamTest.cpp b/src/test/DBusTypeStreamTest.cpp index a96ef8f..83f427c 100644 --- a/src/test/DBusTypeStreamTest.cpp +++ b/src/test/DBusTypeStreamTest.cpp @@ -56,6 +56,15 @@ enum class TestEnum: int32_t { namespace CommonAPI { +inline InputStream& operator>>(InputStream& inputStream, TestEnum& enumValue) { + return inputStream.readEnumValue(enumValue); +} + +inline OutputStream& operator<<(OutputStream& outputStream, const TestEnum& enumValue) { + return outputStream.writeEnumValue(static_cast(enumValue)); +} + + template<> struct BasicTypeWriter { inline static void writeType (TypeOutputStream& typeStream) { @@ -63,10 +72,25 @@ inline static void writeType (TypeOutputStream& typeStream) { } }; +template<> +struct InputStreamVectorHelper { + static void beginReadVector(InputStream& inputStream, const std::vector& vectorValue) { + inputStream.beginReadInt32EnumVector(); + } +}; + +template <> +struct OutputStreamVectorHelper { + static void beginWriteVector(OutputStream& outputStream, const std::vector& vectorValue) { + outputStream.beginWriteInt32EnumVector(vectorValue.size()); + } +}; + + } // namespace CommonAPI -typedef std::vector testEnumList; +typedef std::vector TestEnumList; class TypeOutputStreamTest: public ::testing::Test { @@ -184,6 +208,12 @@ TEST_F(TypeOutputStreamTest, CreatesVectorOfStringsSignature) { ASSERT_TRUE(signature.compare("as") == 0); } +TEST_F(TypeOutputStreamTest, CreatesVectorOfInt32EnumsSignature) { + CommonAPI::TypeWriter::writeType(typeStream_); + std::string signature = typeStream_.retrieveSignature(); + ASSERT_TRUE(signature.compare("ai") == 0); +} + TEST_F(TypeOutputStreamTest, CreatesVectorOfVersionsSignature) { CommonAPI::TypeWriter >::writeType(typeStream_); std::string signature = typeStream_.retrieveSignature(); @@ -282,6 +312,16 @@ TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericUInt32TypeVariantsCorrectly ASSERT_TRUE(signature.compare("u") == 0); } +TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericInt32EnumTypeVariantsCorrectly) { + TestEnum fromInt32Enum = TestEnum::CF_MAX; + CommonAPI::Variant myVariant(fromInt32Enum); + CommonAPI::SerializableVariant* genericVariant = &myVariant; + + genericVariant->writeToTypeOutputStream(typeStream_); + + std::string signature = typeStream_.retrieveSignature(); + ASSERT_TRUE(signature.compare("i") == 0); +} TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericStringTypeVariantsCorrectly) { std::string fromString = "Hai!"; @@ -307,6 +347,18 @@ TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericVectorTypeVariantsCorrectly } +TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericVectorOfInt32EnumTypeVariantsCorrectly) { + TestEnumList fromEnumVector; + CommonAPI::Variant myVariant(fromEnumVector); + CommonAPI::SerializableVariant* genericVariant = &myVariant; + + genericVariant->writeToTypeOutputStream(typeStream_); + + std::string signature = typeStream_.retrieveSignature(); + ASSERT_TRUE(signature.compare("ai") == 0); +} + + TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericTestStructTypeVariantsCorrectly) { TestStruct fromTestStruct; CommonAPI::Variant myVariant(fromTestStruct); @@ -319,7 +371,6 @@ TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericTestStructTypeVariantsCorre } - int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -- 2.7.4