summaryrefslogtreecommitdiffstats
path: root/mcop/pool.h
blob: d3ca05d00ae337af907e43658e6547338a4cf8d2 (plain)
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
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#ifndef ARTS_POOL_H
#define ARTS_POOL_H


/*
 * BC - Status (2002-03-08): Pool<type>
 *
 * Needs to be kept binary compatible by NOT TOUCHING. When you want something
 * else, write a fresh one (used as part of Arts::Dispatcher, thus changing
 * this breaks Arts::Dispatcher binary compatibility).
 */


/**
 * A pool object of the type T keeps a pool of T* pointers, that are numbered.
 *
 * You allocate and release slots, and store T*'s in there. It should take
 * about no time to find a new free slot to store the T object into and to
 * release a slot to be reused.
 *
 * The pool object internally keeps track which slots are used.
 */
#include <stack>
#include <vector>
#include <list>

namespace Arts {

template <class T>
class Pool {
	std::stack<unsigned long> freeIDs;
	std::vector<T *> storage;
public:
	inline T*& operator[](unsigned long n) { return storage[n]; }
	inline void releaseSlot(unsigned long n) {
		freeIDs.push(n);
		storage[n] = 0;
	}
	unsigned long allocSlot() {
		unsigned long slot;
		if(freeIDs.empty())
		{
			unsigned long n;
			for(n=0;n<32;n++) {
				freeIDs.push(storage.size());
				storage.push_back(0);
			}
		}
		slot = freeIDs.top();
		freeIDs.pop();
		return slot;
	}
	std::list<T *> enumerate() {
		std::list<T *> items;
		//std::vector<T *>::iterator i;
		int n,max = storage.size();

		for(n=0; n < max; n++)
			if(storage[n]) items.push_back(storage[n]);

		return items;
	}
	unsigned long max() { return storage.size(); }
};

}
#endif /* POOL_H */