replace obsolete call to ftime(3)
[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{
751c68b4
TJ
116 enum State rtn = Undef;
117
49f91b40
TJ
118 switch (Value)
119 {
120 case True:
751c68b4
TJ
121 rtn = False;
122 break;
49f91b40 123 case False:
751c68b4
TJ
124 rtn = True;
125 break;
126 case Undef:
127 default:
128 break;
49f91b40
TJ
129 } //lint !e787
130
751c68b4 131 return rtn;
49f91b40
TJ
132}
133
134/**
135 * @brief Logical OR - operating in tristate space
136 *
137 * @param x Object to compare against
138 * @return :Tribool Tribool state as result
139 **/
140Tribool Tribool::operator ||(const Tribool& x) const //lint !e1753
141{
142 if ((True==Value) || (True==x.Value))
143 return True;
144
145 if ((False==Value) && (False==x.Value))
146 return False;
147
148 return Undef;
149}
150
151/**
152 * @brief Logical AND operator - operating in tristate space
153 *
154 * @param x Object to compare against
155 * @return :Tribool Tristate state as result
156 **/
157Tribool Tribool::operator &&(const Tribool& x) const //lint !e1753
158{
159 if ((True==Value) && (True==x.Value))
160 return True;
161
162 if ((False==Value) || (False==x.Value))
163 return False;
164
165 return Undef;
166}
167
168/**
169 * @brief Constructor with bool conversion
170 *
171 * @param v Initial value in bistate space: true or false
172 **/
173Tribool::Tribool(const bool &v)
174{
175 if (v == true)
176 Value = True;
177 else
178 Value = False;
179}
180
181/**
182 * @brief Assignment operator
183 *
184 * @param v Value in bistate space: true or false
185 * @return :Tribool& Pointer to this class
186 **/
187Tribool& Tribool::operator=(const bool &v)
188{
189 if (v == true)
190 Value = True;
191 else
192 Value = False;
193
194 return *this;
195}
196
197/**
198 * @brief Element comparision for easier access in bistate space
199 *
200 * This is lossy!
201 *
202 * @param v Boolean value to compare against
203 * @return bool True if it's exact the bool value, false otherwise.
204 * Undef state will return false.
205 **/
206bool Tribool::is_exact(const bool &v) const
207{
208 if (Value==Undef)
209 return false;
210
211 if (v == true && Value == True)
212 return true;
213 else if(v == false && Value == False)
214 return true;
215
216 return false;
217}
218
219}