ledger-core
Either.hpp
1 /*
2  *
3  * Either
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 20/02/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_EITHER_HPP
32 #define LEDGER_CORE_EITHER_HPP
33 
34 #include "Option.hpp"
35 #include "Either.hpp"
36 #include <functional>
37 #include "Exception.hpp"
38 
39 namespace ledger {
40  namespace core {
41 
42  template<typename Left, typename Right>
43  class Either {
44  public:
45  Either() {
46 
47  }
48 
49  Either(const Left &left) {
50  _left = Option<Left>(left);
51  };
52 
53  Either(const Right &right) {
54  _right = Option<Right>(right);
55  };
56 
57  Either(const Option<Left> &left, const Option<Right> &right) {
58  _left = left;
59  _right = right;
60  };
61 
62  Either(Either<Left, Right>&& either) {
63  _left = std::move(either._left);
64  _right = std::move(either._right);
65  }
66 
67  Either(const Either<Left, Right>& either) {
68  _left = either._left;
69  _right = either._right;
70  }
71 
72  Either(Either& either) {
73  _left = either._left;
74  _right = either._right;
75  }
76 
77  Either<Left, Right>&operator=(const Either<Left, Right>& either) {
78  _left = either._left;
79  _right = either._right;
80  return *this;
81  };
82 
83  Either<Left, Right>&operator=(const Left& left) {
84  _left = Option<Left>(left);
85  _right = Option<Right>();
86  return *this;
87  };
88 
89  Either<Left, Right>&operator=(const Right& right) {
90  _left = Option<Left>();
91  _right = Option<Right>(right);
92  return *this;
93  };
94 
95  bool isRight() const {
96  return _right.nonEmpty();
97  };
98 
99  bool isLeft() const {
100  return _left.nonEmpty();
101  };
102 
103  operator bool() {
104  return isRight();
105  };
106 
107  const Right &operator*() const {
108  return _right.getValue();
109  }
110 
111  const Right *operator->() const {
112  return &_right.getValue();
113  }
114 
115  Left &getLeft() {
116  return _left.getValue();
117  }
118 
119  Right &getRight() {
120  return _right.getValue();
121  }
122 
123  const Left &getLeft() const {
124  return _left.getValue();
125  };
126 
127  const Right &getRight() const {
128  return _right.getValue();
129  };
130 
131  operator Left() const {
132  return _left.getValue();
133  };
134 
135  operator Right() const {
136  return _right.getValue();
137  };
138 
139  Either<Right, Left> swap() const {
140  return Either<Right, Left>(_right, _left);
141  };
142 
143  template<typename T>
144  T fold(std::function<T (const Left&)> f1, std::function<T (const Right&)> f2) const {
145  if (isRight())
146  return f2(getRight());
147  else
148  return f1(getLeft());
149  };
150 
151 
152  private:
153  Option<Left> _left;
154  Option<Right> _right;
155  };
156  }
157 }
158 
159 
160 #endif //LEDGER_CORE_EITHER_HPP
Definition: Either.hpp:43
Definition: Account.cpp:8