bitfield.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2001-2006, William Joseph.
00003 All Rights Reserved.
00004 
00005 This file is part of GtkRadiant.
00006 
00007 GtkRadiant is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 GtkRadiant is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GtkRadiant; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 
00022 #if !defined(INCLUDED_GENERIC_BITFIELD_H)
00023 #define INCLUDED_GENERIC_BITFIELD_H
00024 
00027 
00035 template<typename Enumeration>
00036 class BitFieldValue : public Enumeration {
00037     unsigned m_value;
00038 protected:
00039     explicit BitFieldValue(unsigned value) : m_value(value) {
00040     }
00041 public:
00042     BitFieldValue() : m_value(0) {
00043     }
00044     explicit BitFieldValue(typename Enumeration::Value value) : m_value(1 << value) {
00045     }
00046     unsigned get() const {
00047         return m_value;
00048     }
00049 };
00050 
00051 template<typename Enumeration>
00052 class BitFieldValueUnsafe : public BitFieldValue<Enumeration> {
00053 public:
00054     explicit BitFieldValueUnsafe(unsigned value) : BitFieldValue<Enumeration>(value) {
00055     }
00056 };
00057 
00058 template<typename Enumeration>
00059 inline bool operator==(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00060     return self.get() == other.get();
00061 }
00062 template<typename Enumeration>
00063 inline bool operator!=(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00064     return !operator==(self, other);
00065 }
00066 
00067 template<typename Enumeration>
00068 inline BitFieldValue<Enumeration> operator|(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00069     return BitFieldValueUnsafe<Enumeration>(self.get() | other.get());
00070 }
00071 template<typename Enumeration>
00072 inline BitFieldValue<Enumeration>& operator|=(BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other) {
00073     return self = self | other;
00074 }
00075 template<typename Enumeration>
00076 inline BitFieldValue<Enumeration> operator&(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00077     return BitFieldValueUnsafe<Enumeration>(self.get() & other.get());
00078 }
00079 template<typename Enumeration>
00080 inline BitFieldValue<Enumeration>& operator&=(BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other) {
00081     return self = self & other;
00082 }
00083 template<typename Enumeration>
00084 inline BitFieldValue<Enumeration> operator~(BitFieldValue<Enumeration> self) {
00085     return BitFieldValueUnsafe<Enumeration>(~self.get());
00086 }
00087 
00088 
00089 
00090 inline unsigned int bitfield_enable(unsigned int bitfield, unsigned int mask) {
00091     return bitfield | mask;
00092 }
00093 inline unsigned int bitfield_disable(unsigned int bitfield, unsigned int mask) {
00094     return bitfield & ~mask;
00095 }
00096 inline bool bitfield_enabled(unsigned int bitfield, unsigned int mask) {
00097     return (bitfield & mask) != 0;
00098 }
00099 
00100 template<typename Enumeration>
00101 inline BitFieldValue<Enumeration> bitfield_enable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask) {
00102     return bitfield | mask;
00103 }
00104 template<typename Enumeration>
00105 inline BitFieldValue<Enumeration> bitfield_disable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask) {
00106     return bitfield & ~mask;
00107 }
00108 template<typename Enumeration>
00109 inline bool bitfield_enabled(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask) {
00110     return (bitfield & mask).get() != 0;
00111 }
00112 
00113 #endif

Generated by  doxygen 1.6.2