CppCMS
applications_pool.h
1 //
3 // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
4 //
5 // See accompanying file COPYING.TXT file for licensing details.
6 //
8 #ifndef CPPCMS_APPLICATIONS_POOL_H
9 #define CPPCMS_APPLICATIONS_POOL_H
10 
11 #include <cppcms/defs.h>
12 #include <booster/noncopyable.h>
13 #include <booster/hold_ptr.h>
14 #include <booster/intrusive_ptr.h>
15 #include <booster/shared_ptr.h>
16 #include <booster/weak_ptr.h>
17 #include <booster/enable_shared_from_this.h>
18 
19 #include <booster/auto_ptr_inc.h>
20 #include <string>
21 
22 namespace cppcms {
23  class application;
24 }
25 
26 namespace booster {
27  void CPPCMS_API intrusive_ptr_add_ref(cppcms::application *p);
28  void CPPCMS_API intrusive_ptr_release(cppcms::application *p);
29  namespace aio {
30  class io_service;
31  }
32 }
33 
34 namespace cppcms {
35 
36  class service;
37  class mount_point;
38  class application_specific_pool;
39  class applications_pool;
40  namespace http {
41  class context;
42  }
43 
48  namespace app {
49  static const int synchronous = 0x0000;
50  static const int asynchronous = 0x0001;
51 
52  static const int op_mode_mask = 0x000F;
53 
54  static const int thread_specific= 0x0010;
55  static const int prepopulated = 0x0020;
56  static const int content_filter = 0x0040;
57  static const int legacy = 0x8000;
59  }
61 
66  class CPPCMS_API application_specific_pool :
67  public booster::noncopyable,
68  public booster::enable_shared_from_this<application_specific_pool>
69  {
70  public:
72  virtual ~application_specific_pool();
73 
84  booster::intrusive_ptr<application> asynchronous_application_by_io_service(booster::aio::io_service &io_srv,cppcms::service &srv);
94  booster::intrusive_ptr<application> asynchronous_application_by_io_service(booster::aio::io_service &io_srv);
95 
96  protected:
101  virtual application *new_application(service &srv) = 0;
102  private:
103  int flags();
104  void flags(int f);
105 
106  void prepopulate(cppcms::service &srv);
107  void application_requested(cppcms::service &srv);
108  friend class applications_pool;
109  friend class application;
110  friend class http::context;
111  friend void booster::intrusive_ptr_release(cppcms::application *app);
112 
113  application *get_new(service &srv);
114 
115  void size(size_t n);
117  void put(application *app);
118 
119  struct _data;
120  class _policy;
121  class _tls_policy;
122  class _pool_policy;
123  class _async_policy;
124  class _async_legacy_policy;
125  class _legacy_pool_policy;
127  };
128 
145  class CPPCMS_API applications_pool {
146  public:
147 
154  struct factory : public booster::noncopyable {
158  virtual std::auto_ptr<application> operator()(service &) const = 0;
159  virtual ~factory(){}
160  };
161 
170  void mount(std::auto_ptr<factory> aps);
171 
180  void mount(std::auto_ptr<factory> aps,mount_point const &point);
181 
190  void mount(booster::intrusive_ptr<application> app);
199  void mount(booster::intrusive_ptr<application> app,mount_point const &point);
200 
212  void mount(booster::shared_ptr<application_specific_pool> gen,int application_options = 0);
213 
225  void mount(booster::shared_ptr<application_specific_pool> gen,mount_point const &point,int application_options = 0);
226 
227 
241 
242 
244 
247  get(char const *h,char const *s,char const *path_info,std::string &match);
248 
250  get_application_specific_pool(char const *h,char const *s,char const *path_info,std::string &match);
251 
252  // put is not in use any more
253  void put(application *app);
254  applications_pool(service &srv,int unused);
256 
258 
259  private:
260  struct _data;
261  service *srv_;
263  };
264 
266  namespace details {
267  template<typename T>
268  struct simple_factory0 : public applications_pool::factory
269  {
270  std::auto_ptr<application> operator()(service &s) const
271  {
272  std::auto_ptr<application> app(new T(s));
273  return app;
274  }
275  };
276  template<typename T,typename P1>
277  struct simple_factory1 : public applications_pool::factory
278  {
279  simple_factory1(P1 p1) : p1_(p1) {}
280  P1 p1_;
281  std::auto_ptr<application> operator()(service &s) const
282  {
283  std::auto_ptr<application> app(new T(s,p1_));
284  return app;
285  }
286  };
287  template<typename T,typename P1,typename P2>
288  struct simple_factory2 : public applications_pool::factory
289  {
290  simple_factory2(P1 p1,P2 p2) : p1_(p1),p2_(p2) {}
291  P1 p1_;
292  P2 p2_;
293  std::auto_ptr<application> operator()(service &s) const
294  {
295  std::auto_ptr<application> app(new T(s,p1_,p2_));
296  return app;
297  }
298  };
299  } // details
300 
302 
309  template<typename T>
310  std::auto_ptr<applications_pool::factory> applications_factory()
311  {
312  std::auto_ptr<applications_pool::factory> f(new details::simple_factory0<T>);
313  return f;
314  }
315 
322  template<typename T,typename P1>
323  std::auto_ptr<applications_pool::factory> applications_factory(P1 p1)
324  {
325  std::auto_ptr<applications_pool::factory> f(new details::simple_factory1<T,P1>(p1));
326  return f;
327  }
328 
335  template<typename T,typename P1,typename P2>
336  std::auto_ptr<applications_pool::factory> applications_factory(P1 p1,P2 p2)
337  {
338  std::auto_ptr<applications_pool::factory> f(new details::simple_factory2<T,P1,P2>(p1,p2));
339  return f;
340  }
341 
343  namespace details {
344  template<typename T>
345  struct simple_application_specific_pool0 : public application_specific_pool
346  {
347  T *new_application(service &s)
348  {
349  return new T(s);
350  }
351  };
352  template<typename T,typename P1>
353  struct simple_application_specific_pool1 : public application_specific_pool
354  {
355  simple_application_specific_pool1(P1 p1) : p1_(p1) {}
356  P1 p1_;
357  T *new_application(service &s)
358  {
359  return new T(s,p1_);
360  }
361  };
362  template<typename T,typename P1,typename P2>
363  struct simple_application_specific_pool2 : public application_specific_pool
364  {
365  simple_application_specific_pool2(P1 p1,P2 p2) : p1_(p1),p2_(p2) {}
366  P1 p1_;
367  P2 p2_;
368  T *new_application(service &s)
369  {
370  return new T(s,p1_,p2_);
371  }
372  };
373  } // details
374 
376 
381  template<typename T>
383  {
384  booster::shared_ptr<application_specific_pool> f(new details::simple_application_specific_pool0<T>);
385  return f;
386  }
387 
392  template<typename T,typename P1>
394  {
395  booster::shared_ptr<application_specific_pool> f(new details::simple_application_specific_pool1<T,P1>(p1));
396  return f;
397  }
398 
403  template<typename T,typename P1,typename P2>
405  {
406  booster::shared_ptr<application_specific_pool> f(new details::simple_application_specific_pool2<T,P1,P2>(p1,p2));
407  return f;
408  }
409 
410 } // cppcms
411 
412 
413 
414 #endif
static const int asynchronous
Asynchronous application that operates in asynchronous mode.
Definition: applications_pool.h:50
static const int thread_specific
Make synchronous application thread specific.
Definition: applications_pool.h:54
static const int op_mode_mask
mask to select sync vs async flags
Definition: applications_pool.h:52
a base class for user application factories - to be deprecated, use application_specific_pool instead...
Definition: applications_pool.h:154
This class represent the central event loop of the CppCMS applications.
Definition: service.h:62
std::auto_ptr< applications_pool::factory > applications_factory()
Definition: applications_pool.h:310
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
context is a central class that holds all specific connection related information. It encapsulates CGI request and response, cache, session and locale information
Definition: http_context.h:47
Definition: log.h:25
booster::shared_ptr< application_specific_pool > create_pool()
Definition: applications_pool.h:382
intrusive_ptr is the class taken as-is from boost.
Definition: intrusive_ptr.h:42
This class represents application&#39;s mount point or the rule on which specific application is selected...
Definition: mount_point.h:24
application class is the base class for all user created applications.
Definition: application.h:82
static const int prepopulated
Make sure all applications are created from the beginning (ignored in thread_specific is set) ...
Definition: applications_pool.h:55
an interface for creating user applications
Definition: applications_pool.h:66
static const int synchronous
Synchronous application.
Definition: applications_pool.h:49
Application pool is the central class that holds user created applications.
Definition: applications_pool.h:145
This class is borrowed from boost.
Definition: enable_shared_from_this.h:30
static const int content_filter
Definition: applications_pool.h:56
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
this is the central event loop that dispatches all requests.
Definition: io_service.h:37
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15