CppCMS
url_dispatcher.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_URL_DISPATCHER_H
9 #define CPPCMS_URL_DISPATCHER_H
10 
11 #include <booster/noncopyable.h>
12 #include <cppcms/defs.h>
13 #include <booster/function.h>
14 #include <booster/hold_ptr.h>
15 #include <booster/traits/enable_if.h>
16 #include <booster/traits/is_base_of.h>
17 #include <cppcms/application.h>
18 #include <string>
19 #include <list>
20 
21 namespace cppcms {
22 
23  class application;
24 
44 
45  class CPPCMS_API url_dispatcher : public booster::noncopyable {
46  public:
47  // Handlers
53 
58  void assign(std::string const &regex,handler handler);
69  void assign(std::string const &regex,handler1 handler,int exp1);
75  void assign(std::string const &regex,handler2 handler,int exp1,int exp2);
81  void assign(std::string const &regex,handler3 handler,int exp1,int exp2,int exp3);
87  void assign(std::string const &regex,handler4 handler,int exp1,int exp2,int exp3,int exp4);
88 
95 
96  bool dispatch(std::string url);
97 
99  ~url_dispatcher();
100 
109  template<typename C>
110  void assign(std::string const &regex,void (C::*member)(),C *object)
111  {
112  assign(regex,binder0<C>(member,object));
113  }
121  template<typename C>
122  void assign(std::string const &regex,void (C::*member)(std::string),C *object,int e1)
123  {
124  assign(regex,binder1<C>(member,object),e1);
125  }
133  template<typename C>
134  void assign(std::string const &regex,void (C::*member)(std::string,std::string),C *object,int e1,int e2)
135  {
136  assign(regex,binder2<C>(member,object),e1,e2);
137  }
138  template<typename C>
146  void assign(std::string const &regex,void (C::*member)(std::string,std::string,std::string),C *object,int e1,int e2,int e3)
147  {
148  assign(regex,binder3<C>(member,object),e1,e2,e3);
149  }
157  template<typename C>
158  void assign(std::string const &regex,void (C::*member)(std::string,std::string,std::string,std::string),C *object,int e1,int e2,int e3,int e4)
159  {
160  assign(regex,binder4<C>(member,object),e1,e2,e3,e4);
161  }
162 
175  void mount(std::string const &match,application &app,int part);
176 
177  private:
178 
179  template<typename C,typename Enable = void>
180  class page_guard {
181  public:
182  page_guard(C * /*o*/) {}
183  };
184 
185  template<typename C>
186  class page_guard<C,typename booster::enable_if<booster::is_base_of< cppcms::application,C> >::type > {
187  public:
188  page_guard(C *o) :
189  object_(o)
190  {
191  object_->init();
192  }
193  ~page_guard()
194  {
195  object_->clear();
196  }
197  private:
198  application *object_;
199  };
200 
201  template<typename C>
202  struct binder0{
203  typedef void (C::*member_type)();
204  member_type member;
205  C *object;
206 
207  binder0(member_type m,C *o) :
208  member(m),
209  object(o)
210  {
211  }
212  void operator()() const
213  {
214  page_guard<C> guard(object);
215  (object->*member)();
216  }
217  };
218 
219  template<typename C>
220  struct binder1{
221  typedef void (C::*member_type)(std::string);
222  member_type member;
223  C *object;
224 
225  binder1(member_type m,C *o) :
226  member(m),
227  object(o)
228  {
229  }
230  void operator()(std::string p1) const
231  {
232  page_guard<C> guard(object);
233  (object->*member)(p1);
234  }
235  };
236 
237  template<typename C>
238  struct binder2{
239  typedef void (C::*member_type)(std::string,std::string);
240  member_type member;
241  C *object;
242 
243  binder2(member_type m,C *o) :
244  member(m),
245  object(o)
246  {
247  }
248  void operator()(std::string p1,std::string p2) const
249  {
250  page_guard<C> guard(object);
251  (object->*member)(p1,p2);
252  }
253  };
254  template<typename C>
255  struct binder3{
256  typedef void (C::*member_type)(std::string,std::string,std::string);
257  member_type member;
258  C *object;
259 
260  binder3(member_type m,C *o) :
261  member(m),
262  object(o)
263  {
264  }
265  void operator()(std::string p1,std::string p2,std::string p3) const
266  {
267  page_guard<C> guard(object);
268  (object->*member)(p1,p2,p3);
269  }
270  };
271  template<typename C>
272  struct binder4{
273  typedef void (C::*member_type)(std::string,std::string,std::string,std::string);
274  member_type member;
275  C *object;
276 
277  binder4(member_type m,C *o) :
278  member(m),
279  object(o)
280  {
281  }
282  void operator()(std::string p1,std::string p2,std::string p3,std::string p4) const
283  {
284  page_guard<C> guard(object);
285  (object->*member)(p1,p2,p3,p4);
286  }
287  };
288 
289 
290  struct _data;
292  };
293 
294 } // cppcms
295 
296 #endif
void assign(std::string const &regex, void(C::*member)(std::string, std::string, std::string), C *object, int e1, int e2, int e3)
Definition: url_dispatcher.h:146
void assign(std::string const &regex, void(C::*member)(std::string, std::string, std::string, std::string), C *object, int e1, int e2, int e3, int e4)
Definition: url_dispatcher.h:158
void assign(std::string const &regex, void(C::*member)(std::string, std::string), C *object, int e1, int e2)
Definition: url_dispatcher.h:134
void assign(std::string const &regex, void(C::*member)(std::string), C *object, int e1)
Definition: url_dispatcher.h:122
std::map< string_key, value > object
The json::object - std::map of json::value's.
Definition: json.h:53
void assign(std::string const &regex, void(C::*member)(), C *object)
Definition: url_dispatcher.h:110
Definition: function.h:16
application class is the base class for all user created applications.
Definition: application.h:81
This class is used to glue between member function of application class and urls. ...
Definition: url_dispatcher.h:45
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15