Browse Source

improve Either class

Kolja Strohm 3 weeks ago
parent
commit
000e6d2b02
2 changed files with 32 additions and 6 deletions
  1. 2 4
      Array.h
  2. 30 2
      Either.h

+ 2 - 4
Array.h

@@ -527,8 +527,7 @@ namespace Framework
         ArrayEntry<TYP*>* entries;
         ArrayEntry<TYP*>* last;
         int count;
-        std::function<ArrayEntry<TYP*>*(ArrayEntry<TYP*>* removed)>
-            onRemove;
+        std::function<ArrayEntry<TYP*>*(ArrayEntry<TYP*>* removed)> onRemove;
 
     public:
         //! Creates a new linked list
@@ -717,8 +716,7 @@ namespace Framework
                 delete e;
                 e = tmp;
             }
-            if (entries && entries->var && entries->set)
-                entries->var->release();
+            if (entries->var && entries->set) entries->var->release();
             entries->set = 0;
             entries->next = 0;
             last = entries;

+ 30 - 2
Either.h

@@ -9,8 +9,12 @@ namespace Framework
     template<typename A, typename B> class Either
     {
     private:
-        A aValue;
-        B bValue;
+        union
+        {
+            A aValue;
+            B bValue;
+        };
+
         bool a;
 
     public:
@@ -78,5 +82,29 @@ namespace Framework
             assert(!a);
             return bValue;
         }
+
+        Either<A, B>& operator=(const Either<A, B>& b)
+        {
+            a = b.isA();
+            if (a)
+                aValue = (A)b;
+            else
+                bValue = (B)b;
+            return *this;
+        }
+
+        Either<A, B>& operator=(const A& a)
+        {
+            a = 1;
+            aValue = a;
+            return *this;
+        }
+
+        Either<A, B>& operator=(const B& b)
+        {
+            a = 0;
+            bValue = b;
+            return *this;
+        }
     };
 } // namespace Framework