mirror of https://github.com/pvnis/srsRAN_4G.git
creation of a scope exit standalone file. Created a new folder ADT. Added test for scope exit.
parent
6c12728cc7
commit
0f67bee556
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2013-2020 Software Radio Systems Limited
|
||||
*
|
||||
* This file is part of srsLTE.
|
||||
*
|
||||
* srsLTE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* srsLTE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* A copy of the GNU Affero General Public License can be found in
|
||||
* the LICENSE file in the top-level directory of this distribution
|
||||
* and at http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRSLTE_SCOPE_EXIT_H
|
||||
#define SRSLTE_SCOPE_EXIT_H
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace srslte {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename Callable>
|
||||
struct scope_exit {
|
||||
explicit scope_exit(Callable&& f_) : exit_function(std::forward<Callable>(f_)) {}
|
||||
scope_exit(scope_exit&& rhs) noexcept : exit_function(std::move(rhs.exit_function)), active(rhs.active)
|
||||
{
|
||||
rhs.release();
|
||||
}
|
||||
scope_exit(const scope_exit&) = delete;
|
||||
scope_exit& operator=(const scope_exit&) = delete;
|
||||
scope_exit& operator=(scope_exit&&) noexcept = delete;
|
||||
~scope_exit()
|
||||
{
|
||||
if (active) {
|
||||
exit_function();
|
||||
}
|
||||
}
|
||||
|
||||
void release() { active = false; }
|
||||
|
||||
private:
|
||||
bool active = true;
|
||||
Callable exit_function;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/**
|
||||
* @brief Defers callable call to scope exit
|
||||
* @tparam Callable Any type with a call operator
|
||||
* @param callable function that is called at scope exit
|
||||
* @return object that has to be stored in a local variable
|
||||
*/
|
||||
template <typename Callable>
|
||||
detail::scope_exit<Callable> make_scope_exit(Callable&& callable)
|
||||
{
|
||||
return detail::scope_exit<Callable>{std::forward<Callable>(callable)};
|
||||
}
|
||||
#define DEFER(FUNC) auto on_exit_call = make_scope_exit([&]() { FUNC })
|
||||
|
||||
} // namespace srslte
|
||||
|
||||
#endif // SRSLTE_SCOPE_EXIT_H
|
@ -0,0 +1,28 @@
|
||||
#
|
||||
# Copyright 2013-2020 Software Radio Systems Limited
|
||||
#
|
||||
# This file is part of srsLTE
|
||||
#
|
||||
# srsLTE is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of
|
||||
# the License, or (at your option) any later version.
|
||||
#
|
||||
# srsLTE is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# A copy of the GNU Affero General Public License can be found in
|
||||
# the LICENSE file in the top-level directory of this distribution
|
||||
# and at http://www.gnu.org/licenses/.
|
||||
#
|
||||
|
||||
#######################################################################
|
||||
# ADT TESTS
|
||||
#######################################################################
|
||||
|
||||
add_executable(scope_exit_test scope_exit_test.cc)
|
||||
target_link_libraries(scope_exit_test srslte_common)
|
||||
add_test(scope_exit_test scope_exit_test)
|
||||
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2013-2020 Software Radio Systems Limited
|
||||
*
|
||||
* This file is part of srsLTE.
|
||||
*
|
||||
* srsLTE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* srsLTE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* A copy of the GNU Affero General Public License can be found in
|
||||
* the LICENSE file in the top-level directory of this distribution
|
||||
* and at http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "srslte/adt/scope_exit.h"
|
||||
#include "srslte/common/test_common.h"
|
||||
|
||||
int test_scope_exit(int* value)
|
||||
{
|
||||
auto test_exit = srslte::make_scope_exit([value]() {
|
||||
*value = SRSLTE_SUCCESS;
|
||||
printf("Finished successfully\n");
|
||||
});
|
||||
|
||||
// TEST: simple scope_exit call
|
||||
int nof_calls = 0;
|
||||
{
|
||||
auto scope_exit = srslte::make_scope_exit([&]() {
|
||||
nof_calls++;
|
||||
printf("TEST1: Exited first scope\n");
|
||||
});
|
||||
printf("TEST1: Entered first scope\n");
|
||||
}
|
||||
TESTASSERT(nof_calls == 1);
|
||||
|
||||
// TEST: Cancelling scope_exit call via release()
|
||||
nof_calls = 0;
|
||||
{
|
||||
auto scope_exit = srslte::make_scope_exit([&]() {
|
||||
nof_calls++;
|
||||
printf("TEST2: This should not be called\n");
|
||||
});
|
||||
scope_exit.release();
|
||||
printf("TEST2: Entered second scope\n");
|
||||
}
|
||||
TESTASSERT(nof_calls == 0);
|
||||
|
||||
// TEST: move is safe
|
||||
nof_calls = 0;
|
||||
{
|
||||
printf("TEST3: Entered third scope\n");
|
||||
auto scope_exit = srslte::make_scope_exit([&]() { nof_calls++; });
|
||||
auto scope_exit2{std::move(scope_exit)};
|
||||
}
|
||||
TESTASSERT(nof_calls == 1);
|
||||
|
||||
return SRSLTE_SUCCESS;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ret = SRSLTE_ERROR;
|
||||
TESTASSERT(test_scope_exit(&ret) == SRSLTE_SUCCESS);
|
||||
TESTASSERT(ret == SRSLTE_SUCCESS);
|
||||
}
|
Loading…
Reference in New Issue