Project Description

MFC Collection Utilities is a small library that enables MFC developers to use MFC containers (arrays, lists, maps) with range-based for loops. The library consists of a single header that you include in your MFC projects.

Usage

To be able to use the MFC containers in range-based for loops include the "mfciterators.h" header.

The following sample shows how to iterate over a CStringArray using range-based for loops.

#include "mfciterators.h"

void func(CStringArray const & arr)
{
   for(auto const & str : arr)
   {
      // do something with str
   }
}

For maps you get access to the content through a key-value pair that has two fields: key and value.

The following example shows how to iterate through a CMap<int, CString>.

#include "mfciterators.h"

CMap<int, CString> map;
map.SetAt(1, "one");
map.SetAt(2, "two");
map.SetAt(3, "three");
for(auto const & kvp : map)
{
   // do something with the key-value pair
   TRACE("%d-%s\n", kvp.key, kvp.value);
}

Motivation

C++11 has provided support for range-based for loops. They allow iterating over the elements of a range without an index.

std::vector<int> v = {1, 2, 3, 4, 5};
for(auto& e : v)
  e *= 2;

However, if you try the above MFC code you get some errors because the compiler is looking for a begin() and end() function that provides access to the first and last element of the range:

1>error C3312: no callable 'begin' function found for type 'CStringArray'
1>error C3312: no callable 'end' function found for type 'CStringArray'

MFC does not define such functions for its containers. The MFC Collections Utilities libraries provides this for all MFC containers.

Nuget

To simplify project deployment, a nuget package is available: MFC Collection Utilities 1.0.0.

Compiler support

The library requires Visual Studio 2012 or a newer version.

Supported collections

Template collections
Arrays Lists Maps
CArray CList CMap
CTypedPtrArray CTypedPtrList CTypedPtrMap

Non-template collections
Arrays Lists Maps
CObArray CObList CMapPtrToWord
CByteArray CPtrList CMapPtrToPtr
CDWordArray CStringList CMapStringToOb
CPtrArray CMapStringToPtr
CStringArray CMapStringToString
CWordArray CMapWordToOb
CUIntArray CMapWordToPtr

Last edited Oct 28, 2014 at 10:45 PM by MariusBancila, version 8