libimobiledevice / libplist

A library to handle Apple Property List format in binary or XML
https://libimobiledevice.org
GNU Lesser General Public License v2.1
528 stars 304 forks source link

Add PList::Array iterator member functions #218

Closed daaannieeellll closed 1 year ago

daaannieeellll commented 1 year ago

... returning both iterators and const_iterators:

Cryptiiiic commented 1 year ago

@daaannieeellll this needs to be merged lol I need this for my tooling. Also I have a question, is it a general c++ issue or exclusive to apple clang: for each is looking for begin/end method but can't use the capitalized ones Begin/End? I had to derive a custom class and define a lower case begin/end method than call Begin/End respectively.

daaannieeellll commented 1 year ago

@Cryptiiiic from my understanding range based for loops require both a begin and end function, either as member functions or found using argument-dependent lookup (ADL). This does not depend on the compiler, as it is standard behavior.

For this PR, however, I decided to use the capitalized function names for it to be more likely to be merged (looking at the used cpp code-style). Though, I could consider another PR with added lowercase members.

You can use the iterator-based for loop as an alternative for the range-based for loop:

{
  auto &&range = /* range-expression */;
  auto begin = range.Begin() ;
  auto end = range.End() ;
  for ( ; begin != end; ++begin)
  {
    /* range-declaration */ = *begin;
    /* loop-statement */
  }
}

Or if you’re using for-each, you can just provide the Begin and End functions as begin and end.

std::for_each(
  /* PList object */.Begin(),
  /* PList object */.End(),
  /* function */
);
nikias commented 1 year ago

I'm not using the C++ extensions to be honest, but if you feel the lowercase ones would be more helpful, we can use the lowercase ones. This would allow stuff like this, if I am not mistaken:

for each (auto item in my_PList_array) {
}
daaannieeellll commented 1 year ago

Absolutely. So using these lowercase function names is more convenient and more readable. If you agree I can update this PR so that it contains both capitalized and lowercase function names (as renaming them might introduce breaking changes).

nikias commented 1 year ago

Go ahead. As for begin and end I don't see anything breaking.

nikias commented 1 year ago

Merged after rebase.