File: NstCollectionRouter.hpp

package info (click to toggle)
nestopia 1.52.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,140 kB
  • sloc: cpp: 127,444; xml: 27,234; ansic: 3,635; makefile: 949; sh: 19
file content (269 lines) | stat: -rw-r--r-- 6,196 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#ifndef NST_COLLECTION_ROUTER_H
#define NST_COLLECTION_ROUTER_H

#pragma once

#include <new>
#include "NstCollectionVector.hpp"
#include "NstObjectDelegate.hpp"

namespace Nestopia
{
	namespace Collection
	{
		template<typename T> struct ConstParam
		{
			typedef const T& Type;
		};

		template<typename T> struct ConstParam<T*>
		{
			typedef const T* const Type;
		};

		template<> struct ConstParam< bool   > { typedef const bool   Type; };
		template<> struct ConstParam< char   > { typedef const int    Type; };
		template<> struct ConstParam< schar  > { typedef const int    Type; };
		template<> struct ConstParam< uchar  > { typedef const uint   Type; };
		template<> struct ConstParam< short  > { typedef const int    Type; };
		template<> struct ConstParam< ushort > { typedef const uint   Type; };
		template<> struct ConstParam< int    > { typedef const int    Type; };
		template<> struct ConstParam< uint   > { typedef const uint   Type; };
		template<> struct ConstParam< long   > { typedef const long   Type; };
		template<> struct ConstParam< ulong  > { typedef const ulong  Type; };

		template<typename Output,typename Input,typename Key=uint>
		class Router
		{
			typedef typename ConstParam<Key>::Type KeyParam;

		public:

			typedef Object::Delegate<Output,Input> Callback;

			template<typename T> struct Entry
			{
				Key key;
				Output (T::*function)(Input);
			};

			template<typename T> struct HookEntry
			{
				Key key;
				void (T::*function)(Input);
			};

			struct Item
			{
				const Key key;
				Callback callback;

				Item(KeyParam k)
				: key(k) {}
			};

			template<typename Data,typename Code>
			Router(KeyParam,Data*,Code);

			template<typename Data,typename Array>
			Router(Data*,const Array&);

			~Router();

			NST_NO_INLINE void Remove(const void*);
			void RemoveAll(const void*);
			void Defrag();

			Callback& operator [] (KeyParam);

		private:

			NST_NO_INLINE void Add(KeyParam,const Callback&);
			NST_NO_INLINE void Set(KeyParam,const Callback&);
			NST_NO_INLINE void Remove(KeyParam,const Callback&);

			template<typename Data>
			void Add(Data*,const Entry<Data>*,uint);

			template<typename Data>
			void Set(Data*,const Entry<Data>*,uint);

			class Items : public Collection::Vector<Item>
			{
			public:

				Item& GetSorted(KeyParam,bool&);
				Item* FindSorted(KeyParam);
				Item& AtSorted(KeyParam);

			private:

				NST_FORCE_INLINE uint LowerBound(KeyParam key) const
				{
					uint left = 0, right = Size();

					while (left < right)
					{
						const uint middle = (left + right) / 2;

						if (this->At(middle)->key < key)
							left = middle + 1;
						else
							right = middle;
					}

					return left;
				}

			public:

				const Item* FindSorted(KeyParam key) const
				{
					return const_cast<Items*>(this)->FindSorted(key);
				}
			};

		private:

			struct Hook
			{
				typedef Object::Delegate<void,Input> Item;
				typedef Vector<Item> Items;

				Output Invoke(Input);

				Items items;
				Callback main;
				Hook* next;

				Hook()
				: next(NULL) {}
			};

			NST_NO_INLINE void AddHook(KeyParam,const typename Hook::Item&);
			NST_NO_INLINE uint RemoveHook(Item*,Hook*,typename Hook::Item*);
			NST_NO_INLINE void RemoveHook(KeyParam,const typename Hook::Item&);
			NST_NO_INLINE void RemoveHooks(const void*);

			class HookRouter
			{
				Router& router;

				template<typename Data>
				void Add(Data*,const HookEntry<Data>*,uint);

			public:

				HookRouter(Router& ref)
				: router(ref) {}

				typedef typename Hook::Item Callback;

				template<typename Data,typename Code>
				void Add(KeyParam key,Data* data,Code code)
				{
					router.AddHook( key, Callback(data,code) );
				}

				template<typename Data,typename Hooks>
				void Add(Data* data,const Hooks& hooks)
				{
					Add( data, hooks, sizeof(array(hooks)) );
				}

				void Remove(const void* data)
				{
					router.RemoveHooks( data );
				}
			};

			Items items;
			Hook* hooks;

		public:

			Router()
			: hooks(NULL) {}

			const Item* operator () (KeyParam key) const
			{
				return items.FindSorted( key );
			}

			uint Size() const
			{
				return items.Size();
			}

			HookRouter Hooks()
			{
				return *this;
			}

			template<typename Data,typename Code>
			void Add(KeyParam key,Data* data,Code code)
			{
				Add( key, Callback(data,code) );
			}

			template<typename Data,typename Array>
			void Add(Data* data,const Array& arr)
			{
				Add( data, arr, sizeof(array(arr)) );
			}

			template<typename Data,typename Array,typename HookArray>
			void Add(Data* data,const Array& arr,const HookArray& hookArray)
			{
				Add( data, arr, sizeof(array(arr)) );
				Hooks().Add( data, hookArray );
			}

			template<typename Data,typename Code>
			void Set(KeyParam key,Data* data,Code code)
			{
				Set( key, Callback(data,code) );
			}

			template<typename Data,typename Array>
			void Set(Data* data,const Array& arr)
			{
				Set( data, arr, sizeof(array(arr)) );
			}

			template<typename Data,typename Code>
			void Remove(KeyParam key,Data* data,Code code)
			{
				Remove( key, Callback(data,code) );
			}
		};
	}
}

#include "NstCollectionRouter.inl"

#endif