url_dispatcher.h
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
#ifndef CPPCMS_URL_DISPATCHER_H
#define CPPCMS_URL_DISPATCHER_H
#include <booster/noncopyable.h>
#include <cppcms/defs.h>
#include <booster/function.h>
#include <booster/hold_ptr.h>
#include <booster/traits/enable_if.h>
#include <booster/traits/is_base_of.h>
#include <cppcms/application.h>
#include <string>
#include <list>
namespace cppcms {
class application;
class CPPCMS_API url_dispatcher : public booster::noncopyable {
public:
// Handlers
typedef booster::function<void()> handler;
typedef booster::function<void(std::string)> handler1;
typedef booster::function<void(std::string,std::string)> handler2;
typedef booster::function<void(std::string,std::string,std::string)> handler3;
typedef booster::function<void(std::string,std::string,std::string,std::string)> handler4;
void assign(std::string const &regex,handler handler);
void assign(std::string const &regex,handler1 handler,int exp1);
void assign(std::string const &regex,handler2 handler,int exp1,int exp2);
void assign(std::string const &regex,handler3 handler,int exp1,int exp2,int exp3);
void assign(std::string const &regex,handler4 handler,int exp1,int exp2,int exp3,int exp4);
bool dispatch(std::string url);
url_dispatcher();
~url_dispatcher();
template<typename C>
void assign(std::string const &regex,void (C::*member)(),C *object)
{
assign(regex,binder0<C>(member,object));
}
template<typename C>
void assign(std::string const &regex,void (C::*member)(std::string),C *object,int e1)
{
assign(regex,binder1<C>(member,object),e1);
}
template<typename C>
void assign(std::string const &regex,void (C::*member)(std::string,std::string),C *object,int e1,int e2)
{
assign(regex,binder2<C>(member,object),e1,e2);
}
template<typename C>
void assign(std::string const &regex,void (C::*member)(std::string,std::string,std::string),C *object,int e1,int e2,int e3)
{
assign(regex,binder3<C>(member,object),e1,e2,e3);
}
template<typename C>
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)
{
assign(regex,binder4<C>(member,object),e1,e2,e3,e4);
}
void mount(std::string const &match,application &app,int part);
private:
template<typename C,typename Enable = void>
class page_guard {
public:
page_guard(C * /*o*/) {}
};
template<typename C>
class page_guard<C,typename booster::enable_if<booster::is_base_of< cppcms::application,C> >::type > {
public:
page_guard(C *o) :
object_(o)
{
object_->init();
}
~page_guard()
{
object_->clear();
}
private:
application *object_;
};
template<typename C>
struct binder0{
typedef void (C::*member_type)();
member_type member;
C *object;
binder0(member_type m,C *o) :
member(m),
object(o)
{
}
void operator()() const
{
page_guard<C> guard(object);
(object->*member)();
}
};
template<typename C>
struct binder1{
typedef void (C::*member_type)(std::string);
member_type member;
C *object;
binder1(member_type m,C *o) :
member(m),
object(o)
{
}
void operator()(std::string p1) const
{
page_guard<C> guard(object);
(object->*member)(p1);
}
};
template<typename C>
struct binder2{
typedef void (C::*member_type)(std::string,std::string);
member_type member;
C *object;
binder2(member_type m,C *o) :
member(m),
object(o)
{
}
void operator()(std::string p1,std::string p2) const
{
page_guard<C> guard(object);
(object->*member)(p1,p2);
}
};
template<typename C>
struct binder3{
typedef void (C::*member_type)(std::string,std::string,std::string);
member_type member;
C *object;
binder3(member_type m,C *o) :
member(m),
object(o)
{
}
void operator()(std::string p1,std::string p2,std::string p3) const
{
page_guard<C> guard(object);
(object->*member)(p1,p2,p3);
}
};
template<typename C>
struct binder4{
typedef void (C::*member_type)(std::string,std::string,std::string,std::string);
member_type member;
C *object;
binder4(member_type m,C *o) :
member(m),
object(o)
{
}
void operator()(std::string p1,std::string p2,std::string p3,std::string p4) const
{
page_guard<C> guard(object);
(object->*member)(p1,p2,p3,p4);
}
};
struct _data;
booster::hold_ptr<_data> d;
};
} // cppcms
#endif
cppcms::url_dispatcher::assign
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
cppcms::url_dispatcher::assign
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
booster::hold_ptr< _data >
cppcms::url_dispatcher::assign
void assign(std::string const &regex, void(C::*member)(std::string, std::string), C *object, int e1, int e2)
Definition: url_dispatcher.h:134
cppcms::url_dispatcher::assign
void assign(std::string const &regex, void(C::*member)(std::string), C *object, int e1)
Definition: url_dispatcher.h:122
cppcms::json::object
std::map< string_key, value > object
The json::object - std::map of json::value's.
Definition: json.h:53
cppcms::url_dispatcher::assign
void assign(std::string const &regex, void(C::*member)(), C *object)
Definition: url_dispatcher.h:110
booster::function
Definition: function.h:16
cppcms::application
application class is the base class for all user created applications.
Definition: application.h:81
cppcms::url_dispatcher
This class is used to glue between member function of application class and urls. ...
Definition: url_dispatcher.h:45
booster::noncopyable
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15