options.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2007, 2009 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_OPTIONS_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_OPTIONS_HH 1
00022 
00023 #include <paludis/util/options-fwd.hh>
00024 #include <paludis/util/private_implementation_pattern.hh>
00025 
00026 /** \file
00027  * Declarations for the Options<> class.
00028  *
00029  * \ingroup g_data_structures
00030  *
00031  * \section Examples
00032  *
00033  * - None at this time.
00034  */
00035 
00036 namespace paludis
00037 {
00038     /**
00039      * Used by Options<> for underlying storage.
00040      *
00041      * Holds a collection of bits, similar to std::bitset<>, but with no fixed
00042      * underlying size.
00043      *
00044      * \see Options<>
00045      * \ingroup g_data_structures
00046      */
00047     class PALUDIS_VISIBLE OptionsStore :
00048         private PrivateImplementationPattern<OptionsStore>
00049     {
00050         public:
00051             ///\name Basic operations
00052             ///\{
00053 
00054             OptionsStore();
00055             OptionsStore(const OptionsStore &);
00056             const OptionsStore & operator= (const OptionsStore &);
00057             ~OptionsStore();
00058 
00059             ///\}
00060 
00061             ///\name Modifications
00062             ///\{
00063 
00064             /**
00065              * Set the specified bit.
00066              */
00067             void add(const unsigned);
00068 
00069             /**
00070              * Unset the specified bit.
00071              */
00072             void remove(const unsigned);
00073 
00074             /**
00075              * Set any bit that is set in the parameter.
00076              */
00077             void combine(const OptionsStore &);
00078 
00079             /**
00080              * Unset any bit that is set in the parameter.
00081              */
00082             void subtract(const OptionsStore &);
00083 
00084             ///\}
00085 
00086             ///\name Tests
00087             ///\{
00088 
00089             /**
00090              * Is a particular bit set?
00091              */
00092             bool test(const unsigned) const;
00093 
00094             /**
00095              * Is any bit set?
00096              */
00097             bool any() const;
00098 
00099             /**
00100              * The highest bit that might be set.
00101              *
00102              * \since 0.40.1
00103              */
00104             unsigned highest_bit() const;
00105 
00106             ///\}
00107     };
00108 
00109     /**
00110      * Holds a series of true/false values mapped on an enum type, like a
00111      * std::bitset<> without the static size requirement.
00112      *
00113      * \ingroup g_data_structures
00114      */
00115     template <typename E_>
00116     class Options
00117     {
00118         private:
00119             OptionsStore _store;
00120 
00121         public:
00122             /**
00123              * Return a copy of ourself with the specified bit enabled.
00124              */
00125             Options operator+ (const E_ & e) const
00126             {
00127                 Options result(*this);
00128                 result._store.add(static_cast<unsigned>(e));
00129                 return result;
00130             }
00131 
00132             /**
00133              * Enable the specified bit.
00134              */
00135             Options & operator+= (const E_ & e)
00136             {
00137                 _store.add(static_cast<unsigned>(e));
00138                 return *this;
00139             }
00140 
00141             /**
00142              * Return a copy of ourself with the specified bit disabled.
00143              */
00144             Options operator- (const E_ & e) const
00145             {
00146                 Options result(*this);
00147                 result._store.remove(static_cast<unsigned>(e));
00148                 return result;
00149             }
00150 
00151             /**
00152              * Disable the specified bit.
00153              */
00154             Options & operator-= (const E_ & e)
00155             {
00156                 _store.remove(static_cast<unsigned>(e));
00157                 return *this;
00158             }
00159 
00160             /**
00161              * Return a copy of ourself, bitwise 'or'ed with another Options set.
00162              */
00163             Options operator| (const Options<E_> & e) const
00164             {
00165                 Options result(*this);
00166                 result._store.combine(e._store);
00167                 return result;
00168             }
00169 
00170             /**
00171              * Enable any bits that are enabled in the parameter.
00172              */
00173             Options & operator|= (const Options<E_> & e)
00174             {
00175                 _store.combine(e._store);
00176                 return *this;
00177             }
00178 
00179             /**
00180              * Disable any bits that are enabled in the parameter.
00181              */
00182             Options & subtract(const Options<E_> & e)
00183             {
00184                 _store.subtract(e._store);
00185                 return *this;
00186             }
00187 
00188             /**
00189              * Returns whether the specified bit is enabled.
00190              */
00191             bool operator[] (const E_ & e) const
00192             {
00193                 return _store.test(static_cast<unsigned>(e));
00194             }
00195 
00196             /**
00197              * Returns whether any bit is enabled.
00198              */
00199             bool any() const
00200             {
00201                 return _store.any();
00202             }
00203 
00204             /**
00205              * Returns whether all bits are disabled.
00206              */
00207             bool none() const
00208             {
00209                 return ! _store.any();
00210             }
00211 
00212             /**
00213              * Return the value of the highest bit that might be enabled.
00214              *
00215              * \since 0.40.1
00216              */
00217             E_ highest_bit() const
00218             {
00219                 return static_cast<E_>(_store.highest_bit());
00220             }
00221     };
00222 }
00223 
00224 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4