summaryrefslogtreecommitdiff
path: root/ace/Singleton.h
blob: 7b40bd353d5c07555f2bf066fe84b1f75138943f (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
125
126
127
128
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    ace
//
// = FILENAME
//    Singleton.h
//
// = DESCRIPTION
//
// = AUTHOR
//    Tim Harrison (harrison@cs.wustl.edu), Douglas C. Schmidt, Chris
//    Lahey, Rich Christy, and David Levine.
//
// ============================================================================

#if !defined (ACE_SINGLETON_H)
#define ACE_SINGLETON_H

#include "ace/Synch.h"

template <class TYPE, class ACE_LOCK>
class ACE_Singleton : public ACE_Cleanup
  // = TITLE
  //     A Singleton Adapter the uses the Adapter pattern to turn
  //     ordinary classes into Singletons optimized with the
  //     Double-Checked Locking optimization pattern.
  //
  // = DESCRIPTION
  //     This implementation is a slight variation on the GoF
  //     Singleton pattern.  In particular, a single
  //     <ACE_Singleton<TYPE, ACE_LOCK> > instance is allocated here, not
  //     a <TYPE> instance.  The reason for this is to allow
  //     registration with the <ACE_Object_Manager>, so that the
  //     Singleton can be cleaned up when the process exits.  For this
  //     scheme to work, a (static) <cleanup> function must be
  //     provided.  <ACE_Singleton> provides one so that TYPE doesn't
  //     need to.
{
public:
  static TYPE *instance (void);
  // Global access point to the Singleton.

  virtual void cleanup (void *param = 0);
  // Cleanup method, used by <ace_cleanup_destroyer> to destroy the
  // <ACE_Singleton>.

  static void dump (void);
  // Dump the state of the object.

protected:
  ACE_Singleton (void);
  // Default constructor.

  TYPE instance_;
  // Contained instance.

#if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
  static ACE_Singleton<TYPE, ACE_LOCK> *singleton_;
  // Pointer to the Singleton (ACE_Cleanup) instance.
#endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */

  static ACE_Singleton<TYPE, ACE_LOCK> *&instance_i (void);
  // Get pointer to the Singleton instance.
};

template <class TYPE, class ACE_LOCK>
class ACE_TSS_Singleton : public ACE_Cleanup
  // = TITLE
  //     This class uses the Adapter pattern to turn ordinary classes
  //     into Thread-specific Singletons optimized with the
  //     Double-Checked Locking optimization pattern.
  //
  // = DESCRIPTION
  //     This implementation is another variation on the GoF Singleton
  //     pattern.  In this case, a single <ACE_TSS_Singleton<TYPE,
  //     LOCK> > instance is allocated here, not a <TYPE> instance.
  //     Each call to the <instance> static method returns a Singleton
  //     whose pointer resides in thread-specific storage.  As with
  //     <ACE_Singleton>, we use the <ACE_Object_Manager> so that the
  //     Singleton can be cleaned up when the process exits.  For this
  //     scheme to work, a (static) <cleanup> function must be
  //     provided.  <ACE_Singleton> provides one so that TYPE doesn't
  //     need to.
{
public:
  static TYPE *instance (void);
  // Global access point to the Singleton.

  virtual void cleanup (void *param = 0);
  // Cleanup method, used by <ace_cleanup_destroyer> to destroy the
  // singleton.

  static void dump (void);
  // Dump the state of the object.

protected:
  ACE_TSS_Singleton (void);
  // Default constructor.

  ACE_TSS<TYPE> instance_;
  // Contained instance.

#if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
  static ACE_TSS_Singleton<TYPE, ACE_LOCK> *singleton_;
  // Pointer to the Singleton (ACE_Cleanup) instance.
#endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */

  static ACE_TSS_Singleton<TYPE, ACE_LOCK> *&instance_i (void);
  // Get pointer to the TSS Singleton instance.
};

#if defined (__ACE_INLINE__)
#include "ace/Singleton.i"
#endif /* __ACE_INLINE__ */

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Singleton.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Singleton.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#endif /* ACE_SINGLETON_H */