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.
 
 
 
 
 
 

385 lines
7.8 KiB

  1. #ifndef CPPCMS_FILTERS_H
  2. #define CPPCMS_FILTERS_H
  3. #include <locale>
  4. #include <typeinfo>
  5. #include <sstream>
  6. #include <vector>
  7. #include <iostream>
  8. #include "defs.h"
  9. #include "copy_ptr.h"
  10. #include "localization.h"
  11. namespace cppcms {
  12. namespace filters {
  13. #define CPPCMS_STREAMED(Streamable) \
  14. inline std::ostream &operator<<(std::ostream &out,Streamable const &object) \
  15. { \
  16. object(out); \
  17. return out; \
  18. };
  19. class CPPCMS_API streamable {
  20. public:
  21. typedef void (*to_stream_type)(std::ostream &,void const *ptr);
  22. typedef std::string (*to_string_type)(std::ios &,void const *ptr);
  23. streamable();
  24. ~streamable();
  25. streamable(streamable const &other);
  26. streamable const &operator=(streamable const &other);
  27. template<typename S>
  28. streamable(S const &ptr)
  29. {
  30. void const *p=&ptr;
  31. to_stream_type s1=&to_stream<S>;
  32. to_string_type s2=&to_string<S>;
  33. std::type_info const *info=&typeid(S);
  34. set(p,s1,s2,info);
  35. }
  36. template<typename T>
  37. T const &get() const
  38. {
  39. if(typeid(T) != type())
  40. throw std::bad_cast();
  41. T const *object=reinterpret_cast<T const *>(ptr_);
  42. return *object;
  43. }
  44. streamable(char const *ptr);
  45. void operator()(std::ostream &output) const;
  46. std::string get(std::ios &ios) const;
  47. private:
  48. void set(void const *ptr,to_stream_type f1,to_string_type f2,std::type_info const *type);
  49. template<typename T>
  50. static void to_stream(std::ostream &out,void const *ptr)
  51. {
  52. T const *object=reinterpret_cast<T const *>(ptr);
  53. out << *object;
  54. }
  55. template<typename T>
  56. static std::string to_string(std::ios &out,void const *ptr)
  57. {
  58. T const *object=reinterpret_cast<T const *>(ptr);
  59. std::ostringstream oss;
  60. oss.copyfmt(out);
  61. oss << *object;
  62. return oss.str();
  63. }
  64. std::type_info const &type() const;
  65. private:
  66. void const *ptr_;
  67. to_stream_type to_stream_;
  68. to_string_type to_string_;
  69. std::type_info const *type_;
  70. };
  71. ///
  72. /// \brief we can specialize for string because std::string get(ios &) const may be much more
  73. /// efficient.
  74. ///
  75. template<>
  76. CPPCMS_API streamable::streamable(std::string const &str);
  77. CPPCMS_STREAMED(streamable)
  78. ///
  79. /// \brief Output filter to_upper
  80. ///
  81. /// Convert text to upper case according to locale
  82. ///
  83. class CPPCMS_API to_upper {
  84. public:
  85. to_upper();
  86. ~to_upper();
  87. to_upper(to_upper const &);
  88. to_upper const &operator=(to_upper const &other);
  89. void operator()(std::ostream &out) const;
  90. to_upper(streamable const &obj);
  91. private:
  92. streamable obj_;
  93. struct data;
  94. util::copy_ptr<data> d;
  95. };
  96. inline std::ostream &operator<<(std::ostream &out,to_upper const &obj)
  97. {
  98. obj(out);
  99. return out;
  100. }
  101. ///
  102. /// \brief Output filter to_lower
  103. ///
  104. /// Convert text to lower case according to locale
  105. ///
  106. class CPPCMS_API to_lower {
  107. public:
  108. to_lower();
  109. ~to_lower();
  110. to_lower(to_lower const &);
  111. to_lower const &operator=(to_lower const &other);
  112. void operator()(std::ostream &out) const;
  113. to_lower(streamable const &obj);
  114. private:
  115. streamable obj_;
  116. struct data;
  117. util::copy_ptr<data> d;
  118. };
  119. inline std::ostream &operator<<(std::ostream &out,to_lower const &obj)
  120. {
  121. obj(out);
  122. return out;
  123. }
  124. ///
  125. /// \brief Output filter to_title
  126. ///
  127. /// Convert text to title case according to locale
  128. ///
  129. class CPPCMS_API to_title {
  130. public:
  131. to_title();
  132. ~to_title();
  133. to_title(to_title const &);
  134. to_title const &operator=(to_title const &other);
  135. void operator()(std::ostream &out) const;
  136. to_title(streamable const &obj);
  137. private:
  138. streamable obj_;
  139. struct data;
  140. util::copy_ptr<data> d;
  141. };
  142. inline std::ostream &operator<<(std::ostream &out,to_title const &obj)
  143. {
  144. obj(out);
  145. return out;
  146. }
  147. ///
  148. /// \brief Output filter escape
  149. ///
  150. /// Escape text for HTML -- make text safe
  151. ///
  152. class CPPCMS_API escape {
  153. public:
  154. escape();
  155. ~escape();
  156. escape(escape const &);
  157. escape const &operator=(escape const &other);
  158. void operator()(std::ostream &out) const;
  159. escape(streamable const &obj);
  160. private:
  161. streamable obj_;
  162. struct data;
  163. util::copy_ptr<data> d;
  164. };
  165. inline std::ostream &operator<<(std::ostream &out,escape const &obj)
  166. {
  167. obj(out);
  168. return out;
  169. }
  170. ///
  171. /// \brief Output filter urlencode
  172. ///
  173. /// Perform urlencoding(percent encoding) of the text
  174. ///
  175. class CPPCMS_API urlencode {
  176. public:
  177. urlencode();
  178. ~urlencode();
  179. urlencode(urlencode const &);
  180. urlencode const &operator=(urlencode const &other);
  181. void operator()(std::ostream &out) const;
  182. urlencode(streamable const &obj);
  183. private:
  184. streamable obj_;
  185. struct data;
  186. util::copy_ptr<data> d;
  187. };
  188. inline std::ostream &operator<<(std::ostream &out,urlencode const &obj)
  189. {
  190. obj(out);
  191. return out;
  192. }
  193. ///
  194. /// \brief Output filter base64_urlencode
  195. ///
  196. /// Convert text to base64 format that is friendly to URL.
  197. ///
  198. class CPPCMS_API base64_urlencode {
  199. public:
  200. base64_urlencode();
  201. ~base64_urlencode();
  202. base64_urlencode(base64_urlencode const &);
  203. base64_urlencode const &operator=(base64_urlencode const &other);
  204. void operator()(std::ostream &out) const;
  205. base64_urlencode(streamable const &obj);
  206. private:
  207. streamable obj_;
  208. struct data;
  209. util::copy_ptr<data> d;
  210. };
  211. inline std::ostream &operator<<(std::ostream &out,base64_urlencode const &obj)
  212. {
  213. obj(out);
  214. return out;
  215. }
  216. ///
  217. /// \brief Output filter raw
  218. ///
  219. /// Filter that does nothing
  220. ///
  221. class CPPCMS_API raw {
  222. public:
  223. raw();
  224. ~raw();
  225. raw(raw const &);
  226. raw const &operator=(raw const &other);
  227. void operator()(std::ostream &out) const;
  228. raw(streamable const &obj);
  229. private:
  230. streamable obj_;
  231. struct data;
  232. util::copy_ptr<data> d;
  233. };
  234. inline std::ostream &operator<<(std::ostream &out,raw const &obj)
  235. {
  236. obj(out);
  237. return out;
  238. }
  239. ///
  240. /// \brief Format date to ouput stream
  241. ///
  242. /// Formats date to the stream, date is represented as time_t
  243. ///
  244. class CPPCMS_API date {
  245. public:
  246. date();
  247. date(date const &other);
  248. date const &operator=(date const &other);
  249. ~date();
  250. date(double time);
  251. void operator()(std::ostream &out) const;
  252. private:
  253. struct data;
  254. double time_;
  255. util::copy_ptr<data> d;
  256. };
  257. inline std::ostream &operator<<(std::ostream &out,date const &obj)
  258. {
  259. obj(out);
  260. return out;
  261. }
  262. ///
  263. /// \brief Format time to ouput stream
  264. ///
  265. /// Formats time to the stream, time is represented as time_t
  266. ///
  267. class CPPCMS_API time {
  268. public:
  269. time();
  270. time(time const &other);
  271. time const &operator=(time const &other);
  272. ~time();
  273. time(double time);
  274. void operator()(std::ostream &out) const;
  275. private:
  276. struct data;
  277. double time_;
  278. util::copy_ptr<data> d;
  279. };
  280. inline std::ostream &operator<<(std::ostream &out,time const &obj)
  281. {
  282. obj(out);
  283. return out;
  284. }
  285. ///
  286. /// \brief Format date and time to ouput stream
  287. ///
  288. /// Formats date and time to the stream, date and time is represented as time_t
  289. ///
  290. class CPPCMS_API datetime {
  291. public:
  292. datetime();
  293. datetime(datetime const &other);
  294. datetime const &operator=(datetime const &other);
  295. ~datetime();
  296. datetime(double t);
  297. void operator()(std::ostream &out) const;
  298. private:
  299. struct data;
  300. double time_;
  301. util::copy_ptr<data> d;
  302. };
  303. inline std::ostream &operator<<(std::ostream &out,datetime const &obj)
  304. {
  305. obj(out);
  306. return out;
  307. }
  308. using locale::translate;
  309. using locale::format;
  310. }
  311. ///////////////////////////////
  312. }
  313. #undef CPPCMS_STREAMED
  314. #endif