Menu

[r2375]: / framework / trunk / cppcms / views_pool.h  Maximize  Restore  History

Download this file

206 lines (180 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
///////////////////////////////////////////////////////////////////////////////
//
// 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; }
///
/// \brief this namespace holds all classes used for rendering CppCMS views.
///
namespace views {
///
/// \brief The class that represents a single skin and generates its views.
///
/// Usually used by templates compiler
///
class CPPCMS_API generator : public booster::noncopyable {
public:
/// The callback that creates a single view
typedef std::auto_ptr<base_view> view_factory_type(std::ostream &,base_content *c);
generator();
~generator();
///
/// Add a single view of type \a View that uses content of type \a Content
/// Using name \a view_name.
///
/// If \a safe is true that dynamic cast is used to ensure that content has proper type
/// otherwise static cast.
///
/// Usually used by templates 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);
}
///
/// Add a view that uses a callback
///
void add_factory(std::string const &name,view_factory_type *factory);
///
/// Get skin name
///
std::string name() const;
///
/// Set skin name
///
void name(std::string const &n);
///
/// Create a view by its name that writes that data to \a outout using
/// a content \a content.
///
std::auto_ptr<base_view> create(std::string const &view_name,
std::ostream &output,
base_content *content) const;
///
/// Enumerate view names
///
std::vector<std::string> enumerate() 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;
};
///
/// \brief This is a singleton object that holds all views in the process. Any view
/// is registered and unregistered via this object.
///
/// It is usually not used directly
///
class CPPCMS_API pool : public booster::noncopyable {
public:
///
/// Add new skin to pool
///
/// This function is thread safe
///
void add(generator const &generator);
///
/// Remove the skin from pool
///
/// This function is thread safe
///
void remove(generator const &generator);
///
/// Render Skin
///
/// This member function is used to render templates. Generally you should not use
/// it directly, unless you have very good reasons.
///
/// \param skin - the name of the skin that should be used
/// \param template_name - the name of template (class) that should be rendered.
/// \param out - the output stream into which the view should be rendered
/// \param content - the content that should be rendered using this view.
///
/// This function is thread safe
///
void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
///
/// Get all loaded views
///
/// This function is thread safe
///
/// \ver{v1_2}
std::vector<std::string> enumerate();
///
/// Get the singleton instance of the views pool
///
static pool &instance();
private:
pool();
~pool();
struct data;
booster::hold_ptr<data> d;
};
///
/// \brief This class controls the views used my application it knows to load them dynamically
/// and reload if needed
///
class CPPCMS_API manager : public booster::noncopyable {
public:
///
/// Create new views manager
///
/// Usually created by cppcms::service()
///
manager(json::value const &settings);
~manager();
///
/// Render a template in a skin. Checks if any of shared objects/dlls should be reloaded
///
void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content);
///
/// Get default skin
///
std::string default_skin();
private:
struct data;
booster::hold_ptr<data> d;
};
} // views
}
#endif
MongoDB Logo MongoDB