CppCMS
cppcms/applications_pool.h
00001 
00002 //                                                                             
00003 //  Copyright (C) 2008-2012  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
00004 //                                                                             
00005 //  See accompanying file COPYING.TXT file for licensing details.
00006 //
00008 #ifndef CPPCMS_APPLICATIONS_POOL_H
00009 #define CPPCMS_APPLICATIONS_POOL_H
00010 
00011 #include <cppcms/defs.h>
00012 #include <booster/noncopyable.h>
00013 #include <booster/hold_ptr.h>
00014 #include <booster/intrusive_ptr.h>
00015 
00016 #include <memory>
00017 #include <string>
00018 
00019 namespace cppcms {
00020 
00021         class application;
00022         class service;
00023         class mount_point;
00024         
00041         class CPPCMS_API applications_pool {
00042         public:
00043 
00047                 struct factory : public booster::noncopyable {
00051                         virtual std::auto_ptr<application> operator()(service &) const = 0;
00052                         virtual ~factory(){}
00053                 };
00054 
00061                 void mount(std::auto_ptr<factory> aps);
00062                 
00069 
00070                 void mount(std::auto_ptr<factory> aps,mount_point const &point);
00071 
00078                 void mount(booster::intrusive_ptr<application> app);
00085                 void mount(booster::intrusive_ptr<application> app,mount_point const &point);
00086 
00087 
00089 
00090                 booster::intrusive_ptr<application> 
00091                 get(char const *h,char const *s,char const *path_info,std::string &match);
00092 
00093                 void put(application *app);
00094                 applications_pool(service &srv,int pool_size_limit);
00095                 ~applications_pool();
00096 
00098 
00099         private:
00100                 struct basic_app_data;
00101                 struct app_data;
00102                 struct long_running_app_data;
00103                 struct _data;
00104                 service *srv_;
00105                 booster::hold_ptr<_data> d;
00106         };
00107 
00109         namespace details {
00110                 template<typename T>
00111                 struct simple_factory0 : public applications_pool::factory
00112                 {
00113                         std::auto_ptr<application> operator()(service &s) const
00114                         {
00115                                 std::auto_ptr<application> app(new T(s));
00116                                 return app;
00117                         }
00118                 };
00119                 template<typename T,typename P1>
00120                 struct simple_factory1 : public applications_pool::factory
00121                 {
00122                         simple_factory1(P1 p1) : p1_(p1) {}
00123                         P1 p1_;
00124                         std::auto_ptr<application> operator()(service &s) const
00125                         {
00126                                 std::auto_ptr<application> app(new T(s,p1_));
00127                                 return app;
00128                         }
00129                 };
00130                 template<typename T,typename P1,typename P2>
00131                 struct simple_factory2 : public applications_pool::factory 
00132                 {
00133                         simple_factory2(P1 p1,P2 p2) : p1_(p1),p2_(p2) {}
00134                         P1 p1_;
00135                         P2 p2_;
00136                         std::auto_ptr<application> operator()(service &s) const
00137                         {
00138                                 std::auto_ptr<application> app(new T(s,p1_,p2_));
00139                                 return app;
00140                         }
00141                 };
00142         } // details
00143 
00145 
00150         template<typename T>
00151         std::auto_ptr<applications_pool::factory> applications_factory()
00152         {
00153                 std::auto_ptr<applications_pool::factory> f(new details::simple_factory0<T>);
00154                 return f;
00155         }
00156         
00161         template<typename T,typename P1>
00162         std::auto_ptr<applications_pool::factory> applications_factory(P1 p1)
00163         {
00164                 std::auto_ptr<applications_pool::factory> f(new details::simple_factory1<T,P1>(p1));
00165                 return f;
00166         }
00167         
00172         template<typename T,typename P1,typename P2>
00173         std::auto_ptr<applications_pool::factory> applications_factory(P1 p1,P2 p2)
00174         {
00175                 std::auto_ptr<applications_pool::factory> f(new details::simple_factory2<T,P1,P2>(p1,p2));
00176                 return f;
00177         }
00178 
00179 
00180 } // cppcms
00181 
00182 
00183 
00184 #endif