remove \r
[extl.git] / extl / memory / singleton_pool.h
blobacac956ca1c5b8fe5b457375cd77779e72dbe9be
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: singleton_pool.h
4 * Created: 08.04.14
5 * Updated: 08.04.15
7 * Brief: singleton_pool class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_MEMORY_SINGLETON_POOL_H
14 #define EXTL_MEMORY_SINGLETON_POOL_H
16 /*!\file singleton_pool.h
17 * \brief singleton_pool class
19 #ifndef __cplusplus
20 # error singleton_pool.h need be supported by c++.
21 #endif
23 /* ///////////////////////////////////////////////////////////////////////
24 * Includes
26 #include "../utility/singleton.h"
27 #include "../utility/uncopyable.h"
28 #include "../synch/synch_traits_selector.h"
29 //#include "../synch/scoped_lock.h"
31 /* ///////////////////////////////////////////////////////////////////////
32 * ::extl namespace
34 EXTL_BEGIN_NAMESPACE
36 /*!\brief singleton_pool class
38 * \param Tag The tag of the different singleton pool class
39 * \param P The pool type
40 * \param ST The synchronous traits type
42 * \ingroup extl_group_memory
44 template< typename_param_k Tag
45 , typename_param_k P
46 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
47 , typename_param_k ST = typename_type_def_k synch_traits_selector::synch_traits_type
48 #else
49 , typename_param_k ST
50 #endif
52 class singleton_pool
53 : private uncopyable< singleton_pool<Tag, P, ST> > /* Prohibit copying constructor and copy assignment */
55 /// \name Types
56 /// @{
57 public:
58 typedef singleton_pool<Tag, P, ST> class_type;
59 typedef ST synch_traits_type;
60 typedef typename_type_k synch_traits_type::lock_type lock_type;
61 typedef typename_type_k P::pointer pointer;
62 typedef typename_type_k P::const_pointer const_pointer;
63 typedef typename_type_k P::reference reference;
64 typedef typename_type_k P::const_reference const_reference;
65 typedef typename_type_k P::value_type value_type;
66 typedef typename_type_k P::size_type size_type;
67 typedef Tag tag_type;
68 /// @}
70 private:
71 /* Prohibit create an instance */
72 singleton_pool(){}
74 private:
75 /* old singleton
76 /// Bind pool and lock as a singleton
77 struct pool_type
78 : P, lock_type
80 pool_type():P(), lock_type(){}
82 /// The singleton type
83 typedef singleton<pool_type> singleton_type;
86 // new singleton
88 typedef P pool_type;
89 /// The singleton type
90 typedef singleton<pool_type, lock_type, tag_type> singleton_type;
92 public:
93 /// Allocates memory
94 static pointer allocate(size_type n)
96 pool_type& pool = singleton_type::instance();
97 //scoped_lock<lock_type> guard(pool);
98 return pool.allocate(n);
100 /// Deallocates memory
101 static void deallocate(pointer p)
103 pool_type& pool = singleton_type::instance();
104 //scoped_lock<lock_type> guard(pool);
105 pool.deallocate(p);
107 /// Reallocates memory
108 static void* reallocate(pointer p, size_type n)
110 pool_type& pool = singleton_type::instance();
111 //scoped_lock<lock_type> guard(pool);
112 return pool.reallocate(p, n);
114 /// Gets the pool
115 static pool_type& get_pool()
117 return singleton_type::instance();
121 /* ///////////////////////////////////////////////////////////////////////
122 * ::extl namespace
124 EXTL_END_NAMESPACE
125 /* //////////////////////////////////////////////////////////////////// */
126 #endif /* EXTL_MEMORY_SINGLETON_POOL_H */
127 /* //////////////////////////////////////////////////////////////////// */