mariusbancila / mfccollectionutilities

A small library that enables developers to use MFC containers (arrays, lists, maps) with range-based for loops.
GNU General Public License v3.0
14 stars 5 forks source link

Feature req: being able to use MFC Collections in std algorithms #1

Open jpj-verstappen opened 4 years ago

jpj-verstappen commented 4 years ago

Nice work on the range based for loops! Working like a charm. I would like to use the MFC collection classes in the std algorithms, like std::all_of. Looks like I need an iterator for the begin and the end of the array I'm using (CTypedPtrArray). Can someone give an example of how to implement this?

Usage example:

CTypedPtrArray<CObArray, CAppointment*> appointmentArr;
bool bAllAppointmentsToday = std::all_of(...begin..., ....end..., 
    [](CAppointment* app) { return app->IsToday(); });
assert(bAllAppointmentsToday);
mariusbancila commented 4 years ago

This just works with begin() and end().

Here is an example:

CTypedPtrArray<CObArray, IntObject*> arr;
arr.Add(new IntObject(1));
arr.Add(new IntObject(3));
arr.Add(new IntObject(5));
arr.Add(new IntObject(7));

bool all = std::all_of(begin(arr), end(arr),
   [](IntObject* o) { return o->value % 2 == 1; });
Assert::IsTrue(all);

for (auto p : arr)
   delete p;

where IntObject is

struct IntObject : CObject
{
   int value;

   explicit IntObject(int const v) noexcept : value(v) {}

   explicit operator int()
   {
      return value;
   }
};

You can find examples in the unit tests project.