fastdo  0.6.8
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:
55 public:
56  __TemplateLang( Template * tpl );
57 
59  winux::Mixed evalExprVal( winux::SimplePointer<eienexpr::Expression> & expr, winux::String const & exprStr );
60 
62  bool evalExprMixedPtr( winux::SimplePointer<eienexpr::Expression> & expr, winux::String const & exprStr, winux::Mixed ** ppv );
63 
64 };
65 
68 {
69 public:
70  Template( winux::String const & tplDir = "", winux::String const & localEncoding = "", winux::String const & targetEncoding = "" );
71  virtual ~Template();
72 
74  void assign( winux::String const & name, winux::Mixed const & v, bool convertEncoding = true );
78  void assignPtr( winux::String const & name, winux::Mixed * p, bool convertEncoding = true );
80  void clear();
81 
83  winux::String output( winux::String const & tplFile, winux::String const & cacheId = "" );
85  winux::String outputStr( winux::String const & tplStr, winux::String const & cacheId );
86 
88  winux::String _outputInternal( winux::String const & tplFilePath );
89 
93  void setTplDirPath( winux::String const & dirPath ) { this->_tplDirPath = winux::PathWithSep(dirPath); }
95  winux::String const & getTplDirPath() const { return this->_tplDirPath; }
96 
98  void setCache( TemplateCache * cache = NULL ) { this->_cache = cache; }
99 
101  __TemplateLang * getTplLangObj();
103  eienexpr::VarContext * getVarContext() { return &this->_varCtx; }
105  winux::Mixed const & getVar( winux::String const & name ) const;
106 
108  winux::String convFrom( winux::String const & str ) const;
110  winux::String convTo( winux::String const & str ) const;
111 
112  winux::String const & getLocalEncoding() const { return _localEncoding; }
113  winux::String const & getTargetEncoding() const { return _targetEncoding; }
114 private:
115  winux::String _tplDirPath; // 模板目录
116  winux::String _localEncoding; // 本地编码
117  winux::String _targetEncoding; // 目标编码
118  TemplateCache * _cache; // 模板缓存
119  eienexpr::VarContext _varCtx; // 变量场景
120 
121  winux::MembersWrapper<struct Template_Data> _self; // 隐藏的数据
122 
124 };
125 
128 {
129 public:
130  virtual ~TemplateCache() {}
132  virtual bool hasCache( winux::String const & cacheName ) const = 0;
134  virtual bool getCacheName( winux::String * cacheName, winux::String const & tplFile, winux::String const & cacheId = "" ) const = 0;
136  virtual bool readCache( winux::String const & cacheName, winux::String * str ) = 0;
138  virtual winux::String get( winux::String const & cacheName ) const = 0;
140  virtual bool writeCache( winux::String const & cacheName, winux::String const & str ) = 0;
141 
143  virtual void setLifeTime( time_t lifeTime = 0 ) = 0;
145  virtual time_t getLifeTime() const = 0;
146 };
147 
150 {
151 public:
157  TemplateFileCache( time_t lifeTime, winux::String const & cacheDir, winux::String const & cacheExt = ".cah" );
158 
159  virtual bool hasCache( winux::String const & cacheName ) const override;
160  virtual bool getCacheName( winux::String * cacheName, winux::String const & tplFile, winux::String const & cacheId = "" ) const override;
161  virtual bool readCache( winux::String const & cacheName, winux::String * str ) override;
162  virtual winux::String get( winux::String const & cacheName ) const override;
163  virtual bool writeCache( winux::String const & cacheName, winux::String const & str ) override;
164 
165  virtual void setLifeTime( time_t lifeTime = 0 ) override;
166  virtual time_t getLifeTime() const override;
167 
168  void setCacheDirPath( winux::String const & dirPath );
169  winux::String const & getCacheDirPath() const { return this->_cacheDirPath; }
170 
171  void setCacheExt( winux::String const & extName );
172  winux::String const & getCacheExt() const { return this->_cacheExt; }
173 
174 private:
175  time_t _cacheLifeTime; // 缓存生命期 0表示不缓存
176  winux::String _cacheDirPath; // 缓存目录带分隔符
177  winux::String _cacheExt; //缓存文件扩展名 默认为`.cah`
178 };
179 
182 {
183 public:
184  struct CacheItem
185  {
186  time_t timeStamp;
188  };
189 
191  typedef std::map< winux::String, CacheItem > CacheItemsMap;
193  typedef std::map< winux::String, CacheItemsMap > CacheStruct;
194 
199  TemplateMemoryCache( time_t lifeTime, CacheItemsMap * cacheItems );
200 
201  virtual bool hasCache( winux::String const & cacheName ) const override;
202  virtual bool getCacheName( winux::String * cacheName, winux::String const & tplFile, winux::String const & cacheId = "" ) const override;
203  virtual bool readCache( winux::String const & cacheName, winux::String * str ) override;
204  virtual winux::String get( winux::String const & cacheName ) const override;
205  virtual bool writeCache( winux::String const & cacheName, winux::String const & str ) override;
206 
207  virtual void setLifeTime( time_t lifeTime = 0 ) override;
208  virtual time_t getLifeTime() const override;
209 
210 private:
211  time_t _cacheLifeTime; // 缓存生命期 0表示不缓存
212  CacheItemsMap * _cacheItems;
213 };
214 
215 } // namespace eientpl
216 
217 #endif // __EIENTPL_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
String PathWithSep(String const &path)
路径分隔符整理
winux::String const & getTplDirPath() const
取得模板相对目录路径
Definition: eientpl.hpp:95
TplError(int errNo, winux::AnsiString const &err)
Definition: eientpl.hpp:41
winux::StringArray _loadFilesChains
防止load无穷递归,记录载入文件链
Definition: eientpl.hpp:54
模板缓存基类
Definition: eientpl.hpp:127
XString< tchar > String
Definition: utilities.hpp:216
eienexpr::ExprPackage _exprPack
表达式包
Definition: eientpl.hpp:52
模板-文件式缓存
Definition: eientpl.hpp:149
eienml::ElemPackage _elemPack
模板语言元素包
Definition: eientpl.hpp:53
eienexpr::VarContext * getVarContext()
访问内部变量场景对象,以便直接操作变量
Definition: eientpl.hpp:103
void setCache(TemplateCache *cache=NULL)
设置缓存机制是否开启,NULL为关闭
Definition: eientpl.hpp:98
#define EIENTPL_DLL
Definition: eientpl.hpp:26
winux::String const & getCacheDirPath() const
Definition: eientpl.hpp:169
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
winux::String const & getCacheExt() const
Definition: eientpl.hpp:172
模板类
Definition: eientpl.hpp:67
模板-内存式缓存
Definition: eientpl.hpp:181
winux::String const & getLocalEncoding() const
Definition: eientpl.hpp:112
winux::String const & getTargetEncoding() const
Definition: eientpl.hpp:113
模板错误
Definition: eientpl.hpp:34
模板语言类
Definition: eientpl.hpp:48
模板引擎
Definition: eientpl.hpp:9
std::map< winux::String, CacheItemsMap > CacheStruct
map< webappname, cache_items >
Definition: eientpl.hpp:193
Template * _tpl
指向一个模板对象
Definition: eientpl.hpp:51
变量场景类
Definition: eienexpr.hpp:344
virtual ~TemplateCache()
Definition: eientpl.hpp:130
混合体,能表示多种类型的值
Definition: utilities.hpp:750
简单指针
Definition: smartptr.hpp:235
错误类
Definition: utilities.hpp:505
std::map< winux::String, CacheItem > CacheItemsMap
map< name, cache_item >
Definition: eientpl.hpp:191
void setTplDirPath(winux::String const &dirPath)
设置模板相对目录路径(末尾不是目录分隔符则加上分隔符)
Definition: eientpl.hpp:93
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
表达式包(包含支持的算符和函数)
Definition: eienexpr.hpp:414