summaryrefslogtreecommitdiff
path: root/TAO/tao/AnyTypeCode/True_RefCount_Policy.h
blob: e1198f14cbe3a2a7adf75e42f607001f154a96d2 (plain)
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
// -*- C++ -*-

//=============================================================================
/**
 *  @file    True_RefCount_Policy.h
 *
 *  Header file for TAO's true reference count policy (unrelated to
 *  CORBA policies).
 *
 *  @author Ossama Othman <ossama@dre.vanderbilt.edu>
 */
//=============================================================================

#ifndef TAO_TRUE_REFCOUNT_POLICY_H
#define TAO_TRUE_REFCOUNT_POLICY_H

#include /**/ "ace/pre.h"

#include "tao/AnyTypeCode/TAO_AnyTypeCode_Export.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "tao/orbconf.h"

#include "ace/Thread_Mutex.h"
#if defined (ACE_HAS_CPP11)
# include <atomic>
#else
# include "ace/Atomic_Op.h"
#endif /* ACE_HAS_CPP11 */

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  /**
   * @class True_RefCount_Policy
   *
   * @brief True reference counting policy.
   *
   * This class is intended to be used as a "policy" argument to a
   * host class template that requires true/functional reference
   * counting.  That class would then inherit privately from it like
   * so:
   *
   * @code
   *   template <class RefCountPolicy>
   *   class MyHostClass : private RefCountPolicy
   *   {
   *   public:
   *     void my_add_ref (void)    { this->RefCountPolicy::add_ref (); }
   *     void my_remove_ref (void) { this->RefCountPolicy::remove_ref (); }
   *   };
   * @endcode
   *
   * and use it like so:
   *
   * @code
   *   typedef MyHostClass<TAO::True_RefCount_Policy> MyRefCountedClass;
   *   MyRefCountedClass * p = new MyRefCountedClass;
   *   ...
   *   p->my_remove_ref ();
   * @endcode
   *
   * @note Ideally, the host class should declare a protected
   *       destructor to enforce proper memory management through the
   *       reference counting mechanism, i.e. to prevent clients from
   *       calling @c operator @c delete() directly on the host class
   *       object.
   */
  class TAO_AnyTypeCode_Export True_RefCount_Policy
  {
  public:

    /// Increase the reference count on this object.
    void add_ref (void);

    /// Decrease the reference count on this object.
    /**
     * Decrease the reference count on this object.  Once the
     * reference count drops to zero, call @c operator @c delete()
     * on this object.
     */
    void remove_ref (void);

  protected:
    /// Constructor.
    /**
     * @note This constructor is protected since it not meant to be
     *       instantiated/used as a standalone object.
     */
    True_RefCount_Policy (void);

    /// Destructor.
    /**
     * @note The destructor must be virtual to ensure that subclass
     *       destructors are called when the reference count drops to
     *       zero, i.e. when @c remove_ref() calls @c operator
     *       @c delete @c this.
     */
    virtual ~True_RefCount_Policy (void);

  private:
    /// Reference counter.
#if defined (ACE_HAS_CPP11)
    std::atomic<uint32_t> refcount_;
#else
    ACE_Atomic_Op<TAO_SYNCH_MUTEX, unsigned long> refcount_;
#endif /* ACE_HAS_CPP11 */
  };

}  // End namespace TAO

TAO_END_VERSIONED_NAMESPACE_DECL

#ifdef __ACE_INLINE__
# include "tao/AnyTypeCode/True_RefCount_Policy.inl"
#endif  /* __ACE_INLINE__ */

#include /**/ "ace/post.h"

#endif /* TAO_TRUE_REFCOUNT_POLICY_H */