CppCMS
views_pool.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_VIEWS_POOL_H
9 #define CPPCMS_VIEWS_POOL_H
10 
11 #include <cppcms/defs.h>
12 #include <booster/noncopyable.h>
13 #include <cppcms/base_view.h>
14 #include <cppcms/cppcms_error.h>
15 
16 #include <memory>
17 #include <map>
18 #include <vector>
19 #include <ostream>
20 
21 namespace cppcms {
22 
23  namespace json { class value; }
24 
28  namespace views {
29 
35  class CPPCMS_API generator : public booster::noncopyable {
36  public:
38  typedef std::auto_ptr<base_view> view_factory_type(std::ostream &,base_content *c);
39 
40  generator();
41  ~generator();
42 
52  template<typename View,typename Content>
53  void add_view(std::string const &view_name,bool safe = true)
54  {
55  view_factory_type *factory = 0;
56  if(safe)
57  factory = view_builder<View,Content>;
58  else
59  factory = unsafe_view_builder<View,Content>;
60  add_factory(view_name,factory);
61  }
62 
66  void add_factory(std::string const &name,view_factory_type *factory);
70  std::string name() const;
74  void name(std::string const &n);
79  std::auto_ptr<base_view> create(std::string const &view_name,
80  std::ostream &output,
81  base_content *content) const;
82  private:
83 
84  template<typename View,typename Content>
85  static std::auto_ptr<base_view> view_builder(std::ostream &stream,base_content *c)
86  {
87  std::auto_ptr<base_view> p;
88 
89  try {
90  p.reset(new View(stream,dynamic_cast<Content &>(*c)));
91  }
92  catch(std::bad_cast const &) {
93  throw cppcms_error("cppcms::views::generator: an attempt to use content of invalid type");
94  }
95  return p;
96  }
97 
98  template<typename View,typename Content>
99  static std::auto_ptr<base_view> unsafe_view_builder(std::ostream &stream,base_content *c)
100  {
101  std::auto_ptr<base_view> p(new View(stream,static_cast<Content &>(*c)));
102  return p;
103  }
104 
105 
106  struct data;
107  typedef std::map<std::string,view_factory_type *> views_type;
108  views_type views_;
109  std::string name_;
111  };
112 
119  class CPPCMS_API pool : public booster::noncopyable {
120  public:
126  void add(generator const &generator);
132  void remove(generator const &generator);
133 
147  void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
148 
154  std::vector<std::string> enumerate();
155 
159  static pool &instance();
160 
161  private:
162  pool();
163  ~pool();
164 
165  struct data;
167  };
168 
173  class CPPCMS_API manager : public booster::noncopyable {
174  public:
180  manager(json::value const &settings);
181  ~manager();
182 
186  void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
190  std::string default_skin();
191  private:
192  struct data;
194  };
195  } // views
196 
197 }
198 
199 
200 #endif
This class controls the views used my application it knows to load them dynamically and reload if nee...
Definition: views_pool.h:173
void add_view(std::string const &view_name, bool safe=true)
Definition: views_pool.h:53
This class is central representation of json objects.
Definition: json.h:142
The class that represents a single skin and generates its views.
Definition: views_pool.h:35
Exception thrown by CppCMS framework.
Definition: cppcms_error.h:22
This is a simple polymorphic class that every content for templates rendering should be derided from ...
Definition: base_content.h:23
This is a singleton object that holds all views in the process. Any view is registered and unregister...
Definition: views_pool.h:119
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
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15