PageRenderTime 39ms CodeModel.GetById 11ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/TGame/TUtil/Data/Flags.h

http://awoe.googlecode.com/
C++ Header | 143 lines | 122 code | 21 blank | 0 comment | 7 complexity | fe036643438cf31100d9068cc7cb35b2 MD5 | raw file
  1#ifndef __DATA_UTIL_FLAGS__
  2#define __DATA_UTIL_FLAGS__
  3
  4namespace woe
  5{
  6	template<int length>
  7	class Flags
  8	{
  9		static const short	len_int = (length>>5)+1;
 10		static const short	len_cha = (length>>3)+1;
 11
 12		public:
 13		Flags(const char& val = 0)
 14		{
 15			memset((void*)&_data, val, sizeof(_data));
 16		}
 17
 18		Flags(const Flags& flg)
 19		{
 20			memcpy((void*)&_data, (void*)&flg._data, sizeof(_data));
 21		}
 22
 23		Flags& operator = (const Flags& flg)
 24		{
 25			memcpy((void*)&_data, (void*)&flg._data, sizeof(_data));
 26			return *this;
 27		}
 28
 29		bool set(const int * val)
 30		{
 31			memcpy((void*)&_data, (void*)val, sizeof(_data));
 32			return true;
 33		}
 34
 35		bool set(const char * val)
 36		{
 37			memcpy((void*)&_data, (void*)val, sizeof(_data));
 38			return true;
 39		}
 40
 41		bool set(const short& idx, const int& val)
 42		{
 43			if (idx<len_int)
 44			{
 45				_data._i[idx] = val;
 46				return true;
 47			}
 48			else
 49			{
 50				return false;
 51			}
 52		}
 53
 54		bool set(const short& idx, const char& val)
 55		{
 56			if (idx<len_cha)
 57			{
 58				_data._c[idx] = val;
 59				return true;
 60			}
 61			else
 62			{
 63				return false;
 64			}
 65		}
 66
 67		bool get(const short& idx, int& val)const
 68		{
 69			if (idx<len_int)
 70			{
 71				val = _data._i[idx];
 72				return true;
 73			}
 74			else
 75			{
 76				return false;
 77			}
 78		}
 79
 80		bool get(const short& idx, char& val)const
 81		{
 82			if (idx<len_cha)
 83			{
 84				val = _data._c[idx];
 85				return true;
 86			}
 87			else
 88			{
 89				return false;
 90			}
 91		}
 92
 93		void setFlg(const short& idx)
 94		{
 95			short idx_b = idx>>3;
 96			short flg = 1<< (idx&7);
 97
 98			if (idx_b<len_cha)
 99			{
100				_data._c[idx_b] |= flg; 
101			}
102		}
103
104		void clrFlg(const short& idx)
105		{
106			short idx_b = idx>>3;
107			short flg = ~( 1<< (idx&7) );
108
109			if (idx_b<len_cha)
110			{
111				_data._c[idx_b] &= flg; 
112			}
113		}
114		bool hasFlg(const short& idx) const
115		{
116			short idx_b = idx>>3;
117			short flg = 1<< (idx&7);
118
119			if (idx_b<len_cha)
120			{
121				return (_data._c[idx_b] & flg)!=0; 
122			}
123			else
124			{
125				return false;
126			}
127		}
128
129		protected:
130		union Data
131		{
132			int		_i[len_int];
133			char	_c[len_cha];
134		};
135
136		Data	_data;
137	};
138
139}
140
141#endif
142
143