CYCLUS
intrusive_base.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_INTRUSIVE_BASE_H_
2 #define CYCLUS_SRC_INTRUSIVE_BASE_H_
3 
4 #include <boost/intrusive_ptr.hpp>
5 #include <boost/assert.hpp>
6 
7 #include "logger.h"
8 
9 namespace cyclus {
10 
11 /// IntrusiveBase provides a base class that fulfulls basic requirements
12 /// for a (sub) class to be used in a boost::intrusive_ptr.
13 ///
14 /// Allows subclasses to track their reference count via the onboard
15 /// "counter_" member variable. To use a class as a boost::intrusive_ptr,
16 /// it should inherit from IntrusiveBase with default access (NOT
17 /// public). All destructors of subclasses (and subsub, etc.) should be
18 /// virtual to ensure memory deallocation occurs properly for objects
19 /// that have been up-casted:
20 /// @code
21 /// class Resource: IntrusiveBase<Resource> {
22 /// ...
23 /// virtual ~Resource();
24 /// ...
25 /// }
26 ///
27 /// int main(...) {
28 /// boost::intrusive_ptr<Resource> Resource(new Resource());
29 /// boost::intrusive_ptr<Resource> resource2(new Resource());
30 ///
31 /// // use resource as if it were a regular pointer, e.g.
32 /// double quantity = resource->quantity();
33 ///
34 /// // equals operator compares raw pointer values:
35 ///
36 /// // always true
37 /// if (resource == resource) {}
38 ///
39 /// // always false
40 /// if (resource == resource2) {}
41 ///
42 /// // don't worry about deallocation - it will be automatic.
43 /// }
44 /// @endcode
45 template <class Derived> class IntrusiveBase {
46  /// used by boost::intrusive_ptr to increase object's reference count
47  friend void intrusive_ptr_add_ref(const Derived* p) {
48  BOOST_ASSERT(p);
49  ++((const IntrusiveBase*) p)->counter_;
50  }
51 
52  /// used by boost::intrusive_ptr to decrease object's reference count
53  /// and deallocate the object if the ref count is zero.
54  friend void intrusive_ptr_release(const Derived* p) {
55  BOOST_ASSERT(p);
56  if (--((const IntrusiveBase*) p)->counter_ == 0) {
57  delete p;
58  }
59  }
60 
61  protected:
62  /// protected because we don't want direct instantiations of
63  IntrusiveBase(): counter_(0) {}
64 
66 
67  /// the copy constructor must zero out the ref count
68  IntrusiveBase(const IntrusiveBase&) : counter_(0) {}
69 
71  return *this;
72  }
73 
74  private:
75  /// tracks an object's reference count
76  mutable unsigned long counter_;
77 };
78 
79 } // namespace cyclus
80 
81 #endif // CYCLUS_SRC_INTRUSIVE_BASE_H_
IntrusiveBase(const IntrusiveBase &)
the copy constructor must zero out the ref count
IntrusiveBase()
protected because we don&#39;t want direct instantiations of
IntrusiveBase & operator=(const IntrusiveBase &)
friend void intrusive_ptr_release(const Derived *p)
used by boost::intrusive_ptr to decrease object&#39;s reference count and deallocate the object if the re...
IntrusiveBase provides a base class that fulfulls basic requirements for a (sub) class to be used in ...
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
friend void intrusive_ptr_add_ref(const Derived *p)
used by boost::intrusive_ptr to increase object&#39;s reference count