remove \r
[extl.git] / extl / utility / _old_singleton.h
blob0c4683dc7e89a8c0050384b2dc744b142c37e973
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: singleton.h
4 * Created: 08.04.17
5 * Updated: 08.04.17
7 * Brief: Create an instance only before main() begins
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_UTILITY_SINGLETON_H
14 #define EXTL_UTILITY_SINGLETON_H
16 /*!\file singleton.h
17 * \brief Create an instance only before main() begins
19 #ifndef __cplusplus
20 # error singleton.h need be supported by c++.
21 #endif
23 /* ///////////////////////////////////////////////////////////////////////
24 * Includes
26 #include "../prefix.h"
27 #include "../synch/synch_traits_selector.h"
28 #include "../synch/scoped_lock.h"
29 /* ///////////////////////////////////////////////////////////////////////
30 * ::extl namespace
32 EXTL_BEGIN_NAMESPACE
34 /*!\brief singleton class
36 * \param T The object type
38 * \ingroup extl_group_synch
40 template< typename_param_k T >
41 class singleton
43 public:
44 typedef singleton<T> class_type;
45 typedef T object_type;
47 private:
48 static class_type s_self;
49 static object_type* s_object;
51 private:
52 void do_nothing() const {}
54 public:
55 /* Ensure the instance() is called before main() begins */
56 singleton()
58 object_type& obj = class_type::instance();
60 ~singleton()
62 uninit();
64 /* Initializes the object and allows multiple initialization */
65 static void init()
67 if(NULL == object())
69 object() = new object_type();
73 /* Uninitializes the object */
74 static void uninit()
76 if(NULL != object())
78 delete object();
79 object() = NULL;
83 /* Returns the pointer reference to static object */
84 static object_type*& object()
86 #ifdef EXTL_COMPILER_IS_DMC
87 static object_type* p = NULL;
88 return p;
89 #else /* DMC: Link error */
90 return s_object;
91 #endif
94 public:
95 /* Returns a reference of the object */
96 static object_type& instance()
98 /* Initializes the object and allows multiple initialization */
99 init();
101 /* Force the instantiation of singleton<T>,
102 * whose constructor is called before main() begins
104 s_self.do_nothing();
106 EXTL_MESSAGE_ASSERT(NULL != object(), "The singleton is not existed!");
107 return *object();
110 template < typename_param_k T >
111 typename_type_k singleton<T>::object_type* singleton<T>::s_object = NULL;
113 template < typename_param_k T >
114 singleton<T> singleton<T>::s_self;
116 /* ///////////////////////////////////////////////////////////////////////
117 * ::extl namespace
119 EXTL_END_NAMESPACE
121 /* //////////////////////////////////////////////////////////////////// */
122 #endif /* EXTL_UTILITY_SINGLETON_H */
123 /* //////////////////////////////////////////////////////////////////// */