Using fibers to simplify enumerators, part 4: Filtering

Date:January 3, 2005 / year-entry #1
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050103-00/?p=36823
Comments:    1
Summary:One type of higher-order enumeration is filtering, where one enumerator takes the output of another enumerator and removes some elements. In a producer-driven enumerator, you would implement filtering by substituting a new callback function that responds to callbacks on behalf of the client for items that should be filtered, and forwarding callbacks to the client...

One type of higher-order enumeration is filtering, where one enumerator takes the output of another enumerator and removes some elements.

In a producer-driven enumerator, you would implement filtering by substituting a new callback function that responds to callbacks on behalf of the client for items that should be filtered, and forwarding callbacks to the client for items that are not filtered.

In a consumer-driven enumerator, you would implement composition by wrapping the enumerator inside another enumerator which drives the inner enumerator and forwards items that it wishes the caller to see.

A fiber-based enumerator behaves more like a consumer-driven enumerator, but,with easier state management.

Let's write a filter enumerator that removes all directories and suppresses recursing into them.

class FilteredEnumerator : public FiberEnumerator {
public:
 FilteredEnumerator(LPCTSTR pszDir) : m_e(pszDir) { }

 LPCTSTR GetCurDir()
    { return m_e.GetCurDir(); }
 LPCTSTR GetCurPath()
    { return m_e.GetCurPath(); }
 const WIN32_FIND_DATA* GetCurFindData()
    { return m_e.GetCurFindData(); }

private:
 void FiberProc();

private:
 DirectoryTreeEnumerator m_e;
};

void FilteredEnumerator::FiberProc()
{
 FEFOUND fef;
 while ((fef = m_e.Next()) != FEF_DONE) {
  FERESULT fer;
  if (fef == FEF_DIR) {
   fer = FER_SKIP; // don't recurse into directories
  } else {
   fer = Produce(fef);
  }
  m_e.SetResult(fer);
 }
}

To produce items from this filtered enumerator, we run the real enumerator (m_e) and remove all directories, preventing them from being propagated to the filter's consumer and just responding "skip it" to the real enumerator.

You can test out this filtered enumerator with the same TestWalk function we've been using for the past few days. The only change you'll need to make is to the main function:

int __cdecl main(int argc, char **argv)
{
 ConvertThreadToFiber(NULL);
 FilteredEnumerator e(TEXT("."));
 TestWalk(&e);
 ConvertFiberToThread();
 return 0;
}

Observe that the program no longer recurses into subdirectories. It just tallies the sizes of the files in the current directory.

Next time, composition.


Comments (1)
  1. A while back there was an article in MSDN magazine about wrapping up the unmaged fibers API to implement…

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index