Maybe.h 1018 B

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