Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/Flags.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef Flags_INCLUDE_ONCE
00033 #define Flags_INCLUDE_ONCE
00034 
00035 namespace vl
00036 {
00038   template <typename T_FlagEnum>
00039   class Flags
00040   {
00041     // friends:
00042     template <typename T> friend Flags<T> operator|(T flag1, T flag2);
00043     template <typename T> friend Flags<T> operator&(T flag1, T flag2);
00044 
00045   public:
00046     typedef T_FlagEnum flag_type;
00047     typedef unsigned int flag_storage;
00048 
00049   public:
00050     Flags(): mFlags(0)  { }
00051 
00052     flag_storage flags() const { return mFlags; }
00053 
00054     void reset() { mFlags = 0; }
00055 
00056     // --- using Flags ---
00057 
00058     Flags operator|(const Flags& flag) const
00059     {
00060       Flags other = *this;
00061       other.mFlags |= flag.mFlags;
00062       return other;
00063     }
00064 
00065     Flags operator&(const Flags& flag) const
00066     {
00067       Flags other = *this;
00068       other.mFlags &= flag.mFlags;
00069       return other;
00070     }
00071 
00072     Flags operator^(const Flags& flag) const
00073     {
00074       Flags other = *this;
00075       other.mFlags ^= flag.mFlags;
00076       return other;
00077     }
00078 
00079     Flags operator-(const Flags& flag) const
00080     {
00081       Flags other = *this;
00082       other.mFlags &= ~flag.mFlags;
00083       return other;
00084     }
00085 
00086     // --- using T_FlagEnum ---
00087 
00088     Flags& set(T_FlagEnum flag)
00089     {
00090       mFlags |= (flag_storage)flag;
00091       return *this;
00092     }
00093 
00094     Flags& unset(T_FlagEnum flag)
00095     {
00096       mFlags &= ~(flag_storage)flag;
00097       return *this;
00098     }
00099 
00100     Flags& operator=(T_FlagEnum flag)
00101     {
00102       mFlags = flag;
00103       return this;
00104     }
00105 
00106     Flags operator|(T_FlagEnum flag) const
00107     {
00108       Flags other = *this;
00109       other.mFlags |= (flag_storage)flag;
00110       return other;
00111     }
00112 
00113     Flags operator&(T_FlagEnum flag) const
00114     {
00115       Flags other = *this;
00116       other.mFlags &= (flag_storage)flag;
00117       return other;
00118     }
00119 
00120     Flags operator^(T_FlagEnum flag) const
00121     {
00122       Flags other = *this;
00123       other.mFlags ^= (flag_storage)flag;
00124       return other;
00125     }
00126 
00127     Flags operator-(T_FlagEnum flag) const
00128     {
00129       Flags other = *this;
00130       other.unset(flag);
00131       return other;
00132     }
00133 
00134     operator bool() const 
00135     {
00136       return mFlags != 0;
00137     }
00138 
00139   private:
00140     flag_storage mFlags;
00141   };
00142 
00143   template <typename T>
00144   Flags<T> operator|(T flag1, T flag2)
00145   {
00146     Flags<T> flags;
00147     flags.mFlags = (typename Flags<T>::flag_storage)flag1 | (typename Flags<T>::flag_storage)flag2;
00148     return flags;
00149   }
00150 
00151   template <typename T>
00152   Flags<T> operator&(T flag1, T flag2)
00153   {
00154     Flags<T> flags;
00155     flags.mFlags = (typename Flags<T>::flag_storage)flag1 & (typename Flags<T>::flag_storage)flag2;
00156     return flags;
00157   }
00158 }
00159 
00160 #define VL_DECLARE_FLAGS(EnumType, FlagTypeName)                                \
00161   template<EnumType> Flags<EnumType> operator|(EnumType flag1, EnumType flag2); \
00162   template<EnumType> Flags<EnumType> operator&(EnumType flag1, EnumType flag2); \
00163   typedef Flags<EnumType> FlagTypeName;
00164 
00165 /*** 
00166 
00167 usage:
00168  
00169 enum MyFlagEnum
00170 {
00171   Flag1 = 0x1,
00172   Flag2 = 0x2,
00173   Flag3 = 0x4,
00174 };
00175  
00176 VL_DECLARE_FLAGS(MyFlagEnum, MyFlags)
00177 
00178 ...
00179 
00180 MyFlags f = Flag3 | Flag1;
00181 
00182 ***/
00183 
00184 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:28.
Permission is granted to use this page to write and publish articles regarding Visualization Library.