filters.h
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
#ifndef CPPCMS_FILTERS_H
#define CPPCMS_FILTERS_H
#include <locale>
#include <typeinfo>
#include <sstream>
#include <vector>
#include <iostream>
#include <cppcms/defs.h>
#include <booster/copy_ptr.h>
#include <cppcms/localization.h>
namespace cppcms {
namespace filters {
class CPPCMS_API streamable {
public:
typedef void (*to_stream_type)(std::ostream &,void const *ptr);
typedef std::string (*to_string_type)(std::ios &,void const *ptr);
streamable();
~streamable();
streamable(streamable const &other);
streamable const &operator=(streamable const &other);
template<typename S>
streamable(S const &ptr)
{
void const *p=&ptr;
to_stream_type s1=&to_stream<S>;
to_string_type s2=&to_string<S>;
std::type_info const *info=&typeid(S);
set(p,s1,s2,info);
}
template<typename T>
T const &get() const
{
if(typeid(T) != type())
throw std::bad_cast();
T const *object=reinterpret_cast<T const *>(ptr_);
return *object;
}
streamable(char const *ptr);
void operator()(std::ostream &output) const;
std::string get(std::ios &ios) const;
private:
void set(void const *ptr,to_stream_type f1,to_string_type f2,std::type_info const *type);
template<typename T>
static void to_stream(std::ostream &out,void const *ptr)
{
T const *object=reinterpret_cast<T const *>(ptr);
out << *object;
}
template<typename T>
static std::string to_string(std::ios &out,void const *ptr)
{
T const *object=reinterpret_cast<T const *>(ptr);
std::ostringstream oss;
oss.copyfmt(out);
oss << *object;
return oss.str();
}
std::type_info const &type() const;
private:
void const *ptr_;
to_stream_type to_stream_;
to_string_type to_string_;
std::type_info const *type_;
};
template<>
CPPCMS_API streamable::streamable(std::string const &str);
class CPPCMS_API to_upper {
public:
to_upper();
~to_upper();
to_upper(to_upper const &);
to_upper const &operator=(to_upper const &other);
void operator()(std::ostream &out) const;
to_upper(streamable const &obj);
private:
streamable obj_;
struct _data;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,to_upper const &obj)
{
obj(out);
return out;
}
class CPPCMS_API to_lower {
public:
to_lower();
~to_lower();
to_lower(to_lower const &);
to_lower const &operator=(to_lower const &other);
void operator()(std::ostream &out) const;
to_lower(streamable const &obj);
private:
streamable obj_;
struct _data;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,to_lower const &obj)
{
obj(out);
return out;
}
class CPPCMS_API to_title {
public:
to_title();
~to_title();
to_title(to_title const &);
to_title const &operator=(to_title const &other);
void operator()(std::ostream &out) const;
to_title(streamable const &obj);
private:
streamable obj_;
struct _data;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,to_title const &obj)
{
obj(out);
return out;
}
class CPPCMS_API escape {
public:
escape();
~escape();
escape(escape const &);
escape const &operator=(escape const &other);
void operator()(std::ostream &out) const;
escape(streamable const &obj);
private:
streamable obj_;
struct _data;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,escape const &obj)
{
obj(out);
return out;
}
class CPPCMS_API urlencode {
public:
urlencode();
~urlencode();
urlencode(urlencode const &);
urlencode const &operator=(urlencode const &other);
void operator()(std::ostream &out) const;
urlencode(streamable const &obj);
private:
streamable obj_;
struct _data;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,urlencode const &obj)
{
obj(out);
return out;
}
class CPPCMS_API base64_urlencode {
public:
base64_urlencode();
~base64_urlencode();
base64_urlencode(base64_urlencode const &);
base64_urlencode const &operator=(base64_urlencode const &other);
void operator()(std::ostream &out) const;
base64_urlencode(streamable const &obj);
private:
streamable obj_;
struct _data;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,base64_urlencode const &obj)
{
obj(out);
return out;
}
class CPPCMS_API raw {
public:
raw();
~raw();
raw(raw const &);
raw const &operator=(raw const &other);
void operator()(std::ostream &out) const;
raw(streamable const &obj);
private:
streamable obj_;
struct _data;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,raw const &obj)
{
obj(out);
return out;
}
class CPPCMS_API date {
public:
date();
date(date const &other);
date const &operator=(date const &other);
~date();
date(streamable const &time);
date(streamable const &time,std::string const &timezone);
void operator()(std::ostream &out) const;
private:
struct _data;
streamable time_;
std::string tz_;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,date const &obj)
{
obj(out);
return out;
}
class CPPCMS_API time {
public:
time();
time(time const &other);
time const &operator=(time const &other);
~time();
time(streamable const &t);
time(streamable const &time,std::string const &timezone);
void operator()(std::ostream &out) const;
private:
struct _data;
streamable time_;
std::string tz_;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,time const &obj)
{
obj(out);
return out;
}
class CPPCMS_API datetime {
public:
datetime();
datetime(datetime const &other);
datetime const &operator=(datetime const &other);
~datetime();
datetime(streamable const &t);
datetime(streamable const &time,std::string const &timezone);
void operator()(std::ostream &out) const;
private:
struct _data;
streamable time_;
std::string tz_;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,datetime const &obj)
{
obj(out);
return out;
}
class CPPCMS_API strftime {
public:
strftime();
strftime(strftime const &other);
strftime const &operator=(strftime const &other);
~strftime();
strftime(streamable const &t,std::string const &fmt);
strftime(streamable const &time,std::string const &timezone,std::string const &fmt);
void operator()(std::ostream &out) const;
private:
struct _data;
streamable time_;
std::string tz_;
std::string format_;
booster::copy_ptr<_data> d;
};
inline std::ostream &operator<<(std::ostream &out,strftime const &obj)
{
obj(out);
return out;
}
using locale::translate;
using locale::format;
}
}
#endif
cppcms::filters::streamable::streamable
streamable(S const &ptr)
Definition: filters.h:50
cppcms::filters::urlencode
Output filter urlencode.
Definition: filters.h:240
booster::locale::as::date
std::ios_base & date(std::ios_base &ios)
Definition: formatting.h:319
booster::locale::to_lower
std::basic_string< CharType > to_lower(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.h:248
cppcms::filters::raw
Output filter raw.
Definition: filters.h:294
booster::locale::to_title
std::basic_string< CharType > to_title(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.h:286
cppcms::filters::to_lower
Output filter to_lower.
Definition: filters.h:158
booster::locale::operator<<
std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &out, date_time const &t)
Definition: date_time.h:874
booster::locale::as::datetime
std::ios_base & datetime(std::ios_base &ios)
Definition: formatting.h:337
booster::locale::to_upper
std::basic_string< CharType > to_upper(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.h:209
cppcms::filters::base64_urlencode
Output filter base64_urlencode.
Definition: filters.h:267
booster::copy_ptr< _data >
booster::locale::translate
basic_message< CharType > translate(CharType const *msg)
Translate a message, msg is not copied.
Definition: message.h:530
cppcms::filters::date
Formats date to the stream, date is represented as number - POSIX time, a plain number.
Definition: filters.h:321
cppcms::filters::datetime
Format date and time to ouput stream.
Definition: filters.h:390
cppcms::filters::to_title
Output filter to_title.
Definition: filters.h:185
cppcms::filters::strftime
Custom time formating filter.
Definition: filters.h:424
booster::locale::format
basic_format< char > format
Definition: format.h:469
booster::locale::as::strftime
std::ios_base & strftime(std::ios_base &ios)
Definition: formatting.h:347
cppcms::filters::escape
Output filter escape.
Definition: filters.h:212
cppcms::filters::streamable
A special proxy object for writing any object to a std::ostream.
Definition: filters.h:34
cppcms::filters::to_upper
Output filter to_upper.
Definition: filters.h:131
cppcms::filters::time
Format local time to ouput stream.
Definition: filters.h:356
booster::locale::as::time
std::ios_base & time(std::ios_base &ios)
Definition: formatting.h:328