Variants fully integrated into inputStream, Bugfixing Variants and
authorPhilip Rauwolf <rauwolf@itestra.de>
Thu, 17 Jan 2013 16:09:01 +0000 (17:09 +0100)
committerPhilip Rauwolf <rauwolf@itestra.de>
Thu, 17 Jan 2013 16:09:01 +0000 (17:09 +0100)
datatype "ByteBuffer"

src/CommonAPI/InputStream.h
src/CommonAPI/SerializableStruct.h
src/CommonAPI/SerializableVariant.h
src/CommonAPI/SerializableVariant.hpp

index 1be52ec..b31411a 100644 (file)
@@ -151,10 +151,6 @@ inline InputStream& operator>>(InputStream& inputStream, std::string& stringValu
        return inputStream.readValue(stringValue);
 }
 
-inline InputStream& operator>>(InputStream& inputStream, ByteBuffer& byteBufferValue) {
-       return inputStream.readValue(byteBufferValue);
-}
-
 inline InputStream& operator>>(InputStream& inputStream, Version& versionValue) {
        return inputStream.readVersionValue(versionValue);
 }
index 828479b..d3749b1 100644 (file)
@@ -15,6 +15,7 @@ namespace CommonAPI {
 
 class InputStream;
 class OutputStream;
+class TypeOutputStream;
 
 struct SerializableStruct {
        virtual ~SerializableStruct() { }
index 1aae1d2..d7af954 100644 (file)
@@ -115,26 +115,36 @@ public:
                     typename std::enable_if<!std::is_same<_Type, Variant>::value>::type* = 0);
 
     template <typename _Type>
-    const typename VariantTypeSelector<_Type, _Types...>::type & get(bool& success) const;
+    const typename VariantTypeSelector<_Type, _Types...>::type& get() const;
 
     inline uint8_t getValueType() const {
        return valueType_;
     }
 
+private:
+    inline bool hasValue() const {
+        return valueType_ < TypesTupleSize::value;
+    }
+
     template<typename _U>
     void set( const _U& value, const bool clear);
 
     template<typename _U>
     void set( _U&& value, const bool clear);
 
-private:
-    inline bool hasValue() const {
-        return valueType_ < TypesTupleSize::value;
-    }
+    template<typename _FriendType>
+    friend struct TypeWriter;
+    template<typename ... _FriendTypes>
+    friend struct AssignmentVisitor;
+    template<typename _FriendType>
+    friend struct TypeEqualsVisitor;
+    template<typename ... _FriendTypes>
+    friend struct PartialEqualsVisitor;
+    template<typename ... _FriendTypes>
+    friend struct InputStreamReadVisitor;
 
     uint8_t valueType_;
     typename std::aligned_storage<maxSize>::type valueStorage_;
-
 };
 } // namespace CommonAPI
 
index 19b09e8..b42e4de 100644 (file)
@@ -9,21 +9,11 @@
 
 #include "OutputStream.h"
 #include "InputStream.h"
-#include <iostream>
 
-namespace CommonAPI {
+#include <exception>
 
-template<typename _Type>
-struct TypeWriter;
 
-template<typename ... _Types>
-struct AssignmentVisitor;
-
-template<typename _Type>
-struct TypeEqualsVisitor;
-
-template<typename ... _Types>
-struct PartialEqualsVisitor;
+namespace CommonAPI {
 
 template<class Visitor, class Variant, typename ... _Types>
 struct ApplyVoidVisitor;
@@ -50,21 +40,17 @@ struct ApplyVoidVisitor<Visitor, Variant, _Type, _Types...> {
     static const uint8_t index = ApplyVoidVisitor<Visitor, Variant,
                     _Types...>::index + 1;
 
-    static
-    void visit(Visitor& visitor, Variant& var) {
+    static void visit(Visitor& visitor, Variant& var) {
         if (var.getValueType() == index) {
-            bool b;
-            visitor(var.template get<_Type>(b));
+            visitor(var.template get<_Type>());
         } else {
             ApplyVoidVisitor<Visitor, Variant, _Types...>::visit(visitor, var);
         }
     }
 
-    static
-    void visit(Visitor& visitor, const Variant& var) {
+    static void visit(Visitor& visitor, const Variant& var) {
         if (var.getValueType() == index) {
-            bool b;
-            visitor(var.template get<_Type>(b));
+            visitor(var.template get<_Type>());
         } else {
             ApplyVoidVisitor<Visitor, Variant, _Types...>::visit(visitor, var);
         }
@@ -92,8 +78,7 @@ struct ApplyBoolVisitor<Visitor, Variant, _Type, _Types...> {
 
     static bool visit(Visitor& visitor, Variant& var) {
         if (var.getValueType() == index) {
-            bool b;
-            return visitor(var.template get<_Type>(b));
+            return visitor(var.template get<_Type>());
         } else {
             return ApplyBoolVisitor<Visitor, Variant, _Types...>::visit(visitor,
                             var);
@@ -173,21 +158,15 @@ template<typename _Type>
 struct TypeEqualsVisitor
 {
 public:
-    TypeEqualsVisitor(const _Type& rhs) :
-                    rhs_(rhs)
-    {
+    TypeEqualsVisitor(const _Type& rhs): rhs_(rhs) {
     }
 
-    bool
-    operator()(const _Type& lhs) const
-                      {
+    bool operator()(const _Type& lhs) const {
         return lhs == rhs_;
     }
 
     template<typename _U>
-    bool
-    operator()(const _U&) const
-                      {
+    bool operator()(const _U&) const {
         return false;
     }
 
@@ -391,9 +370,8 @@ Variant<_Types...>& Variant<_Types...>::operator=(Variant<_Types...>&& rhs) {
 template<typename ... _Types>
 template<typename _Type>
 typename std::enable_if<!std::is_same<_Type, Variant<_Types...>>::value, Variant<_Types...>&>::type
-Variant<_Types...>::operator=(const _Type& value)
-                              {
-    set<typename TypeSelector<_Type, _Types...>::type>(value);
+Variant<_Types...>::operator=(const _Type& value) {
+    set<typename TypeSelector<_Type, _Types...>::type>(value, hasValue());
     return *this;
 }
 
@@ -427,20 +405,21 @@ typename std::enable_if<!std::is_same<_Type, Variant<_Types...>>::value>::type*)
     set<typename TypeSelector<_Type, _Types...>::type>(std::move(value), false);
 }
 
+
 template<typename ... _Types>
 template<typename _Type>
-const typename VariantTypeSelector<_Type, _Types...>::type & Variant<_Types...>::get(bool& success) const {
+const typename VariantTypeSelector<_Type, _Types...>::type & Variant<_Types...>::get() const {
     typedef typename TypeSelector<_Type, _Types...>::type selected_type_t;
     uint8_t cType = TypeIndex<_Types...>::template get<selected_type_t>();
     if (cType == valueType_) {
-        success = true;
         return *(reinterpret_cast<const _Type *>(&valueStorage_));
     } else {
-        success = false;
-        return *(reinterpret_cast<const _Type *>(&valueStorage_));
+        std::bad_cast toThrow;
+        throw toThrow;
     }
 }
 
+
 template<typename ... _Types>
 template<typename _U>
 void Variant<_Types...>::set(const _U& value, const bool clear) {