RefBank.h
1 
2 /*
3 
4 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
5 All rights reserved.
6 
7 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
8 Dynacoe was released under the MIT License, as detailed below.
9 
10 
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is furnished
17 to do so, subject to the following conditions:
18 
19 The above copyright notice and this permission notice shall
20 be included in all copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
29 
30 
31 
32 */
33 #ifndef DC_REFBANK_H_INCLUDED
34 #define DC_REFBANK_H_INCLUDED
35 
36 
37 
38 #include <set>
39 #include <map>
40 
41 
42 namespace Dynacoe {
43 
44 
45 
46 
47 
48 template<typename T>
61 class RefBank {
62  public:
63 
68  public:
69  virtual void operator()(const T &) = 0;
70  };
71 
72  RefBank();
73 
80  void Deposit(const T & account, uint32_t amount = 1);
81 
88  void Withdraw(const T & account, uint32_t amount = 1);
89 
93  uint32_t GetBalance(const T & account);
94 
95 
99  void WithdrawAll();
100 
105 
106  private:
107  std::map<T, uint32_t> counter;
108  AccountRemover * collector;
109 
110 };
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 template<typename T>
124  collector = nullptr;
125 }
126 
127 
128 
129 template<typename T>
130 void RefBank<T>::Deposit(const T & ref, uint32_t amount) {
131  uint32_t oldBalance = counter[ref];
132  counter[ref] = (oldBalance + amount < oldBalance ? UINT32_MAX : oldBalance + amount);
133 
134 }
135 
136 template<typename T>
137 void RefBank<T>::Withdraw(const T & ref, uint32_t amount) {
138  auto iter = counter.find(ref);
139  if (iter != counter.end()) {
140  iter->second -= (iter->second < amount ? iter->second : amount);
141  if (iter->second == 0) {
142  if (collector)(*collector)(ref);
143  counter.erase(ref);
144  }
145  }
146 }
147 
148 template<typename T>
149 uint32_t RefBank<T>::GetBalance(const T & ref) {
150  return counter[ref];
151 }
152 
153 template<typename T>
155  for(const std::pair<T, uint32_t> & ref : counter) {
156  if (collector)
157  (*collector)(ref.first);
158  }
159  counter.clear();
160 }
161 
162 
163 template<typename T>
165  collector = col;
166 }
167 
168 }
169 
170 
171 #endif
void SetAccountRemover(AccountRemover *)
Sets the account remover callback.
Definition: RefBank.h:164
Class to handle the case where all references to an object are exhausted.
Definition: RefBank.h:67
void WithdrawAll()
Removes all accounts and runs the AccountRemover on all accounts.
Definition: RefBank.h:154
A simple, generic reference counter.
Definition: RefBank.h:61
Definition: AssetID.h:37
uint32_t GetBalance(const T &account)
Returns the number of reference counts accumulated in the account.
Definition: RefBank.h:149
void Withdraw(const T &account, uint32_t amount=1)
Deduct a reference count amount.
Definition: RefBank.h:137
void Deposit(const T &account, uint32_t amount=1)
Adds a reference count amount.
Definition: RefBank.h:130