Flags.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _Flags_hxx_
00023 #define _Flags_hxx_
00024
00025 #include <bitset>
00026 #include <string>
00027 #include <iosfwd>
00028 #include "Component.hxx"
00029
00030 #include "Enum.hxx"
00031
00032 namespace CLAM {
00033
00034
00040 class FlagsBase : public Component {
00041
00042 public:
00044 typedef unsigned int tValue;
00046 typedef struct {tValue value; const char*name;} tFlagValue;
00047
00048
00049 protected:
00054 const tFlagValue * mFlagValues;
00055
00056
00057 public:
00061 virtual unsigned int GetNFlags() const=0;
00062 virtual void SetFlag(unsigned int whichOne, bool value)=0;
00063 virtual bool IsSetFlag(unsigned int whichOne) const =0;
00069 std::string GetFlagString(unsigned int whichOne) const throw (IllegalValue);
00070
00076 unsigned int GetFlagPosition(const std::string & whichOne) const throw (IllegalValue);
00077
00078
00079
00080
00081
00082
00083
00084 virtual void StoreOn (Storage & storage) const;
00085
00086
00087
00088
00089
00090
00091
00092 virtual void LoadFrom (Storage & storage);
00093
00094
00095 public:
00106 inline bool CheckInvariant();
00107 };
00108
00109 std::istream & operator >> (std::istream & is, FlagsBase & f);
00110 std::ostream & operator << (std::ostream & os, const FlagsBase & f);
00111
00251 template <unsigned int N> class Flags : public FlagsBase, public std::bitset<N>
00252 {
00253
00254 protected:
00256 Flags(tFlagValue * names) : std::bitset<N>() {
00257 mFlagValues=names;
00258 };
00262 Flags(tFlagValue * names, const Flags<N> &t) : std::bitset<N>() {
00263 mFlagValues=names;
00264 };
00271 template <class T> Flags(tFlagValue * names,const T &t) : std::bitset<N>(t) {
00272 mFlagValues=names;
00273 };
00282 template <class T1, class T2> Flags(tFlagValue * names,const T1 &t1,const T2 &t2) :
00283 std::bitset<N>(t1, t2)
00284 {
00285 mFlagValues=names;
00286 };
00287 public:
00289 virtual ~Flags () {};
00290
00292 const char * GetClassName() const {return NULL;}
00293
00294
00295 public:
00296 virtual unsigned int GetNFlags () const {
00297 return N;
00298 }
00299 protected:
00300 virtual bool IsSetFlag(unsigned int whichOne) const {
00301 return std::bitset<N>::test(whichOne);
00302 }
00303 virtual void SetFlag(unsigned int whichOne, bool value=true) {
00304 this->set(whichOne, value);
00305 }
00306
00307
00308 public:
00315 virtual Component * Species () const = 0;
00316
00317 virtual Component * DeepCopy () const {
00318 Flags<N> * pe = (Flags<N>*)Species();
00319 *pe=*this;
00320 return pe;
00321 }
00322
00323 virtual Component * ShallowCopy () const {
00324 Flags<N> * pe = (Flags<N>*)Species();
00325 *pe=*this;
00326 return pe;
00327 }
00328
00329 };
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 #ifdef _MSC_VER
00342 template <unsigned int N>
00343 std::istream & operator >> (std::istream & is, Flags<N> & f) {
00344 return (is >> static_cast<FlagsBase&>(f) );
00345 }
00346
00347 template <unsigned int N>
00348 std::ostream & operator << (std::ostream & os, const Flags<N> & f){
00349 return (os << static_cast<const FlagsBase&>(f));
00350 }
00351 #endif //_MSC_VER
00352
00353 }
00354
00355 #endif//_Flags_hxx_
00356