Switch license from Intranator license to GPLv2 + linking exception (ACKed by Steffen)
[libi2ncommon] / src / tribool.cpp
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
49f91b40
TJ
20/** @file
21 * @brief Implementation of intuitive tristate logic
22 *
23 * (c) Copyright 2011 by Intra2net AG
24 * Written by Erich Willems and Thomas Jarosch
49f91b40
TJ
25 */
26
27#include <tribool.hpp>
28
29namespace I2n
30{
31
32/**
33 * @brief Constructor
34 * Default to Undef state
35 *
36 **/
37Tribool::Tribool()
38 : Value(Undef)
39{
40}
41
42/**
43 * @brief Constructor
44 *
45 * @param v Initial state
46 **/
47Tribool::Tribool(const State &v)
48 : Value(v)
49{
50}
51
52/**
53 * @brief Copy constructor
54 *
55 * @param src Class to copy the value from
56 **/
57Tribool::Tribool(const Tribool &src)
58 : Value(src.Value)
59{
60}
61
62/**
63 * @brief Assignment operator
64 *
65 * @param v New state
66 * @return :Tribool& Pointer to this class
67 **/
68Tribool& Tribool::operator= (const State &v)
69{
70 Value=v;
71 return *this;
72}
73
74/**
75 * @brief Assignment operator
76 *
77 * @param src Class to copy the state from
78 * @return :Tribool& Pointer to this class
79 **/
80Tribool& Tribool::operator= (const Tribool& src)
81{
82 Value=src.Value;
83 return *this;
84} //lint !e1529
85
86/**
87 * @brief Element comparator
88 *
89 * @param x Class to compare against
90 * @return bool True if the are in 100% the same state, false otherwise
91 * Will return true if both elements are "Undef".
92 **/
93bool Tribool::operator==(const Tribool& x) const
94{
95 return Value==x.Value;
96}
97
98/**
99 * @brief Element comparator, NOT version
100 *
101 * @param x Class to compare against
102 * @return bool True if they are not in the same state, false otherwise
103 **/
104bool Tribool::operator!=(const Tribool& x) const
105{
106 return Value!=x.Value;
107}
108
109/**
110 * @brief Logical NOT operator - operating in tristate space
111 *
112 * @return :Tribool Tribool state as the result
113 **/
114Tribool Tribool::operator !() const
115{
116 switch (Value)
117 {
118 case True:
119 return False;
120 case False:
121 return True;
122 } //lint !e787
123
124 return Undef;
125}
126
127/**
128 * @brief Logical OR - operating in tristate space
129 *
130 * @param x Object to compare against
131 * @return :Tribool Tribool state as result
132 **/
133Tribool Tribool::operator ||(const Tribool& x) const //lint !e1753
134{
135 if ((True==Value) || (True==x.Value))
136 return True;
137
138 if ((False==Value) && (False==x.Value))
139 return False;
140
141 return Undef;
142}
143
144/**
145 * @brief Logical AND operator - operating in tristate space
146 *
147 * @param x Object to compare against
148 * @return :Tribool Tristate state as result
149 **/
150Tribool Tribool::operator &&(const Tribool& x) const //lint !e1753
151{
152 if ((True==Value) && (True==x.Value))
153 return True;
154
155 if ((False==Value) || (False==x.Value))
156 return False;
157
158 return Undef;
159}
160
161/**
162 * @brief Constructor with bool conversion
163 *
164 * @param v Initial value in bistate space: true or false
165 **/
166Tribool::Tribool(const bool &v)
167{
168 if (v == true)
169 Value = True;
170 else
171 Value = False;
172}
173
174/**
175 * @brief Assignment operator
176 *
177 * @param v Value in bistate space: true or false
178 * @return :Tribool& Pointer to this class
179 **/
180Tribool& Tribool::operator=(const bool &v)
181{
182 if (v == true)
183 Value = True;
184 else
185 Value = False;
186
187 return *this;
188}
189
190/**
191 * @brief Element comparision for easier access in bistate space
192 *
193 * This is lossy!
194 *
195 * @param v Boolean value to compare against
196 * @return bool True if it's exact the bool value, false otherwise.
197 * Undef state will return false.
198 **/
199bool Tribool::is_exact(const bool &v) const
200{
201 if (Value==Undef)
202 return false;
203
204 if (v == true && Value == True)
205 return true;
206 else if(v == false && Value == False)
207 return true;
208
209 return false;
210}
211
212}