There are happy people working with boost and Qt. In my current "embedded" project I have to use home-made container classes. OK, enough complaining.
I've tried to implement an easy and self-contained foreach like that:
#define ForEachString(S,C) TString S;\
for ( int i=0; i<C.GetSize() && (!!(&(S=C[i]))); ++i )
It iterates through a string-list which has op[] and GetSize() methods. E.g.:
TStringList tables;
ForEachString( table, tables )
{
//do sth. with tab.
}
Of cause, the ugly thing is, each container type requires its own macro. Therefore my question: Is it possible to do it container independant and still self contained (all required stuff within the macro definition)?
Regards, Valentin
Perhaps you could parameterise on the type T:
#define ForEach(T,S,C) T S;\
for ( int i=0; i<C.GetSize() && (!!(&(S=C[i]))); ++i )
TStringList tables;
ForEach( TString, table, tables )
{
//do sth. with tab.
}
I would recommend this one
#define ForEachString(S,C) \
if(bool _j_ = false) ; else
for (int _i_ = 0; _i_ < C.GetSize() && !_j_; ++_i_, _j_ = !_j_)
for(S = C[_i_]; !_j_; _j_ = true)
TStringList tables;
ForEachString(TString table, tables)
{
//do sth. with table
}
The weird actions with _j_
are needed to not break break
inside the loop. Best use names like _i_
and _j_
so to not interfere with user's local loop variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With