First integration of DBusTypeOutputStream into DBusOutputStream.
authorPhilip Rauwolf <rauwolf@itestra.de>
Tue, 15 Jan 2013 16:34:53 +0000 (17:34 +0100)
committerPhilip Rauwolf <rauwolf@itestra.de>
Tue, 15 Jan 2013 16:34:53 +0000 (17:34 +0100)
Testing and possible class partitioning and encapsulation remains to be
done.

src/CommonAPI/DBus/DBusOutputStream.h
src/test/DBusTypeStreamTest.cpp

index 35fdea5..15f4817 100644 (file)
@@ -277,9 +277,6 @@ class DBusOutputStream: public OutputStream {
        virtual void beginWriteSerializableStruct(const SerializableStruct& serializableStruct) { alignToBoundary(8); }
        virtual void endWriteSerializableStruct(const SerializableStruct& serializableStruct) { }
 
-    virtual void beginWriteSerializableVariant(const SerializableVariant& serializableVariant) { }
-    virtual void endWriteSerializableVariant(const SerializableVariant& serializableVariant) { }
-
        virtual void beginWriteMap(size_t elementCount) {
         alignToBoundary(sizeof(uint32_t));
         rememberCurrentStreamPosition();
@@ -293,11 +290,15 @@ class DBusOutputStream: public OutputStream {
         writeBasicTypeValueAtPosition(popRememberedStreamPosition(), numOfWrittenBytes);
        }
 
-    virtual void beginWriteVariant(const SerializableVariant& serializableVariant) {
+    virtual void beginWriteSerializableVariant(const SerializableVariant& serializableVariant) {
         writeValue(serializableVariant.getValueType());
+
+        DBusTypeOutputStream typeOutputStream;
+        serializableVariant.writeToTypeOutputStream(typeOutputStream);
+        writeSignature(std::move(typeOutputStream.retrieveSignature()));
     }
 
-    virtual void endWriteVariant(const SerializableVariant& serializableVariant) {
+    virtual void endWriteSerializableVariant(const SerializableVariant& serializableVariant) {
         //TODO
     }
 
@@ -398,10 +399,6 @@ class DBusOutputStream: public OutputStream {
 
     bool writeRawDataAtPosition(size_t position, const char* rawDataPtr, const size_t sizeInByte);
 
-    virtual std::shared_ptr<TypeOutputStream> getNewTypeOutputStream() {
-        return std::static_pointer_cast<TypeOutputStream>(std::make_shared<DBusTypeOutputStream>());
-    }
-
   protected:
     std::string payload_;
 
@@ -412,7 +409,7 @@ class DBusOutputStream: public OutputStream {
         writeBasicTypeValue((uint32_t) 0);
     }
 
-    inline void writeSignature(std::string& signature) {
+    inline void writeSignature(std::string signature) {
         uint8_t length = (uint8_t) signature.length();
         assert(length < 256);
         *this << length;
index 1d86a36..ec593c3 100644 (file)
@@ -197,10 +197,9 @@ TEST_F(TypeOutputStreamTest, ParsesSignatureOfTestStructCorrectly) {
 
 TEST_F(TypeOutputStreamTest, ParsesSignatureOfUInt32TypeVariantsCorrectly) {
     uint32_t fromUInt = 42;
-    CommonAPI::Variant<uint32_t, double, std::string> myVariant(fromUInt);
+    CommonAPI::Variant<uint32_t, double, uint16_t> myVariant(fromUInt);
 
-    CommonAPI::TypeSearchVisitor searchVisitor(typeStream_);
-    CommonAPI::ApplyVoidVisitor<CommonAPI::TypeSearchVisitor, CommonAPI::Variant<uint32_t, double, std::string>, uint32_t, double, std::string>::visit(searchVisitor, myVariant);
+    myVariant.writeToTypeOutputStream(typeStream_);
 
     std::string signature = typeStream_.retrieveSignature();
     ASSERT_TRUE(signature.compare("u") == 0);
@@ -211,8 +210,7 @@ TEST_F(TypeOutputStreamTest, ParsesSignatureOfStringTypeVariantsCorrectly) {
     std::string fromString = "Hai!";
     CommonAPI::Variant<uint32_t, double, std::string> myVariant(fromString);
 
-    CommonAPI::TypeSearchVisitor searchVisitor(typeStream_);
-    CommonAPI::ApplyVoidVisitor<CommonAPI::TypeSearchVisitor, CommonAPI::Variant<uint32_t, double, std::string>, uint32_t, double, std::string>::visit(searchVisitor, myVariant);
+    myVariant.writeToTypeOutputStream(typeStream_);
 
     std::string signature = typeStream_.retrieveSignature();
     ASSERT_TRUE(signature.compare("s") == 0);
@@ -223,8 +221,7 @@ TEST_F(TypeOutputStreamTest, ParsesSignatureOfVectorTypeVariantsCorrectly) {
     std::vector<std::string> fromStringVector;
     CommonAPI::Variant<uint32_t, double, std::vector<std::string>> myVariant(fromStringVector);
 
-    CommonAPI::TypeSearchVisitor searchVisitor(typeStream_);
-    CommonAPI::ApplyVoidVisitor<CommonAPI::TypeSearchVisitor, CommonAPI::Variant<uint32_t, double, std::vector<std::string>>, uint32_t, double, std::vector<std::string>>::visit(searchVisitor, myVariant);
+    myVariant.writeToTypeOutputStream(typeStream_);
 
     std::string signature = typeStream_.retrieveSignature();
     ASSERT_TRUE(signature.compare("as") == 0);
@@ -235,8 +232,60 @@ TEST_F(TypeOutputStreamTest, ParsesSignatureOfTestStructTypeVariantsCorrectly) {
     TestStruct fromTestStruct;
     CommonAPI::Variant<uint32_t, double, TestStruct> myVariant(fromTestStruct);
 
-    CommonAPI::TypeSearchVisitor searchVisitor(typeStream_);
-    CommonAPI::ApplyVoidVisitor<CommonAPI::TypeSearchVisitor, CommonAPI::Variant<uint32_t, double, TestStruct>, uint32_t, double, TestStruct>::visit(searchVisitor, myVariant);
+    myVariant.writeToTypeOutputStream(typeStream_);
+
+    std::string signature = typeStream_.retrieveSignature();
+    ASSERT_TRUE(signature.compare("(qsv)") == 0);
+}
+
+
+
+
+
+
+
+TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericUInt32TypeVariantsCorrectly) {
+    uint32_t fromUInt = 42;
+    CommonAPI::Variant<uint32_t, double, uint16_t> myVariant(fromUInt);
+    CommonAPI::SerializableVariant* genericVariant = &myVariant;
+
+    genericVariant->writeToTypeOutputStream(typeStream_);
+
+    std::string signature = typeStream_.retrieveSignature();
+    ASSERT_TRUE(signature.compare("u") == 0);
+}
+
+
+TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericStringTypeVariantsCorrectly) {
+    std::string fromString = "Hai!";
+    CommonAPI::Variant<uint32_t, double, std::string> myVariant(fromString);
+    CommonAPI::SerializableVariant* genericVariant = &myVariant;
+
+    genericVariant->writeToTypeOutputStream(typeStream_);
+
+    std::string signature = typeStream_.retrieveSignature();
+    ASSERT_TRUE(signature.compare("s") == 0);
+}
+
+
+TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericVectorTypeVariantsCorrectly) {
+    std::vector<std::string> fromStringVector;
+    CommonAPI::Variant<uint32_t, double, std::vector<std::string>> myVariant(fromStringVector);
+    CommonAPI::SerializableVariant* genericVariant = &myVariant;
+
+    genericVariant->writeToTypeOutputStream(typeStream_);
+
+    std::string signature = typeStream_.retrieveSignature();
+    ASSERT_TRUE(signature.compare("as") == 0);
+}
+
+
+TEST_F(TypeOutputStreamTest, ParsesSignatureOfGenericTestStructTypeVariantsCorrectly) {
+    TestStruct fromTestStruct;
+    CommonAPI::Variant<uint32_t, double, TestStruct> myVariant(fromTestStruct);
+    CommonAPI::SerializableVariant* genericVariant = &myVariant;
+
+    genericVariant->writeToTypeOutputStream(typeStream_);
 
     std::string signature = typeStream_.retrieveSignature();
     ASSERT_TRUE(signature.compare("(qsv)") == 0);