Skip to content

Commit e7bd309

Browse files
Add template parameter to reference_counting
This is so that the class can be used by types that do not have a static `blank` field. A particular case which would be useful in our codebase is std::map.
1 parent 9f06b86 commit e7bd309

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/util/reference_counting.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include "invariant.h"
1616

17-
template<typename T>
17+
/// \tparam empty: pointer to empty data, if unspecified use a reference to
18+
/// T::blank
19+
template <typename T, const T &empty = T::blank>
1820
// NOLINTNEXTLINE(readability/identifiers)
1921
class reference_counting
2022
{
@@ -67,7 +69,7 @@ class reference_counting
6769
const T &read() const
6870
{
6971
if(d==nullptr)
70-
return T::blank;
72+
return empty;
7173
return *d;
7274
}
7375

@@ -115,8 +117,8 @@ class reference_counting
115117
}
116118
};
117119

118-
template<class T>
119-
void reference_counting<T>::remove_ref(dt *old_d)
120+
template <class T, const T &empty>
121+
void reference_counting<T, empty>::remove_ref(dt *old_d)
120122
{
121123
if(old_d==nullptr)
122124
return;
@@ -144,8 +146,8 @@ void reference_counting<T>::remove_ref(dt *old_d)
144146
}
145147
}
146148

147-
template<class T>
148-
void reference_counting<T>::detach()
149+
template <class T, const T &empty>
150+
void reference_counting<T, empty>::detach()
149151
{
150152
#ifdef REFERENCE_COUNTING_DEBUG
151153
std::cout << "DETACH1: " << d << '\n';
@@ -179,20 +181,20 @@ void reference_counting<T>::detach()
179181
#endif
180182
}
181183

182-
template<class T>
184+
template <class T, const T &empty>
183185
bool operator==(
184-
const reference_counting<T> &o1,
185-
const reference_counting<T> &o2)
186+
const reference_counting<T, empty> &o1,
187+
const reference_counting<T, empty> &o2)
186188
{
187189
if(o1.get_d()==o2.get_d())
188190
return true;
189191
return o1.read()==o2.read();
190192
}
191193

192-
template<class T>
194+
template <class T, const T &empty>
193195
inline bool operator!=(
194-
const reference_counting<T> &i1,
195-
const reference_counting<T> &i2)
196+
const reference_counting<T, empty> &i1,
197+
const reference_counting<T, empty> &i2)
196198
{ return !(i1==i2); }
197199

198200
#endif // CPROVER_UTIL_REFERENCE_COUNTING_H

0 commit comments

Comments
 (0)