NAMD
OwnerBox.h
Go to the documentation of this file.
1 
7 #ifndef OWNERBOX_H
8 #define OWNERBOX_H
9 
10 #include "charm++.h"
11 
12 template <class Owner, class Data> class Box;
13 
14 template <class Owner, class Data> class OwnerBox {
15 
16  friend class Box<Owner,Data>;
17 
18  int bid, type;
19 
20  public:
21 
22  OwnerBox(Owner *o, void (Owner::*fn)() ) :
23  bid(-1), type(-1),
24  owner(o), callback(fn), data(0),
25  numberUsers(0), openCount(0), closeCount(0) {};
26 
27  OwnerBox(Owner *o, void (Owner::*fn)(), int id ,int tp) :
28  bid(id), type(tp),
29  owner(o), callback(fn), data(0),
30  numberUsers(0), openCount(0), closeCount(0) {};
31 
32 
33  ~OwnerBox(void) {
34  if (numberUsers) {
35  CkPrintf("OwnerBox::~OwnerBox() - still have boxes out there!\n");
36  }
37  }
38 
39  void open(Data* d) {
40  closeCount = openCount = numberUsers;
41  data = d;
42  if ( ! closeCount ) close();
43  }
44 
45  inline void close(void);
46 
47  inline Box<Owner,Data> *checkOut(int id);
48 
49  inline void checkIn(Box<Owner,Data> * box);
50 
51  int isOpen() {
52  return (closeCount != numberUsers || openCount != numberUsers);
53  }
54 
55  // manipulate counters without Box object
56  void clientAdd();
57  void clientRemove();
58  Data* clientOpen(int count=1) {
59  openCount -= count;
60  return data;
61  }
62  void clientClose(int count=1) {
63  if ( ! (closeCount -= count) ){
64  //fprintf(stderr, "Calling clientClose()!\n");
65  close();
66  }
67  }
68 
69  private:
70  Owner *owner;
71  void (Owner::*callback)(void);
72  Data* data;
73  int numberUsers, openCount, closeCount;
74 };
75 
76 template <class Owner, class Data>
78  if (closeCount != numberUsers || openCount != numberUsers) {
79  CkPrintf("OwnerBox::clientAdd() while in use\n");
80  }
81  ++numberUsers; ++closeCount; ++openCount;
82 }
83 
84 template <class Owner, class Data>
86  clientAdd();
87  return (new Box<Owner,Data>(this,id));
88 }
89 
90 template <class Owner, class Data>
92  if (closeCount != numberUsers || openCount != numberUsers) {
93  CkPrintf("OwnerBox::clientRemove() while in use\n");
94  }
95  if ( ! numberUsers-- ) {
96  CkPrintf("OwnerBox::clientRemove() - no registrants remaining\n");
97  numberUsers = 0;
98  } else {
99  closeCount--; openCount--;
100  }
101 }
102 
103 template <class Owner, class Data>
105  delete box;
106  clientRemove();
107 }
108 
109 template <class Owner, class Data>
110 inline void OwnerBox<Owner,Data>::close(void) {
111  if (!closeCount && !openCount) {
112  data = 0; closeCount = openCount = numberUsers;
113  (owner->*callback)();
114  } else {
115  CkPrintf("OwnerBox::close() - close called, but closeCount %d openCount %d\n", closeCount, openCount);
116  }
117 }
118 
119 #endif
Data * clientOpen(int count=1)
Definition: OwnerBox.h:58
void checkIn(Box< Owner, Data > *box)
Definition: OwnerBox.h:104
void clientRemove()
Definition: OwnerBox.h:91
OwnerBox(Owner *o, void(Owner::*fn)())
Definition: OwnerBox.h:22
void open(Data *d)
Definition: OwnerBox.h:39
~OwnerBox(void)
Definition: OwnerBox.h:33
OwnerBox(Owner *o, void(Owner::*fn)(), int id, int tp)
Definition: OwnerBox.h:27
void close(void)
Definition: OwnerBox.h:110
void clientClose(int count=1)
Definition: OwnerBox.h:62
Box< Owner, Data > * checkOut(int id)
Definition: OwnerBox.h:85
Definition: Box.h:14
void clientAdd()
Definition: OwnerBox.h:77
int isOpen()
Definition: OwnerBox.h:51