summaryrefslogtreecommitdiff
path: root/XMPCommon/BaseClasses/TSingleton.h
blob: 9a14c93d74dc52adf040a9624f0585f2b3b9c40c (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
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
#ifndef __TSingletonImpl_h__
#define __TSingletonImpl_h__ 1

// =================================================================================================
// Copyright Adobe
// Copyright 2014 Adobe
// All Rights Reserved
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it. 
// =================================================================================================

#include "XMPCommon/XMPCommonDefines_I.h"
#include <stdexcept>
#include <cstdlib>

namespace AdobeXMPCommon_Int {

	template< typename T >
	class TSingleThreaded {
	public:
		typedef T VolatileType;

	protected:
		inline explicit TSingleThreaded() {}
		inline ~TSingleThreaded() {}

	protected:
		class LockThread {
		public:
			inline explicit LockThread() {
				TSingleThreaded::Lock();
			}
			inline ~LockThread() {
				TSingleThreaded::Unlock();
			}

		private:
			inline explicit LockThread( const LockThread & ) {}
			inline LockThread & operator=( const LockThread & ) { return *this; }
		};

	private:
		friend class LockThread;

		inline static void Lock() {}
		inline static void Unlock() {}

	private:
		inline explicit TSingleThreaded( const TSingleThreaded & ) {}
		inline TSingleThreaded & operator=( const TSingleThreaded & ) { return *this; }
	};

	template< typename T >
	class TDefaultLifetime;

	template< typename T >
	class TInitTermiateControlledLifeTime;

	template< typename T >
	class TCreateUsingNew;

	template< typename T, typename CreationPolicy = TCreateUsingNew<T>,
		template < typename > class LifetimePolicy = TInitTermiateControlledLifeTime,
		template < typename > class ThreadingModel = TSingleThreaded >
	class TSingleton
		: public CreationPolicy,
		public LifetimePolicy<T>,
		public ThreadingModel<T>
	{
	public:
		typedef T &		reference;
		typedef T *		pointer;

		static reference Instance() {
			if ( TSingleton::mInstance == 0 ) {
				typename ThreadingModel<T>::LockThread lock;
				if ( TSingleton::mInstance == 0 ) {
					if ( TSingleton::mDestroyed ) {
						LifetimePolicy<T>::OnDeadReference();
						TSingleton::mDestroyed = false;
					}
					TSingleton::mInstance = CreationPolicy::CreateInstance();
					try {
						LifetimePolicy<T>::ScheduleForDestruction( TSingleton::Destroy );
					} catch( ... ) {
						CreationPolicy::DestroyInstance( TSingleton::mInstance );
					}
				}
			}
			return *(TSingleton::mInstance);
		}
		static void Destroy();

	protected:
		inline explicit TSingleton() {
			assert(TSingleton::mInstance == 0);
			TSingleton::mInstance = static_cast< pointer >( this );
			TSingleton::mDestroyed = false;
		}
		inline ~TSingleton() {
			TSingleton::mInstance = 0;
			TSingleton::mDestroyed = true;
		}

	private:
		static pointer										mInstance;
		static bool											mDestroyed;

	private:
		inline explicit TSingleton( const TSingleton & ) {}
		inline TSingleton & operator=( const TSingleton & ) { return *this; }
	};

	template< typename Ty, typename C, template < typename > class L, template < typename > class T >
	void TSingleton< Ty, C, L, T >::Destroy() {
		if ( TSingleton::mInstance != 0 ) {
			typename T<Ty>::LockThread lock;
			if ( TSingleton::mInstance != 0 ) {
				C::DestroyInstance(TSingleton::mInstance);
				TSingleton::mInstance = 0;
				TSingleton::mDestroyed = true;
			}
		}
	}

	template< typename Ty, typename C, template < typename > class L, template < typename > class T >
	typename TSingleton<Ty, C, L, T>::pointer TSingleton<Ty, C, L, T>::mInstance = 0;
	//typename TSingleton<Ty, C, L, T>::template T<Ty>::VolatileType * TSingleton<Ty, C, L, T>::mInstance = 0;

	template< typename Ty, typename C, template < typename > class L, template < typename > class T >
	bool TSingleton<Ty, C, L, T>::mDestroyed = false;

	template< typename T >
	class TDefaultLifetime {
	protected:
		inline explicit TDefaultLifetime() {}
		inline ~TDefaultLifetime() {}

		inline static void OnDeadReference() {
			throw std::logic_error("Dead Reference Detected"); 
		}
		inline static void ScheduleForDestruction(void (*pFun)()) {
			std::atexit(pFun); 
		}

	private:
		inline explicit TDefaultLifetime( const TDefaultLifetime & ) {}
		inline TDefaultLifetime & operator=( const TDefaultLifetime & ) { return *this; }
	};

	template< typename T >
	class TCreateUsingNew {
	public:
		typedef T * pointer;
	protected :
		inline explicit TCreateUsingNew() {}
		inline ~TCreateUsingNew() {}

		inline static pointer CreateInstance() { return new T(); }
		inline static void DestroyInstance(pointer t) { delete t; }

	private : 
		inline explicit TCreateUsingNew( const TCreateUsingNew & ) {}
		inline TCreateUsingNew & operator=( const TCreateUsingNew & ) { return *this; }
	};

	template< typename T >
	class TInitTermiateControlledLifeTime {
	protected:
		inline explicit TInitTermiateControlledLifeTime() {}
		inline ~TInitTermiateControlledLifeTime() {}

		inline static void OnDeadReference() {
			//do nothing
		}
		inline static void ScheduleForDestruction(void (*pFun)()) {
			
		}

	private:
		inline explicit TInitTermiateControlledLifeTime( const TDefaultLifetime< T > & ) {}
		inline TInitTermiateControlledLifeTime & operator=( const TDefaultLifetime< T > & ) { return *this; }
	};

}
#endif  // __TSingletonImpl_h__