CppCMS
cppcms/filters.h
00001 
00002 //                                                                             
00003 //  Copyright (C) 2008-2012  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
00004 //                                                                             
00005 //  See accompanying file COPYING.TXT file for licensing details.
00006 //
00008 #ifndef CPPCMS_FILTERS_H
00009 #define CPPCMS_FILTERS_H
00010 
00011 #include <locale>
00012 #include <typeinfo>
00013 #include <sstream>
00014 #include <vector>
00015 #include <iostream>
00016 #include <cppcms/defs.h>
00017 #include <booster/copy_ptr.h>
00018 #include <cppcms/localization.h>
00019 
00020 namespace cppcms {
00025         namespace filters {
00026 
00034                 class CPPCMS_API streamable {
00035                 public:
00037                         typedef void (*to_stream_type)(std::ostream &,void const *ptr);
00038                         typedef std::string (*to_string_type)(std::ios &,void const *ptr);
00040 
00041                         streamable();
00042                         ~streamable();
00043                         streamable(streamable const &other);
00044                         streamable const &operator=(streamable const &other);
00045 
00049                         template<typename S>
00050                         streamable(S const &ptr)
00051                         {
00052                                 void const *p=&ptr;
00053                                 to_stream_type s1=&to_stream<S>;
00054                                 to_string_type s2=&to_string<S>;
00055                                 std::type_info const *info=&typeid(S);
00056 
00057                                 set(p,s1,s2,info);
00058                         }
00059 
00063                         template<typename T>
00064                         T const &get() const
00065                         {
00066                                 if(typeid(T) != type())
00067                                         throw std::bad_cast();
00068                                 T const *object=reinterpret_cast<T const *>(ptr_);
00069                                 return *object;
00070                         }
00071                         
00075                         streamable(char const *ptr);
00076 
00080 
00081                         void operator()(std::ostream &output) const;
00082 
00086                         std::string get(std::ios &ios) const;
00087                 private:
00088                         void set(void const *ptr,to_stream_type f1,to_string_type f2,std::type_info const *type);
00089 
00090                         template<typename T>
00091                         static void to_stream(std::ostream &out,void const *ptr)
00092                         {
00093                                 T const *object=reinterpret_cast<T const *>(ptr);
00094                                 out << *object;
00095                         }
00096 
00097 
00098                         template<typename T>
00099                         static std::string to_string(std::ios &out,void const *ptr)
00100                         {
00101                                 T const *object=reinterpret_cast<T const *>(ptr);
00102                                 std::ostringstream oss;
00103                                 oss.copyfmt(out);
00104                                 oss << *object;
00105                                 return oss.str();
00106                         }
00107 
00108                         std::type_info const &type() const;
00109                 private:
00110 
00111                         void const *ptr_;
00112                         to_stream_type to_stream_;
00113                         to_string_type to_string_;
00114                         std::type_info const *type_;
00115 
00116                 };
00117 
00122                 template<>
00123                 CPPCMS_API streamable::streamable(std::string const &str);
00124 
00130                 
00131                 class CPPCMS_API to_upper {                                     
00132                 public: 
00133                         to_upper();                                             
00134                         ~to_upper();                                    
00135                         to_upper(to_upper const &);                             
00136                         to_upper const &operator=(to_upper const &other);       
00137                         void operator()(std::ostream &out) const;
00138                         to_upper(streamable const &obj);
00139 
00140                 private:                                                
00141                         streamable obj_;                        
00142                         struct _data;                                   
00143                         booster::copy_ptr<_data> d;                             
00144                 };
00145 
00146                 inline std::ostream &operator<<(std::ostream &out,to_upper const &obj)
00147                 {
00148                         obj(out);
00149                         return out;
00150                 }
00151         
00157                 
00158                 class CPPCMS_API to_lower {                                     
00159                 public: 
00160                         to_lower();                                             
00161                         ~to_lower();                                    
00162                         to_lower(to_lower const &);                             
00163                         to_lower const &operator=(to_lower const &other);       
00164                         void operator()(std::ostream &out) const;
00165                         to_lower(streamable const &obj);
00166 
00167                 private:                                                
00168                         streamable obj_;                        
00169                         struct _data;                                   
00170                         booster::copy_ptr<_data> d;                             
00171                 };
00172 
00173                 inline std::ostream &operator<<(std::ostream &out,to_lower const &obj)
00174                 {
00175                         obj(out);
00176                         return out;
00177                 }
00178                 
00184                 
00185                 class CPPCMS_API to_title {                                     
00186                 public: 
00187                         to_title();                                             
00188                         ~to_title();                                    
00189                         to_title(to_title const &);                             
00190                         to_title const &operator=(to_title const &other);       
00191                         void operator()(std::ostream &out) const;
00192                         to_title(streamable const &obj);
00193 
00194                 private:                                                
00195                         streamable obj_;                        
00196                         struct _data;                                   
00197                         booster::copy_ptr<_data> d;                             
00198                 };
00199 
00200                 inline std::ostream &operator<<(std::ostream &out,to_title const &obj)
00201                 {
00202                         obj(out);
00203                         return out;
00204                 }
00205         
00211                 
00212                 class CPPCMS_API escape {                                       
00213                 public: 
00214                         escape();                                               
00215                         ~escape();                                      
00216                         escape(escape const &);                         
00217                         escape const &operator=(escape const &other);   
00218                         void operator()(std::ostream &out) const;
00219                         escape(streamable const &obj);
00220 
00221                 private:                                                
00222                         streamable obj_;                        
00223                         struct _data;                                   
00224                         booster::copy_ptr<_data> d;                             
00225                 };
00226 
00227                 inline std::ostream &operator<<(std::ostream &out,escape const &obj)
00228                 {
00229                         obj(out);
00230                         return out;
00231                 }
00232 
00233 
00239                 
00240                 class CPPCMS_API urlencode {                                    
00241                 public: 
00242                         urlencode();                                            
00243                         ~urlencode();                                   
00244                         urlencode(urlencode const &);                           
00245                         urlencode const &operator=(urlencode const &other);     
00246                         void operator()(std::ostream &out) const;
00247                         urlencode(streamable const &obj);
00248 
00249                 private:                                                
00250                         streamable obj_;                        
00251                         struct _data;                                   
00252                         booster::copy_ptr<_data> d;                             
00253                 };
00254 
00255                 inline std::ostream &operator<<(std::ostream &out,urlencode const &obj)
00256                 {
00257                         obj(out);
00258                         return out;
00259                 }
00260         
00266                 
00267                 class CPPCMS_API base64_urlencode {                                     
00268                 public: 
00269                         base64_urlencode();                                             
00270                         ~base64_urlencode();                                    
00271                         base64_urlencode(base64_urlencode const &);                             
00272                         base64_urlencode const &operator=(base64_urlencode const &other);       
00273                         void operator()(std::ostream &out) const;
00274                         base64_urlencode(streamable const &obj);
00275 
00276                 private:                                                
00277                         streamable obj_;                        
00278                         struct _data;                                   
00279                         booster::copy_ptr<_data> d;                             
00280                 };
00281 
00282                 inline std::ostream &operator<<(std::ostream &out,base64_urlencode const &obj)
00283                 {
00284                         obj(out);
00285                         return out;
00286                 }
00287                 
00293                 
00294                 class CPPCMS_API raw {                                  
00295                 public: 
00296                         raw();                                          
00297                         ~raw();                                 
00298                         raw(raw const &);                               
00299                         raw const &operator=(raw const &other); 
00300                         void operator()(std::ostream &out) const;
00301                         raw(streamable const &obj);
00302 
00303                 private:                                                
00304                         streamable obj_;                        
00305                         struct _data;                                   
00306                         booster::copy_ptr<_data> d;                             
00307                 
00308                 
00309                 
00310                 };
00311 
00312                 inline std::ostream &operator<<(std::ostream &out,raw const &obj)
00313                 {
00314                         obj(out);
00315                         return out;
00316                 }
00317                 
00321                 class CPPCMS_API date {
00322                 public:
00323                         date();
00324                         date(date const &other);
00325                         date const &operator=(date const &other);
00326                         ~date();
00327 
00331                         date(streamable const &time);
00336                         date(streamable const &time,std::string const &timezone);
00337                         void operator()(std::ostream &out) const;
00338                 private:
00339                         struct _data;
00340                         streamable time_;
00341                         std::string tz_;
00342                         booster::copy_ptr<_data> d;
00343                 };
00344                 
00345                 inline std::ostream &operator<<(std::ostream &out,date const &obj)
00346                 {
00347                         obj(out);
00348                         return out;
00349                 }
00350                 
00356                 class CPPCMS_API time {
00357                 public:
00358                         time();
00359                         time(time const &other);
00360                         time const &operator=(time const &other);
00361                         ~time();
00362 
00366                         time(streamable const &t);
00371                         time(streamable const &time,std::string const &timezone);
00372                         void operator()(std::ostream &out) const;
00373                 private:
00374                         struct _data;
00375                         streamable time_;
00376                         std::string tz_;
00377                         booster::copy_ptr<_data> d;
00378                 };
00379                 
00380                 inline std::ostream &operator<<(std::ostream &out,time const &obj)
00381                 {
00382                         obj(out);
00383                         return out;
00384                 }
00390                 class CPPCMS_API datetime {
00391                 public:
00392                         datetime();
00393                         datetime(datetime const &other);
00394                         datetime const &operator=(datetime const &other);
00395                         ~datetime();
00396 
00400                         datetime(streamable const &t);
00405                         datetime(streamable const &time,std::string const &timezone);
00406                         void operator()(std::ostream &out) const;
00407                 private:
00408                         struct _data;
00409                         streamable time_;
00410                         std::string tz_;
00411                         booster::copy_ptr<_data> d;
00412                 };
00413 
00414                 inline std::ostream &operator<<(std::ostream &out,datetime const &obj)
00415                 {
00416                         obj(out);
00417                         return out;
00418                 }
00424                 class CPPCMS_API strftime {
00425                 public:
00426                         strftime();
00427                         strftime(strftime const &other);
00428                         strftime const &operator=(strftime const &other);
00429                         ~strftime();
00430 
00436                         strftime(streamable const &t,std::string const &fmt);
00443                         strftime(streamable const &time,std::string const &timezone,std::string const &fmt);
00444 
00445                         void operator()(std::ostream &out) const;
00446                 private:
00447                         struct _data;
00448                         streamable time_;
00449                         std::string tz_;
00450                         std::string format_;
00451                         booster::copy_ptr<_data> d;
00452                 };
00453                 
00454                 inline std::ostream &operator<<(std::ostream &out,strftime const &obj)
00455                 {
00456                         obj(out);
00457                         return out;
00458                 }
00459 
00460                 using locale::translate;
00461                 using locale::format;
00462         
00463         }
00464 
00466 
00467 }
00468 
00469 
00470 #endif