R16 iterators [SOLVED]



  • On 21/10/2014 at 08:49, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R16 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
    Hi,

    I'm having trouble with R16 iterators.
    I must first confess that I don't use iterators in C++ because I think it's very ugly looking code. But I would like to force myself to use them more often.

    This code yells at me that <BaseArray<Int> is not a template.
    I do have #include "c4d_misc.h" in my code. Do I need to write my own template?
    How do I make this work?

        maxon::BaseArray<Int> myarr;  
      
      for (maxon::AutoIterator<BaseArray<Int> > it(myarr); it; ++it)  
      {  
          //... do something with *it or it->  
      }
    

    Can someone also please post an example how to use an R16 iterator to iterate the objects in the hierarchy?

    Thanks,
    -ScottA



  • On 21/10/2014 at 08:54, xxxxxxxx wrote:

    It's similar to the STD iterators.

    for (maxon::BaseArray<Int>::Iterator it = myarr.Begin(); it != myarr.End(); ++it)
    {
        // ...
    }
    

    I prefer to use auto for iterators.

    for (auto it = myarr.Begin(); it != myarr.End(); ++it)
    {
        // ...
    }
    

    There's also a FOREACH macro in the SDK. I think you use it like this

    FOREACH (it, myarr)
    {
        // ...
    }
    


  • On 21/10/2014 at 09:59, xxxxxxxx wrote:

    Thanks Niklas.
    This is what I have working so far:

        //Create an array and fill it with some data  
      maxon::BaseArray<Int> myarr;      
      for(Int32 i = 0; i < 10; i++){ myarr.Append(i); }  
      
      //Iterate through the array using an iterator  
      //The iterator 'it' is the value of each array element  
      //for(maxon::BaseArray<Int>::Iterator it = myarr.Begin(); it != myarr.End(); ++it)  
      for(auto it = myarr.Begin(); it != myarr.End(); ++it)  
      {  
          auto value = *it;      
          GePrint(String::IntToString(value));  
      
          if (*it == 5) { GePrint("found value of 5"); break; }          
      }  
      
      FOREACH(it, myarr)  
      {  
          GePrint("ForEach macro results: " + String::IntToString(*it));  
      }
    

    Now I just need to figure out how to iterate objects.

    -ScottA

    P.S. @ MAXON: Please document these things better with examples like the one I just posted



  • On 21/10/2014 at 10:09, xxxxxxxx wrote:

    What do you mean with "how to iterate objects"?



  • On 21/10/2014 at 10:38, xxxxxxxx wrote:

    Something like this:

      
          FOREACH(obj, MyAtomArray)  
          {  
              GePrint(obj->GetName());  
          }  
    

    I know how to do this with an AtomArray and a standard for() loop.
    But I'd like to use the new FOREACH and/or iterators instead.

    -ScottA



  • On 21/10/2014 at 12:04, xxxxxxxx wrote:

    The AtomArray has no built-in iterator capabilities. You have to use GetIndex().



  • On 21/10/2014 at 12:11, xxxxxxxx wrote:

    Well. That's just an example.
    Is there no way to use the new iterators for iterating object hierarchies?

    -ScottA



  • On 21/10/2014 at 12:13, xxxxxxxx wrote:

    Sorry, forget this post. I wasn't aware you were already a few steps further... :(

    Hi Scott,

    I'm not quite sure, as you seemed to already have it. Here's your example with PolygonObject:

    maxon::BaseArray<PolygonObject*> polyarr;
    for (Int32 i = 0; i < 10; i++) { polyarr.Append(PolygonObject::Alloc(8, 6)); }
      
    //Iterate through the array using an iterator
    //The iterator 'it' is the value of each array element
    for (auto it = polyarr.Begin(); it != polyarr.End(); ++it) {
      auto pPObj = *it;
      GePrint(String::IntToString(pPObj->GetPolygonCount()));
      
      if (pPObj->GetPolygonCount() == 6) { GePrint("polyarr found possible cube"); break; }
    }
      
    FOREACH(it, polyarr)
    {
      GePrint("polyarr ForEach macro results: " + String::IntToString((*it)->GetPolygonCount()));
    }
    


  • On 21/10/2014 at 12:36, xxxxxxxx wrote:

    Anyway, I'll add iterators on our ever growing todo list of documentation topics.

    Regarding your question: I don't think so (Niklas may correct me, if I'm mistaken). In order to use the iterator, the class to iterate needs to have one defined. Like BaseArray



  • On 24/10/2014 at 04:57, xxxxxxxx wrote:

    That's correct. Only the new array classes support iterators, the old ones don't. That's one of the many advantages the new classes have, and one more reason to use them whenever possible.


Log in to reply