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 <booster/auto_ptr_inc.h>
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;
85  std::vector<std::string> enumerate() const;
86  private:
87 
88  template<typename View,typename Content>
89  static std::auto_ptr<base_view> view_builder(std::ostream &stream,base_content *c)
90  {
91  std::auto_ptr<base_view> p;
92 
93  try {
94  p.reset(new View(stream,dynamic_cast<Content &>(*c)));
95  }
96  catch(std::bad_cast const &) {
97  throw cppcms_error("cppcms::views::generator: an attempt to use content of invalid type");
98  }
99  return p;
100  }
101 
102  template<typename View,typename Content>
103  static std::auto_ptr<base_view> unsafe_view_builder(std::ostream &stream,base_content *c)
104  {
105  std::auto_ptr<base_view> p(new View(stream,static_cast<Content &>(*c)));
106  return p;
107  }
108 
109 
110  struct data;
111  typedef std::map<std::string,view_factory_type *> views_type;
112  views_type views_;
113  std::string name_;
115  };
116 
126  class CPPCMS_API view_lock : public booster::noncopyable {
127  public:
131  view_lock(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
135  ~view_lock();
139  template<typename View>
140  View &use_view()
141  {
142  return dynamic_cast<View &>(view());
143  }
147  base_view &view();
148  private:
149  struct _data;
152  };
153 
160  class CPPCMS_API pool : public booster::noncopyable {
161  public:
167  void add(generator const &generator);
173  void remove(generator const &generator);
174 
188  void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
189 
196  std::vector<std::string> enumerate();
197 
201  static pool &instance();
202 
203  private:
204  friend class view_lock;
205  void lock();
206  void unlock();
207 
208  // called on locked object
209  base_view *create_view(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
210  pool();
211  ~pool();
212 
213  struct data;
215  };
216 
221  class CPPCMS_API manager : public booster::noncopyable {
222  public:
228  manager(json::value const &settings);
229  ~manager();
230 
234  void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
238  std::string default_skin();
239  private:
240  struct data;
242  };
243  } // views
244 
245 }
246 
247 
248 #endif
This class controls the views used my application it knows to load them dynamically and reload if nee...
Definition: views_pool.h:221
void add_view(std::string const &view_name, bool safe=true)
Definition: views_pool.h:53
A class that allows to use the view withing the internal lock used inside pool class.
Definition: views_pool.h:126
This class is central representation of json objects.
Definition: json.h:140
The class that represents a single skin and generates its views.
Definition: views_pool.h:35
View & use_view()
Definition: views_pool.h:140
Exception thrown by CppCMS framework.
Definition: cppcms_error.h:22
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
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:160
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 is base class for all views (skins) rendered by CppCMS template engine.
Definition: base_view.h:35
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15