views_pool.h
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
#ifndef CPPCMS_VIEWS_POOL_H
#define CPPCMS_VIEWS_POOL_H
#include <cppcms/defs.h>
#include <booster/noncopyable.h>
#include <cppcms/base_view.h>
#include <cppcms/cppcms_error.h>
#include <memory>
#include <map>
#include <vector>
#include <ostream>
namespace cppcms {
namespace json { class value; }
namespace views {
class CPPCMS_API generator : public booster::noncopyable {
public:
typedef std::auto_ptr<base_view> view_factory_type(std::ostream &,base_content *c);
generator();
~generator();
template<typename View,typename Content>
void add_view(std::string const &view_name,bool safe = true)
{
view_factory_type *factory = 0;
if(safe)
factory = view_builder<View,Content>;
else
factory = unsafe_view_builder<View,Content>;
add_factory(view_name,factory);
}
void add_factory(std::string const &name,view_factory_type *factory);
std::string name() const;
void name(std::string const &n);
std::auto_ptr<base_view> create(std::string const &view_name,
std::ostream &output,
base_content *content) const;
private:
template<typename View,typename Content>
static std::auto_ptr<base_view> view_builder(std::ostream &stream,base_content *c)
{
std::auto_ptr<base_view> p;
try {
p.reset(new View(stream,dynamic_cast<Content &>(*c)));
}
catch(std::bad_cast const &) {
throw cppcms_error("cppcms::views::generator: an attempt to use content of invalid type");
}
return p;
}
template<typename View,typename Content>
static std::auto_ptr<base_view> unsafe_view_builder(std::ostream &stream,base_content *c)
{
std::auto_ptr<base_view> p(new View(stream,static_cast<Content &>(*c)));
return p;
}
struct data;
typedef std::map<std::string,view_factory_type *> views_type;
views_type views_;
std::string name_;
booster::hold_ptr<data> d;
};
class CPPCMS_API pool : public booster::noncopyable {
public:
void add(generator const &generator);
void remove(generator const &generator);
void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
std::vector<std::string> enumerate();
static pool &instance();
private:
pool();
~pool();
struct data;
booster::hold_ptr<data> d;
};
class CPPCMS_API manager : public booster::noncopyable {
public:
manager(json::value const &settings);
~manager();
void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
std::string default_skin();
private:
struct data;
booster::hold_ptr<data> d;
};
} // views
}
#endif
cppcms::views::manager
This class controls the views used my application it knows to load them dynamically and reload if nee...
Definition: views_pool.h:173
cppcms::views::generator::add_view
void add_view(std::string const &view_name, bool safe=true)
Definition: views_pool.h:53
cppcms::json::value
This class is central representation of json objects.
Definition: json.h:142
booster::hold_ptr< data >
cppcms::views::generator
The class that represents a single skin and generates its views.
Definition: views_pool.h:35
cppcms::cppcms_error
Exception thrown by CppCMS framework.
Definition: cppcms_error.h:22
cppcms::base_content
This is a simple polymorphic class that every content for templates rendering should be derided from ...
Definition: base_content.h:23
cppcms::views::pool
This is a singleton object that holds all views in the process. Any view is registered and unregister...
Definition: views_pool.h:119
cppcms::views::generator::view_factory_type
std::auto_ptr< base_view > view_factory_type(std::ostream &, base_content *c)
The callback that creates a single view.
Definition: views_pool.h:38
booster::noncopyable
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15