ISLEman
template.h
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 
100  Delegate() : m_objectPtr(0) , m_stubPtr(0) {}
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 
122  {
123  return (*m_stubPtr)(m_objectPtr, args);
124  }
125 
126  private:
127  const void* m_objectPtr;
128  StubType m_stubPtr;
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 
139  enum Type { None, Bool, Integer, String, Struct, List, Function };
140 
142  Type type() const { return m_type; }
143 
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 
164  TemplateVariant() : m_type(None), m_strukt(0), m_raw(FALSE) {}
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  }
217  if (m_type==TemplateVariant::List && other.m_type==TemplateVariant::List)
218  {
219  return m_list==other.m_list; // TODO: improve me
220  }
221  else if (m_type==TemplateVariant::Struct && other.m_type==TemplateVariant::Struct)
222  {
223  return m_strukt==other.m_strukt; // TODO: improve me
224  }
225  else
226  {
227  return toString()==other.toString();
228  }
229  }
230 
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 
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:
290  Type m_type;
291  QCString m_strVal;
292  union
293  {
294  int m_intVal;
295  bool m_boolVal;
296  TemplateStructIntf *m_strukt;
297  TemplateListIntf *m_list;
298  };
299  Delegate m_delegate;
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  }
312  ~TemplateAutoRef()
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;
382  virtual TemplateListIntf::ConstIterator *createIterator() 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 
398  friend class TemplateListConstIterator;
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 
571  TemplateContext *createContext() const;
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
Abstract interface for a template context.
Definition: template.h:489
Class representing an &#39;create&#39; tag in a template.
Definition: template.cpp:3618
TemplateListIntf * toList() const
Returns the pointer to list referenced by this variant or 0 if this variant does not have list type...
Definition: template.h:256
Abstract interface for a template.
Definition: template.h:542
TemplateVariant operator()(const QValueList< TemplateVariant > &args) const
Invokes the function/method stored in the delegate.
Definition: template.h:121
~TemplateVariant()
Destroys the Variant object.
Definition: template.cpp:159
TemplateVariant(* StubType)(const void *obj, const QValueList< TemplateVariant > &args)
Callback type to use when creating a delegate from a function.
Definition: template.h:98
TemplateVariant(int v)
Constructs a new variant with a integer value v.
Definition: template.h:170
static Delegate fromMethod(const T *objectPtr)
Creates a delegate given an object.
Definition: template.h:104
TemplateStructIntf * toStruct() const
Returns the pointer to struct referenced by this variant or 0 if this variant does not have struct ty...
Definition: template.h:264
bool raw() const
Returns whether or not the value of the Value is raw.
Definition: template.h:287
Default implementation of a context value of type list.
Definition: template.h:376
TemplateVariant call(const QValueList< TemplateVariant > &args)
Return the result of apply this function with args.
Definition: template.h:272
Simplified and optimized version of QTextStream.
Definition: ftextstream.h:11
Abstract interface for a context value of type struct.
Definition: template.h:406
static Delegate fromFunction(const void *obj, StubType func)
Creates a delegate given an object, and a plain function.
Definition: template.h:112
Private data of the template engine.
Definition: template.cpp:5059
Class representing a &#39;block&#39; tag in a template.
Definition: template.cpp:3381
TemplateVariant & operator=(const TemplateVariant &v)
Assigns the value of the variant v to this variant.
Definition: template.cpp:181
bool operator==(TemplateVariant &other)
Compares this QVariant with v and returns true if they are equal; otherwise returns false...
Definition: template.h:211
Definition: template.h:305
Helper class to create a delegate that can store a function/method call.
Definition: template.h:94
Private data of a template list object.
Definition: template.cpp:304
QCString toString() const
Returns the variant as a string.
Definition: template.h:232
virtual ~ConstIterator()
Destructor for the iterator.
Definition: template.h:337
virtual ~TemplateListIntf()
Destroys the list.
Definition: template.h:355
virtual ~TemplateStructIntf()
Destroys the struct.
Definition: template.h:410
TemplateVariant(const Delegate &delegate)
Constructs a new variant which represents a method call.
Definition: template.h:195
Abstract interface for a iterator of a list.
Definition: template.h:333
Interface used to escape characters in a string.
Definition: template.h:457
TemplateVariant()
Constructs an invalid variant.
Definition: template.h:164
bool toBool() const
Returns the variant as a boolean.
Definition: template.cpp:208
Variant type which can hold one value of a fixed set of types.
Definition: template.h:90
Private data of a template struct object.
Definition: template.cpp:242
Engine to create templates and template contexts.
Definition: template.h:559
void setRaw(bool b)
Sets whether or not the value of the Variant should be escaped or written as-is (raw).
Definition: template.h:282
int toInt() const
Returns the variant as an integer.
Definition: template.cpp:223
QCString typeAsString() const
Return a string representation of the type of the value stored in the variant.
Definition: template.h:145
TemplateVariant(bool b)
Constructs a new variant with a boolean value b.
Definition: template.h:167
virtual ~Template()
Destructor.
Definition: template.h:546
TemplateVariant(const char *s, bool raw=FALSE)
Constructs a new variant with a string value s.
Definition: template.h:173
Type
Types of data that can be stored in a TemplateVariant.
Definition: template.h:139
Type type() const
Returns the type of the value stored in the variant.
Definition: template.h:142
Abstract read-only interface for a context value of type list.
Definition: template.h:329
bool isValid() const
Returns TRUE if the variant holds a valid value, or FALSE otherwise.
Definition: template.h:161
This is an alternative implementation of QCString.
Definition: qcstring.h:131
Default implementation of a context value of type struct.
Definition: template.h:426
Definition: template.cpp:350
TemplateVariant(const QCString &s, bool raw=FALSE)
Constructs a new variant with a string value s.
Definition: template.h:176
Interface used to remove redundant spaces inside a spaceless block.
Definition: template.h:469