Iterators
To access the individual members of a Bag, Set or List, the C++ EXPRESS API offers the Iterator concept. An Iterator is a data structure that keeps track of the current position when the program loops trough an aggregate. The aggregate is input parameter to the Iterator constructor.
An Iterator has a first method that returns the first element of an aggregate. After getting the first element of an aggregate, one advances to the next element by means of the next method.
After getting an aggregate element by either the first or the next method, one must check if the iterator is exhausted. For pointer elements like object, aggregate or select, one simply checks if the pointer is different from NULL. For elements that not are pointers, like Enumeration, Integer, Real, Logical or Boolean one must use the method moreElems. The example below shows the pointer case.
Example : (the set of products from above)
// Iterator for the set of products
printf("\n\nProducts in set:");
Iterator<product*, entityType> prodIter(prodSet);
int i =0;
for(product *prod = prodIter.first(); prod; prod = prodIter.next()){
++i;
printf("\nElement : %d - %s",i,prod->get_id());
}
Iterators in programs using the "in database" object model are optimized for minimal memory usage. Therfore objects, aggregates and selects are retrieved from an iterator only within the scope of the iterator. If such an object, aggregate or select shall survive the iterator loop, one must "clone" it by the method clone defined on Model.
product *prod1 = NULL;
product *prod2 = NULL;
entityType mytype;
int i = 0;
Iterator<product*, entityType> prodIter(aaa.get_items());
for(product *prod = prodIter.first(); prod; prod = prodIter.next()){
printf("\nProduct during iteration : %s",prod->get_id());
++i;
if(i ==1)
prod1 = (product*)m->clone(prod,(int*)&mytype);
if(i ==2)
prod2 = (product*)m->clone(prod,(int*)&mytype);
}
printf("\nProduct 1 after iteration: %s",prod1->get_id());
printf("\nProduct 2 after iteration: %s",prod2->get_id());