CppCMS
filters.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_FILTERS_H
9 #define CPPCMS_FILTERS_H
10 
11 #include <locale>
12 #include <typeinfo>
13 #include <sstream>
14 #include <vector>
15 #include <iostream>
16 #include <cppcms/defs.h>
17 #include <booster/copy_ptr.h>
18 #include <cppcms/localization.h>
19 
20 namespace cppcms {
25  namespace filters {
26 
34  class CPPCMS_API streamable {
35  public:
37  typedef void (*to_stream_type)(std::ostream &,void const *ptr);
38  typedef std::string (*to_string_type)(std::ios &,void const *ptr);
40 
41  streamable();
42  ~streamable();
43  streamable(streamable const &other);
44  streamable const &operator=(streamable const &other);
45 
49  template<typename S>
50  streamable(S const &ptr)
51  {
52  void const *p=&ptr;
53  to_stream_type s1=&to_stream<S>;
54  to_string_type s2=&to_string<S>;
55  std::type_info const *info=&typeid(S);
56 
57  set(p,s1,s2,info);
58  }
59 
63  template<typename T>
64  T const &get() const
65  {
66  if(typeid(T) != type())
67  throw std::bad_cast();
68  T const *object=reinterpret_cast<T const *>(ptr_);
69  return *object;
70  }
71 
75  streamable(char const *ptr);
76 
80 
81  void operator()(std::ostream &output) const;
82 
86  std::string get(std::ios &ios) const;
87  private:
88  void set(void const *ptr,to_stream_type f1,to_string_type f2,std::type_info const *type);
89 
90  template<typename T>
91  static void to_stream(std::ostream &out,void const *ptr)
92  {
93  T const *object=reinterpret_cast<T const *>(ptr);
94  out << *object;
95  }
96 
97 
98  template<typename T>
99  static std::string to_string(std::ios &out,void const *ptr)
100  {
101  T const *object=reinterpret_cast<T const *>(ptr);
102  std::ostringstream oss;
103  oss.copyfmt(out);
104  oss << *object;
105  return oss.str();
106  }
107 
108  std::type_info const &type() const;
109  private:
110 
111  void const *ptr_;
112  to_stream_type to_stream_;
113  to_string_type to_string_;
114  std::type_info const *type_;
115 
116  };
117 
122  template<>
123  CPPCMS_API streamable::streamable(std::string const &str);
124 
130 
131  class CPPCMS_API to_upper {
132  public:
133  to_upper();
134  ~to_upper();
135  to_upper(to_upper const &);
136  to_upper const &operator=(to_upper const &other);
137  void operator()(std::ostream &out) const;
138  to_upper(streamable const &obj);
139 
140  private:
141  streamable obj_;
142  struct _data;
144  };
145 
146  inline std::ostream &operator<<(std::ostream &out,to_upper const &obj)
147  {
148  obj(out);
149  return out;
150  }
151 
157 
158  class CPPCMS_API to_lower {
159  public:
160  to_lower();
161  ~to_lower();
162  to_lower(to_lower const &);
163  to_lower const &operator=(to_lower const &other);
164  void operator()(std::ostream &out) const;
165  to_lower(streamable const &obj);
166 
167  private:
168  streamable obj_;
169  struct _data;
171  };
172 
173  inline std::ostream &operator<<(std::ostream &out,to_lower const &obj)
174  {
175  obj(out);
176  return out;
177  }
178 
184 
185  class CPPCMS_API to_title {
186  public:
187  to_title();
188  ~to_title();
189  to_title(to_title const &);
190  to_title const &operator=(to_title const &other);
191  void operator()(std::ostream &out) const;
192  to_title(streamable const &obj);
193 
194  private:
195  streamable obj_;
196  struct _data;
198  };
199 
200  inline std::ostream &operator<<(std::ostream &out,to_title const &obj)
201  {
202  obj(out);
203  return out;
204  }
205 
211 
212  class CPPCMS_API escape {
213  public:
214  escape();
215  ~escape();
216  escape(escape const &);
217  escape const &operator=(escape const &other);
218  void operator()(std::ostream &out) const;
219  escape(streamable const &obj);
220 
221  private:
222  streamable obj_;
223  struct _data;
225  };
226 
227  inline std::ostream &operator<<(std::ostream &out,escape const &obj)
228  {
229  obj(out);
230  return out;
231  }
232 
233 
239 
240  class CPPCMS_API urlencode {
241  public:
242  urlencode();
243  ~urlencode();
244  urlencode(urlencode const &);
245  urlencode const &operator=(urlencode const &other);
246  void operator()(std::ostream &out) const;
247  urlencode(streamable const &obj);
248 
249  private:
250  streamable obj_;
251  struct _data;
253  };
254 
255  inline std::ostream &operator<<(std::ostream &out,urlencode const &obj)
256  {
257  obj(out);
258  return out;
259  }
260 
266 
267  class CPPCMS_API base64_urlencode {
268  public:
269  base64_urlencode();
270  ~base64_urlencode();
272  base64_urlencode const &operator=(base64_urlencode const &other);
273  void operator()(std::ostream &out) const;
274  base64_urlencode(streamable const &obj);
275 
276  private:
277  streamable obj_;
278  struct _data;
280  };
281 
282  inline std::ostream &operator<<(std::ostream &out,base64_urlencode const &obj)
283  {
284  obj(out);
285  return out;
286  }
287 
293 
294  class CPPCMS_API raw {
295  public:
296  raw();
297  ~raw();
298  raw(raw const &);
299  raw const &operator=(raw const &other);
300  void operator()(std::ostream &out) const;
301  raw(streamable const &obj);
302 
303  private:
304  streamable obj_;
305  struct _data;
307 
308 
309 
310  };
311 
312  inline std::ostream &operator<<(std::ostream &out,raw const &obj)
313  {
314  obj(out);
315  return out;
316  }
317 
321  class CPPCMS_API date {
322  public:
323  date();
324  date(date const &other);
325  date const &operator=(date const &other);
326  ~date();
327 
331  date(streamable const &time);
336  date(streamable const &time,std::string const &timezone);
337  void operator()(std::ostream &out) const;
338  private:
339  struct _data;
340  streamable time_;
341  std::string tz_;
343  };
344 
345  inline std::ostream &operator<<(std::ostream &out,date const &obj)
346  {
347  obj(out);
348  return out;
349  }
350 
356  class CPPCMS_API time {
357  public:
358  time();
359  time(time const &other);
360  time const &operator=(time const &other);
361  ~time();
362 
366  time(streamable const &t);
371  time(streamable const &time,std::string const &timezone);
372  void operator()(std::ostream &out) const;
373  private:
374  struct _data;
375  streamable time_;
376  std::string tz_;
378  };
379 
380  inline std::ostream &operator<<(std::ostream &out,time const &obj)
381  {
382  obj(out);
383  return out;
384  }
390  class CPPCMS_API datetime {
391  public:
392  datetime();
393  datetime(datetime const &other);
394  datetime const &operator=(datetime const &other);
395  ~datetime();
396 
400  datetime(streamable const &t);
405  datetime(streamable const &time,std::string const &timezone);
406  void operator()(std::ostream &out) const;
407  private:
408  struct _data;
409  streamable time_;
410  std::string tz_;
412  };
413 
414  inline std::ostream &operator<<(std::ostream &out,datetime const &obj)
415  {
416  obj(out);
417  return out;
418  }
424  class CPPCMS_API strftime {
425  public:
426  strftime();
427  strftime(strftime const &other);
428  strftime const &operator=(strftime const &other);
429  ~strftime();
430 
436  strftime(streamable const &t,std::string const &fmt);
443  strftime(streamable const &time,std::string const &timezone,std::string const &fmt);
444 
445  void operator()(std::ostream &out) const;
446  private:
447  struct _data;
448  streamable time_;
449  std::string tz_;
450  std::string format_;
452  };
453 
454  inline std::ostream &operator<<(std::ostream &out,strftime const &obj)
455  {
456  obj(out);
457  return out;
458  }
459 
460  using locale::translate;
461  using locale::format;
462 
463  }
464 
466 
467 }
468 
469 
470 #endif
streamable(S const &ptr)
Definition: filters.h:50
Output filter urlencode.
Definition: filters.h:240
std::ios_base & date(std::ios_base &ios)
Definition: formatting.h:319
std::basic_string< CharType > to_lower(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.h:248
Output filter raw.
Definition: filters.h:294
std::basic_string< CharType > to_title(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.h:286
Output filter to_lower.
Definition: filters.h:158
std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &out, date_time const &t)
Definition: date_time.h:874
std::ios_base & datetime(std::ios_base &ios)
Definition: formatting.h:337
std::basic_string< CharType > to_upper(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.h:209
Output filter base64_urlencode.
Definition: filters.h:267
basic_message< CharType > translate(CharType const *msg)
Translate a message, msg is not copied.
Definition: message.h:530
Formats date to the stream, date is represented as number - POSIX time, a plain number.
Definition: filters.h:321
Format date and time to ouput stream.
Definition: filters.h:390
Output filter to_title.
Definition: filters.h:185
Custom time formating filter.
Definition: filters.h:424
basic_format< char > format
Definition: format.h:469
std::ios_base & strftime(std::ios_base &ios)
Definition: formatting.h:347
Output filter escape.
Definition: filters.h:212
A special proxy object for writing any object to a std::ostream.
Definition: filters.h:34
Output filter to_upper.
Definition: filters.h:131
Format local time to ouput stream.
Definition: filters.h:356
std::ios_base & time(std::ios_base &ios)
Definition: formatting.h:328