antarey
antarey

Reputation: 37

Delphi for...in in C++Builder

Delphi code

var
  BookNode, EntityNode: TXmlNode;
  Books: TXmlNodeList;
...
for BookNode in Books do

In CLang compiler in C++Builder

for (auto && BookNode : Books)

How to write this code in a classic compiler?

The Count/RecordCount/Items->Count e.g. property is missing.

I'm using the classic compiler because some components do not support CLang.

Upvotes: 0

Views: 354

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596307

The classic compiler does not support C++11, so you can't use a range-based for loop. You have to use a traditional for loop instead.

Delphi's for..in loop is based on the concept of an Enumerator (see Iteration Over Containers Using For Statements). However, Delphi's TXMLNodeList does not implement an Enumerator, so you can't use it in a for..in loop.

C++11's range-based for loop is based on the concept of iterators. Embarcadero's CLang compilers implement iterators for many Delphi-style containers that implement a GetEnumerator() method or Count+operator[] properties. See C++ Iterator Support for Delphi Enumerable Types and Containers. In the classic compiler, you would have to use such accesses manually, eg:

for(Iterator iter = list->begin(); iter != list->end(); ++iter)
{
    ElementType &elem = *iter;
    ...
}
for(int index = 0; index < list->Count; ++index)
{
    ElementType &elem = (*list)[index]; // or list->Items[index], etc...
    ...
}
EnumeratorType *enum = list->GetEnumerator();
while (enum->MoveNext())
{
    ElementType elem = enum->Current;
    ...
}

Despite your claim, Delphi's TXMLNodeList DOES have public Count and Nodes[] properties (inherited from the IXMLNodeList interface) for indexing through nodes (Delphi's XML framework predates C++11, after all), eg:

_di_IXMLNodeList Books;
...
for(int i = 0; i < Books->Count; ++i)
{
    _di_IXMLNode BookNode = Books->Nodes[i];
    ...
}

UPDATE: the above was based on an assumption that you were using Embarcadero's XML framework, which has its own TXMLNode and TXMLNodeList classes. Based on your comment that you are actually using VerySimpleXML instead, which has similarly named classes, I looked at its code and see that its TXmlNodeList class derives from Delphi's TObjectList<T> class, which has public Count and Items[] properties. So, you can use those in a for loop, eg:

TXMLNodeList *Books;
...
for(int i = 0; i < Books->Count; ++i)
{
    TXMLNode *BookNode = Books->Items[i];
    ...
}

Upvotes: 4

Related Questions