Blame src/template.h

Packit 1c1d7e
/******************************************************************************
Packit 1c1d7e
 *
Packit 1c1d7e
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
Packit 1c1d7e
 *
Packit 1c1d7e
 * Permission to use, copy, modify, and distribute this software and its
Packit 1c1d7e
 * documentation under the terms of the GNU General Public License is hereby
Packit 1c1d7e
 * granted. No representations are made about the suitability of this software
Packit 1c1d7e
 * for any purpose. It is provided "as is" without express or implied warranty.
Packit 1c1d7e
 * See the GNU General Public License for more details.
Packit 1c1d7e
 *
Packit 1c1d7e
 * Documents produced by Doxygen are derivative works derived from the
Packit 1c1d7e
 * input used in their production; they are not affected by this license.
Packit 1c1d7e
 *
Packit 1c1d7e
 */
Packit 1c1d7e
Packit 1c1d7e
#ifndef TEMPLATE_H
Packit 1c1d7e
#define TEMPLATE_H
Packit 1c1d7e
Packit 1c1d7e
#include <qcstring.h>
Packit 1c1d7e
#include <qvaluelist.h>
Packit 1c1d7e
Packit 1c1d7e
class FTextStream;
Packit 1c1d7e
Packit 1c1d7e
class TemplateListIntf;
Packit 1c1d7e
class TemplateStructIntf;
Packit 1c1d7e
class TemplateEngine;
Packit 1c1d7e
Packit 1c1d7e
/** @defgroup template_api Template API
Packit 1c1d7e
 *
Packit 1c1d7e
 *  This is the API for a
Packit 1c1d7e
 *  Django
Packit 1c1d7e
 *  compatible template system written in C++.
Packit 1c1d7e
 *  It is somewhat inspired by Stephen Kelly's
Packit 1c1d7e
 *  Grantlee.
Packit 1c1d7e
 *
Packit 1c1d7e
 *  A template is simply a text file.
Packit 1c1d7e
 *  A template contains \b variables, which get replaced with values when the
Packit 1c1d7e
 *  template is evaluated, and \b tags, which control the logic of the template.
Packit 1c1d7e
 *
Packit 1c1d7e
 *  Variables look like this: `{{ variable }}`
Packit 1c1d7e
 *  When the template engine encounters a variable, it evaluates that variable and
Packit 1c1d7e
 *  replaces it with the result. Variable names consist of any combination of
Packit 1c1d7e
 *  alphanumeric characters and the underscore ("_").
Packit 1c1d7e
 *  Use a dot (.) to access attributes of a structured variable.
Packit 1c1d7e
 *
Packit 1c1d7e
 *  One can modify variables for display by using \b filters, for example:
Packit 1c1d7e
 *  `{{ value|default:"nothing" }}`
Packit 1c1d7e
 *
Packit 1c1d7e
 *  Tags look like this: `{% tag %}`. Tags are more complex than variables:
Packit 1c1d7e
 *  Some create text in the output, some control flow by performing loops or logic,
Packit 1c1d7e
 *  and some load external information into the template to be used by later variables.
Packit 1c1d7e
 *
Packit 1c1d7e
 *  To comment-out part of a line in a template, use the comment syntax:
Packit 1c1d7e
 *  `{# comment text #}`.
Packit 1c1d7e
 *
Packit 1c1d7e
 *  Supported Django tags:
Packit 1c1d7e
 *  - `for ... empty ... endfor`
Packit 1c1d7e
 *  - `if ... else ... endif`
Packit 1c1d7e
 *  - `block ... endblock`
Packit 1c1d7e
 *  - `extend`
Packit 1c1d7e
 *  - `include`
Packit 1c1d7e
 *  - `with ... endwith`
Packit 1c1d7e
 *  - `spaceless ... endspaceless`
Packit 1c1d7e
 *  - `cycle`
Packit 1c1d7e
 *
Packit 1c1d7e
 *  Extension tags:
Packit 1c1d7e
 *  - `create` which instantiates a template and writes the result to a file.
Packit 1c1d7e
 *     The syntax is `{% create 'filename' from 'template' %}`.
Packit 1c1d7e
 *  - `recursetree`
Packit 1c1d7e
 *  - `markers`
Packit 1c1d7e
 *  - `msg` ... `endmsg`
Packit 1c1d7e
 *  - `set`
Packit 1c1d7e
 *
Packit 1c1d7e
 *  Supported Django filters:
Packit 1c1d7e
 *  - `default`
Packit 1c1d7e
 *  - `length`
Packit 1c1d7e
 *  - `add`
Packit 1c1d7e
 *  - `divisibleby`
Packit 1c1d7e
 *
Packit 1c1d7e
 *  Extension filters:
Packit 1c1d7e
 *  - `stripPath`
Packit 1c1d7e
 *  - `nowrap`
Packit 1c1d7e
 *  - `prepend`
Packit 1c1d7e
 *  - `append`
Packit 1c1d7e
 *
Packit 1c1d7e
 *  @{
Packit 1c1d7e
 */
Packit 1c1d7e
Packit 1c1d7e
/** @brief Variant type which can hold one value of a fixed set of types. */
Packit 1c1d7e
class TemplateVariant
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    /** @brief Helper class to create a delegate that can store a function/method call. */
Packit 1c1d7e
    class Delegate
Packit 1c1d7e
    {
Packit 1c1d7e
      public:
Packit 1c1d7e
        /** Callback type to use when creating a delegate from a function. */
Packit 1c1d7e
        typedef TemplateVariant (*StubType)(const void *obj, const QValueList<TemplateVariant> &args);
Packit 1c1d7e
Packit 1c1d7e
        Delegate() : m_objectPtr(0) , m_stubPtr(0) {}
Packit 1c1d7e
Packit 1c1d7e
        /** Creates a delegate given an object. The method to call is passed as a template parameter */
Packit 1c1d7e
        template <class T, TemplateVariant (T::*TMethod)(const QValueList<TemplateVariant> &) const>
Packit 1c1d7e
        static Delegate fromMethod(const T* objectPtr)
Packit 1c1d7e
        {
Packit 1c1d7e
          Delegate d;
Packit 1c1d7e
          d.m_objectPtr = objectPtr;
Packit 1c1d7e
          d.m_stubPtr   = &methodStub<T, TMethod>;
Packit 1c1d7e
          return d;
Packit 1c1d7e
        }
Packit 1c1d7e
        /** Creates a delegate given an object, and a plain function. */
Packit 1c1d7e
        static Delegate fromFunction(const void *obj,StubType func)
Packit 1c1d7e
        {
Packit 1c1d7e
          Delegate d;
Packit 1c1d7e
          d.m_objectPtr = obj;
Packit 1c1d7e
          d.m_stubPtr = func;
Packit 1c1d7e
          return d;
Packit 1c1d7e
        }
Packit 1c1d7e
Packit 1c1d7e
        /** Invokes the function/method stored in the delegate */
Packit 1c1d7e
        TemplateVariant operator()(const QValueList<TemplateVariant> &args) const
Packit 1c1d7e
        {
Packit 1c1d7e
          return (*m_stubPtr)(m_objectPtr, args);
Packit 1c1d7e
        }
Packit 1c1d7e
Packit 1c1d7e
      private:
Packit 1c1d7e
        const void* m_objectPtr;
Packit 1c1d7e
        StubType    m_stubPtr;
Packit 1c1d7e
Packit 1c1d7e
        template <class T, TemplateVariant (T::*TMethod)(const QValueList<TemplateVariant> &) const>
Packit 1c1d7e
        static TemplateVariant methodStub(const void* objectPtr, const QValueList<TemplateVariant> &args)
Packit 1c1d7e
        {
Packit 1c1d7e
          T* p = (T*)(objectPtr);
Packit 1c1d7e
          return (p->*TMethod)(args);
Packit 1c1d7e
        }
Packit 1c1d7e
    };
Packit 1c1d7e
Packit 1c1d7e
    /** Types of data that can be stored in a TemplateVariant */
Packit 1c1d7e
    enum Type { None, Bool, Integer, String, Struct, List, Function };
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the type of the value stored in the variant */
Packit 1c1d7e
    Type type() const { return m_type; }
Packit 1c1d7e
Packit 1c1d7e
    /** Return a string representation of the type of the value stored in the variant */
Packit 1c1d7e
    QCString typeAsString() const
Packit 1c1d7e
    {
Packit 1c1d7e
      switch (m_type)
Packit 1c1d7e
      {
Packit 1c1d7e
        case None:     return "none";
Packit 1c1d7e
        case Bool:     return "bool";
Packit 1c1d7e
        case Integer:  return "integer";
Packit 1c1d7e
        case String:   return "string";
Packit 1c1d7e
        case Struct:   return "struct";
Packit 1c1d7e
        case List:     return "list";
Packit 1c1d7e
        case Function: return "function";
Packit 1c1d7e
      }
Packit 1c1d7e
      return "invalid";
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    /** Returns TRUE if the variant holds a valid value, or FALSE otherwise */
Packit 1c1d7e
    bool isValid() const { return m_type!=None; }
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs an invalid variant. */
Packit 1c1d7e
    TemplateVariant() : m_type(None), m_strukt(0), m_raw(FALSE) {}
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a new variant with a boolean value \a b. */
Packit 1c1d7e
    explicit TemplateVariant(bool b) : m_type(Bool), m_boolVal(b), m_raw(FALSE) {}
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a new variant with a integer value \a v. */
Packit 1c1d7e
    TemplateVariant(int v) : m_type(Integer), m_intVal(v), m_raw(FALSE) {}
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a new variant with a string value \a s. */
Packit 1c1d7e
    TemplateVariant(const char *s,bool raw=FALSE) : m_type(String), m_strVal(s), m_strukt(0), m_raw(raw) {}
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a new variant with a string value \a s. */
Packit 1c1d7e
    TemplateVariant(const QCString &s,bool raw=FALSE) : m_type(String), m_strVal(s), m_strukt(0), m_raw(raw) {}
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a new variant with a struct value \a s.
Packit 1c1d7e
     *  @note. The variant will hold a reference to the object.
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateVariant(TemplateStructIntf *s);
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a new variant with a list value \a l.
Packit 1c1d7e
     *  @note. The variant will hold a reference to the object.
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateVariant(TemplateListIntf *l);
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a new variant which represents a method call
Packit 1c1d7e
     *  @param[in] delegate Delegate object to invoke when
Packit 1c1d7e
     *             calling call() on this variant.
Packit 1c1d7e
     *  @note Use TemplateVariant::Delegate::fromMethod() and
Packit 1c1d7e
     *  TemplateVariant::Delegate::fromFunction() to create
Packit 1c1d7e
     *  Delegate objects.
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateVariant(const Delegate &delegate) : m_type(Function), m_strukt(0), m_delegate(delegate), m_raw(FALSE) {}
Packit 1c1d7e
Packit 1c1d7e
    /** Destroys the Variant object */
Packit 1c1d7e
    ~TemplateVariant();
Packit 1c1d7e
Packit 1c1d7e
    /** Constructs a copy of the variant, \a v,
Packit 1c1d7e
     *  passed as the argument to this constructor.
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateVariant(const TemplateVariant &v);
Packit 1c1d7e
Packit 1c1d7e
    /** Assigns the value of the variant \a v to this variant. */
Packit 1c1d7e
    TemplateVariant &operator=(const TemplateVariant &v);
Packit 1c1d7e
Packit 1c1d7e
    /** Compares this QVariant with v and returns true if they are equal;
Packit 1c1d7e
     *  otherwise returns false.
Packit 1c1d7e
     */
Packit 1c1d7e
    bool operator==(TemplateVariant &other)
Packit 1c1d7e
    {
Packit 1c1d7e
      if (m_type==None)
Packit 1c1d7e
      {
Packit 1c1d7e
        return FALSE;
Packit 1c1d7e
      }
Packit 1c1d7e
      if (m_type==TemplateVariant::List && other.m_type==TemplateVariant::List)
Packit 1c1d7e
      {
Packit 1c1d7e
        return m_list==other.m_list; // TODO: improve me
Packit 1c1d7e
      }
Packit 1c1d7e
      else if (m_type==TemplateVariant::Struct && other.m_type==TemplateVariant::Struct)
Packit 1c1d7e
      {
Packit 1c1d7e
        return m_strukt==other.m_strukt; // TODO: improve me
Packit 1c1d7e
      }
Packit 1c1d7e
      else
Packit 1c1d7e
      {
Packit 1c1d7e
        return toString()==other.toString();
Packit 1c1d7e
      }
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the variant as a string. */
Packit 1c1d7e
    QCString toString() const
Packit 1c1d7e
    {
Packit 1c1d7e
      switch (m_type)
Packit 1c1d7e
      {
Packit 1c1d7e
        case None:     return QCString();
Packit 1c1d7e
        case Bool:     return m_boolVal ? "true" : "false";
Packit 1c1d7e
        case Integer:  return QCString().setNum(m_intVal);
Packit 1c1d7e
        case String:   return m_strVal;
Packit 1c1d7e
        case Struct:   return "[struct]";
Packit 1c1d7e
        case List:     return "[list]";
Packit 1c1d7e
        case Function: return "[function]";
Packit 1c1d7e
      }
Packit 1c1d7e
      return QCString();
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the variant as a boolean. */
Packit 1c1d7e
    bool toBool() const;
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the variant as an integer. */
Packit 1c1d7e
    int toInt() const;
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the pointer to list referenced by this variant
Packit 1c1d7e
     *  or 0 if this variant does not have list type.
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateListIntf   *toList() const
Packit 1c1d7e
    {
Packit 1c1d7e
      return m_type==List ? m_list : 0;
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the pointer to struct referenced by this variant
Packit 1c1d7e
     *  or 0 if this variant does not have struct type.
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateStructIntf *toStruct() const
Packit 1c1d7e
    {
Packit 1c1d7e
      return m_type==Struct ? m_strukt : 0;
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    /** Return the result of apply this function with \a args.
Packit 1c1d7e
     *  Returns an empty string if the variant type is not a function.
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateVariant call(const QValueList<TemplateVariant> &args)
Packit 1c1d7e
    {
Packit 1c1d7e
      if (m_type==Function) return m_delegate(args);
Packit 1c1d7e
      return TemplateVariant();
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    /** Sets whether or not the value of the Variant should be
Packit 1c1d7e
     *  escaped or written as-is (raw).
Packit 1c1d7e
     *  @param[in] b TRUE means write as-is, FALSE means apply escaping.
Packit 1c1d7e
     */
Packit 1c1d7e
    void setRaw(bool b) { m_raw = b; }
Packit 1c1d7e
Packit 1c1d7e
    /** Returns whether or not the value of the Value is raw.
Packit 1c1d7e
     *  @see setRaw()
Packit 1c1d7e
     */
Packit 1c1d7e
    bool raw() const { return m_raw; }
Packit 1c1d7e
Packit 1c1d7e
  private:
Packit 1c1d7e
    Type                  m_type;
Packit 1c1d7e
    QCString              m_strVal;
Packit 1c1d7e
    union
Packit 1c1d7e
    {
Packit 1c1d7e
      int                 m_intVal;
Packit 1c1d7e
      bool                m_boolVal;
Packit 1c1d7e
      TemplateStructIntf *m_strukt;
Packit 1c1d7e
      TemplateListIntf   *m_list;
Packit 1c1d7e
    };
Packit 1c1d7e
    Delegate              m_delegate;
Packit 1c1d7e
    bool                  m_raw;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
template<class T> class TemplateAutoRef
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    TemplateAutoRef(T *obj) : m_obj(obj)
Packit 1c1d7e
    {
Packit 1c1d7e
      m_obj->addRef();
Packit 1c1d7e
    }
Packit 1c1d7e
   ~TemplateAutoRef()
Packit 1c1d7e
    {
Packit 1c1d7e
      m_obj->release();
Packit 1c1d7e
    }
Packit 1c1d7e
    T &operator*() const { return *m_obj; }
Packit 1c1d7e
    T *operator->() const { return m_obj; }
Packit 1c1d7e
    T *get() const { return m_obj; }
Packit 1c1d7e
Packit 1c1d7e
  private:
Packit 1c1d7e
   T *m_obj;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
/** @brief Abstract read-only interface for a context value of type list.
Packit 1c1d7e
 *  @note The values of the list are TemplateVariants.
Packit 1c1d7e
 */
Packit 1c1d7e
class TemplateListIntf
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    /** @brief Abstract interface for a iterator of a list. */
Packit 1c1d7e
    class ConstIterator
Packit 1c1d7e
    {
Packit 1c1d7e
      public:
Packit 1c1d7e
        /** Destructor for the iterator */
Packit 1c1d7e
        virtual ~ConstIterator() {}
Packit 1c1d7e
        /** Moves iterator to the first element in the list */
Packit 1c1d7e
        virtual void toFirst() = 0;
Packit 1c1d7e
        /** Moves iterator to the last element in the list */
Packit 1c1d7e
        virtual void toLast() = 0;
Packit 1c1d7e
        /** Moves iterator to the next element in the list */
Packit 1c1d7e
        virtual void toNext() = 0;
Packit 1c1d7e
        /** Moves iterator to the previous element in the list */
Packit 1c1d7e
        virtual void toPrev() = 0;
Packit 1c1d7e
        /* Returns TRUE if the iterator points to a valid element
Packit 1c1d7e
         * in the list, or FALSE otherwise.
Packit 1c1d7e
         * If TRUE is returned, the value pointed to be the
Packit 1c1d7e
         * iterator is assigned to \a v.
Packit 1c1d7e
         */
Packit 1c1d7e
        virtual bool current(TemplateVariant &v) const = 0;
Packit 1c1d7e
    };
Packit 1c1d7e
Packit 1c1d7e
    /** Destroys the list */
Packit 1c1d7e
    virtual ~TemplateListIntf() {}
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the number of elements in the list */
Packit 1c1d7e
    virtual int count() const = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the element at index position \a index. */
Packit 1c1d7e
    virtual TemplateVariant  at(int index) const = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Creates a new iterator for this list.
Packit 1c1d7e
     *  @note the user should call delete on the returned pointer.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual TemplateListIntf::ConstIterator *createIterator() const = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Increase object's reference count */
Packit 1c1d7e
    virtual int addRef() = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Decreases object's referenc count, destroy object if 0 */
Packit 1c1d7e
    virtual int release() = 0;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
/** @brief Default implementation of a context value of type list. */
Packit 1c1d7e
class TemplateList : public TemplateListIntf
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    // TemplateListIntf methods
Packit 1c1d7e
    virtual int  count() const;
Packit 1c1d7e
    virtual TemplateVariant at(int index) const;
Packit 1c1d7e
    virtual TemplateListIntf::ConstIterator *createIterator() const;
Packit 1c1d7e
    virtual int addRef();
Packit 1c1d7e
    virtual int release();
Packit 1c1d7e
Packit 1c1d7e
    /** Creates an instance with ref count set to 0 */
Packit 1c1d7e
    static TemplateList *alloc();
Packit 1c1d7e
Packit 1c1d7e
    /** Appends element \a v to the end of the list */
Packit 1c1d7e
    virtual void append(const TemplateVariant &v);
Packit 1c1d7e
Packit 1c1d7e
  private:
Packit 1c1d7e
    /** Creates a list */
Packit 1c1d7e
    TemplateList();
Packit 1c1d7e
    /** Destroys the list */
Packit 1c1d7e
   ~TemplateList();
Packit 1c1d7e
Packit 1c1d7e
    friend class TemplateListConstIterator;
Packit 1c1d7e
    class Private;
Packit 1c1d7e
    Private *p;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
/** @brief Abstract interface for a context value of type struct. */
Packit 1c1d7e
class TemplateStructIntf
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    /** Destroys the struct */
Packit 1c1d7e
    virtual ~TemplateStructIntf() {}
Packit 1c1d7e
Packit 1c1d7e
    /** Gets the value for a field name.
Packit 1c1d7e
     *  @param[in] name The name of the field.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual TemplateVariant get(const char *name) const = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Increase object's reference count */
Packit 1c1d7e
    virtual int addRef() = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Decreases object's referenc count, destroy object if 0 */
Packit 1c1d7e
    virtual int release() = 0;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/** @brief Default implementation of a context value of type struct. */
Packit 1c1d7e
class TemplateStruct : public TemplateStructIntf
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    // TemplateStructIntf methods
Packit 1c1d7e
    virtual TemplateVariant get(const char *name) const;
Packit 1c1d7e
    virtual int addRef();
Packit 1c1d7e
    virtual int release();
Packit 1c1d7e
Packit 1c1d7e
    /** Creates an instance with ref count set to 0. */
Packit 1c1d7e
    static TemplateStruct *alloc();
Packit 1c1d7e
Packit 1c1d7e
    /** Sets the value the field of a struct
Packit 1c1d7e
     *  @param[in] name The name of the field.
Packit 1c1d7e
     *  @param[in] v The value to set.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual void set(const char *name,const TemplateVariant &v);
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
  private:
Packit 1c1d7e
    /** Creates a struct */
Packit 1c1d7e
    TemplateStruct();
Packit 1c1d7e
    /** Destroys the struct */
Packit 1c1d7e
    virtual ~TemplateStruct();
Packit 1c1d7e
Packit 1c1d7e
    class Private;
Packit 1c1d7e
    Private *p;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
/** @brief Interface used to escape characters in a string */
Packit 1c1d7e
class TemplateEscapeIntf
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    /** Returns the \a input after escaping certain characters */
Packit 1c1d7e
    virtual QCString escape(const QCString &input) = 0;
Packit 1c1d7e
    /** Setting tabbing mode on or off (for LaTeX) */
Packit 1c1d7e
    virtual void enableTabbing(bool b) = 0;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
/** @brief Interface used to remove redundant spaces inside a spaceless block */
Packit 1c1d7e
class TemplateSpacelessIntf
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    /** Returns the \a input after removing redundant whitespace */
Packit 1c1d7e
    virtual QCString remove(const QCString &input) = 0;
Packit 1c1d7e
    /** Reset filter state */
Packit 1c1d7e
    virtual void reset() = 0;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
/** @brief Abstract interface for a template context.
Packit 1c1d7e
 *
Packit 1c1d7e
 *  A Context consists of a stack of dictionaries.
Packit 1c1d7e
 *  A dictionary consists of a mapping of string keys onto TemplateVariant values.
Packit 1c1d7e
 *  A key is searched starting with the dictionary at the top of the stack
Packit 1c1d7e
 *  and searching downwards until it is found. The stack is used to create
Packit 1c1d7e
 *  local scopes.
Packit 1c1d7e
 *  @note This object must be created by TemplateEngine::createContext()
Packit 1c1d7e
 */
Packit 1c1d7e
class TemplateContext
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    virtual ~TemplateContext() {}
Packit 1c1d7e
Packit 1c1d7e
    /** Push a new scope on the stack. */
Packit 1c1d7e
    virtual void push() = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Pop the current scope from the stack. */
Packit 1c1d7e
    virtual void pop() = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Sets a value in the current scope.
Packit 1c1d7e
     *  @param[in] name The name of the value; the key in the dictionary.
Packit 1c1d7e
     *  @param[in] v The value associated with the key.
Packit 1c1d7e
     *  @note When a given key is already present,
Packit 1c1d7e
     *  its value will be replaced by \a v
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual void set(const char *name,const TemplateVariant &v) = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Gets the value for a given key
Packit 1c1d7e
     *  @param[in] name The name of key.
Packit 1c1d7e
     *  @returns The value, which can be an invalid variant in case the
Packit 1c1d7e
     *  key was not found.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual TemplateVariant get(const QCString &name) const = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Returns a pointer to the value corresponding to a given key.
Packit 1c1d7e
     *  @param[in] name The name of key.
Packit 1c1d7e
     *  @returns A pointer to the value, or 0 in case the key was not found.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual const TemplateVariant *getRef(const QCString &name) const = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** When files are created (i.e. by {% create ... %}) they written
Packit 1c1d7e
     *  to the directory \a dir.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual void setOutputDirectory(const QCString &dir) = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Sets the interface that will be used for escaping the result
Packit 1c1d7e
     *  of variable expansion before writing it to the output.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual void setEscapeIntf(const QCString &extension, TemplateEscapeIntf *intf) = 0;
Packit 1c1d7e
Packit 1c1d7e
    /** Sets the interface that will be used inside a spaceless block
Packit 1c1d7e
     *  to remove any redundant whitespace.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual void setSpacelessIntf(TemplateSpacelessIntf *intf) = 0;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
/** @brief Abstract interface for a template.
Packit 1c1d7e
 *  @note Must be created and is deleted by the TemplateEngine
Packit 1c1d7e
 */
Packit 1c1d7e
class Template
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    /** Destructor */
Packit 1c1d7e
    virtual ~Template() {}
Packit 1c1d7e
Packit 1c1d7e
    /** Renders a template instance to a stream.
Packit 1c1d7e
     *  @param[in] ts The text stream to write the results to.
Packit 1c1d7e
     *  @param[in] c The context containing data that can be used
Packit 1c1d7e
     *  when instantiating the template.
Packit 1c1d7e
     */
Packit 1c1d7e
    virtual void render(FTextStream &ts,TemplateContext *c) = 0;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
//------------------------------------------------------------------------
Packit 1c1d7e
Packit 1c1d7e
/** @brief Engine to create templates and template contexts. */
Packit 1c1d7e
class TemplateEngine
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    /** Create a template engine. */
Packit 1c1d7e
    TemplateEngine();
Packit 1c1d7e
Packit 1c1d7e
    /** Destroys the template engine. */
Packit 1c1d7e
   ~TemplateEngine();
Packit 1c1d7e
Packit 1c1d7e
    /** Creates a new context that can be using to render a template.
Packit 1c1d7e
     *  @see Template::render()
Packit 1c1d7e
     */
Packit 1c1d7e
    TemplateContext *createContext() const;
Packit 1c1d7e
Packit 1c1d7e
    /** Destroys a context created via createContext().
Packit 1c1d7e
     *  @param[in] ctx The context.
Packit 1c1d7e
     */
Packit 1c1d7e
    void destroyContext(TemplateContext *ctx);
Packit 1c1d7e
Packit 1c1d7e
    /** Creates a new template whose contents are in a file.
Packit 1c1d7e
     *  @param[in] fileName The name of the file containing the template data
Packit 1c1d7e
     *  @param[in] fromLine The line number of the statement that triggered the load
Packit 1c1d7e
     *  @return the new template, the engine will keep ownership of the object.
Packit 1c1d7e
     */
Packit 1c1d7e
    Template *loadByName(const QCString &fileName,int fromLine);
Packit 1c1d7e
Packit 1c1d7e
    /** Indicates that template \a t is no longer needed. The engine
Packit 1c1d7e
     *  may decide to delete it.
Packit 1c1d7e
     */
Packit 1c1d7e
    void unload(Template *t);
Packit 1c1d7e
Packit 1c1d7e
    /** Prints the current template file include stack */
Packit 1c1d7e
    void printIncludeContext(const char *fileName,int line) const;
Packit 1c1d7e
Packit 1c1d7e
    /** Sets the search directory where to look for template files */
Packit 1c1d7e
    void setTemplateDir(const char *dirName);
Packit 1c1d7e
Packit 1c1d7e
  private:
Packit 1c1d7e
    friend class TemplateNodeBlock;
Packit 1c1d7e
    friend class TemplateNodeCreate;
Packit 1c1d7e
Packit 1c1d7e
    void enterBlock(const QCString &fileName,const QCString &blockName,int line);
Packit 1c1d7e
    void leaveBlock();
Packit 1c1d7e
Packit 1c1d7e
    /** Sets the extension of the output file. This is used to control the
Packit 1c1d7e
     *  format of 'special' tags in the template
Packit 1c1d7e
     */
Packit 1c1d7e
    void setOutputExtension(const char *extension);
Packit 1c1d7e
Packit 1c1d7e
    /** Returns the output extension, set via setOutputExtension() */
Packit 1c1d7e
    QCString outputExtension() const;
Packit 1c1d7e
Packit 1c1d7e
    class Private;
Packit 1c1d7e
    Private *p;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
/** @} */
Packit 1c1d7e
Packit 1c1d7e
#endif