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.
 
 
 
 
 
 

187 lines
4.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #define CPPCMS_SOURCE
  20. #include "url_dispatcher.h"
  21. #include "application.h"
  22. #include "config.h"
  23. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  24. # include <boost/regex.hpp>
  25. #else // Internal Boost
  26. # include <cppcms_boost/regex.hpp>
  27. namespace boost = cppcms_boost;
  28. #endif
  29. namespace cppcms {
  30. namespace /* anon */ {
  31. struct option : public booster::noncopyable {
  32. option(std::string expr) :
  33. expr_(expr)
  34. {
  35. }
  36. virtual ~option()
  37. {
  38. }
  39. bool matches(std::string const &path)
  40. {
  41. return boost::regex_match(path.c_str(),match_,expr_);
  42. }
  43. virtual bool dispatch(std::string url) = 0;
  44. protected:
  45. boost::regex expr_;
  46. boost::cmatch match_;
  47. };
  48. struct mounted : public option {
  49. mounted(std::string expr,int select,application *app) :
  50. option(expr),
  51. app_(app),
  52. select_(select)
  53. {
  54. }
  55. virtual bool dispatch(std::string url)
  56. {
  57. if(matches(url))
  58. return app_->dispatcher().dispatch(match_[select_]);
  59. return false;
  60. }
  61. private:
  62. application *app_;
  63. int select_;
  64. };
  65. template<typename H>
  66. struct base_handler : public option {
  67. base_handler(std::string expr,H handle,int a=0,int b=0,int c=0,int d=0)
  68. : option(expr),handle_(handle)
  69. {
  70. select_[0]=a;
  71. select_[1]=b;
  72. select_[2]=c;
  73. select_[3]=d;
  74. }
  75. virtual bool dispatch(std::string url)
  76. {
  77. if(matches(url)) {
  78. execute_handler(handle_);
  79. return true;
  80. }
  81. return false;
  82. }
  83. private:
  84. void execute_handler(url_dispatcher::handler const &h)
  85. {
  86. h();
  87. }
  88. void execute_handler(url_dispatcher::handler1 const &h)
  89. {
  90. h(match_[select_[0]]);
  91. }
  92. void execute_handler(url_dispatcher::handler2 const &h)
  93. {
  94. h(match_[select_[0]],match_[select_[1]]);
  95. }
  96. void execute_handler(url_dispatcher::handler3 const &h)
  97. {
  98. h(match_[select_[0]],match_[select_[1]],match_[select_[2]]);
  99. }
  100. void execute_handler(url_dispatcher::handler4 const &h)
  101. {
  102. h(match_[select_[0]],match_[select_[1]],match_[select_[2]],match_[select_[3]]);
  103. }
  104. int select_[4];
  105. H handle_;
  106. };
  107. template<typename H>
  108. boost::shared_ptr<option> make_handler(std::string expr,H const &handler,int a=0,int b=0,int c=0,int d=0)
  109. {
  110. return boost::shared_ptr<option>(new base_handler<H>(expr,handler,a,b,c,d));
  111. }
  112. } // anonynoys
  113. struct url_dispatcher::data {
  114. std::vector<boost::shared_ptr<option> > options;
  115. boost::shared_ptr<option> last_option;
  116. };
  117. // Meanwhile nothing
  118. url_dispatcher::url_dispatcher() :
  119. d(new url_dispatcher::data())
  120. {
  121. }
  122. url_dispatcher::~url_dispatcher()
  123. {
  124. }
  125. bool url_dispatcher::dispatch(std::string url)
  126. {
  127. unsigned i;
  128. for(i=0;i<d->options.size();i++) {
  129. if(d->options[i]->dispatch(url))
  130. return true;
  131. }
  132. return false;
  133. }
  134. void url_dispatcher::mount(std::string match,application &app,int select)
  135. {
  136. d->options.push_back(boost::shared_ptr<option>(new mounted(match,select,&app)));
  137. }
  138. void url_dispatcher::assign(std::string expr,handler h)
  139. {
  140. d->options.push_back(make_handler(expr,h));
  141. }
  142. void url_dispatcher::assign(std::string expr,handler1 h,int p1)
  143. {
  144. d->options.push_back(make_handler(expr,h,p1));
  145. }
  146. void url_dispatcher::assign(std::string expr,handler2 h,int p1,int p2)
  147. {
  148. d->options.push_back(make_handler(expr,h,p1,p2));
  149. }
  150. void url_dispatcher::assign(std::string expr,handler3 h,int p1,int p2,int p3)
  151. {
  152. d->options.push_back(make_handler(expr,h,p1,p2,p3));
  153. }
  154. void url_dispatcher::assign(std::string expr,handler4 h,int p1,int p2,int p3,int p4)
  155. {
  156. d->options.push_back(make_handler(expr,h,p1,p2,p3,p4));
  157. }
  158. } // namespace cppcms