Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

enum_set.h

00001 #ifndef _LCORE_enum_set_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright (C) 2001 Eric M. Hopper <hopper@omnifarious.mn.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifdef __GNUG__
00022 #  pragma interface
00023 #endif
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/LCore/LCore/enum_set.h,v 1.13 2002/05/09 21:55:21 hopper Exp $ */
00026 
00027 // For a log, see ../ChangeLog
00028 
00029 #include <LCore/simple_bitset.h>
00030 #include <string>
00031 
00032 #define _LCORE_enum_set_H_
00033 
00034 namespace strmod {
00035 namespace lcore {
00036 
00037 /**
00038  * \class enum_set enum_set.h LCore/enum_set.h
00039  *
00040  * \brief This class is to easily manipulate a set of flags corresponding to
00041  * members of an enum.
00042  *
00043  * \param enum_t The enum type of the flags.
00044  * \param first The lowest valued enum flag.
00045  * \param last The highest valued enum flag.
00046  */
00047 template <class enum_t, enum_t first, enum_t last>
00048 //class enum_set : private std::bitset<last - first + 1>
00049 class enum_set : private simple_bitset<last - first + 1>
00050 {
00051   private:
00052    typedef enum_set<enum_t, first, last> self_t;
00053    //! Used to refer to private parent type to avoid errors in template parameter.
00054    typedef simple_bitset<last - first + 1> parent_t;
00055 //     typedef simple_bitset<last - first + 1>::reference pref;
00056   public:
00057    //! Construct an enum_set with no bits set.
00058    inline enum_set();
00059    //! Construct a copy of a different enum_set.
00060 //     inline enum_set(const enum_set<enum_t, first, last> &b);
00061    //! Construct an enum_set with at most one bit set.
00062    explicit inline enum_set(enum_t val);
00063    //! Construct an enum_set with at most two bits set.
00064    inline enum_set(enum_t val1, enum_t val2);
00065    //! Construct an enum_set with at most three bits set.
00066    inline enum_set(enum_t val1, enum_t val2, enum_t val3);
00067    //! Construct an enum_set with at most four bits set.
00068    inline enum_set(enum_t val1, enum_t val2, enum_t val3, enum_t val4);
00069    //! Construct an enum_set with at most five bits set.
00070    inline enum_set(enum_t val1, enum_t val2, enum_t val3, enum_t val4,
00071                    enum_t val5);
00072    //! Construct an enum_set with at most six bits set.
00073    inline enum_set(enum_t val1, enum_t val2, enum_t val3, enum_t val4,
00074                    enum_t val5, enum_t val6);
00075    //! Copy an enum_set.
00076    inline enum_set(const self_t &other);
00077 
00078    //! Compute bit1 & bit2 for all corresponding bits in both bitsets.
00079    inline self_t &operator &=(const self_t &__rhs);
00080    //! Compute bit1 | bit2 for all corresponding bits in both bitsets.
00081    inline self_t &operator |=(const self_t &__rhs);
00082    //! Compute bit1 ^ bit2 for all corresponding bits in both bitsets.
00083    inline self_t &operator ^=(const self_t &__rhs);
00084    //! Set a bit
00085    inline self_t &set(enum_t __pos);
00086    //! Set a bit to a particular boolean value.
00087    inline self_t &set(enum_t __pos, bool __val);
00088    //! Clear all bits.
00089    inline self_t &reset();
00090    //! Clear a bit.
00091    inline self_t &reset(enum_t __pos);
00092    //! Flip a bit from 0 to 1 or 1 to 0
00093    inline self_t &flip(enum_t __pos);
00094 //     //! Allow bitset[enum_val] syntax.
00095 //     inline pref operator[](enum_t __pos);
00096    //! Allow bitset[enum_val] syntax.
00097    inline bool operator [](enum_t __pos) const;
00098    //! How many bits are set?
00099    size_t count() const                            { return parent_t::count(); }
00100    //! How many total bits?
00101    size_t size() const                             { return parent_t::size(); }
00102 
00103    //! Copy one bitset into another
00104    inline const self_t &operator =(const self_t &b);
00105 
00106    //! Do all corresponding bits match in both bitsets?
00107    inline bool operator==(const self_t &__rhs) const; 
00108    //! Do any corresponding bits differ in both bitsets?
00109    inline bool operator!=(const self_t &__rhs) const;
00110    //! Is the bit on?
00111    inline bool test(enum_t __pos) const;
00112    //! Are any bits set (1, true)?
00113    bool any() const                                { return parent_t::any(); }
00114    //! Are all bits clear (0, false)?
00115    bool none() const                               { return parent_t::none(); }
00116 
00117    //! Return an ASCII representation of the bitset.
00118    inline std::string to_string() const;
00119 
00120    //! Are any bits set (1, true)?
00121    operator bool() const                           { return any(); }
00122 };
00123 
00124 //-----------------------------inline functions--------------------------------
00125 
00126 template <class enum_t, enum_t first, enum_t last>
00127 inline enum_set<enum_t, first, last>::enum_set()
00128 {
00129 }
00130 
00131 template <class enum_t, enum_t first, enum_t last>
00132 inline enum_set<enum_t, first, last>::enum_set(enum_t val)
00133 {
00134    set(val);
00135 }
00136 
00137 template <class enum_t, enum_t first, enum_t last>
00138 inline enum_set<enum_t, first, last>::enum_set(enum_t val1, enum_t val2)
00139 {
00140    set(val1);
00141    set(val2);
00142 }
00143 
00144 template <class enum_t, enum_t first, enum_t last>
00145 inline enum_set<enum_t, first, last>::enum_set(enum_t val1, enum_t val2,
00146                                                enum_t val3)
00147 {
00148    set(val1);
00149    set(val2);
00150    set(val3);
00151 }
00152 
00153 template <class enum_t, enum_t first, enum_t last>
00154 inline enum_set<enum_t, first, last>::enum_set(enum_t val1, enum_t val2,
00155                                                enum_t val3, enum_t val4)
00156 {
00157    set(val1);
00158    set(val2);
00159    set(val3);
00160    set(val4);
00161 }
00162 
00163 template <class enum_t, enum_t first, enum_t last>
00164 inline enum_set<enum_t, first, last>::enum_set(enum_t val1, enum_t val2,
00165                                                enum_t val3, enum_t val4,
00166                                                enum_t val5)
00167 {
00168    set(val1);
00169    set(val2);
00170    set(val3);
00171    set(val4);
00172    set(val5);
00173 }
00174 
00175 template <class enum_t, enum_t first, enum_t last>
00176 inline enum_set<enum_t, first, last>::enum_set(enum_t val1, enum_t val2,
00177                                                enum_t val3, enum_t val4,
00178                                                enum_t val5, enum_t val6)
00179 {
00180    set(val1);
00181    set(val2);
00182    set(val3);
00183    set(val4);
00184    set(val5);
00185    set(val6);
00186 }
00187 
00188 template <class enum_t, enum_t first, enum_t last>
00189 inline enum_set<enum_t, first, last>::enum_set(const self_t &other)
00190      : parent_t(other)
00191 {
00192 }
00193 
00194 template <class enum_t, enum_t first, enum_t last>
00195 inline  enum_set<enum_t, first, last> &
00196 enum_set<enum_t, first, last>::operator &=(const self_t &__rhs)
00197 {
00198    parent_t::operator &=(__rhs);
00199    return *this;
00200 }
00201 
00202 template <class enum_t, enum_t first, enum_t last>
00203 inline  enum_set<enum_t, first, last> &
00204 enum_set<enum_t, first, last>::operator |=(const self_t &__rhs)
00205 {
00206    parent_t::operator |=(__rhs);
00207    return *this;
00208 }
00209 
00210 template <class enum_t, enum_t first, enum_t last>
00211 inline enum_set<enum_t, first, last> &
00212 enum_set<enum_t, first, last>::operator ^=(const self_t &__rhs)
00213 {
00214    parent_t::operator ^=(__rhs);
00215    return *this;
00216 }
00217 
00218 template <class enum_t, enum_t first, enum_t last>
00219 inline enum_set<enum_t, first, last> &
00220 enum_set<enum_t, first, last>::set(enum_t __pos)
00221 {
00222    parent_t::set(__pos - first); 
00223    return *this;
00224 }
00225 
00226 template <class enum_t, enum_t first, enum_t last>
00227 inline enum_set<enum_t, first, last> &
00228 enum_set<enum_t, first, last>::set(enum_t __pos, bool __val)
00229 {
00230    parent_t::set(__pos - first, __val);
00231    return *this;
00232 }
00233 
00234 template <class enum_t, enum_t first, enum_t last>
00235 inline enum_set<enum_t, first, last> &
00236 enum_set<enum_t, first, last>::reset()
00237 {
00238    parent_t::reset();
00239    return *this;
00240 }
00241 
00242 template <class enum_t, enum_t first, enum_t last>
00243 inline enum_set<enum_t, first, last> &
00244 enum_set<enum_t, first, last>::reset(enum_t __pos)
00245 {
00246    parent_t::reset(__pos - first);
00247    return *this;
00248 }
00249 
00250 template <class enum_t, enum_t first, enum_t last>
00251 inline enum_set<enum_t, first, last> &
00252 enum_set<enum_t, first, last>::flip(enum_t __pos)
00253 {
00254    parent_t::flip(__pos - first);
00255    return *this;
00256 }
00257 
00258 //  template <class enum_t, enum_t first, enum_t last>
00259 //  inline typename enum_set<enum_t, first, last>::pref
00260 //  enum_set<enum_t, first, last>::operator[](enum_t __pos)
00261 //  {
00262 //     return parent_t::operator[](__pos - first);
00263 //  }
00264 
00265 template <class enum_t, enum_t first, enum_t last>
00266 inline bool enum_set<enum_t, first, last>::operator[](enum_t __pos) const
00267 {
00268    return parent_t::operator [](__pos - first);
00269 }
00270 
00271 template <class enum_t, enum_t first, enum_t last>
00272 inline const enum_set<enum_t, first, last> &
00273 enum_set<enum_t, first, last>::operator =(const self_t &b)
00274 {
00275    parent_t::operator =(b);
00276    return *this;
00277 }
00278 
00279 template <class enum_t, enum_t first, enum_t last>
00280 inline bool enum_set<enum_t, first, last>::operator==(const self_t &__rhs) const
00281 {
00282    return parent_t::operator ==(__rhs);
00283 }
00284 
00285 template <class enum_t, enum_t first, enum_t last>
00286 inline bool enum_set<enum_t, first, last>::operator!=(const self_t &__rhs) const
00287 {
00288    return parent_t::operator !=(__rhs);
00289 }
00290 
00291 template <class enum_t, enum_t first, enum_t last>
00292 inline bool enum_set<enum_t, first, last>::test(enum_t __pos) const
00293 {
00294    return parent_t::test(__pos - first);
00295 }
00296 
00297 template <class enum_t, enum_t first, enum_t last>
00298 inline std::string enum_set<enum_t, first, last>::to_string() const
00299 {
00300 //     std::string fred;
00301 //     parent_t::_M_copy_to_string(fred);
00302 //     return fred;
00303    return parent_t::to_string();
00304 }
00305 
00306 //--
00307 
00308 //! Return bitset containing result of bit1 & bit2 for all corresponding bits.
00309 template <class enum_t, enum_t first, enum_t last>
00310 inline enum_set<enum_t, first, last>
00311 operator&(const enum_set<enum_t, first, last> &__x,
00312           const enum_set<enum_t, first, last> &__y)
00313 {
00314    enum_set<enum_t, first, last> result(__x);
00315    result &= __y;
00316    return result;
00317 }
00318 
00319 //! Return bitset containing result of bit1 | bit2 for all corresponding bits.
00320 template <class enum_t, enum_t first, enum_t last>
00321 inline enum_set<enum_t, first, last>
00322 operator|(const enum_set<enum_t, first, last> &__x,
00323           const enum_set<enum_t, first, last> &__y)
00324 {
00325    enum_set<enum_t, first, last> result(__x);
00326    result |= __y;
00327    return result;
00328 }
00329 
00330 //! Return bitset containing result of bit1 ^ bit2 for all corresponding bits.
00331 template <class enum_t, enum_t first, enum_t last>
00332 inline enum_set<enum_t, first, last>
00333 operator^(const enum_set<enum_t, first, last> &__x,
00334           const enum_set<enum_t, first, last> &__y)
00335 {
00336    enum_set<enum_t, first, last> result(__x);
00337    result ^= __y;
00338    return result;
00339 }
00340 
00341 } // namespace lcore
00342 } // namespace strmod
00343 
00344 #endif

Generated on Wed Jan 29 00:32:43 2003 for libNet by doxygen1.3-rc1