Either.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include <functional>
  3. #include "Betriebssystem.h"
  4. namespace Framework
  5. {
  6. template<typename A, typename B>
  7. class Either
  8. {
  9. private:
  10. A aValue;
  11. B bValue;
  12. bool a;
  13. public:
  14. Either( A a )
  15. {
  16. aValue = a;
  17. this->a = 1;
  18. }
  19. Either( B b )
  20. {
  21. bValue = b;
  22. a = 0;
  23. }
  24. bool isA() const
  25. {
  26. return a;
  27. }
  28. bool isB() const
  29. {
  30. return !a;
  31. }
  32. A getA() const
  33. {
  34. return aValue;
  35. }
  36. B getB() const
  37. {
  38. return bValue;
  39. }
  40. void doIfA( std::function<A> action )
  41. {
  42. if( a )
  43. action( aValue );
  44. }
  45. void doIfB( std::function<B> action )
  46. {
  47. if( !a )
  48. action( bValue );
  49. }
  50. operator A() const
  51. {
  52. assert( a );
  53. return aValue;
  54. }
  55. operator B() const
  56. {
  57. assert( !a );
  58. return bValue;
  59. }
  60. };
  61. }