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 <booster/hold_ptr.h>
19 #include <booster/noncopyable.h>
20 #include <cppcms/localization.h>
21 
22 namespace cppcms {
27  class CPPCMS_API translation_domain_scope : public booster::noncopyable {
28  public:
32  static int domain_id(std::ostream &out,std::string const &domain);
33 
37  translation_domain_scope(std::ostream &output,int domain_id);
41  translation_domain_scope(std::ostream &output,std::string const &domain);
46  private:
47  void set_and_save(int id);
48  struct _data;
50  std::ostream *output_;
51  int prev_id_;
52  };
58  namespace filters {
59 
67  class CPPCMS_API streamable {
68  public:
70  typedef void (*to_stream_type)(std::ostream &,void const *ptr);
71  typedef std::string (*to_string_type)(std::ios &,void const *ptr);
73 
74  streamable();
75  ~streamable();
76  streamable(streamable const &other);
77  streamable const &operator=(streamable const &other);
78 
82  template<typename S>
83  streamable(S const &ptr)
84  {
85  void const *p=&ptr;
86  to_stream_type s1=&to_stream<S>;
87  to_string_type s2=&to_string<S>;
88  std::type_info const *info=&typeid(S);
89 
90  set(p,s1,s2,info);
91  }
92 
96  template<typename T>
97  T const &get() const
98  {
99  if(typeid(T) != type())
100  throw std::bad_cast();
101  T const *object=reinterpret_cast<T const *>(ptr_);
102  return *object;
103  }
104 
108  streamable(char const *ptr);
109 
113 
114  void operator()(std::ostream &output) const;
115 
119  std::string get(std::ios &ios) const;
120  private:
121  void set(void const *ptr,to_stream_type f1,to_string_type f2,std::type_info const *type);
122 
123  template<typename T>
124  static void to_stream(std::ostream &out,void const *ptr)
125  {
126  T const *object=reinterpret_cast<T const *>(ptr);
127  out << *object;
128  }
129 
130 
131  template<typename T>
132  static std::string to_string(std::ios &out,void const *ptr)
133  {
134  T const *object=reinterpret_cast<T const *>(ptr);
135  std::ostringstream oss;
136  oss.copyfmt(out);
137  oss << *object;
138  return oss.str();
139  }
140 
141  std::type_info const &type() const;
142  private:
143 
144  void const *ptr_;
145  to_stream_type to_stream_;
146  to_string_type to_string_;
147  std::type_info const *type_;
148 
149  };
150 
155  template<>
156  CPPCMS_API streamable::streamable(std::string const &str);
157 
163 
164  class CPPCMS_API to_upper {
165  public:
166  to_upper();
167  ~to_upper();
168  to_upper(to_upper const &);
169  to_upper const &operator=(to_upper const &other);
170  void operator()(std::ostream &out) const;
171  to_upper(streamable const &obj);
172 
173  private:
174  streamable obj_;
175  struct _data;
177  };
178 
179  inline std::ostream &operator<<(std::ostream &out,to_upper const &obj)
180  {
181  obj(out);
182  return out;
183  }
184 
190 
191  class CPPCMS_API to_lower {
192  public:
193  to_lower();
194  ~to_lower();
195  to_lower(to_lower const &);
196  to_lower const &operator=(to_lower const &other);
197  void operator()(std::ostream &out) const;
198  to_lower(streamable const &obj);
199 
200  private:
201  streamable obj_;
202  struct _data;
204  };
205 
206  inline std::ostream &operator<<(std::ostream &out,to_lower const &obj)
207  {
208  obj(out);
209  return out;
210  }
211 
217 
218  class CPPCMS_API to_title {
219  public:
220  to_title();
221  ~to_title();
222  to_title(to_title const &);
223  to_title const &operator=(to_title const &other);
224  void operator()(std::ostream &out) const;
225  to_title(streamable const &obj);
226 
227  private:
228  streamable obj_;
229  struct _data;
231  };
232 
233  inline std::ostream &operator<<(std::ostream &out,to_title const &obj)
234  {
235  obj(out);
236  return out;
237  }
238 
244 
245  class CPPCMS_API escape {
246  public:
247  escape();
248  ~escape();
249  escape(escape const &);
250  escape const &operator=(escape const &other);
251  void operator()(std::ostream &out) const;
252  escape(streamable const &obj);
253 
254  private:
255  streamable obj_;
256  struct _data;
258  };
259 
260  inline std::ostream &operator<<(std::ostream &out,escape const &obj)
261  {
262  obj(out);
263  return out;
264  }
271  class CPPCMS_API jsescape {
272  public:
273  jsescape();
274  ~jsescape();
275  jsescape(jsescape const &);
276  jsescape const &operator=(jsescape const &other);
277  void operator()(std::ostream &out) const;
278  jsescape(streamable const &obj);
279 
280  private:
281  streamable obj_;
282  struct _data;
284  };
285 
286  inline std::ostream &operator<<(std::ostream &out,jsescape const &obj)
287  {
288  obj(out);
289  return out;
290  }
291 
292 
298 
299  class CPPCMS_API urlencode {
300  public:
301  urlencode();
302  ~urlencode();
303  urlencode(urlencode const &);
304  urlencode const &operator=(urlencode const &other);
305  void operator()(std::ostream &out) const;
306  urlencode(streamable const &obj);
307 
308  private:
309  streamable obj_;
310  struct _data;
312  };
313 
314  inline std::ostream &operator<<(std::ostream &out,urlencode const &obj)
315  {
316  obj(out);
317  return out;
318  }
319 
325 
326  class CPPCMS_API base64_urlencode {
327  public:
328  base64_urlencode();
329  ~base64_urlencode();
331  base64_urlencode const &operator=(base64_urlencode const &other);
332  void operator()(std::ostream &out) const;
333  base64_urlencode(streamable const &obj);
334 
335  private:
336  streamable obj_;
337  struct _data;
339  };
340 
341  inline std::ostream &operator<<(std::ostream &out,base64_urlencode const &obj)
342  {
343  obj(out);
344  return out;
345  }
346 
352 
353  class CPPCMS_API raw {
354  public:
355  raw();
356  ~raw();
357  raw(raw const &);
358  raw const &operator=(raw const &other);
359  void operator()(std::ostream &out) const;
360  raw(streamable const &obj);
361 
362  private:
363  streamable obj_;
364  struct _data;
366 
367 
368 
369  };
370 
371  inline std::ostream &operator<<(std::ostream &out,raw const &obj)
372  {
373  obj(out);
374  return out;
375  }
376 
380  class CPPCMS_API date {
381  public:
382  date();
383  date(date const &other);
384  date const &operator=(date const &other);
385  ~date();
386 
390  date(streamable const &time);
395  date(streamable const &time,std::string const &timezone);
396  void operator()(std::ostream &out) const;
397  private:
398  struct _data;
399  streamable time_;
400  std::string tz_;
402  };
403 
404  inline std::ostream &operator<<(std::ostream &out,date const &obj)
405  {
406  obj(out);
407  return out;
408  }
409 
415  class CPPCMS_API time {
416  public:
417  time();
418  time(time const &other);
419  time const &operator=(time const &other);
420  ~time();
421 
425  time(streamable const &t);
430  time(streamable const &time,std::string const &timezone);
431  void operator()(std::ostream &out) const;
432  private:
433  struct _data;
434  streamable time_;
435  std::string tz_;
437  };
438 
439  inline std::ostream &operator<<(std::ostream &out,time const &obj)
440  {
441  obj(out);
442  return out;
443  }
449  class CPPCMS_API datetime {
450  public:
451  datetime();
452  datetime(datetime const &other);
453  datetime const &operator=(datetime const &other);
454  ~datetime();
455 
459  datetime(streamable const &t);
464  datetime(streamable const &time,std::string const &timezone);
465  void operator()(std::ostream &out) const;
466  private:
467  struct _data;
468  streamable time_;
469  std::string tz_;
471  };
472 
473  inline std::ostream &operator<<(std::ostream &out,datetime const &obj)
474  {
475  obj(out);
476  return out;
477  }
483  class CPPCMS_API strftime {
484  public:
485  strftime();
486  strftime(strftime const &other);
487  strftime const &operator=(strftime const &other);
488  ~strftime();
489 
495  strftime(streamable const &t,std::string const &fmt);
502  strftime(streamable const &time,std::string const &timezone,std::string const &fmt);
503 
504  void operator()(std::ostream &out) const;
505  private:
506  struct _data;
507  streamable time_;
508  std::string tz_;
509  std::string format_;
511  };
512 
513  inline std::ostream &operator<<(std::ostream &out,strftime const &obj)
514  {
515  obj(out);
516  return out;
517  }
518 
519  using locale::translate;
520  using locale::format;
521 
522  }
523 
525 
526 }
527 
528 
529 #endif
streamable(S const &ptr)
Definition: filters.h:83
Output filter urlencode.
Definition: filters.h:299
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:353
Output filter escape.
Definition: filters.h:271
details::set_domain domain(std::string const &id)
Definition: message.h:789
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:191
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:326
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
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:380
Format date and time to ouput stream.
Definition: filters.h:449
Output filter to_title.
Definition: filters.h:218
set gettext domain id withing specific scope
Definition: filters.h:27
Custom time formating filter.
Definition: filters.h:483
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:245
A special proxy object for writing any object to a std::ostream.
Definition: filters.h:67
Output filter to_upper.
Definition: filters.h:164
archive & operator<<(archive &a, Archivable const &object)
Definition: serialization_classes.h:176
Format local time to ouput stream.
Definition: filters.h:415
std::ios_base & time(std::ios_base &ios)
Definition: formatting.h:328
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15