My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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< QCString > m_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 3188 of file template.cpp.

Constructor & Destructor Documentation

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

Definition at line 3191 of file template.cpp.

References TemplateToken::data, m_emptyNodes, m_expr, m_loopNodes, m_reversed, TemplateNodeCreator< TemplateNodeFor >::m_templateName, m_vars, TemplateParser::parse(), ExpressionParser::parse(), TemplateParser::removeNextToken(), split(), TemplateParser::takeNextToken(), TRACE, and TemplateParser::warn().

{
TRACE(("{TemplateNodeFor(%s)\n",data.data()));
QCString exprStr;
int i = data.find(" in ");
if (i==-1)
{
if (data.right(3)==" in")
{
parser->warn(m_templateName,line,"for is missing container after 'in' keyword");
}
else if (data=="in")
{
parser->warn(m_templateName,line,"for needs at least one iterator variable");
}
else
{
parser->warn(m_templateName,line,"for is missing 'in' keyword");
}
}
else
{
m_vars = split(data.left(i),",");
if (m_vars.count()==0)
{
parser->warn(m_templateName,line,"for needs at least one iterator variable");
}
int j = data.find(" reversed",i);
m_reversed = (j!=-1);
if (j==-1) j=data.length();
if (j>i+4)
{
exprStr = data.mid(i+4,j-i-4); // skip over " in " part
}
if (exprStr.isEmpty())
{
parser->warn(m_templateName,line,"for is missing container after 'in' keyword");
}
}
ExpressionParser expParser(parser,line);
m_expr = expParser.parse(exprStr);
QStrList stopAt;
stopAt.append("endfor");
stopAt.append("empty");
parser->parse(this,line,stopAt,m_loopNodes);
TemplateToken *tok = parser->takeNextToken();
if (tok && tok->data=="empty")
{
stopAt.removeLast();
parser->parse(this,line,stopAt,m_emptyNodes);
parser->removeNextToken(); // skip over endfor
}
delete tok;
TRACE(("}TemplateNodeFor(%s)\n",data.data()));
}
TemplateNodeFor::~TemplateNodeFor ( )
inline

Definition at line 3251 of file template.cpp.

References m_expr.

{
delete m_expr;
}

Member Function Documentation

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

Implements TemplateNode.

Definition at line 3256 of file template.cpp.

References TemplateStruct::alloc(), TemplateVariant::call(), TemplateListIntf::count(), TemplateListIntf::createIterator(), TemplateListIntf::ConstIterator::current(), TemplateVariant::Function, TemplateAutoRef< T >::get(), TemplateStructIntf::get(), TemplateContext::getRef(), m_emptyNodes, m_expr, TemplateNodeCreator< TemplateNodeFor >::m_line, m_loopNodes, m_reversed, TemplateNodeCreator< TemplateNodeFor >::m_templateName, m_vars, TemplateContext::pop(), TemplateContext::push(), TemplateNodeList::render(), ExprAst::resolve(), TemplateStruct::set(), TemplateContext::set(), TemplateContextImpl::setLocation(), TemplateVariant::Struct, TemplateListIntf::ConstIterator::toFirst(), TemplateListIntf::ConstIterator::toLast(), TemplateVariant::toList(), TemplateListIntf::ConstIterator::toNext(), TemplateListIntf::ConstIterator::toPrev(), TemplateVariant::toStruct(), TemplateVariant::type(), TemplateVariant::typeAsString(), and TemplateContextImpl::warn().

{
TemplateContextImpl* ci = dynamic_cast<TemplateContextImpl*>(c);
if (ci==0) return; // should not happen
//printf("TemplateNodeFor::render #loopNodes=%d #emptyNodes=%d\n",
// m_loopNodes.count(),m_emptyNodes.count());
if (m_expr)
{
{
v = v.call(QValueList<TemplateVariant>());
}
const TemplateListIntf *list = v.toList();
if (list)
{
uint listSize = list->count();
if (listSize==0) // empty for loop
{
return;
}
c->push();
//int index = m_reversed ? list.count() : 0;
const TemplateVariant *parentLoop = c->getRef("forloop");
uint index = m_reversed ? listSize-1 : 0;
for (m_reversed ? it->toLast() : it->toFirst();
(it->current(v));
m_reversed ? it->toPrev() : it->toNext())
{
s->set("counter0", (int)index);
s->set("counter", (int)(index+1));
s->set("revcounter", (int)(listSize-index));
s->set("revcounter0", (int)(listSize-index-1));
s->set("first",index==0);
s->set("last", index==listSize-1);
s->set("parentloop",parentLoop ? *parentLoop : TemplateVariant());
c->set("forloop",s.get());
// add variables for this loop to the context
//obj->addVariableToContext(index,m_vars,c);
uint vi=0;
if (m_vars.count()==1) // loop variable represents an item
{
c->set(m_vars[vi++],v);
}
else if (m_vars.count()>1 && v.type()==TemplateVariant::Struct)
// loop variables represent elements in a list item
{
for (uint i=0;i<m_vars.count();i++,vi++)
{
c->set(m_vars[vi],v.toStruct()->get(m_vars[vi]));
}
}
for (;vi<m_vars.count();vi++)
{
}
// render all items for this iteration of the loop
if (m_reversed) index--; else index++;
}
c->pop();
delete it;
}
else // simple type...
{
ci->warn(m_templateName,m_line,"for requires a variable of list type, got type '%s'!",v.typeAsString().data());
}
}
}

Member Data Documentation

TemplateNodeList TemplateNodeFor::m_emptyNodes
private

Definition at line 3339 of file template.cpp.

Referenced by render(), and TemplateNodeFor().

ExprAst* TemplateNodeFor::m_expr
private

Definition at line 3336 of file template.cpp.

Referenced by render(), TemplateNodeFor(), and ~TemplateNodeFor().

TemplateNodeList TemplateNodeFor::m_loopNodes
private

Definition at line 3338 of file template.cpp.

Referenced by render(), and TemplateNodeFor().

bool TemplateNodeFor::m_reversed
private

Definition at line 3335 of file template.cpp.

Referenced by render(), and TemplateNodeFor().

QValueList<QCString> TemplateNodeFor::m_vars
private

Definition at line 3337 of file template.cpp.

Referenced by render(), and TemplateNodeFor().


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