Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Callback Enumeration APIs & the Input Iterator Concept


February 04:

Listing 2: child_window_sequence class.

/* /////////////////////////////////////////////////////////////
 * Extracts from child_window_sequence_test.cpp
 * www:   http://www.synesis.com.au/winstl
 *        http://www.winstl.org/
 * Copyright (C) 2002, Synesis Software Pty Ltd.
 * (Licensed under the Synesis Software Standard Source License:
 *  http://www.synesis.com.au/licenses/ssssl.html)
 * ////////////////////////////////////////////////////////// */

/* Holds state of enumeration and relationship between fibers */
struct cws_fiber_data
{
  LPVOID  main_fiber, worker_fiber;
  HWND    hwndParent, hwndChild;

  cws_fiber_data()
    : main_fiber(0), worker_fiber(0)
    , hwndParent(0), hwndChild(0)
  {}
};
/* The iterator class */
class child_window_sequence_const_iterator
{
public:
  typedef child_window_sequence_const_iterator  class_type;
// Construction
public:
  child_window_sequence_const_iterator(cws_fiber_data *data)
    : m_data(data)
  {}
  // Only Input-Iterator Concept supported, so  implement move semantics.
  child_window_sequence_const_iterator(class_type &rhs);
  class_type &operator =(class_type &);
  ~child_window_sequence_const_iterator()
  {
    // Clean up here, in case enumeration not completed.
    if(m_data != 0)
    {
      ::DeleteFiber(m_data->worker_fiber);
      delete m_data;
    }
  }
  HWND operator *() const
  {
    return m_data->hwndChild;
  }
  child_window_sequence_const_iterator &operator ++()
  {
    // Switch to the enumeration fiber
    ::SwitchToFiber(m_data->worker_fiber);
    // Having switched back, hwndChild will contain next
    // child, or NULL, in which case enumeration complete.
    if(m_data->hwndChild == 0)
    {
      ::DeleteFiber(m_data->worker_fiber);
      delete m_data;
      m_data = 0;
    }
    return *this;
  }
// Comparison
public:
  bool operator ==(class_type const &rhs) const
  {
    // == if both NULL, or both non-NULL and have same child.
    return  ( m_data == 0 && 
              rhs.m_data == 0) || 
            ( m_data != 0 && 
              rhs.m_data != 0 && 
              m_data->hwndChild == rhs.m_data->hwndChild);
  }
  bool operator !=(class_type const &rhs) const;
// Members
protected:
  cws_fiber_data  *m_data;
};
class child_window_sequence
{
public:
  typedef child_window_sequence                 class_type;
  typedef child_window_sequence_const_iterator  const_iterator;
// Construction
public:
  child_window_sequence(HWND hwnd)
    : m_fiberMain(GetMainFiber()), m_hwnd(hwnd)
  {}
// Iteration
public:
  const_iterator begin() const
  {
    // (i) Create a shared area
    cws_fiber_data  *data = new cws_fiber_data;
    // (ii) Create the callback fiber
    data->main_fiber   = m_fiberMain;
    data->worker_fiber = ::CreateFiber(0, WorkerProc, data);
    data->hwndParent   = m_hwnd;
    // (iii) Switch to fiber and start the enumeration
    ::SwitchToFiber(data->worker_fiber);
    // (iv) set the first window and the fiber data into the iterator
    if(data->hwndChild == 0)
    {
      ::DeleteFiber(data->worker_fiber);
      delete data;
      data = 0;
    }
    return child_window_sequence_const_iterator(data);
  }
  const_iterator end() const
  {
    return const_iterator(0);
  }
// Implementation
protected:
  // This reduces fragility of Fibers somewhat, by ensuring all
  // instances of sequence class share a particular main fiber.
  static LPVOID GetMainFiber()
  {
    static LPVOID s_fiberMain = ::ConvertThreadToFiber(0);
    return s_fiberMain;
  }
  static void WINAPI WorkerProc(PVOID lpParam)
  {
    // This makes enumeration call, then scopes enumeration via fiber 
    // exchanges between this fiber and main fiber (where iterators are used)
    cws_fiber_data  &data = *(cws_fiber_data*)lpParam;
    // Start the enumeration
    ::EnumChildWindows(data.hwndParent, EnumChildProc, (LPARAM)lpParam);
    // End enumeration by setting child to NULL ...
    data.hwndChild = 0;
    // ... and switch back for the last time (main will delete this fiber).
    ::SwitchToFiber(data.main_fiber);
  }
  static BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
  {
    cws_fiber_data  &data = *(cws_fiber_data*)lParam;
    // Place this result in the data ...
    data.hwndChild = hwnd;
    // ... and switch back to main fiber
    ::SwitchToFiber(data.main_fiber);
    return true;
  }
// Members
protected:
  LPVOID  m_fiberMain;  // The main (/ ctor caller) fiber
  HWND    m_hwnd;       // Window whose children are enumerated
};


	
		

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.