CppCMS
|
00001 00002 // 00003 // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com> 00004 // 00005 // See accompanying file COPYING.TXT file for licensing details. 00006 // 00008 #ifndef CPPCMS_VIEWS_POOL_H 00009 #define CPPCMS_VIEWS_POOL_H 00010 00011 #include <cppcms/defs.h> 00012 #include <booster/noncopyable.h> 00013 #include <cppcms/base_view.h> 00014 #include <cppcms/cppcms_error.h> 00015 00016 #include <memory> 00017 #include <map> 00018 #include <vector> 00019 #include <ostream> 00020 00021 namespace cppcms { 00022 00023 namespace json { class value; } 00024 00028 namespace views { 00029 00035 class CPPCMS_API generator : public booster::noncopyable { 00036 public: 00038 typedef std::auto_ptr<base_view> view_factory_type(std::ostream &,base_content *c); 00039 00040 generator(); 00041 ~generator(); 00042 00052 template<typename View,typename Content> 00053 void add_view(std::string const &view_name,bool safe = true) 00054 { 00055 view_factory_type *factory = 0; 00056 if(safe) 00057 factory = view_builder<View,Content>; 00058 else 00059 factory = unsafe_view_builder<View,Content>; 00060 add_factory(view_name,factory); 00061 } 00062 00066 void add_factory(std::string const &name,view_factory_type *factory); 00070 std::string name() const; 00074 void name(std::string const &n); 00079 std::auto_ptr<base_view> create(std::string const &view_name, 00080 std::ostream &output, 00081 base_content *content) const; 00082 private: 00083 00084 template<typename View,typename Content> 00085 static std::auto_ptr<base_view> view_builder(std::ostream &stream,base_content *c) 00086 { 00087 std::auto_ptr<base_view> p; 00088 00089 try { 00090 p.reset(new View(stream,dynamic_cast<Content &>(*c))); 00091 } 00092 catch(std::bad_cast const &) { 00093 throw cppcms_error("cppcms::views::generator: an attempt to use content if invalid type"); 00094 } 00095 return p; 00096 } 00097 00098 template<typename View,typename Content> 00099 static std::auto_ptr<base_view> unsafe_view_builder(std::ostream &stream,base_content *c) 00100 { 00101 std::auto_ptr<base_view> p(new View(stream,static_cast<Content &>(*c))); 00102 return p; 00103 } 00104 00105 00106 struct data; 00107 typedef std::map<std::string,view_factory_type *> views_type; 00108 views_type views_; 00109 std::string name_; 00110 booster::hold_ptr<data> d; 00111 }; 00112 00119 class CPPCMS_API pool : public booster::noncopyable { 00120 public: 00126 void add(generator const &generator); 00132 void remove(generator const &generator); 00133 00147 void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content); 00148 00154 std::vector<std::string> enumerate(); 00155 00159 static pool &instance(); 00160 00161 private: 00162 pool(); 00163 ~pool(); 00164 00165 struct data; 00166 booster::hold_ptr<data> d; 00167 }; 00168 00173 class CPPCMS_API manager : public booster::noncopyable { 00174 public: 00180 manager(json::value const &settings); 00181 ~manager(); 00182 00186 void render(std::string const &skin,std::string const &template_name,std::ostream &out,base_content &content); 00190 std::string default_skin(); 00191 private: 00192 struct data; 00193 booster::hold_ptr<data> d; 00194 }; 00195 } // views 00196 00197 } 00198 00199 00200 #endif