00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_template_h_
00029 #define _terimber_template_h_
00030
00031 #include "base/primitives.h"
00032 #include "base/list.h"
00033 #include "base/keymaker.hpp"
00034
00035 BEGIN_TERIMBER_NAMESPACE
00036 #pragma pack(4)
00039 template < class C >
00040 class pool
00041 {
00042 public:
00043
00046 typedef C CREATOR;
00049 typedef TYPENAME C::TYPE TYPE;
00052 typedef TYPENAME C::ARG ARG;
00053 private:
00058 class pool_entry
00059 {
00062 friend class pool< C >;
00063 public:
00065 pool_entry( TYPE* obj
00066 ) :
00067 _obj(obj),
00068 _rest(0)
00069 {
00070 }
00072 pool_entry(const pool_entry& x)
00073 {
00074 *this = x;
00075 }
00077 pool_entry&
00078 operator=(const pool_entry& x)
00079 {
00080 if (this != &x)
00081 {
00082 _obj = x._obj;
00083 _rest = x._rest;
00084 }
00085 return *this;
00086 }
00087
00088 private:
00089 TYPE* _obj;
00090 sb8_t _rest;
00091 };
00092
00093
00096 typedef list< pool_entry > list_pool_entry_t;
00097
00098 public:
00100 pool< C >( C& creator = C::static_constructor(),
00101 size_t pool_size = os_def_size
00102 );
00104 ~pool< C >();
00106 inline
00107 TYPE*
00108 loan_object( const ARG& arg = C::get_default_arg(),
00109 size_t timeout = C::get_default_timeout()
00110 );
00112 inline
00113 void
00114 return_object( TYPE* obj,
00115 const ARG& arg = C::get_default_arg()
00116 );
00118 inline
00119 void
00120 clear( const ARG& arg = C::get_default_arg()
00121 );
00123 inline
00124 void
00125 deactivate( size_t maxrest,
00126 const ARG& arg = C::get_default_arg()
00127 );
00129 inline
00130 void
00131 purge( size_t maxrest,
00132 const ARG& arg = C::get_default_arg()
00133 );
00134
00136 inline
00137 void
00138 get_stats( size_t& free_objects,
00139 size_t& busy_objects
00140 ) const;
00141 private:
00142 C& _creator;
00143 keylocker _locker;
00144 mutex _mtx;
00145 list_pool_entry_t _busy_objects;
00146 list_pool_entry_t _free_objects;
00147 };
00148
00152 template < class C >
00153 class smart_pointer
00154 {
00155 public:
00158 typedef TYPENAME C::TYPE TYPE;
00161 typedef TYPENAME C::ARG ARG;
00164 explicit
00165 smart_pointer< C >(C& crt,
00166 const ARG& n
00167 );
00170 explicit
00171 smart_pointer< C >(C& crt
00172 );
00174 ~smart_pointer< C >();
00176 smart_pointer< C >& operator=(const TYPE* x);
00178 operator TYPENAME smart_pointer< C >::TYPE*()
00179 {
00180 return _ptr;
00181 }
00183 operator
00184 const TYPENAME smart_pointer< C >::TYPE*() const
00185 {
00186 return _ptr;
00187 }
00189 TYPENAME smart_pointer< C >::TYPE* operator->();
00191 const TYPENAME smart_pointer< C >::TYPE* operator->() const;
00194 TYPENAME smart_pointer< C >::TYPE** operator&();
00196 bool
00197 operator!() const;
00199 void
00200 clear();
00202 TYPENAME smart_pointer< C >::TYPE*
00203 detach();
00205 void
00206 attach( TYPE* obj,
00207 bool free = true
00208 );
00209 private:
00210 TYPENAME smart_pointer< C >::TYPE* _ptr;
00211 C& _crt;
00212 };
00213
00216 template < class P >
00217 class pool_object_keeper
00218 {
00219 public:
00222 typedef TYPENAME P::CREATOR::TYPE TYPE;
00225 typedef TYPENAME P::CREATOR::ARG ARG;
00227 explicit
00228 pool_object_keeper< P >(P* pool_,
00229 TYPE* obj
00230 );
00232 explicit
00233 pool_object_keeper< P >(P* pool_,
00234 const ARG& arg,
00235 size_t timeout
00236 );
00238 ~pool_object_keeper();
00240 inline
00241 TYPENAME pool_object_keeper< P >::TYPE* operator->();
00243 inline
00244 const TYPENAME pool_object_keeper< P >::TYPE* operator->() const;
00246 inline
00247 operator TYPENAME pool_object_keeper< P >::TYPE*()
00248 {
00249 return _obj;
00250 }
00252 inline
00253 operator const TYPENAME pool_object_keeper< P >::TYPE*() const
00254 {
00255 return _obj;
00256 }
00258 bool operator!() const;
00259 private:
00260 P* _pool;
00261 TYPENAME pool_object_keeper< P >::TYPE* _obj;
00262 };
00263
00266 class event_creator : public proto_creator< event_creator, event, size_t >
00267 {
00268 };
00269
00272 typedef pool< event_creator > event_pool_t;
00273
00274 #pragma pack()
00275 END_TERIMBER_NAMESPACE
00276
00277 #endif // _terimber_template_h_