1 ////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // Nestopia - NES/Famicom emulator written in C++
4 //
5 // Copyright (C) 2003-2008 Martin Freij
6 //
7 // This file is part of Nestopia.
8 //
9 // Nestopia is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Nestopia is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Nestopia; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 //
23 ////////////////////////////////////////////////////////////////////////////////////////
24 
25 #ifndef NST_COLLECTION_BITSET_H
26 #define NST_COLLECTION_BITSET_H
27 
28 #pragma once
29 
30 #include "NstMain.hpp"
31 
32 namespace Nestopia
33 {
34 	namespace Collection
35 	{
36 		class BitSet
37 		{
38 		public:
39 
40 			typedef uint Bits;
41 
42 		private:
43 
44 			Bits bits;
45 
46 		public:
47 
48 			class Bit
49 			{
50 				Bits& bits;
51 				const Bits mask;
52 
53 			public:
54 
Bit(Bits & b,Bits m)55 				Bit(Bits& b,Bits m)
56 				: bits(b), mask(m) {}
57 
operator bool() const58 				operator bool () const
59 				{
60 					return bits & mask;
61 				}
62 
operator =(bool set)63 				void operator = (bool set)
64 				{
65 					bits = set ? (bits | mask) : (bits & ~mask);
66 				}
67 			};
68 
BitSet(Bits b=0)69 			BitSet(Bits b=0)
70 			: bits(b) {}
71 
operator [](uint index)72 			Bit operator [] (uint index)
73 			{
74 				return Bit( bits, 1U << index );
75 			}
76 
operator [](uint index) const77 			bool operator [] (uint index) const
78 			{
79 				return bits & (1U << index);
80 			}
81 
operator ()(uint mask) const82 			uint operator () (uint mask) const
83 			{
84 				return bits & mask;
85 			}
86 
Word()87 			Bits& Word()
88 			{
89 				return bits;
90 			}
91 
Word() const92 			const Bits& Word() const
93 			{
94 				return bits;
95 			}
96 		};
97 	}
98 }
99 
100 #endif
101