paludis Version 3.0.2
The one true package mangler
options.hh
Go to the documentation of this file.
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2007, 2009, 2010, 2011 Ciaran McCreesh
5 *
6 * This file is part of the Paludis package manager. Paludis is free software;
7 * you can redistribute it and/or modify it under the terms of the GNU General
8 * Public License version 2, as published by the Free Software Foundation.
9 *
10 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef PALUDIS_GUARD_PALUDIS_UTIL_OPTIONS_HH
21#define PALUDIS_GUARD_PALUDIS_UTIL_OPTIONS_HH 1
22
25#include <initializer_list>
26
27/** \file
28 * Declarations for the Options<> class.
29 *
30 * \ingroup g_data_structures
31 *
32 * \section Examples
33 *
34 * - None at this time.
35 */
36
37namespace paludis
38{
39 /**
40 * Used by Options<> for underlying storage.
41 *
42 * Holds a collection of bits, similar to std::bitset<>, but with no fixed
43 * underlying size.
44 *
45 * \see Options<>
46 * \ingroup g_data_structures
47 */
48 class PALUDIS_VISIBLE OptionsStore
49 {
50 private:
51 unsigned long _bits;
52
53 public:
54 ///\name Basic operations
55 ///\{
56
57 OptionsStore();
58 OptionsStore(const OptionsStore &) = default;
59 ~OptionsStore() = default;
60
61 OptionsStore & operator= (const OptionsStore &) = default;
62
63 ///\}
64
65 ///\name Modifications
66 ///\{
67
68 /**
69 * Set the specified bit.
70 */
71 void add(const unsigned);
72
73 /**
74 * Unset the specified bit.
75 */
76 void remove(const unsigned);
77
78 /**
79 * Set any bit that is set in the parameter.
80 */
81 void combine(const OptionsStore &);
82
83 /**
84 * Unset any bit that is set in the parameter.
85 */
86 void subtract(const OptionsStore &);
87
88 /**
89 * Unset any bit that is not set in the parameter.
90 */
91 void intersect(const OptionsStore &);
92
93 ///\}
94
95 ///\name Tests
96 ///\{
97
98 /**
99 * Is a particular bit set?
100 */
101 bool test(const unsigned) const;
102
103 /**
104 * Is any bit set?
105 */
106 bool any() const;
107
108 /**
109 * The highest bit that might be set.
110 *
111 * \since 0.40.1
112 */
113 unsigned highest_bit() const;
114
115 ///\}
116 };
117
118 /**
119 * Holds a series of true/false values mapped on an enum type, like a
120 * std::bitset<> without the static size requirement.
121 *
122 * \ingroup g_data_structures
123 */
124 template <typename E_>
125 class Options
126 {
127 private:
128 OptionsStore _store;
129
130 public:
131 Options() = default;
132 Options(const Options &) = default;
133 Options & operator= (const Options &) = default;
134
135 Options(std::initializer_list<E_> e)
136 {
137 for (const auto option : e)
138 _store.add(static_cast<unsigned>(option));
139 }
140
141 /**
142 * Return a copy of ourself with the specified bit enabled.
143 */
144 Options operator+ (const E_ & e) const
145 {
146 Options result(*this);
147 result._store.add(static_cast<unsigned>(e));
148 return result;
149 }
150
151 /**
152 * Enable the specified bit.
153 */
154 Options & operator+= (const E_ & e)
155 {
156 _store.add(static_cast<unsigned>(e));
157 return *this;
158 }
159
160 /**
161 * Return a copy of ourself with the specified bit disabled.
162 */
163 Options operator- (const E_ & e) const
164 {
165 Options result(*this);
166 result._store.remove(static_cast<unsigned>(e));
167 return result;
168 }
169
170 /**
171 * Disable the specified bit.
172 */
173 Options & operator-= (const E_ & e)
174 {
175 _store.remove(static_cast<unsigned>(e));
176 return *this;
177 }
178
179 /**
180 * Return a copy of ourself, bitwise 'or'ed with another Options set.
181 */
182 Options operator| (const Options<E_> & e) const
183 {
184 Options result(*this);
185 result._store.combine(e._store);
186 return result;
187 }
188
189 /**
190 * Enable any bits that are enabled in the parameter.
191 */
192 Options & operator|= (const Options<E_> & e)
193 {
194 _store.combine(e._store);
195 return *this;
196 }
197
198 /**
199 * Return a copy of ourself, bitwise 'and'ed with another Options set.
200 */
201 Options operator& (const Options<E_> & e) const
202 {
203 Options result(*this);
204 result._store.intersect(e._store);
205 return result;
206 }
207
208 /**
209 * Disable any bits that are not enabled in the parameter.
210 */
211 Options & operator&= (const Options<E_> & e)
212 {
213 _store.intersect(e._store);
214 return *this;
215 }
216
217 /**
218 * Disable any bits that are enabled in the parameter.
219 */
220 Options & subtract(const Options<E_> & e)
221 {
222 _store.subtract(e._store);
223 return *this;
224 }
225
226 /**
227 * Returns whether the specified bit is enabled.
228 */
229 bool operator[] (const E_ & e) const
230 {
231 return _store.test(static_cast<unsigned>(e));
232 }
233
234 /**
235 * Returns whether any bit is enabled.
236 */
237 bool any() const
238 {
239 return _store.any();
240 }
241
242 /**
243 * Returns whether all bits are disabled.
244 */
245 bool none() const
246 {
247 return ! _store.any();
248 }
249
250 /**
251 * Return the value of the highest bit that might be enabled.
252 *
253 * \since 0.40.1
254 */
255 E_ highest_bit() const
256 {
257 return static_cast<E_>(_store.highest_bit());
258 }
259 };
260}
261
262#endif
Definition options.hh:49
void subtract(const OptionsStore &)
void combine(const OptionsStore &)
void remove(const unsigned)
unsigned highest_bit() const
bool test(const unsigned) const
void intersect(const OptionsStore &)
void add(const unsigned)
Options & operator+=(const E_ &e)
Definition options.hh:154
Options & subtract(const Options< E_ > &e)
Definition options.hh:220
Options operator-(const E_ &e) const
Definition options.hh:163
Options & operator|=(const Options< E_ > &e)
Definition options.hh:192
Options & operator-=(const E_ &e)
Definition options.hh:173
bool operator[](const E_ &e) const
Definition options.hh:229
Options operator+(const E_ &e) const
Definition options.hh:144
E_ highest_bit() const
Definition options.hh:255
Options operator&(const Options< E_ > &e) const
Definition options.hh:201
bool any() const
Definition options.hh:237
Options & operator&=(const Options< E_ > &e)
Definition options.hh:211
bool none() const
Definition options.hh:245
Options operator|(const Options< E_ > &e) const
Definition options.hh:182
#define PALUDIS_VISIBLE
Definition attributes.hh:59
Definition about_metadata-fwd.hh:24