NAMD
Box.h
Go to the documentation of this file.
1 
7 #ifndef BOX_H
8 #define BOX_H
9 
10 // #define BOX_DEBUG
11 
12 #include "OwnerBox.h"
13 
14 template <class Owner, class Data> class Box {
15 
16  friend class OwnerBox<Owner,Data>;
17 
18  private:
19 
20  Box(OwnerBox<Owner,Data>* o, int n = -1):
21 #ifdef BOX_DEBUG
22  state(CLOSED), user(n),
23 #endif
24  ownerBox(o) {}
25 
26  ~Box(void) {};
27 
28 #ifdef BOX_DEBUG
29  enum box_state {OPEN, CLOSED} state;
30 #endif
31 
32  OwnerBox<Owner,Data> *ownerBox;
33 
34  public:
35 #ifdef BOX_DEBUG
36  int user;
37 #endif
38 
39  Data* open(void) {
40  ownerBox->openCount--;
41 #ifdef BOX_DEBUG
42  if (ownerBox->openCount < 0) NAMD_bug("too many boxes opened");
43  if (state != CLOSED) NAMD_bug("box re-opened");
44  state = OPEN;
45 #endif
46  return ownerBox->data;
47  }
48 
49  void close(Data ** const t) {
50  *t = NULL;
51  ownerBox->closeCount--;
52 #ifdef BOX_DEBUG
53  if (ownerBox->closeCount < 0) NAMD_bug("too many boxes closed");
54  if (state != OPEN) NAMD_bug("box re-closed");
55  state = CLOSED;
56 #endif
57  // Trigger callback!
58  if ( ! ownerBox->closeCount ) {
59  ownerBox->close();
60  }
61  }
62 
63  void skip(void) {
64  ownerBox->openCount--;
65  ownerBox->closeCount--;
66 #ifdef BOX_DEBUG
67  if (state != CLOSED) NAMD_bug("box skipped while open");
68  if (ownerBox->openCount < 0) NAMD_bug("too many boxes opened");
69  if (ownerBox->closeCount < 0) NAMD_bug("too many boxes closed");
70 #endif
71  if ( ! ownerBox->closeCount ) {
72  ownerBox->close();
73  }
74  }
75 
76 };
77 
78 #endif // BOX_H
void NAMD_bug(const char *err_msg)
Definition: common.C:123
void skip(void)
Definition: Box.h:63
Definition: Box.h:14
Data * open(void)
Definition: Box.h:39
void close(Data **const t)
Definition: Box.h:49