paludis  Version 2.6.0
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 
37 namespace 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  */
49  {
50  private:
51  unsigned long _bits;
52 
53  public:
54  ///\name Basic operations
55  ///\{
56 
57  OptionsStore();
58  OptionsStore(const OptionsStore &);
59  const OptionsStore & operator= (const OptionsStore &);
60  ~OptionsStore();
61 
62  ///\}
63 
64  ///\name Modifications
65  ///\{
66 
67  /**
68  * Set the specified bit.
69  */
70  void add(const unsigned);
71 
72  /**
73  * Unset the specified bit.
74  */
75  void remove(const unsigned);
76 
77  /**
78  * Set any bit that is set in the parameter.
79  */
80  void combine(const OptionsStore &);
81 
82  /**
83  * Unset any bit that is set in the parameter.
84  */
85  void subtract(const OptionsStore &);
86 
87  /**
88  * Unset any bit that is not set in the parameter.
89  */
90  void intersect(const OptionsStore &);
91 
92  ///\}
93 
94  ///\name Tests
95  ///\{
96 
97  /**
98  * Is a particular bit set?
99  */
100  bool test(const unsigned) const;
101 
102  /**
103  * Is any bit set?
104  */
105  bool any() const;
106 
107  /**
108  * The highest bit that might be set.
109  *
110  * \since 0.40.1
111  */
112  unsigned highest_bit() const;
113 
114  ///\}
115  };
116 
117  /**
118  * Holds a series of true/false values mapped on an enum type, like a
119  * std::bitset<> without the static size requirement.
120  *
121  * \ingroup g_data_structures
122  */
123  template <typename E_>
124  class Options
125  {
126  private:
127  OptionsStore _store;
128 
129  public:
130  Options() = default;
131  Options(const Options &) = default;
132 
133  Options(std::initializer_list<E_> e)
134  {
135  for (auto i(e.begin()), i_end(e.end()) ;
136  i != i_end ; ++i)
137  _store.add(static_cast<unsigned>(*i));
138  }
139 
140  /**
141  * Return a copy of ourself with the specified bit enabled.
142  */
143  Options operator+ (const E_ & e) const
144  {
145  Options result(*this);
146  result._store.add(static_cast<unsigned>(e));
147  return result;
148  }
149 
150  /**
151  * Enable the specified bit.
152  */
153  Options & operator+= (const E_ & e)
154  {
155  _store.add(static_cast<unsigned>(e));
156  return *this;
157  }
158 
159  /**
160  * Return a copy of ourself with the specified bit disabled.
161  */
162  Options operator- (const E_ & e) const
163  {
164  Options result(*this);
165  result._store.remove(static_cast<unsigned>(e));
166  return result;
167  }
168 
169  /**
170  * Disable the specified bit.
171  */
172  Options & operator-= (const E_ & e)
173  {
174  _store.remove(static_cast<unsigned>(e));
175  return *this;
176  }
177 
178  /**
179  * Return a copy of ourself, bitwise 'or'ed with another Options set.
180  */
181  Options operator| (const Options<E_> & e) const
182  {
183  Options result(*this);
184  result._store.combine(e._store);
185  return result;
186  }
187 
188  /**
189  * Enable any bits that are enabled in the parameter.
190  */
191  Options & operator|= (const Options<E_> & e)
192  {
193  _store.combine(e._store);
194  return *this;
195  }
196 
197  /**
198  * Return a copy of ourself, bitwise 'and'ed with another Options set.
199  */
200  Options operator& (const Options<E_> & e) const
201  {
202  Options result(*this);
203  result._store.intersect(e._store);
204  return result;
205  }
206 
207  /**
208  * Disable any bits that are not enabled in the parameter.
209  */
210  Options & operator&= (const Options<E_> & e)
211  {
212  _store.intersect(e._store);
213  return *this;
214  }
215 
216  /**
217  * Disable any bits that are enabled in the parameter.
218  */
220  {
221  _store.subtract(e._store);
222  return *this;
223  }
224 
225  /**
226  * Returns whether the specified bit is enabled.
227  */
228  bool operator[] (const E_ & e) const
229  {
230  return _store.test(static_cast<unsigned>(e));
231  }
232 
233  /**
234  * Returns whether any bit is enabled.
235  */
236  bool any() const
237  {
238  return _store.any();
239  }
240 
241  /**
242  * Returns whether all bits are disabled.
243  */
244  bool none() const
245  {
246  return ! _store.any();
247  }
248 
249  /**
250  * Return the value of the highest bit that might be enabled.
251  *
252  * \since 0.40.1
253  */
254  E_ highest_bit() const
255  {
256  return static_cast<E_>(_store.highest_bit());
257  }
258  };
259 }
260 
261 #endif
Definition: options.hh:48
Definition: about_metadata-fwd.hh:23
void intersect(const OptionsStore &)
bool test(const unsigned) const
bool any() const
Definition: options.hh:236
bool none() const
Definition: options.hh:244
Generator operator&(const Generator &, const Generator &) PALUDIS_ATTRIBUTE((warn_unused_result)) PALUDIS_VISIBLE
void combine(const OptionsStore &)
void subtract(const OptionsStore &)
Definition: options-fwd.hh:34
FilteredGenerator operator|(const FilteredGenerator &, const Filter &) PALUDIS_ATTRIBUTE((warn_unused_result)) PALUDIS_VISIBLE
void remove(const unsigned)
void add(const unsigned)
Generator operator+(const Generator &, const Generator &) PALUDIS_ATTRIBUTE((warn_unused_result)) PALUDIS_VISIBLE
unsigned highest_bit() const
E_ highest_bit() const
Definition: options.hh:254
#define PALUDIS_VISIBLE
Definition: attributes.hh:59
Options & subtract(const Options< E_ > &e)
Definition: options.hh:219