My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
template.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2015 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15 
16 #ifndef TEMPLATE_H
17 #define TEMPLATE_H
18 
19 #include <qcstring.h>
20 #include <qvaluelist.h>
21 
22 class FTextStream;
23 
24 class TemplateListIntf;
25 class TemplateStructIntf;
26 class TemplateEngine;
27 
91 {
92  public:
94  class Delegate
95  {
96  public:
98  typedef TemplateVariant (*StubType)(const void *obj, const QValueList<TemplateVariant> &args);
99 
101 
103  template <class T, TemplateVariant (T::*TMethod)(const QValueList<TemplateVariant> &) const>
104  static Delegate fromMethod(const T* objectPtr)
105  {
106  Delegate d;
107  d.m_objectPtr = objectPtr;
108  d.m_stubPtr = &methodStub<T, TMethod>;
109  return d;
110  }
112  static Delegate fromFunction(const void *obj,StubType func)
113  {
114  Delegate d;
115  d.m_objectPtr = obj;
116  d.m_stubPtr = func;
117  return d;
118  }
119 
121  TemplateVariant operator()(const QValueList<TemplateVariant> &args) const
122  {
123  return (*m_stubPtr)(m_objectPtr, args);
124  }
125 
126  private:
127  const void* m_objectPtr;
129 
130  template <class T, TemplateVariant (T::*TMethod)(const QValueList<TemplateVariant> &) const>
131  static TemplateVariant methodStub(const void* objectPtr, const QValueList<TemplateVariant> &args)
132  {
133  T* p = (T*)(objectPtr);
134  return (p->*TMethod)(args);
135  }
136  };
137 
140 
142  Type type() const { return m_type; }
143 
145  QCString typeAsString() const
146  {
147  switch (m_type)
148  {
149  case None: return "none";
150  case Bool: return "bool";
151  case Integer: return "integer";
152  case String: return "string";
153  case Struct: return "struct";
154  case List: return "list";
155  case Function: return "function";
156  }
157  return "invalid";
158  }
159 
161  bool isValid() const { return m_type!=None; }
162 
165 
167  explicit TemplateVariant(bool b) : m_type(Bool), m_boolVal(b), m_raw(FALSE) {}
168 
170  TemplateVariant(int v) : m_type(Integer), m_intVal(v), m_raw(FALSE) {}
171 
173  TemplateVariant(const char *s,bool raw=FALSE) : m_type(String), m_strVal(s), m_strukt(0), m_raw(raw) {}
174 
176  TemplateVariant(const QCString &s,bool raw=FALSE) : m_type(String), m_strVal(s), m_strukt(0), m_raw(raw) {}
177 
182 
187 
195  TemplateVariant(const Delegate &delegate) : m_type(Function), m_strukt(0), m_delegate(delegate), m_raw(FALSE) {}
196 
199 
204 
207 
212  {
213  if (m_type==None)
214  {
215  return FALSE;
216  }
218  {
219  return m_list==other.m_list; // TODO: improve me
220  }
222  {
223  return m_strukt==other.m_strukt; // TODO: improve me
224  }
225  else
226  {
227  return toString()==other.toString();
228  }
229  }
230 
232  QCString toString() const
233  {
234  switch (m_type)
235  {
236  case None: return QCString();
237  case Bool: return m_boolVal ? "true" : "false";
238  case Integer: return QCString().setNum(m_intVal);
239  case String: return m_strVal;
240  case Struct: return "[struct]";
241  case List: return "[list]";
242  case Function: return "[function]";
243  }
244  return QCString();
245  }
246 
248  bool toBool() const;
249 
251  int toInt() const;
252 
257  {
258  return m_type==List ? m_list : 0;
259  }
260 
265  {
266  return m_type==Struct ? m_strukt : 0;
267  }
268 
272  TemplateVariant call(const QValueList<TemplateVariant> &args)
273  {
274  if (m_type==Function) return m_delegate(args);
275  return TemplateVariant();
276  }
277 
282  void setRaw(bool b) { m_raw = b; }
283 
287  bool raw() const { return m_raw; }
288 
289  private:
291  QCString m_strVal;
292  union
293  {
294  int m_intVal;
295  bool m_boolVal;
298  };
300  bool m_raw;
301 };
302 
303 //------------------------------------------------------------------------
304 
305 template<class T> class TemplateAutoRef
306 {
307  public:
308  TemplateAutoRef(T *obj) : m_obj(obj)
309  {
310  m_obj->addRef();
311  }
313  {
314  m_obj->release();
315  }
316  T &operator*() const { return *m_obj; }
317  T *operator->() const { return m_obj; }
318  T *get() const { return m_obj; }
319 
320  private:
321  T *m_obj;
322 };
323 
324 //------------------------------------------------------------------------
325 
330 {
331  public:
334  {
335  public:
337  virtual ~ConstIterator() {}
339  virtual void toFirst() = 0;
341  virtual void toLast() = 0;
343  virtual void toNext() = 0;
345  virtual void toPrev() = 0;
346  /* Returns TRUE if the iterator points to a valid element
347  * in the list, or FALSE otherwise.
348  * If TRUE is returned, the value pointed to be the
349  * iterator is assigned to \a v.
350  */
351  virtual bool current(TemplateVariant &v) const = 0;
352  };
353 
355  virtual ~TemplateListIntf() {}
356 
358  virtual int count() const = 0;
359 
361  virtual TemplateVariant at(int index) const = 0;
362 
366  virtual TemplateListIntf::ConstIterator *createIterator() const = 0;
367 
369  virtual int addRef() = 0;
370 
372  virtual int release() = 0;
373 };
374 
377 {
378  public:
379  // TemplateListIntf methods
380  virtual int count() const;
381  virtual TemplateVariant at(int index) const;
383  virtual int addRef();
384  virtual int release();
385 
387  static TemplateList *alloc();
388 
390  virtual void append(const TemplateVariant &v);
391 
392  private:
394  TemplateList();
396  ~TemplateList();
397 
399  class Private;
400  Private *p;
401 };
402 
403 //------------------------------------------------------------------------
404 
407 {
408  public:
410  virtual ~TemplateStructIntf() {}
411 
415  virtual TemplateVariant get(const char *name) const = 0;
416 
418  virtual int addRef() = 0;
419 
421  virtual int release() = 0;
422 };
423 
424 
427 {
428  public:
429  // TemplateStructIntf methods
430  virtual TemplateVariant get(const char *name) const;
431  virtual int addRef();
432  virtual int release();
433 
435  static TemplateStruct *alloc();
436 
441  virtual void set(const char *name,const TemplateVariant &v);
442 
443 
444  private:
446  TemplateStruct();
448  virtual ~TemplateStruct();
449 
450  class Private;
451  Private *p;
452 };
453 
454 //------------------------------------------------------------------------
455 
458 {
459  public:
461  virtual QCString escape(const QCString &input) = 0;
463  virtual void enableTabbing(bool b) = 0;
464 };
465 
466 //------------------------------------------------------------------------
467 
470 {
471  public:
473  virtual QCString remove(const QCString &input) = 0;
475  virtual void reset() = 0;
476 };
477 
478 //------------------------------------------------------------------------
479 
490 {
491  public:
492  virtual ~TemplateContext() {}
493 
495  virtual void push() = 0;
496 
498  virtual void pop() = 0;
499 
506  virtual void set(const char *name,const TemplateVariant &v) = 0;
507 
513  virtual TemplateVariant get(const QCString &name) const = 0;
514 
519  virtual const TemplateVariant *getRef(const QCString &name) const = 0;
520 
524  virtual void setOutputDirectory(const QCString &dir) = 0;
525 
529  virtual void setEscapeIntf(const QCString &extension, TemplateEscapeIntf *intf) = 0;
530 
534  virtual void setSpacelessIntf(TemplateSpacelessIntf *intf) = 0;
535 };
536 
537 //------------------------------------------------------------------------
538 
542 class Template
543 {
544  public:
546  virtual ~Template() {}
547 
553  virtual void render(FTextStream &ts,TemplateContext *c) = 0;
554 };
555 
556 //------------------------------------------------------------------------
557 
560 {
561  public:
563  TemplateEngine();
564 
566  ~TemplateEngine();
567 
572 
576  void destroyContext(TemplateContext *ctx);
577 
583  Template *loadByName(const QCString &fileName,int fromLine);
584 
588  void unload(Template *t);
589 
591  void printIncludeContext(const char *fileName,int line) const;
592 
594  void setTemplateDir(const char *dirName);
595 
596  private:
597  friend class TemplateNodeBlock;
598  friend class TemplateNodeCreate;
599 
600  void enterBlock(const QCString &fileName,const QCString &blockName,int line);
601  void leaveBlock();
602 
606  void setOutputExtension(const char *extension);
607 
609  QCString outputExtension() const;
610 
611  class Private;
612  Private *p;
613 };
614 
617 #endif