mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-02-05 13:28:16 +00:00
a/util: Fix missing include for generic callback structure.
Also extend the tests to actually include invocation of callbacks, and rename a template parameter for clearer usage.
This commit is contained in:
parent
8b700b0063
commit
ef7b1133c6
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2021, Collabora, Ltd.
|
||||
// Copyright 2021-2023, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
#include <cstdint>
|
||||
|
||||
namespace xrt::auxiliary::util {
|
||||
template <typename CallbackType, typename EventType> struct GenericCallbacks;
|
||||
|
@ -20,7 +22,7 @@ namespace detail {
|
|||
/*!
|
||||
* @brief Element type stored in @ref GenericCallbacks, for internal use only.
|
||||
*/
|
||||
template <typename CallbackType, typename MaskType = uint32_t> struct GenericCallbackEntry
|
||||
template <typename CallbackType, typename MaskType = std::uint32_t> struct GenericCallbackEntry
|
||||
{
|
||||
CallbackType callback;
|
||||
MaskType event_mask;
|
||||
|
@ -83,17 +85,17 @@ namespace detail {
|
|||
* limit to a single instance in a collection.
|
||||
*
|
||||
* @tparam CallbackType the function pointer type to store for each callback.
|
||||
* @tparam EventType the event enum type.
|
||||
* @tparam EventBitflagType the event enum type.
|
||||
*/
|
||||
template <typename CallbackType, typename EventType> struct GenericCallbacks
|
||||
template <typename CallbackType, typename EventBitflagType> struct GenericCallbacks
|
||||
{
|
||||
|
||||
public:
|
||||
static_assert(std::is_integral<EventType>::value || std::is_enum<EventType>::value,
|
||||
static_assert(std::is_integral<EventBitflagType>::value || std::is_enum<EventBitflagType>::value,
|
||||
"Your event type must either be an integer or an enum");
|
||||
using callback_t = CallbackType;
|
||||
using event_t = EventType;
|
||||
using mask_t = detail::mask_from_enum_t<EventType>;
|
||||
using event_t = EventBitflagType;
|
||||
using mask_t = detail::mask_from_enum_t<EventBitflagType>;
|
||||
|
||||
private:
|
||||
static_assert(std::is_integral<mask_t>::value, "Our enum to mask conversion should have produced an integer");
|
||||
|
@ -195,7 +197,7 @@ public:
|
|||
*/
|
||||
template <typename F>
|
||||
int
|
||||
invokeCallbacks(EventType event, F &&invoker)
|
||||
invokeCallbacks(EventBitflagType event, F &&invoker)
|
||||
{
|
||||
bool needPurge = false;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2021, Collabora, Ltd.
|
||||
// Copyright 2021-2023, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
|
@ -14,8 +14,8 @@ using xrt::auxiliary::util::GenericCallbacks;
|
|||
|
||||
enum class MyEvent
|
||||
{
|
||||
ACQUIRED,
|
||||
LOST,
|
||||
ACQUIRED = 1u << 0u,
|
||||
LOST = 1u << 1u,
|
||||
};
|
||||
|
||||
using mask_t = std::underlying_type_t<MyEvent>;
|
||||
|
@ -78,6 +78,10 @@ TEST_CASE("u_generic_callbacks")
|
|||
&numAcquired, 0, 1));
|
||||
CHECK(callbacks.contains(increment_userdata_int, (mask_t)MyEvent::ACQUIRED,
|
||||
&numAcquired));
|
||||
|
||||
// LOST callback should still be there to remove
|
||||
CHECK(1 == callbacks.removeCallback(increment_userdata_int, (mask_t)MyEvent::LOST,
|
||||
&numLost));
|
||||
}
|
||||
SECTION("large max_remove")
|
||||
{
|
||||
|
@ -85,6 +89,10 @@ TEST_CASE("u_generic_callbacks")
|
|||
&numAcquired, 0, 3));
|
||||
CHECK_FALSE(callbacks.contains(increment_userdata_int, (mask_t)MyEvent::ACQUIRED,
|
||||
&numAcquired));
|
||||
|
||||
// LOST callback should still be there to remove
|
||||
CHECK(1 == callbacks.removeCallback(increment_userdata_int, (mask_t)MyEvent::LOST,
|
||||
&numLost));
|
||||
}
|
||||
SECTION("num_skip")
|
||||
{
|
||||
|
@ -97,8 +105,63 @@ TEST_CASE("u_generic_callbacks")
|
|||
&numAcquired, 1));
|
||||
CHECK(callbacks.contains(increment_userdata_int, (mask_t)MyEvent::ACQUIRED,
|
||||
&numAcquired));
|
||||
|
||||
// LOST callback should still be there to remove
|
||||
CHECK(1 == callbacks.removeCallback(increment_userdata_int, (mask_t)MyEvent::LOST,
|
||||
&numLost));
|
||||
}
|
||||
SECTION("invoke acquired")
|
||||
{
|
||||
CHECK(2 == callbacks.invokeCallbacks(MyEvent::ACQUIRED, invoker));
|
||||
CHECK(2 == numAcquired);
|
||||
CHECK(0 == numLost);
|
||||
{
|
||||
INFO("should have removed themselves");
|
||||
CHECK_FALSE(callbacks.contains(increment_userdata_int,
|
||||
(mask_t)MyEvent::ACQUIRED, &numAcquired));
|
||||
}
|
||||
|
||||
{
|
||||
INFO("LOST callbacks should still be there");
|
||||
CHECK(callbacks.contains(increment_userdata_int, (mask_t)MyEvent::LOST,
|
||||
&numLost));
|
||||
CHECK(1 == callbacks.removeCallback(increment_userdata_int,
|
||||
(mask_t)MyEvent::LOST, &numLost));
|
||||
}
|
||||
}
|
||||
SECTION("invoke lost")
|
||||
{
|
||||
CHECK(1 == callbacks.invokeCallbacks(MyEvent::LOST, invoker));
|
||||
CHECK(0 == numAcquired);
|
||||
CHECK(1 == numLost);
|
||||
{
|
||||
INFO("should have removed themselves");
|
||||
CHECK_FALSE(callbacks.contains(increment_userdata_int, (mask_t)MyEvent::LOST,
|
||||
&numLost));
|
||||
}
|
||||
|
||||
{
|
||||
INFO("ACQUIRED callbacks should still be there");
|
||||
CHECK(callbacks.contains(increment_userdata_int, (mask_t)MyEvent::ACQUIRED,
|
||||
&numAcquired));
|
||||
CHECK(2 == callbacks.removeCallback(increment_userdata_int,
|
||||
(mask_t)MyEvent::ACQUIRED, &numAcquired));
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK(1 == callbacks.removeCallback(increment_userdata_int, (mask_t)MyEvent::LOST, &numLost));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum MyCEvent
|
||||
{
|
||||
MY_C_EVENT_ACQUIRE = 1u << 0u,
|
||||
MY_C_EVENT_LOST = 1u << 1u,
|
||||
};
|
||||
|
||||
using c_callback_type = bool (*)(MyCEvent event, void *userdata);
|
||||
|
||||
TEST_CASE("u_generic_callbacks-C")
|
||||
{
|
||||
GenericCallbacks<c_callback_type, MyCEvent> callbacks;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue