Maybe.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <functional>
  3. #include "Betriebssystem.h"
  4. namespace Frmaework
  5. {
  6. template<typename T>
  7. class Maybe
  8. {
  9. private:
  10. bool set;
  11. T value;
  12. Maybe()
  13. {
  14. set = 0;
  15. }
  16. public:
  17. static Maybe<T> of( T value )
  18. {
  19. return { 1, value };
  20. }
  21. static Maybe<T> empty()
  22. {
  23. return { 0, value };
  24. }
  25. bool isEmpty() const
  26. {
  27. return !set;
  28. }
  29. bool isPresent() const
  30. {
  31. return set;
  32. }
  33. operator T() const
  34. {
  35. assert( set );
  36. return value;
  37. }
  38. void ifPresent( std::function<T> action )
  39. {
  40. if( set )
  41. action( value );
  42. }
  43. void ifNotPresent( std::function<T> action )
  44. {
  45. if( !set )
  46. action( value );
  47. }
  48. T operator->()
  49. {
  50. assert( set );
  51. return current->var;
  52. }
  53. };
  54. }