1 /* Box<ITV>::Status class implementation: inline functions.
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_Box_Status_inlines_hh
25 #define PPL_Box_Status_inlines_hh 1
26 
27 #include <string>
28 
29 namespace Parma_Polyhedra_Library {
30 
31 template <typename ITV>
32 inline
Status(flags_t mask)33 Box<ITV>::Status::Status(flags_t mask)
34   : flags(mask) {
35 }
36 
37 template <typename ITV>
38 inline
Status(const Status & y)39 Box<ITV>::Status::Status(const Status& y)
40   : flags(y.flags) {
41 }
42 
43 template <typename ITV>
44 template <typename Other_ITV>
45 inline
Status(const typename Box<Other_ITV>::Status & y)46 Box<ITV>::Status::Status(const typename Box<Other_ITV>::Status& y)
47   : flags(y.flags) {
48 }
49 
50 template <typename ITV>
51 inline
Status()52 Box<ITV>::Status::Status()
53   : flags(NONE) {
54 }
55 
56 template <typename ITV>
57 inline bool
test_all(flags_t mask) const58 Box<ITV>::Status::test_all(flags_t mask) const {
59   return (flags & mask) == mask;
60 }
61 
62 template <typename ITV>
63 inline bool
test_any(flags_t mask) const64 Box<ITV>::Status::test_any(flags_t mask) const {
65   return (flags & mask) != 0;
66 }
67 
68 template <typename ITV>
69 inline void
set(flags_t mask)70 Box<ITV>::Status::set(flags_t mask) {
71   flags |= mask;
72 }
73 
74 template <typename ITV>
75 inline void
reset(flags_t mask)76 Box<ITV>::Status::reset(flags_t mask) {
77   flags &= ~mask;
78 }
79 
80 template <typename ITV>
81 inline bool
test_empty_up_to_date() const82 Box<ITV>::Status::test_empty_up_to_date() const {
83   return test_any(EMPTY_UP_TO_DATE);
84 }
85 
86 template <typename ITV>
87 inline void
reset_empty_up_to_date()88 Box<ITV>::Status::reset_empty_up_to_date() {
89   reset(EMPTY_UP_TO_DATE);
90 }
91 
92 template <typename ITV>
93 inline void
set_empty_up_to_date()94 Box<ITV>::Status::set_empty_up_to_date() {
95   set(EMPTY_UP_TO_DATE);
96 }
97 
98 template <typename ITV>
99 inline bool
test_empty() const100 Box<ITV>::Status::test_empty() const {
101   return test_any(EMPTY);
102 }
103 
104 template <typename ITV>
105 inline void
reset_empty()106 Box<ITV>::Status::reset_empty() {
107   reset(EMPTY);
108 }
109 
110 template <typename ITV>
111 inline void
set_empty()112 Box<ITV>::Status::set_empty() {
113   set(EMPTY);
114 }
115 
116 template <typename ITV>
117 inline bool
test_universe() const118 Box<ITV>::Status::test_universe() const {
119   return test_any(UNIVERSE);
120 }
121 
122 template <typename ITV>
123 inline void
reset_universe()124 Box<ITV>::Status::reset_universe() {
125   reset(UNIVERSE);
126 }
127 
128 template <typename ITV>
129 inline void
set_universe()130 Box<ITV>::Status::set_universe() {
131   set(UNIVERSE);
132 }
133 
134 template <typename ITV>
135 bool
OK() const136 Box<ITV>::Status::OK() const {
137   if (test_empty_up_to_date()
138       && test_empty()
139       && test_universe()) {
140 #ifndef NDEBUG
141     std::cerr
142       << "The status asserts emptiness and universality at the same time."
143       << std::endl;
144 #endif
145     return false;
146   }
147 
148   // Any other case is OK.
149   return true;
150 }
151 
152 
153 namespace Implementation {
154 
155 namespace Boxes {
156 
157 // These are the keywords that indicate the individual assertions.
158 extern const char* empty_up_to_date;
159 extern const char* empty;
160 extern const char* universe;
161 const char yes = '+';
162 const char no = '-';
163 const char separator = ' ';
164 
165 /*! \relates Parma_Polyhedra_Library::Box::Status
166   Reads a keyword and its associated on/off flag from \p s.
167   Returns <CODE>true</CODE> if the operation is successful,
168   returns <CODE>false</CODE> otherwise.
169   When successful, \p positive is set to <CODE>true</CODE> if the flag
170   is on; it is set to <CODE>false</CODE> otherwise.
171 */
172 inline bool
get_field(std::istream & s,const char * keyword,bool & positive)173 get_field(std::istream& s, const char* keyword, bool& positive) {
174   std::string str;
175   if (!(s >> str)
176       || (str[0] != yes && str[0] != no)
177       || str.substr(1) != keyword) {
178     return false;
179   }
180   positive = (str[0] == yes);
181   return true;
182 }
183 
184 } // namespace Boxes
185 
186 } // namespace Implementation
187 
188 template <typename ITV>
189 void
ascii_dump(std::ostream & s) const190 Box<ITV>::Status::ascii_dump(std::ostream& s) const {
191   using namespace Implementation::Boxes;
192   s << (test_empty_up_to_date() ? yes : no) << empty_up_to_date << separator
193     << (test_empty() ? yes : no) << empty << separator
194     << (test_universe() ? yes : no) << universe << separator;
195 }
196 
PPL_OUTPUT_TEMPLATE_DEFINITIONS_ASCII_ONLY(ITV,Box<ITV>::Status)197 PPL_OUTPUT_TEMPLATE_DEFINITIONS_ASCII_ONLY(ITV, Box<ITV>::Status)
198 
199 template <typename ITV>
200 bool
201 Box<ITV>::Status::ascii_load(std::istream& s) {
202   using namespace Implementation::Boxes;
203   PPL_UNINITIALIZED(bool, positive);
204 
205   if (!get_field(s, Implementation::Boxes::empty_up_to_date, positive)) {
206     return false;
207   }
208   if (positive) {
209     set_empty_up_to_date();
210   }
211 
212   if (!get_field(s, Implementation::Boxes::empty, positive)) {
213     return false;
214   }
215   if (positive) {
216     set_empty();
217   }
218   if (!get_field(s, universe, positive)) {
219     return false;
220   }
221   if (positive) {
222     set_universe();
223   }
224   else {
225     reset_universe();
226   }
227 
228   // Check invariants.
229   PPL_ASSERT(OK());
230   return true;
231 }
232 
233 } // namespace Parma_Polyhedra_Library
234 
235 #endif // !defined(PPL_Box_Status_inlines_hh)
236