Doxygen
TemplateNodeFor类 参考

Class representing a 'for' tag in a template 更多...

+ 类 TemplateNodeFor 继承关系图:
+ TemplateNodeFor 的协作图:

Public 成员函数

 TemplateNodeFor (TemplateParser *parser, TemplateNode *parent, int line, const QCString &data)
 
void render (TextStream &ts, TemplateContext *c)
 
- Public 成员函数 继承自 TemplateNodeCreator< TemplateNodeFor >
 TemplateNodeCreator (TemplateParser *parser, TemplateNode *parent, int line)
 
TemplateImplgetTemplate ()
 
- Public 成员函数 继承自 TemplateNode
 TemplateNode (TemplateNode *parent)
 
virtual ~TemplateNode ()
 
TemplateNodeparent ()
 

Private 属性

bool m_reversed = false
 
ExprAstPtr m_expr
 
std::vector< QCStringm_vars
 
TemplateNodeList m_loopNodes
 
TemplateNodeList m_emptyNodes
 

额外继承的成员函数

- 静态 Public 成员函数 继承自 TemplateNodeCreator< TemplateNodeFor >
static TemplateNodePtr createInstance (TemplateParser *parser, TemplateNode *parent, int line, const QCString &data)
 
- Protected 成员函数 继承自 TemplateNodeCreator< TemplateNodeFor >
void mkpath (const TemplateContextImpl *ci, const std::string &fileName)
 
- Protected 属性 继承自 TemplateNodeCreator< TemplateNodeFor >
QCString m_templateName
 
int m_line
 

详细描述

Class representing a 'for' tag in a template

在文件 template.cpp3409 行定义.

构造及析构函数说明

◆ TemplateNodeFor()

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

在文件 template.cpp3412 行定义.

3414  {
3415  TRACE(("{TemplateNodeFor(%s)\n",qPrint(data)));
3416  QCString exprStr;
3417  int i = data.find(" in ");
3418  if (i==-1)
3419  {
3420  if (data.endsWith(" in"))
3421  {
3422  parser->warn(m_templateName,line,"for is missing container after 'in' keyword");
3423  }
3424  else if (data=="in")
3425  {
3426  parser->warn(m_templateName,line,"for needs at least one iterator variable");
3427  }
3428  else
3429  {
3430  parser->warn(m_templateName,line,"for is missing 'in' keyword");
3431  }
3432  }
3433  else
3434  {
3435  m_vars = split(data.left(i),",");
3436  if (m_vars.size()==0)
3437  {
3438  parser->warn(m_templateName,line,"for needs at least one iterator variable");
3439  }
3440 
3441  int j = data.find(" reversed",i);
3442  m_reversed = (j!=-1);
3443 
3444  if (j==-1) j=data.length();
3445  if (j>i+4)
3446  {
3447  exprStr = data.mid(i+4,j-i-4); // skip over " in " part
3448  }
3449  if (exprStr.isEmpty())
3450  {
3451  parser->warn(m_templateName,line,"for is missing container after 'in' keyword");
3452  }
3453  }
3454  ExpressionParser expParser(parser,line);
3455  m_expr = expParser.parse(exprStr);
3456 
3457  StringVector stopAt = { "endfor", "empty" };
3458  parser->parse(this,line,stopAt,m_loopNodes);
3459  auto tok = parser->takeNextToken();
3460  if (tok && tok->data=="empty")
3461  {
3462  stopAt.pop_back();
3463  parser->parse(this,line,stopAt,m_emptyNodes);
3464  parser->removeNextToken(); // skip over endfor
3465  }
3466  TRACE(("}TemplateNodeFor(%s)\n",qPrint(data)));
3467  }

引用了 QCString::endsWith(), QCString::find(), QCString::isEmpty(), QCString::left(), QCString::length(), m_emptyNodes, m_expr, m_loopNodes, m_reversed, TemplateNodeCreator< TemplateNodeFor >::m_templateName, m_vars, QCString::mid(), TemplateParser::parse(), ExpressionParser::parse(), qPrint(), TemplateParser::removeNextToken(), split(), TemplateParser::takeNextToken(), TRACE , 以及 TemplateParser::warn().

成员函数说明

◆ render()

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

实现了 TemplateNode.

在文件 template.cpp3469 行定义.

3470  {
3471  TemplateContextImpl* ci = dynamic_cast<TemplateContextImpl*>(c);
3472  if (ci==0) return; // should not happen
3474  //printf("TemplateNodeFor::render #loopNodes=%d #emptyNodes=%d\n",
3475  // m_loopNodes.count(),m_emptyNodes.count());
3476  if (m_expr)
3477  {
3478  TemplateVariant v = m_expr->resolve(c);
3479  if (v.isFunction())
3480  {
3481  v = v.call();
3482  }
3483  const TemplateListIntfPtr list = v.toList();
3484  if (list)
3485  {
3486  uint listSize = list->count();
3487  if (listSize==0) // empty for loop
3488  {
3489  m_emptyNodes.render(ts,c);
3490  return;
3491  }
3492  c->push();
3493  //int index = m_reversed ? list.count() : 0;
3494  //TemplateVariant v;
3495  const TemplateVariant *parentLoop = c->getRef("forloop");
3496  uint index = m_reversed ? listSize-1 : 0;
3497  TemplateListIntf::ConstIteratorPtr it = list->createIterator();
3498  TemplateVariant ve;
3499  for (m_reversed ? it->toLast() : it->toFirst();
3500  (it->current(ve));
3501  m_reversed ? it->toPrev() : it->toNext())
3502  {
3504  s->set("counter0", (int)index);
3505  s->set("counter", (int)(index+1));
3506  s->set("revcounter", (int)(listSize-index));
3507  s->set("revcounter0", (int)(listSize-index-1));
3508  s->set("first",index==0);
3509  s->set("last", index==listSize-1);
3510  s->set("parentloop",parentLoop ? *parentLoop : TemplateVariant());
3511  c->set("forloop",std::static_pointer_cast<TemplateStructIntf>(s));
3512 
3513  // add variables for this loop to the context
3514  //obj->addVariableToContext(index,m_vars,c);
3515  uint vi=0;
3516  if (m_vars.size()==1) // loop variable represents an item
3517  {
3518  c->set(m_vars[vi++],ve);
3519  }
3520  else if (m_vars.size()>1 && (ve.isStruct() || ve.isWeakStruct()))
3521  // loop variables represent elements in a list item
3522  {
3523  TemplateStructIntfPtr vs = ve.toStruct();
3524  if (vs)
3525  {
3526  for (uint i=0;i<m_vars.size();i++,vi++)
3527  {
3528  c->set(m_vars[vi],vs->get(m_vars[vi]));
3529  }
3530  }
3531  }
3532  for (;vi<m_vars.size();vi++)
3533  {
3534  c->set(m_vars[vi],TemplateVariant());
3535  }
3536 
3537  // render all items for this iteration of the loop
3538  m_loopNodes.render(ts,c);
3539 
3540  if (m_reversed) index--; else index++;
3541  }
3542  c->pop();
3543  }
3544  else // simple type...
3545  {
3546  ci->warn(m_templateName,m_line,"for requires a variable of list type, got type '%s'!",qPrint(v.typeAsString()));
3547  }
3548  }
3549  }

引用了 TemplateStruct::alloc(), TemplateVariant::call(), TemplateContext::getRef(), TemplateVariant::isFunction(), TemplateVariant::isStruct(), TemplateVariant::isWeakStruct(), m_emptyNodes, m_expr, TemplateNodeCreator< TemplateNodeFor >::m_line, m_loopNodes, m_reversed, TemplateNodeCreator< TemplateNodeFor >::m_templateName, m_vars, TemplateContext::pop(), TemplateContext::push(), qPrint(), TemplateNodeList::render(), TemplateContext::set(), TemplateContextImpl::setLocation(), TemplateVariant::toList(), TemplateVariant::toStruct(), TemplateVariant::typeAsString() , 以及 TemplateContextImpl::warn().

类成员变量说明

◆ m_emptyNodes

TemplateNodeList TemplateNodeFor::m_emptyNodes
private

在文件 template.cpp3556 行定义.

被这些函数引用 render() , 以及 TemplateNodeFor().

◆ m_expr

ExprAstPtr TemplateNodeFor::m_expr
private

在文件 template.cpp3553 行定义.

被这些函数引用 render() , 以及 TemplateNodeFor().

◆ m_loopNodes

TemplateNodeList TemplateNodeFor::m_loopNodes
private

在文件 template.cpp3555 行定义.

被这些函数引用 render() , 以及 TemplateNodeFor().

◆ m_reversed

bool TemplateNodeFor::m_reversed = false
private

在文件 template.cpp3552 行定义.

被这些函数引用 render() , 以及 TemplateNodeFor().

◆ m_vars

std::vector<QCString> TemplateNodeFor::m_vars
private

在文件 template.cpp3554 行定义.

被这些函数引用 render() , 以及 TemplateNodeFor().


该类的文档由以下文件生成:
TemplateVariant::toStruct
TemplateStructIntfPtr toStruct()
Returns the pointer to struct referenced by this variant or 0 if this variant does not have struct ty...
Definition: template.cpp:441
StringVector
std::vector< std::string > StringVector
Definition: containers.h:32
TemplateContextImpl::warn
void warn(const QCString &fileName, int line, const char *fmt,...) const
Definition: template.cpp:2773
TemplateNodeFor::m_loopNodes
TemplateNodeList m_loopNodes
Definition: template.cpp:3555
TemplateVariant::isFunction
constexpr bool isFunction() const
Returns TRUE if the variant holds a function value
Definition: template.h:215
QCString::length
uint length() const
Returns the length of the string, not counting the 0-terminator.
Definition: qcstring.h:147
TemplateContextImpl::setLocation
void setLocation(const QCString &templateName, int line)
Definition: template.cpp:682
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty
Definition: qcstring.h:144
TemplateVariant::call
TemplateVariant call(const std::vector< TemplateVariant > &args=std::vector< TemplateVariant >())
Return the result of apply this function with args.
Definition: template.cpp:454
TemplateNodeCreator< TemplateNodeFor >
split
static std::vector< QCString > split(const QCString &str, const QCString &sep, bool allowEmptyEntries=FALSE, bool cleanup=TRUE)
Definition: template.cpp:47
TemplateNodeFor::m_reversed
bool m_reversed
Definition: template.cpp:3552
TemplateVariant::isWeakStruct
constexpr bool isWeakStruct() const
Returns TRUE if the variant holds a struct value
Definition: template.h:217
QCString::find
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:38
TemplateStructIntfPtr
std::shared_ptr< TemplateStructIntf > TemplateStructIntfPtr
Definition: template.h:33
TemplateListIntf::ConstIteratorPtr
std::unique_ptr< ConstIterator > ConstIteratorPtr
Definition: template.h:292
ExpressionParser
Recursive decent parser for Django style template expressions.
Definition: template.cpp:2010
TemplateParser::removeNextToken
void removeNextToken()
Definition: template.cpp:5153
uint
unsigned uint
Definition: qcstring.h:40
TemplateNodeFor::m_emptyNodes
TemplateNodeList m_emptyNodes
Definition: template.cpp:3556
TemplateParser::warn
void warn(const QCString &fileName, int line, const char *fmt,...) const
Definition: template.cpp:5163
TemplateVariant
Variant type which can hold one value of a fixed set of types.
Definition: template.h:98
TemplateNodeFor::m_vars
std::vector< QCString > m_vars
Definition: template.cpp:3554
QCString::left
QCString left(size_t len) const
Definition: qcstring.h:212
TRACE
#define TRACE(x)
Definition: template.cpp:40
TemplateContext::set
virtual void set(const QCString &name, const TemplateVariant &v)=0
Sets a value in the current scope.
TemplateVariant::isStruct
constexpr bool isStruct() const
Returns TRUE if the variant holds a struct value
Definition: template.h:211
TemplateNodeList::render
void render(TextStream &ts, TemplateContext *c)
Definition: template.cpp:1971
TemplateNodeCreator< TemplateNodeFor >::m_templateName
QCString m_templateName
Definition: template.cpp:3095
TemplateNode::parent
TemplateNode * parent()
Definition: template.cpp:1941
TemplateParser::parse
void parse(TemplateNode *parent, int line, const StringVector &stopAt, TemplateNodeList &nodes)
Definition: template.cpp:5057
QCString::mid
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition: qcstring.h:224
TemplateListIntfPtr
std::shared_ptr< TemplateListIntf > TemplateListIntfPtr
Definition: template.h:32
QCString::endsWith
bool endsWith(const char *s) const
Definition: qcstring.h:420
qPrint
const char * qPrint(const char *s)
Definition: qcstring.h:589
TemplateVariant::typeAsString
const char * typeAsString() const
Returns a string representation of this variant's type
Definition: template.cpp:416
TemplateNodeFor::m_expr
ExprAstPtr m_expr
Definition: template.cpp:3553
TemplateParser::takeNextToken
TemplateTokenPtr takeNextToken()
Definition: template.cpp:5140
TemplateContext::push
virtual void push()=0
Push a new scope on the stack.
TemplateContextImpl
Internal class representing the implementation of a template context
Definition: template.cpp:640
TemplateNodeCreator< TemplateNodeFor >::m_line
int m_line
Definition: template.cpp:3096
TemplateContext::getRef
virtual const TemplateVariant * getRef(const QCString &name) const =0
Returns a pointer to the value corresponding to a given key.
TemplateVariant::toList
TemplateListIntfPtr toList()
Returns the pointer to list referenced by this variant or 0 if this variant does not have list type.
Definition: template.cpp:432
TemplateContext::pop
virtual void pop()=0
Pop the current scope from the stack.
FALSE
#define FALSE
Definition: qcstring.h:33
TemplateStructPtr
std::shared_ptr< TemplateStruct > TemplateStructPtr
Definition: template.cpp:147
TemplateStruct::alloc
static TemplateStructPtr alloc()
Creates an instance and returns a shared pointer to it
Definition: template.cpp:218
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:108