fastdo  0.5.12
eientpl.hpp
浏览该文件的文档.
1 #ifndef __EIENTPL_HPP__
2 #define __EIENTPL_HPP__
3 
4 #include "winux.hpp"
5 #include "eienexpr.hpp"
6 #include "eienml.hpp"
7 
9 namespace eientpl
10 {
11 #ifdef EIENTPL_DLL_USE
12  #if defined(_MSC_VER) || defined(WIN32)
13  #pragma warning( disable: 4251 )
14  #ifdef EIENTPL_DLL_EXPORTS
15  #define EIENTPL_DLL __declspec(dllexport)
16  #else
17  #define EIENTPL_DLL __declspec(dllimport)
18  #endif
19 
20  #define EIENTPL_API __stdcall
21  #else
22  #define EIENTPL_DLL
23  #define EIENTPL_API
24  #endif
25 #else
26  #define EIENTPL_DLL
27  #define EIENTPL_API
28 #endif
29 
30 #define EIENTPL_FUNC_DECL(ret) EIENTPL_DLL ret EIENTPL_API
31 #define EIENTPL_FUNC_IMPL(ret) ret EIENTPL_API
32 
35 {
36 public:
37  enum
38  {
40  };
41  TplError( int errNo, winux::AnsiString const & err ) throw() : winux::Error( errNo, err ) { }
42 };
43 
44 class Template;
45 class TemplateCache;
46 
49 {
50 public:
54 public:
55  __TemplateLang( Template * tpl );
56 
58  winux::Mixed evalExprVal( winux::String const & exprStr );
59 
61  bool evalExprMixedPtr( winux::String const & exprStr, winux::Mixed ** ppv );
62 
63 private:
64  static int _InitExprFuncs;
65  static void InitExprFuncs();
66 };
67 
70 {
71 public:
72  Template( winux::String const & tplDir = "", winux::String const & localEncoding = "", winux::String const & targetEncoding = "" );
73  virtual ~Template();
74 
76  void assign( winux::String const & name, winux::Mixed const & v, bool convertEncoding = true );
80  void assignPtr( winux::String const & name, winux::Mixed * p, bool convertEncoding = true );
82  void clear();
83 
85  winux::String output( winux::String const & tplFile, winux::String const & cacheId = "" );
87  winux::String outputStr( winux::String const & tplStr, winux::String const & cacheId );
88 
90  winux::String _outputInternal( winux::String const & tplFilePath );
91 
95  void setTplDirPath( winux::String const & dirPath ) { this->_tplDirPath = winux::PathWithSep(dirPath); }
97  winux::String const & getTplDirPath() const { return this->_tplDirPath; }
98 
100  void setCache( TemplateCache * cache = NULL ) { this->_cache = cache; }
101 
103  __TemplateLang * getTplLangObj();
105  eienexpr::VarContext * getVarContext() { return &this->_varCtx; }
107  winux::Mixed const & getVar( winux::String const & name ) const;
108 
110  winux::String convFrom( winux::String const & str ) const;
112  winux::String convTo( winux::String const & str ) const;
113 
114  winux::String const & getLocalEncoding() const { return _localEncoding; }
115  winux::String const & getTargetEncoding() const { return _targetEncoding; }
116 private:
117  winux::String _tplDirPath; // 模板目录
118  winux::String _localEncoding; // 本地编码
119  winux::String _targetEncoding; // 目标编码
120  TemplateCache * _cache; // 模板缓存
121  eienexpr::VarContext _varCtx; // 变量场景
122 
123  winux::MembersWrapper<struct Template_Data> _self; // 隐藏的数据
124 
126 };
127 
130 {
131 public:
132  virtual ~TemplateCache() {}
134  virtual bool hasCache( winux::String const & cacheName ) const = 0;
136  virtual bool getCacheName( winux::String * cacheName, winux::String const & tplFile, winux::String const & cacheId = "" ) const = 0;
138  virtual bool readCache( winux::String const & cacheName, winux::String * str ) = 0;
140  virtual winux::String get( winux::String const & cacheName ) = 0;
142  virtual bool writeCache( winux::String const & cacheName, winux::String const & str ) = 0;
144  virtual void setLifeTime( int lifeTime = 0 ) = 0;
146  virtual int getLifeTime() const = 0;
147 };
148 
151 {
152 public:
153  TemplateFileCache( int lifeTime, winux::String const & cacheDir, winux::String const & cacheExt = ".cah" );
154 
155  virtual bool hasCache( winux::String const & cacheName ) const;
156  virtual bool getCacheName( winux::String * cacheName, winux::String const & tplFile, winux::String const & cacheId = "" ) const;
157  virtual bool readCache( winux::String const & cacheName, winux::String * str );
158  virtual winux::String get( winux::String const & cacheName );
159  virtual bool writeCache( winux::String const & cacheName, winux::String const & str );
160 
161  virtual void setLifeTime( int lifeTime = 0 );
162  virtual int getLifeTime() const;
163 
164  void setCacheDirPath( winux::String const & dirPath );
165  winux::String const & getCacheDirPath() const { return this->_cacheDirPath; }
166 
167  void setCacheExt( winux::String const & extName );
168  winux::String const & getCacheExt() const { return this->_cacheExt; }
169 
170 private:
171  int _cacheLifeTime; // 缓存生命期 0表示不缓存
172  winux::String _cacheDirPath; // 缓存目录带分隔符
173  winux::String _cacheExt; //缓存文件扩展名 默认为`.cah`
174 
175 };
176 
179 {
180 public:
181  struct CacheItem
182  {
185  };
186 
187  // map< name, cache_item >
188  typedef std::map< winux::String, CacheItem > CacheItemsMap;
189  // map< webappname, cache_items >
190  typedef std::map< winux::String, CacheItemsMap > CacheStruct;
191 
192  TemplateMemoryCache( int lifeTime, CacheItemsMap * cacheItems );
193 
194  virtual bool hasCache( winux::String const & cacheName ) const;
195  virtual bool getCacheName( winux::String * cacheName, winux::String const & tplFile, winux::String const & cacheId = "" ) const;
196  virtual bool readCache( winux::String const & cacheName, winux::String * str );
197  virtual winux::String get( winux::String const & cacheName );
198  virtual bool writeCache( winux::String const & cacheName, winux::String const & str );
199 
200  virtual void setLifeTime( int lifeTime = 0 );
201  virtual int getLifeTime() const;
202 private:
203  int _cacheLifeTime; // 缓存生命期 0表示不缓存
204  CacheItemsMap * _cacheItems;
205 };
206 
207 }
208 
209 #endif // __EIENTPL_HPP__
String PathWithSep(String const &path)
路径分隔符整理
winux::String const & getTplDirPath() const
取得模板相对目录路径
Definition: eientpl.hpp:97
TplError(int errNo, winux::AnsiString const &err)
Definition: eientpl.hpp:41
winux::StringArray _loadFilesChains
防止load无穷递归,记录载入文件链
Definition: eientpl.hpp:53
模板缓存基类
Definition: eientpl.hpp:129
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
模板-文件式缓存
Definition: eientpl.hpp:150
eienml::ElemPackage _elemPack
模板语言元素包
Definition: eientpl.hpp:52
eienexpr::VarContext * getVarContext()
访问内部变量场景对象,以便直接操作变量
Definition: eientpl.hpp:105
void setCache(TemplateCache *cache=NULL)
设置缓存机制是否开启,NULL为关闭
Definition: eientpl.hpp:100
#define EIENTPL_DLL
Definition: eientpl.hpp:26
winux::String const & getCacheDirPath() const
Definition: eientpl.hpp:165
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:78
winux::String const & getCacheExt() const
Definition: eientpl.hpp:168
std::vector< String > StringArray
Definition: utilities.hpp:171
模板类
Definition: eientpl.hpp:69
模板-内存式缓存
Definition: eientpl.hpp:178
winux::String const & getLocalEncoding() const
Definition: eientpl.hpp:114
winux::String const & getTargetEncoding() const
Definition: eientpl.hpp:115
模板错误
Definition: eientpl.hpp:34
模板语言类
Definition: eientpl.hpp:48
模板引擎
Definition: eientpl.hpp:9
std::map< winux::String, CacheItemsMap > CacheStruct
Definition: eientpl.hpp:190
Template * _tpl
指向一个模板对象
Definition: eientpl.hpp:51
变量场景类
Definition: eienexpr.hpp:316
virtual ~TemplateCache()
Definition: eientpl.hpp:132
混合体,能表示多种类型的值
Definition: utilities.hpp:586
错误类
Definition: utilities.hpp:413
std::map< winux::String, CacheItem > CacheItemsMap
Definition: eientpl.hpp:188
unsigned long ulong
Definition: utilities.hpp:129
void setTplDirPath(winux::String const &dirPath)
设置模板相对目录路径(末尾不是目录分隔符则加上分隔符)
Definition: eientpl.hpp:95
std::basic_string< tchar > String
Definition: utilities.hpp:162