PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/ruby/exception_class/example.h

#
C++ Header | 51 lines | 40 code | 9 blank | 2 comment | 4 complexity | 4adc435d86c942f65407e58a91f37e19 MD5 | raw file
 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
11template<typename T> class Queue {
12  int maxsize;
13  T   *items;
14  int nitems;
15  int last;
16 public: 
17  Queue(int size) {
18    maxsize = size;
19    items = new T[size];
20    nitems = 0;
21    last = 0;
22  }
23  ~Queue() {
24    delete [] items;
25  }
26  void enqueue(T x) throw(FullError) {
27    if (nitems == maxsize) {
28      throw FullError(maxsize);
29    }
30    items[last] = x;
31    last = (last + 1) % maxsize;
32    nitems++;
33  }
34  T dequeue()  {
35    T x;
36    if (nitems == 0) throw EmptyError();
37    x = items[(last + maxsize - nitems) % maxsize];
38    nitems--;
39    return x;
40  }
41  int length() {
42    return nitems;
43  }
44};
45
46
47
48
49
50
51