libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNProp3.cpp
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, CoReNum, INSA-Lyon, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNProp3.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <CRNMath/CRNProp3.h>
23 #include <CRNException.h>
24 #include <CRNData/CRNDataFactory.h>
25 #include <CRNi18n.h>
26 
27 #ifndef TRUE
28 # define TRUE ~0
29 #endif
30 
31 using namespace crn;
32 
38 Prop3::Prop3(int val) noexcept
39 {
40  if ((val == TRUEval) || (val == FALSEval))
41  value = val;
42  else if (val == int(TRUE))
43  value = TRUEval;
44  else
45  value = UNKNOWNval;
46 }
47 
48 /*****************************************************************************/
55 {
56  switch (value)
57  {
58  case TRUEval:
59  return String(_("true"));
60  case FALSEval:
61  return String(_("false"));
62  case UNKNOWNval:
63  default:
64  return String(_("unknown"));
65  }
66 }
67 
75 Prop3 Prop3::operator|(const Prop3 &prop) const noexcept
76 {
77  if ((prop.value == TRUEval) || (value == TRUEval))
78  return Prop3(TRUEval);
79  if ((prop.value == FALSEval) && (value == FALSEval))
80  return Prop3(FALSEval);
81  return Prop3(UNKNOWNval);
82 }
83 
91 Prop3 Prop3::operator&(const Prop3 &prop) const noexcept
92 {
93  if ((prop.value == FALSEval) || (value == FALSEval))
94  return Prop3(FALSEval);
95  if ((prop.value == TRUEval) && (value == TRUEval))
96  return Prop3(TRUEval);
97  return Prop3(UNKNOWNval);
98 }
99 
107 bool Prop3::operator==(const Prop3 &prop) const noexcept
108 {
109  return prop.value == value;
110 }
111 
119 Prop3& Prop3::operator=(const int &prop) noexcept
120 {
121  return (*this = Prop3(prop));
122 }
123 
129 Prop3 Prop3::operator!() const noexcept
130 {
131  if (value == TRUEval)
132  return Prop3(FALSEval);
133  if (value == FALSEval)
134  return Prop3(TRUEval);
135  return Prop3(UNKNOWNval);
136 }
137 
145 Prop3& Prop3::operator|=(const Prop3 &prop) noexcept
146 {
147  if ((prop.value == TRUEval) || (value == TRUEval))
148  value = TRUEval;
149  else if ((prop.value == FALSEval) && (value == FALSEval))
150  value = FALSEval;
151  else
152  value = UNKNOWNval;
153  return *this;
154 }
155 
163 Prop3& Prop3::operator&=(const Prop3 &prop) noexcept
164 {
165  if ((prop.value == FALSEval) || (value == FALSEval))
166  value = FALSEval;
167  else if ((prop.value == TRUEval) && (value == TRUEval))
168  value = TRUEval;
169  else
170  value = UNKNOWNval;
171  return *this;
172 }
173 
175 bool Prop3::IsTrue() const noexcept { return value == TRUEval; }
177 bool Prop3::IsFalse() const noexcept { return value == FALSEval; }
179 bool Prop3::IsUnknown() const noexcept { return value == UNKNOWNval; }
180 
190 {
191  if (el.GetName() != "Prop3")
192  {
193  throw ExceptionInvalidArgument(StringUTF8("bool Prop3::Deserialize(xml::Element &el): ") +
194  _("Wrong XML element."));
195  }
196  value = el.GetAttribute<int>("value", false); // may throw
197 }
198 
207 {
208  xml::Element el(parent.PushBackElement("Prop3"));
209  el.SetAttribute("value", value);
210  return el;
211 }
212 
215  Cloner::Register<Prop3>();
217 
bool operator==(const Prop3 &prop) const noexcept
Comparison.
Definition: CRNProp3.cpp:107
Prop3 & operator=(const Prop3 &)=default
XML element.
Definition: CRNXml.h:135
StringUTF8 GetName() const
Gets the label of the element.
Definition: CRNXml.h:146
#define _(String)
Definition: CRNi18n.h:51
bool IsUnknown() const noexcept
Is unknown?
Definition: CRNProp3.cpp:179
Prop3 operator!() const noexcept
Complementary operator.
Definition: CRNProp3.cpp:129
void Deserialize(xml::Element &el)
Initializes the object from an XML element. Unsafe.
Definition: CRNProp3.cpp:189
bool IsFalse() const noexcept
Is false?
Definition: CRNProp3.cpp:177
#define CRN_END_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:198
bool IsTrue() const noexcept
Is true?
Definition: CRNProp3.cpp:175
static constexpr int UNKNOWNval
Definition: CRNProp3.h:87
Prop3() noexcept
Default constructor.
Definition: CRNProp3.h:44
A UTF32 character string class.
Definition: CRNString.h:61
Prop3 & operator|=(const Prop3 &prop) noexcept
Logical OR operator.
Definition: CRNProp3.cpp:145
xml::Element Serialize(xml::Element &parent) const
Dumps the object to an XML element. Unsafe.
Definition: CRNProp3.cpp:206
Prop3 operator&(const Prop3 &prop) const noexcept
Logical AND operator.
Definition: CRNProp3.cpp:91
#define CRN_DATA_FACTORY_REGISTER(elemname, classname)
Registers a class to the data factory.
void SetAttribute(const StringUTF8 &name, const StringUTF8 &value)
Sets the value of an attribute.
Definition: CRNXml.cpp:595
T GetAttribute(const StringUTF8 &name, bool silent=true) const
Gets an attribute.
Definition: CRNXml.h:219
Prop3 operator|(const Prop3 &prop) const noexcept
Logical OR operator.
Definition: CRNProp3.cpp:75
Prop3 & operator&=(const Prop3 &prop) noexcept
Logical AND operator.
Definition: CRNProp3.cpp:163
A character string class.
Definition: CRNStringUTF8.h:49
static constexpr int TRUEval
Definition: CRNProp3.h:86
A ternary proposition.
Definition: CRNProp3.h:40
static constexpr int FALSEval
Definition: CRNProp3.h:85
#define TRUE
Definition: CRNProp3.cpp:28
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
Definition: CRNXml.cpp:355
String ToString() const
Dumps value to a string.
Definition: CRNProp3.cpp:54
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107
#define CRN_BEGIN_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:185