Browse Source

add Either and Maybe types

Kolja Strohm 2 years ago
parent
commit
7a664a142b
7 changed files with 147 additions and 1 deletions
  1. 63 0
      Either.h
  2. 2 0
      Framework Linux.vcxproj
  3. 6 0
      Framework Linux.vcxproj.filters
  4. 4 0
      Framework.vcxproj
  5. 12 0
      Framework.vcxproj.filters
  6. 1 1
      InMemoryBuffer.cpp
  7. 59 0
      Maybe.h

+ 63 - 0
Either.h

@@ -0,0 +1,63 @@
+#pragma once
+
+#include <functional>
+#include "Betriebssystem.h"
+
+namespace Framework
+{
+    template<typename A, typename B>
+    class Either
+    {
+    private:
+        A aValue;
+        B bValue;
+        bool a;
+
+    public:
+        Either( A a )
+        {
+            aValue = a;
+            this->a = 1;
+            enshureAUnequalB( aValue );
+        }
+
+        Either( B b )
+        {
+            bValue = b;
+            a = 0;
+            enshureAUnequalB( bValue );
+        }
+
+        bool isA() const
+        {
+            return a;
+        }
+
+        bool isB() const
+        {
+            return !a;
+        }
+
+        A getA() const
+        {
+            return aValue;
+        }
+
+        B getB() const
+        {
+            return bValue;
+        }
+
+        void doIfA( std::function<A> action )
+        {
+            if( a )
+                action( aValue );
+        }
+
+        void doIfB( std::function<B> action )
+        {
+            if( !a )
+                action( bValue );
+        }
+    };
+}

+ 2 - 0
Framework Linux.vcxproj

@@ -186,6 +186,7 @@
     <ClInclude Include="DreieckListe.h" />
     <ClInclude Include="DXBuffer.h" />
     <ClInclude Include="Ebene3D.h" />
+    <ClInclude Include="Either.h" />
     <ClInclude Include="Errors.h" />
     <ClInclude Include="Fenster.h" />
     <ClInclude Include="Fortschritt.h" />
@@ -207,6 +208,7 @@
     <ClInclude Include="Mat3.h" />
     <ClInclude Include="Mat4.h" />
     <ClInclude Include="MausEreignis.h" />
+    <ClInclude Include="Maybe.h" />
     <ClInclude Include="Model2D.h" />
     <ClInclude Include="Model3D.h" />
     <ClInclude Include="Model3DList.h" />

+ 6 - 0
Framework Linux.vcxproj.filters

@@ -309,6 +309,12 @@
     <ClInclude Include="Errors.h">
       <Filter>Headerdateien\Framework</Filter>
     </ClInclude>
+    <ClInclude Include="Either.h">
+      <Filter>Headerdateien\Framework\Data</Filter>
+    </ClInclude>
+    <ClInclude Include="Maybe.h">
+      <Filter>Headerdateien\Framework\Data</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Prozess.cpp">

+ 4 - 0
Framework.vcxproj

@@ -210,6 +210,7 @@ copy "x64\Release\Framework.dll" "..\..\Spiele Platform\SMP\Fertig\x64\framework
     <ClInclude Include="Cube.h" />
     <ClInclude Include="Dialog.h" />
     <ClInclude Include="DLLRegister.h" />
+    <ClInclude Include="Either.h" />
     <ClInclude Include="Errors.h" />
     <ClInclude Include="GraphicsApi.h" />
     <ClInclude Include="DXBuffer.h" />
@@ -222,6 +223,7 @@ copy "x64\Release\Framework.dll" "..\..\Spiele Platform\SMP\Fertig\x64\framework
     <ClInclude Include="Fortschritt.h" />
     <ClInclude Include="FrameworkMath.h" />
     <ClInclude Include="Globals.h" />
+    <ClInclude Include="HashMap.h" />
     <ClInclude Include="InitDatei.h" />
     <ClInclude Include="InMemoryBuffer.h" />
     <ClInclude Include="JSON.h" />
@@ -237,6 +239,8 @@ copy "x64\Release\Framework.dll" "..\..\Spiele Platform\SMP\Fertig\x64\framework
     <ClInclude Include="Mat4.h" />
     <ClInclude Include="Maus.h" />
     <ClInclude Include="MausEreignis.h" />
+    <ClInclude Include="Maybe.h" />
+    <ClInclude Include="RCPointer.h" />
     <ClInclude Include="ReferenceCounter.h" />
     <ClInclude Include="Slider.h" />
     <ClInclude Include="UIDialog.h" />

+ 12 - 0
Framework.vcxproj.filters

@@ -339,6 +339,18 @@
     <ClInclude Include="Errors.h">
       <Filter>Headerdateien\Framework</Filter>
     </ClInclude>
+    <ClInclude Include="Maybe.h">
+      <Filter>Headerdateien\Framework\Data</Filter>
+    </ClInclude>
+    <ClInclude Include="Either.h">
+      <Filter>Headerdateien\Framework\Data</Filter>
+    </ClInclude>
+    <ClInclude Include="HashMap.h">
+      <Filter>Headerdateien\Framework\Data</Filter>
+    </ClInclude>
+    <ClInclude Include="RCPointer.h">
+      <Filter>Headerdateien\Framework\Data</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Maus.cpp">

+ 1 - 1
InMemoryBuffer.cpp

@@ -1,9 +1,9 @@
 #include "InMemoryBuffer.h"
 #include "Text.h"
+#include "Either.h"
 
 using namespace Framework;
 
-
 InMemoryBuffer::InMemoryBuffer()
     : ReferenceCounter()
 {

+ 59 - 0
Maybe.h

@@ -0,0 +1,59 @@
+#pragma once
+
+#include <functional>
+#include "Betriebssystem.h"
+
+namespace Frmaework
+{
+    template<typename T>
+    class Maybe
+    {
+    private:
+        bool set;
+        T value;
+
+        Maybe() 
+        { 
+            set = 0;
+        }
+
+    public:
+        static Maybe<T> of( T value )
+        {
+            return { 1, value };
+        }
+
+        static Maybe<T> empty()
+        {
+            return { 0, value };
+        }
+
+        bool isEmpty() const
+        {
+            return !set;
+        }
+
+        bool isPresent() const
+        {
+            return set;
+        }
+
+        T operator(T)() const
+        {
+            assert( set );
+            return value;
+        }
+
+        void ifPresent( std::function<T> action )
+        {
+            if( set )
+                action( value );
+        }
+
+        void ifNotPresent( std::function<T> action )
+        {
+            if( !set )
+                action( value );
+        }
+    };
+}