The check to see if an item was already on the heap was randomly hitting.
[jitcs.git] / include / jitcs_adt_refcounter.h
blob3d4ec038a925fd513476d42345d0a29e9a98832c
1 //===-- jitcs_adt_refcounter.h - pointer helper type ------------*- C++ -*-===//
2 //
3 //
4 //===----------------------------------------------------------------------===//
6 #ifndef _JITCS_ADT_REFCOUNTER_H_
7 #define _JITCS_ADT_REFCOUNTER_H_
9 #include <assert.h>
11 namespace jitcs {
12 // Ref represents a nonnull pointer to some object.
13 template <typename T> struct RefCounter {
14 T* _ptr;
16 RefCounter() = delete;
17 RefCounter& operator =(const RefCounter&) = delete;
18 RefCounter(const RefCounter& r) { _ptr = r._ptr; _ptr->_incRef(); }
19 RefCounter(T* d) { assert(d != nullptr); _ptr = d; _ptr->_incRef(); }
20 ~RefCounter() { _ptr->_decRef(); }
22 T* operator ->() { return _ptr; }
23 T& operator *() { return *_ptr; }
27 #endif
28 // _JITCS_ADT_REFCOUNTER_H_