12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #pragma once
- #include <functional>
- #include "Betriebssystem.h"
- namespace Framework
- {
- template<typename T> class Maybe
- {
- private:
- bool set;
- T value;
- Maybe()
- {
- set = 0;
- }
- Maybe(T value)
- : set(1),
- value(value){};
- public:
- static Maybe<T> of(T value)
- {
- return Maybe(value);
- }
- static Maybe<T> empty()
- {
- return Maybe();
- }
- bool isEmpty() const
- {
- return !set;
- }
- bool isPresent() const
- {
- return set;
- }
- 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);
- }
- template<typename R> Maybe<R> map(std::function<R(T)> action)
- {
- if (set)
- return Maybe<R>::of(action(value));
- else
- return Maybe<R>::empty();
- }
- template<typename R> Maybe<R> flatMap(std::function<Maybe<R>(T)> action)
- {
- if (set)
- return action(value);
- else
- return Maybe<R>::empty();
- }
- T orElse(T elseValue)
- {
- if (set)
- return value;
- else
- return elseValue;
- }
- T operator->()
- {
- assert(set);
- return value;
- }
- };
- } // namespace Framework
|