FOR_EACH and FOR_EACHV


I know, I know. Macros are evil. So just FYI, with those:


 


#define FOR_EACH( ___T, ___V, ___C ) \


               ___T * ___V ; \


               Enumerator<___T> __enum_##___V(___C) ; \


               while ( (__enum_##___V).MoveNext() ? ( ___V = __enum_##___V.get_Current() , true): false )


 


#define FOR_EACHV( ___T, ___V, ___C ) \


               ___T __box * ___V ; \


               ValueEnumerator<___T> __enum_##___V(___C) ; \


               while ( (__enum_##___V).MoveNext() ? ( ___V = __enum_##___V.get_Current() , true): false )


 


You can write:


 


FOR_EACH( Module, m, Assembly::GetExecutingAssembly()->GetModules() )


{


   Console::WriteLine( S”\n\nTypes of module {0}:”, m->Name ) ;


   FOR_EACH( Type, t, m->GetTypes() )


      Console::WriteLine( S”{0}”, t->Name ) ;


 


   Console::WriteLine( S”\n\nMethods of module {0}:”, m->Name ) ;


   FOR_EACH( MethodInfo, mi, m->GetMethods() )


      Console::WriteLine( S”{0}”, mi->Name ) ;


 


   Console::WriteLine( S”\n\nFields of module {0}:”, m->Name ) ;


   FOR_EACH( FieldInfo, fi, m->GetFields() )


      Console::WriteLine( S”{0}”, fi->Name ) ;


}


 


Int32 ai __gc [] = { 1, 2, 3, 4 } ;


FOR_EACHV( Int32, i, ai )


   Console::WriteLine( S”{0}”, i->ToString() ) ;


 

Comments (3)

  1. Check this out for a more elaborate (evil? 😉 FOR_EACH implementation by Eric Niebler: http://www.nwcpp.org/Meetings/2004/01.html

  2. Yves Dolce says:

    Indeed! Thank you very much Fred.

    I really need to keep on reading and finishing that book on Modern C++ Design! 😉

  3. Yves Dolce says:

    I wonder if he’s working on a version that could handle managed C++ reference types.

Skip to main content