Public Member Functions | Private Attributes | List of all members
TemplateNodeFor Class Reference

Class representing a 'for' tag in a template. More...

Inheritance diagram for TemplateNodeFor:
TemplateNodeCreator< TemplateNodeFor > TemplateNode

Public Member Functions

 TemplateNodeFor (TemplateParser *parser, TemplateNode *parent, int line, const QCString &data)
 
 ~TemplateNodeFor ()
 
void render (FTextStream &ts, TemplateContext *c)
 
- Public Member Functions inherited from TemplateNodeCreator< TemplateNodeFor >
 TemplateNodeCreator (TemplateParser *parser, TemplateNode *parent, int line)
 
TemplateImplgetTemplate ()
 
- Public Member Functions inherited from TemplateNode
 TemplateNode (TemplateNode *parent)
 
virtual ~TemplateNode ()
 
TemplateNodeparent ()
 

Private Attributes

bool m_reversed
 
ExprAstm_expr
 
QValueList< QCStringm_vars
 
TemplateNodeList m_loopNodes
 
TemplateNodeList m_emptyNodes
 

Additional Inherited Members

- Static Public Member Functions inherited from TemplateNodeCreator< TemplateNodeFor >
static TemplateNodecreateInstance (TemplateParser *parser, TemplateNode *parent, int line, const QCString &data)
 
- Protected Member Functions inherited from TemplateNodeCreator< TemplateNodeFor >
void mkpath (TemplateContextImpl *ci, const QCString &fileName)
 
- Protected Attributes inherited from TemplateNodeCreator< TemplateNodeFor >
QCString m_templateName
 
int m_line
 

Detailed Description

Class representing a 'for' tag in a template.

Definition at line 3048 of file template.cpp.

Constructor & Destructor Documentation

TemplateNodeFor::TemplateNodeFor ( TemplateParser parser,
TemplateNode parent,
int  line,
const QCString data 
)
inline

Definition at line 3051 of file template.cpp.

3053  {
3054  TRACE(("{TemplateNodeFor(%s)\n",data.data()));
3055  QCString exprStr;
3056  int i = data.find(" in ");
3057  if (i==-1)
3058  {
3059  if (data.right(3)==" in")
3060  {
3061  parser->warn(m_templateName,line,"for is missing container after 'in' keyword");
3062  }
3063  else if (data=="in")
3064  {
3065  parser->warn(m_templateName,line,"for needs at least one iterator variable");
3066  }
3067  else
3068  {
3069  parser->warn(m_templateName,line,"for is missing 'in' keyword");
3070  }
3071  }
3072  else
3073  {
3074  m_vars = split(data.left(i),",");
3075  if (m_vars.count()==0)
3076  {
3077  parser->warn(m_templateName,line,"for needs at least one iterator variable");
3078  }
3079 
3080  int j = data.find(" reversed",i);
3081  m_reversed = (j!=-1);
3082 
3083  if (j==-1) j=data.length();
3084  if (j>i+4)
3085  {
3086  exprStr = data.mid(i+4,j-i-4); // skip over " in " part
3087  }
3088  if (exprStr.isEmpty())
3089  {
3090  parser->warn(m_templateName,line,"for is missing container after 'in' keyword");
3091  }
3092  }
3093  ExpressionParser expParser(parser,line);
3094  m_expr = expParser.parse(exprStr);
3095 
3096  QStrList stopAt;
3097  stopAt.append("endfor");
3098  stopAt.append("empty");
3099  parser->parse(this,line,stopAt,m_loopNodes);
3100  TemplateToken *tok = parser->takeNextToken();
3101  if (tok && tok->data=="empty")
3102  {
3103  stopAt.removeLast();
3104  parser->parse(this,line,stopAt,m_emptyNodes);
3105  parser->removeNextToken(); // skip over endfor
3106  }
3107  delete tok;
3108  TRACE(("}TemplateNodeFor(%s)\n",data.data()));
3109  }
static QValueList< QCString > split(const QCString &str, const QCString &sep, bool allowEmptyEntries=FALSE, bool cleanup=TRUE)
Definition: template.cpp:50
bool isEmpty() const
Definition: qcstring.h:189
QValueList< QCString > m_vars
Definition: template.cpp:3197
TemplateNodeList m_loopNodes
Definition: template.cpp:3198
uint length() const
Definition: qcstring.h:195
const bool FALSE
Definition: qglobal.h:370
TemplateNodeList m_emptyNodes
Definition: template.cpp:3199
QCString left(uint len) const
Definition: qcstring.cpp:213
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:41
Class representing a lexical token in a template.
Definition: template.cpp:2226
uint count() const
Definition: qvaluelist.h:394
QCString data
Definition: template.cpp:2232
TemplateNode * parent()
Definition: template.cpp:1647
ExprAst * m_expr
Definition: template.cpp:3196
QCString right(uint len) const
Definition: qcstring.cpp:231
void append(const type *d)
Definition: qinternallist.h:61
const char * data() const
Definition: qcstring.h:207
QCString mid(uint index, uint len=0xffffffff) const
Definition: qcstring.cpp:246
void line(double t, double *p, double &x, double &y, double &z)
Recursive decent parser for Django style template expressions.
Definition: template.cpp:1680
#define TRACE(x)
Definition: template.cpp:43
bool removeLast()
Definition: qinternallist.h:68
TemplateNodeFor::~TemplateNodeFor ( )
inline

Definition at line 3111 of file template.cpp.

3112  {
3113  delete m_expr;
3114  }
ExprAst * m_expr
Definition: template.cpp:3196

Member Function Documentation

void TemplateNodeFor::render ( FTextStream ts,
TemplateContext c 
)
inlinevirtual

Implements TemplateNode.

Definition at line 3116 of file template.cpp.

3117  {
3118  TemplateContextImpl* ci = dynamic_cast<TemplateContextImpl*>(c);
3119  if (ci==0) return; // should not happen
3121  //printf("TemplateNodeFor::render #loopNodes=%d #emptyNodes=%d\n",
3122  // m_loopNodes.count(),m_emptyNodes.count());
3123  if (m_expr)
3124  {
3125  TemplateVariant v = m_expr->resolve(c);
3127  {
3129  }
3130  const TemplateListIntf *list = v.toList();
3131  if (list)
3132  {
3133  uint listSize = list->count();
3134  if (listSize==0) // empty for loop
3135  {
3136  m_emptyNodes.render(ts,c);
3137  return;
3138  }
3139  c->push();
3140  //int index = m_reversed ? list.count() : 0;
3141  TemplateVariant v;
3142  const TemplateVariant *parentLoop = c->getRef("forloop");
3143  uint index = m_reversed ? listSize-1 : 0;
3145  for (m_reversed ? it->toLast() : it->toFirst();
3146  (it->current(v));
3147  m_reversed ? it->toPrev() : it->toNext())
3148  {
3150  s->set("counter0", (int)index);
3151  s->set("counter", (int)(index+1));
3152  s->set("revcounter", (int)(listSize-index));
3153  s->set("revcounter0", (int)(listSize-index-1));
3154  s->set("first",index==0);
3155  s->set("last", index==listSize-1);
3156  s->set("parentloop",parentLoop ? *parentLoop : TemplateVariant());
3157  c->set("forloop",s.get());
3158 
3159  // add variables for this loop to the context
3160  //obj->addVariableToContext(index,m_vars,c);
3161  uint vi=0;
3162  if (m_vars.count()==1) // loop variable represents an item
3163  {
3164  c->set(m_vars[vi++],v);
3165  }
3166  else if (m_vars.count()>1 && v.type()==TemplateVariant::Struct)
3167  // loop variables represent elements in a list item
3168  {
3169  for (uint i=0;i<m_vars.count();i++,vi++)
3170  {
3171  c->set(m_vars[vi],v.toStruct()->get(m_vars[vi]));
3172  }
3173  }
3174  for (;vi<m_vars.count();vi++)
3175  {
3176  c->set(m_vars[vi],TemplateVariant());
3177  }
3178 
3179  // render all items for this iteration of the loop
3180  m_loopNodes.render(ts,c);
3181 
3182  if (m_reversed) index--; else index++;
3183  }
3184  c->pop();
3185  delete it;
3186  }
3187  else // simple type...
3188  {
3189  ci->warn(m_templateName,m_line,"for requires a variable of list type, got type '%s'!",v.typeAsString().data());
3190  }
3191  }
3192  }
void render(FTextStream &ts, TemplateContext *c)
Definition: template.cpp:2246
virtual void pop()=0
virtual void push()=0
QValueList< QCString > m_vars
Definition: template.cpp:3197
TemplateNodeList m_loopNodes
Definition: template.cpp:3198
Internal class representing the implementation of a template context.
Definition: template.cpp:511
virtual bool current(TemplateVariant &v) const =0
virtual TemplateVariant get(const char *name) const =0
TemplateListIntf * toList() const
Definition: template.h:256
TemplateNodeList m_emptyNodes
Definition: template.cpp:3199
QCString typeAsString() const
Definition: template.h:145
TemplateVariant call(const QValueList< TemplateVariant > &args)
Definition: template.h:272
virtual const TemplateVariant * getRef(const QCString &name) const =0
uint count() const
Definition: qvaluelist.h:394
virtual void set(const char *name, const TemplateVariant &v)=0
ExprAst * m_expr
Definition: template.cpp:3196
virtual TemplateVariant resolve(TemplateContext *)
Definition: template.cpp:1368
virtual int count() const =0
Abstract interface for a iterator of a list.
Definition: template.h:333
const char * data() const
Definition: qcstring.h:207
virtual TemplateListIntf::ConstIterator * createIterator() const =0
Variant type which can hold one value of a fixed set of types.
Definition: template.h:90
void warn(const char *fileName, int line, const char *fmt,...) const
Definition: template.cpp:2431
TemplateStructIntf * toStruct() const
Definition: template.h:264
Type type() const
Definition: template.h:142
void setLocation(const QCString &templateName, int line)
Definition: template.cpp:540
Abstract read-only interface for a context value of type list.
Definition: template.h:329
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
static TemplateStruct * alloc()
Definition: template.cpp:294

Member Data Documentation

TemplateNodeList TemplateNodeFor::m_emptyNodes
private

Definition at line 3199 of file template.cpp.

ExprAst* TemplateNodeFor::m_expr
private

Definition at line 3196 of file template.cpp.

TemplateNodeList TemplateNodeFor::m_loopNodes
private

Definition at line 3198 of file template.cpp.

bool TemplateNodeFor::m_reversed
private

Definition at line 3195 of file template.cpp.

QValueList<QCString> TemplateNodeFor::m_vars
private

Definition at line 3197 of file template.cpp.


The documentation for this class was generated from the following file: