base: Get rid the Callback type.
This leaves only the lambda/std::function based CallbackQueue2, soon to be renamed just CallbackQueue. Issue-on: https://gem5.atlassian.net/browse/GEM5-698 Change-Id: I4e2fd3b7b684c414be6db0e268284ab63e6cfdff Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/32650 Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Jason Lowe-Power <power.jg@gmail.com> Maintainer: Jason Lowe-Power <power.jg@gmail.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -38,8 +38,6 @@ Source('bitfield.cc')
|
||||
GTest('bitfield.test', 'bitfield.test.cc', 'bitfield.cc')
|
||||
Source('imgwriter.cc')
|
||||
Source('bmpwriter.cc')
|
||||
Source('callback.cc')
|
||||
GTest('callback.test', 'callback.test.cc', 'callback.cc')
|
||||
Source('channel_addr.cc')
|
||||
Source('cprintf.cc', add_tags='gtest lib')
|
||||
GTest('cprintf.test', 'cprintf.test.cc')
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2003-2005 The Regents of The University of Michigan
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met: redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer;
|
||||
* redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution;
|
||||
* neither the name of the copyright holders nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "base/callback.hh"
|
||||
|
||||
CallbackQueue::~CallbackQueue()
|
||||
{
|
||||
queue::iterator i = callbacks.begin();
|
||||
queue::iterator end = callbacks.end();
|
||||
for (; i != end; ++i)
|
||||
(*i)->autoDestruct();
|
||||
}
|
||||
@@ -31,54 +31,6 @@
|
||||
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Generic callback class. This base class provides a virtual process
|
||||
* function that gets called when the callback queue is processed.
|
||||
*/
|
||||
class Callback
|
||||
{
|
||||
protected:
|
||||
friend class CallbackQueue;
|
||||
virtual void autoDestruct() {}
|
||||
|
||||
public:
|
||||
/**
|
||||
* virtualize the destructor to make sure that the correct one
|
||||
* gets called.
|
||||
*/
|
||||
virtual ~Callback() {}
|
||||
|
||||
/**
|
||||
* virtual process function that is invoked when the callback
|
||||
* queue is executed.
|
||||
*/
|
||||
virtual void process() = 0;
|
||||
};
|
||||
|
||||
/// Helper template class to turn a simple class member function into
|
||||
/// a callback.
|
||||
template <class T, void (T::* F)()>
|
||||
class MakeCallback : public Callback
|
||||
{
|
||||
protected:
|
||||
T *object;
|
||||
const bool autoDestroy;
|
||||
|
||||
void autoDestruct() { if (autoDestroy) delete this; }
|
||||
|
||||
public:
|
||||
MakeCallback(T *o, bool auto_destroy = false)
|
||||
: object(o), autoDestroy(auto_destroy)
|
||||
{ }
|
||||
|
||||
MakeCallback(T &o, bool auto_destroy = false)
|
||||
: object(&o), autoDestroy(auto_destroy)
|
||||
{ }
|
||||
|
||||
void process() { (object->*F)(); }
|
||||
};
|
||||
|
||||
class CallbackQueue2 : public std::list<std::function<void()>>
|
||||
{
|
||||
@@ -95,76 +47,4 @@ class CallbackQueue2 : public std::list<std::function<void()>>
|
||||
}
|
||||
};
|
||||
|
||||
class CallbackQueue
|
||||
{
|
||||
protected:
|
||||
/**
|
||||
* Simple typedef for the data structure that stores all of the
|
||||
* callbacks.
|
||||
*/
|
||||
typedef std::list<Callback *> queue;
|
||||
|
||||
/**
|
||||
* List of all callbacks. To be called in fifo order.
|
||||
*/
|
||||
queue callbacks;
|
||||
|
||||
public:
|
||||
~CallbackQueue();
|
||||
std::string name() const { return "CallbackQueue"; }
|
||||
|
||||
/**
|
||||
* Add a callback to the end of the queue
|
||||
* @param callback the callback to be added to the queue
|
||||
*/
|
||||
void
|
||||
add(Callback *callback)
|
||||
{
|
||||
callbacks.push_back(callback);
|
||||
}
|
||||
|
||||
template <class T, void (T::* F)()>
|
||||
void
|
||||
add(T *obj)
|
||||
{
|
||||
add(new MakeCallback<T, F>(obj, true));
|
||||
}
|
||||
|
||||
template <class T, void (T::* F)()>
|
||||
void
|
||||
add(T &obj)
|
||||
{
|
||||
add(new MakeCallback<T, F>(&obj, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Find out if there are any callbacks in the queue
|
||||
*/
|
||||
bool empty() const { return callbacks.empty(); }
|
||||
|
||||
/**
|
||||
* process all callbacks
|
||||
*/
|
||||
void
|
||||
process()
|
||||
{
|
||||
queue::iterator i = callbacks.begin();
|
||||
queue::iterator end = callbacks.end();
|
||||
|
||||
while (i != end) {
|
||||
(*i)->process();
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* clear the callback queue
|
||||
*/
|
||||
void
|
||||
clear()
|
||||
{
|
||||
callbacks.clear();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __BASE_CALLBACK_HH__
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2019 The Regents of the University of California
|
||||
* All rights reserved
|
||||
*
|
||||
* The license below extends only to copyright in the software and shall
|
||||
* not be construed as granting a license to any other intellectual
|
||||
* property including but not limited to intellectual property relating
|
||||
* to a hardware implementation of the functionality of the software
|
||||
* licensed hereunder. You may use the software subject to the license
|
||||
* terms below provided that you ensure that this notice is replicated
|
||||
* unmodified and in its entirety in all distributions of the software,
|
||||
* modified or unmodified, in source code or in binary form.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met: redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer;
|
||||
* redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution;
|
||||
* neither the name of the copyright holders nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "base/callback.hh"
|
||||
|
||||
class CallbackImpl : public Callback
|
||||
{
|
||||
public:
|
||||
bool processed = false;
|
||||
void process()
|
||||
{
|
||||
processed = true;
|
||||
}
|
||||
};
|
||||
|
||||
class MockClass
|
||||
{
|
||||
public:
|
||||
bool methodExecuted = false;
|
||||
void method()
|
||||
{
|
||||
methodExecuted = true;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(CallbackQueueTest, GetName)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
EXPECT_EQ("CallbackQueue", callbackQueue.name());
|
||||
}
|
||||
|
||||
TEST(CallbackQueueTest, IsEmpty)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
EXPECT_TRUE(callbackQueue.empty());
|
||||
}
|
||||
|
||||
TEST(CallbackQueueTest, IsNotEmpty)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
CallbackImpl impl;
|
||||
callbackQueue.add(&impl);
|
||||
EXPECT_FALSE(callbackQueue.empty());
|
||||
}
|
||||
|
||||
TEST(CallbackQueueTest, AddOneAndProcess)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
CallbackImpl impl;
|
||||
callbackQueue.add(&impl);
|
||||
EXPECT_FALSE(impl.processed);
|
||||
callbackQueue.process();
|
||||
EXPECT_TRUE(impl.processed);
|
||||
// Processing a queue does not clear it.
|
||||
EXPECT_FALSE(callbackQueue.empty());
|
||||
}
|
||||
|
||||
TEST(CallbackQueueTest, AddManyAndProcess)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
CallbackImpl impl1;
|
||||
CallbackImpl impl2;
|
||||
CallbackImpl impl3;
|
||||
CallbackImpl impl4;
|
||||
callbackQueue.add(&impl1);
|
||||
callbackQueue.add(&impl2);
|
||||
callbackQueue.add(&impl3);
|
||||
callbackQueue.add(&impl4);
|
||||
EXPECT_FALSE(impl1.processed);
|
||||
EXPECT_FALSE(impl2.processed);
|
||||
EXPECT_FALSE(impl3.processed);
|
||||
EXPECT_FALSE(impl4.processed);
|
||||
callbackQueue.process();
|
||||
EXPECT_TRUE(impl1.processed);
|
||||
EXPECT_TRUE(impl2.processed);
|
||||
EXPECT_TRUE(impl3.processed);
|
||||
EXPECT_TRUE(impl4.processed);
|
||||
EXPECT_FALSE(callbackQueue.empty());
|
||||
}
|
||||
|
||||
TEST(CallbackQueueTest, ClearQueue)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
CallbackImpl callbackImpl;
|
||||
callbackQueue.add(&callbackImpl);
|
||||
EXPECT_FALSE(callbackQueue.empty());
|
||||
callbackQueue.clear();
|
||||
EXPECT_TRUE(callbackQueue.empty());
|
||||
}
|
||||
|
||||
TEST(CallbackQueueTest, MakeCallbackAddByReference)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
MockClass mockClass;
|
||||
EXPECT_FALSE(mockClass.methodExecuted);
|
||||
callbackQueue.add<MockClass, &MockClass::method>(mockClass);
|
||||
callbackQueue.process();
|
||||
EXPECT_TRUE(mockClass.methodExecuted);
|
||||
EXPECT_FALSE(callbackQueue.empty());
|
||||
}
|
||||
|
||||
TEST(CallbackQueueTest, MakeCallbackAddByPointer)
|
||||
{
|
||||
CallbackQueue callbackQueue;
|
||||
MockClass mockClass;
|
||||
EXPECT_FALSE(mockClass.methodExecuted);
|
||||
callbackQueue.add<MockClass, &MockClass::method>(&mockClass);
|
||||
callbackQueue.process();
|
||||
EXPECT_TRUE(mockClass.methodExecuted);
|
||||
EXPECT_FALSE(callbackQueue.empty());
|
||||
}
|
||||
Reference in New Issue
Block a user