callback.hh revision 2665
12SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A#ifndef __CALLBACK_HH__
322SN/A#define __CALLBACK_HH__
332SN/A
342SN/A#include <list>
352SN/A
36147SN/A/**
371158SN/A * Generic callback class.  This base class provides a virtual process
38147SN/A * function that gets called when the callback queue is processed.
39147SN/A */
40147SN/Aclass Callback
41147SN/A{
422SN/A  public:
43147SN/A    /**
44147SN/A     * virtualize the destructor to make sure that the correct one
45147SN/A     * gets called.
46147SN/A     */
472SN/A    virtual ~Callback() {}
48147SN/A
49147SN/A    /**
50147SN/A     * virtual process function that is invoked when the callback
51147SN/A     * queue is executed.
52147SN/A     */
532SN/A    virtual void process() = 0;
542SN/A};
552SN/A
562SN/Aclass CallbackQueue
572SN/A{
582SN/A  protected:
59147SN/A    /**
60147SN/A     * Simple typedef for the data structure that stores all of the
61147SN/A     * callbacks.
62147SN/A     */
63147SN/A    typedef std::list<Callback *> queue;
64147SN/A
65147SN/A    /**
66147SN/A     * List of all callbacks.  To be called in fifo order.
67147SN/A     */
68147SN/A    queue callbacks;
692SN/A
702SN/A  public:
71147SN/A    /**
72147SN/A     * Add a callback to the end of the queue
73147SN/A     * @param callback the callback to be added to the queue
74147SN/A     */
75147SN/A    void add(Callback *callback)
76147SN/A    {
77147SN/A        callbacks.push_back(callback);
78147SN/A    }
79147SN/A
80147SN/A    /**
81147SN/A     * Find out if there are any callbacks in the queue
82147SN/A     */
832SN/A    bool empty() const { return callbacks.empty(); }
84147SN/A
85147SN/A    /**
86147SN/A     * process all callbacks
87147SN/A     */
88147SN/A    void process()
89147SN/A    {
90147SN/A        queue::iterator i = callbacks.begin();
91147SN/A        queue::iterator end = callbacks.end();
92147SN/A
93147SN/A        while (i != end) {
94147SN/A            (*i)->process();
95147SN/A            ++i;
96147SN/A        }
972SN/A    }
98147SN/A
99147SN/A    /**
100147SN/A     * clear the callback queue
101147SN/A     */
102147SN/A    void clear()
103147SN/A    {
104147SN/A        callbacks.clear();
105147SN/A    }
1062SN/A};
1072SN/A
1081158SN/A/// Helper template class to turn a simple class member function into
1091158SN/A/// a callback.
1101094SN/Atemplate <class T, void (T::* F)()>
1111094SN/Aclass MakeCallback : public Callback
1121094SN/A{
1131094SN/A  private:
1141094SN/A    T *object;
1151094SN/A
1161094SN/A  public:
1171094SN/A    MakeCallback(T *o)
1181158SN/A        : object(o)
1191094SN/A    { }
1201158SN/A
1211094SN/A    void process() { (object->*F)(); }
1221094SN/A};
1231094SN/A
1242SN/A#endif // __CALLBACK_HH__
125