From: Gabe Black Date: Fri, 14 Aug 2020 08:22:34 +0000 (-0700) Subject: base: Get rid the Callback type. X-Git-Tag: v20.1.0.0~283 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1c6992978f435e4c9794a19fc64ddf5fe7319cb5;p=gem5.git 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 Reviewed-by: Jason Lowe-Power Maintainer: Jason Lowe-Power Tested-by: kokoro --- diff --git a/src/base/SConscript b/src/base/SConscript index b7a0c1b28..2b2ad7c57 100644 --- a/src/base/SConscript +++ b/src/base/SConscript @@ -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') diff --git a/src/base/callback.cc b/src/base/callback.cc deleted file mode 100644 index 65197ef08..000000000 --- a/src/base/callback.cc +++ /dev/null @@ -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(); -} diff --git a/src/base/callback.hh b/src/base/callback.hh index cef28c32a..e7afd64e9 100644 --- a/src/base/callback.hh +++ b/src/base/callback.hh @@ -31,54 +31,6 @@ #include #include -#include - -/** - * 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 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> { @@ -95,76 +47,4 @@ class CallbackQueue2 : public std::list> } }; -class CallbackQueue -{ - protected: - /** - * Simple typedef for the data structure that stores all of the - * callbacks. - */ - typedef std::list 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 - void - add(T *obj) - { - add(new MakeCallback(obj, true)); - } - - template - void - add(T &obj) - { - add(new MakeCallback(&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__ diff --git a/src/base/callback.test.cc b/src/base/callback.test.cc deleted file mode 100644 index 3b022c9c0..000000000 --- a/src/base/callback.test.cc +++ /dev/null @@ -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 - -#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); - callbackQueue.process(); - EXPECT_TRUE(mockClass.methodExecuted); - EXPECT_FALSE(callbackQueue.empty()); -} - -TEST(CallbackQueueTest, MakeCallbackAddByPointer) -{ - CallbackQueue callbackQueue; - MockClass mockClass; - EXPECT_FALSE(mockClass.methodExecuted); - callbackQueue.add(&mockClass); - callbackQueue.process(); - EXPECT_TRUE(mockClass.methodExecuted); - EXPECT_FALSE(callbackQueue.empty()); -}