Skip to content

Commit

Permalink
Start/stop guards
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 27, 2024
1 parent 64912ce commit 90f1ad5
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions nano/test_common/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,28 @@

namespace nano::test
{
template <class... Ts>
// Concept for checking .stop() method
template <typename T>
concept stoppable = requires (T a) {
{
a.stop ()
} -> std::same_as<void>;
};

// Concept for checking if it's callable
template <typename T>
concept callable = requires (T a) {
{
a ()
} -> std::same_as<void>;
};

template <stoppable... Ts>
class start_stop_guard
{
public:
explicit start_stop_guard (Ts &... refs_a) :
refs{ std::forward<Ts &> (refs_a)... }
refs{ std::forward<decltype (refs_a)> (refs_a)... }
{
std::apply ([] (Ts &... refs) { (refs.start (), ...); }, refs);
}
Expand All @@ -136,12 +152,12 @@ class start_stop_guard
std::tuple<Ts &...> refs;
};

template <class... Ts>
template <stoppable... Ts>
class stop_guard
{
public:
explicit stop_guard (Ts &... refs_a) :
refs{ std::forward<Ts &> (refs_a)... }
refs{ std::forward<decltype (refs_a)> (refs_a)... }
{
}

Expand All @@ -153,6 +169,24 @@ class stop_guard
private:
std::tuple<Ts &...> refs;
};

template <callable... Ts>
class call_guard
{
public:
explicit call_guard (Ts &&... calls_a) :
calls{ std::forward<decltype (calls_a)> (calls_a)... }
{
}

~call_guard ()
{
std::apply ([] (Ts &... refs) { (refs (), ...); }, calls);
}

private:
std::tuple<Ts...> calls;
};
}

/* Convenience globals for gtest projects */
Expand Down

0 comments on commit 90f1ad5

Please sign in to comment.