File: RefCount.hh

package info (click to toggle)
topcom 1.1.2%2Bds-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,788 kB
  • sloc: cpp: 37,616; sh: 4,262; makefile: 497; ansic: 49
file content (247 lines) | stat: -rw-r--r-- 6,423 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
////////////////////////////////////////////////////////////////////////////////
// 
// RefCount.hh (completely deprecated since not thread-safe)
//
//    produced: 05/02/98 jr
// last change: 05/02/98 jr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef REFCOUNT_HH
#define REFCOUNT_HH

#include <memory>
#include <atomic>

namespace topcom {

  // reference counter must be atomic for multi-threading:
  typedef std::atomic<unsigned long> refcount_type;
  // typedef unsigned long              refcount_type;

  class RefCount {
  private:
    refcount_type _refcount;
  public:
    inline void pick_reference();
    inline void drop_reference();
    inline RefCount();
    inline RefCount(const RefCount&);
    inline ~RefCount();
    inline const refcount_type& refcount() const;
  };

  inline void RefCount::pick_reference() {
    ++_refcount;
#ifdef REFCOUNT_DEBUG
    std::cout << "pick_reference(): refcount = " << _refcount
	      << " to address " << this << std::endl;
#endif
  }
  inline void RefCount::drop_reference() {
    --_refcount;
#ifdef REFCOUNT_DEBUG
    std::cout << "drop_reference(): refcount = " << _refcount
	      << " to address " << this << std::endl;
#endif
  }
  inline RefCount::RefCount() : _refcount(0) {}
  inline RefCount::RefCount(const RefCount&) : _refcount(0) {}
  inline RefCount::~RefCount() {}
  inline const refcount_type& RefCount::refcount() const {
    return _refcount;
  }

  template <class T>
  class SmartPtr {
  private:
    class RefCountData : public RefCount {
    public:
      T                   _data;
    public:
      RefCountData() : RefCount(), _data() {}
      RefCountData(const RefCountData& rcd) : RefCount(rcd), _data(rcd._data) {}
      RefCountData(const T& obj) : RefCount(), _data(obj) {}
      ~RefCountData() {}
    };
  private:
    //   typedef std::simple_alloc<RefCountData, std::alloc> data_allocator;
    std::allocator<RefCountData> data_allocator;
  private:
    RefCountData* _bufptr;
  private:
    RefCountData* _ptr;
  public:
    // constructors:
    inline SmartPtr();
    inline SmartPtr(const SmartPtr&);
    inline SmartPtr(const T&);
    // destructor:
    inline ~SmartPtr();
    // assignment:
    inline SmartPtr& operator=(const SmartPtr&);
    // static nullpointer:
    inline static SmartPtr& smartnullptr();
    // casts:
    inline operator bool() const;
    // accessors:
    inline const refcount_type refcount() const;
    // operators:
    inline const T* operator->() const;
    inline T* operator->();
    inline const T& operator*() const;
    inline T& operator*();
    inline const bool operator==(const SmartPtr& p) const;
    inline const bool operator!=(const SmartPtr& p) const;
  };

  // constructors:
  template <class T>
  inline SmartPtr<T>::SmartPtr() : _ptr(NULL) {
#ifdef CONSTRUCTOR_DEBUG
    std::cout << "SmartPtr<T>::SmartPtr()" << std::endl;
#endif
  }

  template <class T>
  inline SmartPtr<T>::SmartPtr(const SmartPtr& p) : _ptr(p._ptr) {
#ifdef CONSTRUCTOR_DEBUG
    std::cout << "SmartPtr<T>::SmartPtr(const SmartPtr&)" << std::endl;
#endif
    if (_ptr) {
      _ptr->pick_reference();
    }
  }

  template <class T>
  inline SmartPtr<T>::SmartPtr(const T& obj) {
#ifdef CONSTRUCTOR_DEBUG
    std::cout << "SmartPtr<T>::SmartPtr(const T&)" << std::endl;
#endif
    _ptr = data_allocator.allocate(1);
    std::allocator_traits<std::allocator<RefCountData> >::construct(data_allocator, _ptr, obj);
    _ptr->pick_reference();
  }

  // destructor:
  template <class T>
  inline SmartPtr<T>::~SmartPtr() {
#ifdef CONSTRUCTOR_DEBUG
    std::cout << "SmartPtr<T>::~SmartPtr()" << std::endl;
#endif
    if (_ptr) {
      if (_ptr->refcount() == 1) {
#ifdef REFCOUNT_DEBUG
	std::cout << "DESTROY " << _ptr->_data << std::endl;
#endif
	std::allocator_traits<std::allocator<RefCountData> >::destroy(data_allocator, _ptr);
	data_allocator.deallocate(_ptr, 1);
	_ptr = NULL;
      }
      else {
	_ptr->drop_reference();
      }
    }
  }

  // assignment:
  template <class T>
  inline SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr& p) {
#ifdef CONSTRUCTOR_DEBUG
    std::cout << "SmartPtr<T>::operator=(const SmartPtr&)" << std::endl;
#endif
    if (_ptr) {
      if (_ptr->refcount() == 1) {
	std::allocator_traits<std::allocator<RefCountData> >::destroy(data_allocator, _ptr);
	data_allocator.deallocate(_ptr, 1);
	_ptr = NULL;
      }
      else {	
	_ptr->drop_reference();
      }
    }
    _ptr = p._ptr;
    if (_ptr) {
      _ptr->pick_reference();
    }
    return *this;
  }

  // casts:
  template <class T>
  inline SmartPtr<T>::operator bool() const {
    return (_ptr != NULL);
  }

  // accessors:
  template <class T>
  inline const refcount_type SmartPtr<T>::refcount() const {
    return _ptr->refcount();
  }

  // static nullpointer:
  template <class T>
  inline SmartPtr<T>& SmartPtr<T>::smartnullptr() {
    static SmartPtr<T> smartnullptr;
    return smartnullptr;
  }

  // operators:
  template <class T>
  inline const T* SmartPtr<T>::operator->() const {
    return &_ptr->_data;
  }

  template <class T>
  inline T* SmartPtr<T>::operator->() {
    if (_ptr) {
      if (_ptr->refcount() > 1) {
#ifdef DEBUG
	std::cout << "T* SmartPtr<T>::operator->(): copy for overwrite" << std::endl;
#endif
	_ptr->drop_reference();
	_bufptr = _ptr;
	_ptr = data_allocator.allocate(1);
	std::allocator_traits<std::allocator<RefCountData> >::construct(data_allocator, _ptr, *_bufptr);
	_ptr->pick_reference();
      }
    }
    return &_ptr->_data;
  }

  template <class T>
  inline const T& SmartPtr<T>::operator*() const {
    return std::ref(_ptr->_data);
  }

  template <class T>
  inline T& SmartPtr<T>::operator*() {
    if (_ptr) {
      if (_ptr->refcount() > 1) {
#ifdef DEBUG
	std::cout << "T& SmartPtr<T>::operator*(): copy for overwrite" << std::endl;
#endif
	_ptr->drop_reference();
	_bufptr = _ptr;
	_ptr = data_allocator.allocate(1);
	std::allocator_traits<std::allocator<RefCountData> >::construct(data_allocator, _ptr, *_bufptr);
	_ptr->pick_reference();
      }
    }
    return std::ref(_ptr->_data);
  }

  template <class T>
  inline const bool SmartPtr<T>::operator==(const SmartPtr<T>& p) const {
    return (_ptr == p._ptr);
  }

  template <class T>
  inline const bool SmartPtr<T>::operator!=(const SmartPtr<T>& p) const {
    return (!(*this == p));
  }

}; // namespace topcom

#endif

// eof RefCount.hh