ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

169 lines
3.5 KiB

  1. #define CPPCMS_SOURCE
  2. #include "url_dispatcher.h"
  3. #include "application.h"
  4. #include "config.h"
  5. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  6. # include <boost/regex.hpp>
  7. #else // Internal Boost
  8. # include <cppcms_boost/regex.hpp>
  9. namespace boost = cppcms_boost;
  10. #endif
  11. namespace cppcms {
  12. namespace /* anon */ {
  13. struct option : public util::noncopyable {
  14. option(std::string expr) :
  15. expr_(expr)
  16. {
  17. }
  18. virtual ~option()
  19. {
  20. }
  21. bool matches(std::string const &path)
  22. {
  23. return boost::regex_match(path.c_str(),match_,expr_);
  24. }
  25. virtual bool dispatch(std::string url) = 0;
  26. protected:
  27. boost::regex expr_;
  28. boost::cmatch match_;
  29. };
  30. struct mounted : public option {
  31. mounted(std::string expr,int select,application *app) :
  32. option(expr),
  33. app_(app),
  34. select_(select)
  35. {
  36. }
  37. virtual bool dispatch(std::string url)
  38. {
  39. if(matches(url))
  40. return app_->dispatcher().dispatch(match_[select_]);
  41. return false;
  42. }
  43. private:
  44. application *app_;
  45. int select_;
  46. };
  47. template<typename H>
  48. struct base_handler : public option {
  49. base_handler(std::string expr,H handle,int a=0,int b=0,int c=0,int d=0)
  50. : option(expr),handle_(handle)
  51. {
  52. select_[0]=a;
  53. select_[1]=b;
  54. select_[2]=c;
  55. select_[3]=d;
  56. }
  57. virtual bool dispatch(std::string url)
  58. {
  59. if(matches(url)) {
  60. execute_handler(handle_);
  61. return true;
  62. }
  63. return false;
  64. }
  65. private:
  66. void execute_handler(url_dispatcher::handler const &h)
  67. {
  68. h();
  69. }
  70. void execute_handler(url_dispatcher::handler1 const &h)
  71. {
  72. h(match_[select_[0]]);
  73. }
  74. void execute_handler(url_dispatcher::handler2 const &h)
  75. {
  76. h(match_[select_[0]],match_[select_[1]]);
  77. }
  78. void execute_handler(url_dispatcher::handler3 const &h)
  79. {
  80. h(match_[select_[0]],match_[select_[1]],match_[select_[2]]);
  81. }
  82. void execute_handler(url_dispatcher::handler4 const &h)
  83. {
  84. h(match_[select_[0]],match_[select_[1]],match_[select_[2]],match_[select_[3]]);
  85. }
  86. int select_[4];
  87. H handle_;
  88. };
  89. template<typename H>
  90. boost::shared_ptr<option> make_handler(std::string expr,H const &handler,int a=0,int b=0,int c=0,int d=0)
  91. {
  92. return boost::shared_ptr<option>(new base_handler<H>(expr,handler,a,b,c,d));
  93. }
  94. } // anonynoys
  95. struct url_dispatcher::data {
  96. std::vector<boost::shared_ptr<option> > options;
  97. boost::shared_ptr<option> last_option;
  98. };
  99. // Meanwhile nothing
  100. url_dispatcher::url_dispatcher() :
  101. d(new url_dispatcher::data())
  102. {
  103. }
  104. url_dispatcher::~url_dispatcher()
  105. {
  106. }
  107. bool url_dispatcher::dispatch(std::string url)
  108. {
  109. unsigned i;
  110. for(i=0;i<d->options.size();i++) {
  111. if(d->options[i]->dispatch(url))
  112. return true;
  113. }
  114. return false;
  115. }
  116. void url_dispatcher::mount(std::string match,application &app,int select)
  117. {
  118. d->options.push_back(boost::shared_ptr<option>(new mounted(match,select,&app)));
  119. }
  120. void url_dispatcher::assign(std::string expr,handler h)
  121. {
  122. d->options.push_back(make_handler(expr,h));
  123. }
  124. void url_dispatcher::assign(std::string expr,handler1 h,int p1)
  125. {
  126. d->options.push_back(make_handler(expr,h,p1));
  127. }
  128. void url_dispatcher::assign(std::string expr,handler2 h,int p1,int p2)
  129. {
  130. d->options.push_back(make_handler(expr,h,p1,p2));
  131. }
  132. void url_dispatcher::assign(std::string expr,handler3 h,int p1,int p2,int p3)
  133. {
  134. d->options.push_back(make_handler(expr,h,p1,p2,p3));
  135. }
  136. void url_dispatcher::assign(std::string expr,handler4 h,int p1,int p2,int p3,int p4)
  137. {
  138. d->options.push_back(make_handler(expr,h,p1,p2,p3,p4));
  139. }
  140. } // namespace cppcms