| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #pragma once
- #include <functional>
- #include "OperatingSystem.h"
- namespace Framework
- {
- template<typename A, typename B> class Either
- {
- private:
- union
- {
- A aValue;
- B bValue;
- };
- bool a;
- public:
- Either(const Either<A, B>& b)
- {
- a = b.isA();
- if (a)
- aValue = (A)b;
- else
- bValue = (B)b;
- }
- Either(A a)
- {
- aValue = a;
- this->a = 1;
- }
- Either(B b)
- {
- bValue = b;
- a = 0;
- }
- bool isA() const
- {
- return a;
- }
- bool isB() const
- {
- return !a;
- }
- A getA() const
- {
- assert(a);
- return aValue;
- }
- B getB() const
- {
- assert(!a);
- return bValue;
- }
- void doIfA(std::function<A> action)
- {
- if (a) action(aValue);
- }
- void doIfB(std::function<B> action)
- {
- if (!a) action(bValue);
- }
- operator A() const
- {
- assert(a);
- return aValue;
- }
- operator B() const
- {
- assert(!a);
- return bValue;
- }
- Either<A, B>& operator=(const Either<A, B>& b)
- {
- a = b.isA();
- if (a)
- aValue = (A)b;
- else
- bValue = (B)b;
- return *this;
- }
- Either<A, B>& operator=(const A& a)
- {
- a = 1;
- aValue = a;
- return *this;
- }
- Either<A, B>& operator=(const B& b)
- {
- a = 0;
- bValue = b;
- return *this;
- }
- };
- } // namespace Framework
|