/tags/Root-branch-php-utl/SWIG/Examples/python/exceptshadow/example.h
C++ Header | 54 lines | 46 code | 6 blank | 2 comment | 4 complexity | 3d5feac2d80f40b04c82d536b9d39757 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1/* File : example.h */
2
3// A simple exception
4class EmptyError { };
5class FullError {
6 public:
7 int maxsize;
8 FullError(int m) : maxsize(m) { }
9};
10
11#if defined(_MSC_VER)
12 #pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
13#endif
14
15template<typename T> class Queue {
16 int maxsize;
17 T *items;
18 int nitems;
19 int last;
20 public:
21 Queue(int size) {
22 maxsize = size;
23 items = new T[size];
24 nitems = 0;
25 last = 0;
26 }
27 ~Queue() {
28 delete [] items;
29 }
30 void enqueue(T x) throw(FullError) {
31 if (nitems == maxsize) {
32 throw FullError(maxsize);
33 }
34 items[last] = x;
35 last = (last + 1) % maxsize;
36 nitems++;
37 }
38 T dequeue() {
39 T x;
40 if (nitems == 0) throw EmptyError();
41 x = items[(last + maxsize - nitems) % maxsize];
42 nitems--;
43 return x;
44 }
45 int length() {
46 return nitems;
47 }
48};
49
50
51#if defined(_MSC_VER)
52 #pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
53#endif
54