util: backport scope_exit & co. from the Library Fundamentals TS v3

Implementation taken from scope-lite by Martin Moene,
commit 197cd4240069b2b8c393ad0d9eabdee601b606b4

Change-Id: I872b6821504b3270ebdc4c48b7255731fb2efc57
This commit is contained in:
Davide Pesavento committed 2020-08-27 16:20:07 -04:00
1 parent 599623c303
commit 97ee811129
12 files changed
+1534 -95

No files matched your search

+3
View File
@@ -12,6 +12,9 @@ ndn-cxx relies on third-party software, licensed under the following licenses:
- optional-lite by Martin Moene is licensed under the
[Boost Software License 1.0](https://github.com/martinmoene/optional-lite/blob/master/LICENSE.txt)
- scope-lite by Martin Moene is licensed under the
[Boost Software License 1.0](https://github.com/martinmoene/scope-lite/blob/master/LICENSE.txt)
- variant-lite by Martin Moene is licensed under the
[Boost Software License 1.0](https://github.com/martinmoene/variant-lite/blob/master/LICENSE.txt)
-15
View File
@@ -2128,12 +2128,6 @@ EXTERNAL_GROUPS = YES
EXTERNAL_PAGES = YES
# The PERL_PATH should be the absolute path and name of the perl script
# interpreter (i.e. the result of 'which perl').
# The default file (with absolute path) is: /usr/bin/perl.
PERL_PATH = /usr/bin/perl
#---------------------------------------------------------------------------
# Configuration options related to the dot tool
#---------------------------------------------------------------------------
@@ -2147,15 +2141,6 @@ PERL_PATH = /usr/bin/perl
CLASS_DIAGRAMS = YES
# You can define message sequence charts within doxygen comments using the \msc
# command. Doxygen will then run the mscgen tool (see:
# http://www.mcternan.me.uk/mscgen/)) to produce the chart and insert it in the
# documentation. The MSCGEN_PATH tag allows you to specify the directory where
# the mscgen tool resides. If left empty the tool is assumed to be found in the
# default search path.
MSCGEN_PATH =
# You can include diagrams made with dia in doxygen documentation. Doxygen will
# then run dia to produce the diagram and insert it in the documentation. The
# DIA_PATH tag allows you to specify the directory where the dia binary resides.
+20 -23
View File
@@ -24,6 +24,7 @@
#include "ndn-cxx/impl/face-impl.hpp"
#include "ndn-cxx/net/face-uri.hpp"
#include "ndn-cxx/security/signing-helpers.hpp"
#include "ndn-cxx/util/scope.hpp"
#include "ndn-cxx/util/time.hpp"
// NDN_LOG_INIT(ndn.Face) is declared in face-impl.hpp
@@ -261,32 +262,28 @@ Face::doProcessEvents(time::milliseconds timeout, bool keepThread)
m_ioService.reset(); // ensure that run()/poll() will do some work
}
try {
if (timeout < time::milliseconds::zero()) {
// do not block if timeout is negative, but process pending events
m_ioService.poll();
return;
}
auto onThrow = make_scope_fail([this] { m_impl->shutdown(); });
if (timeout > time::milliseconds::zero()) {
m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.schedule(timeout,
[&io = m_ioService, &work = m_impl->m_ioServiceWork] {
io.stop();
work.reset();
});
}
if (keepThread) {
// work will ensure that m_ioService is running until work object exists
m_impl->m_ioServiceWork = make_unique<boost::asio::io_service::work>(m_ioService);
}
m_ioService.run();
if (timeout < 0_ms) {
// do not block if timeout is negative, but process pending events
m_ioService.poll();
return;
}
catch (...) {
m_impl->shutdown();
throw;
if (timeout > 0_ms) {
m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.schedule(timeout,
[&io = m_ioService, &work = m_impl->m_ioServiceWork] {
io.stop();
work.reset();
});
}
if (keepThread) {
// work will ensure that m_ioService is running until work object exists
m_impl->m_ioServiceWork = make_unique<boost::asio::io_service::work>(m_ioService);
}
m_ioService.run();
}
void
+4 -5
View File
@@ -30,9 +30,9 @@
#include "ndn-cxx/security/key-params.hpp"
#include "ndn-cxx/encoding/buffer-stream.hpp"
#include "ndn-cxx/util/random.hpp"
#include "ndn-cxx/util/scope.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/scope_exit.hpp>
#include <cstring>
#define ENSURE_PRIVATE_KEY_LOADED(key) \
@@ -493,12 +493,11 @@ PrivateKey::generateEcKey(uint32_t keySize)
default:
NDN_THROW(std::invalid_argument("Unsupported EC key length " + to_string(keySize)));
}
if (eckey == nullptr)
if (eckey == nullptr) {
NDN_THROW(Error("Failed to set EC curve"));
}
BOOST_SCOPE_EXIT(&eckey) {
EC_KEY_free(eckey);
} BOOST_SCOPE_EXIT_END
auto guard = make_scope_exit([eckey] { EC_KEY_free(eckey); });
#if OPENSSL_VERSION_NUMBER < 0x1010000fL
EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
File diff suppressed because it is too large. Load diff
+5 -9
View File
@@ -21,8 +21,7 @@
#include "ndn-cxx/util/scheduler.hpp"
#include "ndn-cxx/util/impl/steady-timer.hpp"
#include <boost/scope_exit.hpp>
#include "ndn-cxx/util/scope.hpp"
namespace ndn {
namespace scheduler {
@@ -145,15 +144,12 @@ Scheduler::executeEvent(const boost::system::error_code& error)
return;
}
auto guard = make_scope_exit([this] {
m_isEventExecuting = false;
scheduleNext();
});
m_isEventExecuting = true;
// ASan reports a stack-use-after-scope on armhf when
// BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT
BOOST_SCOPE_EXIT(this_) {
this_->m_isEventExecuting = false;
this_->scheduleNext();
} BOOST_SCOPE_EXIT_END
// process all expired events
auto now = time::steady_clock::now();
while (!m_queue.empty()) {
+42
View File
@@ -0,0 +1,42 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2020 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library 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 Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#ifndef NDN_CXX_UTIL_SCOPE_HPP
#define NDN_CXX_UTIL_SCOPE_HPP
#define scope_CONFIG_SELECT_SCOPE scope_SCOPE_NONSTD
#include "ndn-cxx/util/nonstd/scope-lite.hpp"
namespace ndn {
using ::nonstd::scope_exit;
using ::nonstd::scope_fail;
using ::nonstd::scope_success;
using ::nonstd::unique_resource;
using ::nonstd::make_scope_exit;
using ::nonstd::make_scope_fail;
using ::nonstd::make_scope_success;
using ::nonstd::make_unique_resource_checked;
} // namespace ndn
#endif // NDN_CXX_UTIL_SCOPE_HPP
+14 -20
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2019 Regents of the University of California.
* Copyright (c) 2013-2020 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -22,6 +22,7 @@
#ifndef NDN_UTIL_SIGNAL_SIGNAL_HPP
#define NDN_UTIL_SIGNAL_SIGNAL_HPP
#include "ndn-cxx/util/scope.hpp"
#include "ndn-cxx/util/signal/connection.hpp"
#include <list>
@@ -219,30 +220,23 @@ Signal<Owner, TArgs...>::operator()(const TArgs&... args)
return;
}
auto it = m_slots.begin();
auto last = std::prev(m_slots.end());
auto guard = make_scope_exit([this] { m_isExecuting = false; });
m_isExecuting = true;
try {
bool isLast = false;
while (!isLast) {
m_currentSlot = it;
isLast = it == last;
auto it = m_slots.begin();
auto last = std::prev(m_slots.end());
bool isLast = false;
while (!isLast) {
m_currentSlot = it;
isLast = it == last;
m_currentSlot->handler(args...);
m_currentSlot->handler(args...);
if (m_currentSlot == m_slots.end())
it = m_slots.erase(it);
else
++it;
}
if (m_currentSlot == m_slots.end())
it = m_slots.erase(it);
else
++it;
}
catch (...) {
m_isExecuting = false;
throw;
}
m_isExecuting = false;
}
template<typename Owner, typename ...TArgs>
@@ -21,12 +21,11 @@
#include "ndn-cxx/security/certificate-fetcher-offline.hpp"
#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
#include "ndn-cxx/util/scope.hpp"
#include "tests/boost-test.hpp"
#include "tests/unit/security/validator-fixture.hpp"
#include <boost/scope_exit.hpp>
namespace ndn {
namespace security {
inline namespace v2 {
@@ -49,15 +48,13 @@ using CertificateFetcherOfflineFixture = HierarchicalValidatorFixture<Validation
BOOST_FIXTURE_TEST_SUITE(TestCertificateFetcherOffline, CertificateFetcherOfflineFixture)
typedef boost::mpl::vector<Interest, Data> Packets;
using Packets = boost::mpl::vector<Interest, Data>;
BOOST_AUTO_TEST_CASE_TEMPLATE(Validate, Packet, Packets)
{
// Can't set CanBePrefix on Interests in this test case because of template
// TODO: Remove in #4582
BOOST_SCOPE_EXIT(void) {
Interest::s_errorIfCanBePrefixUnset = true;
} BOOST_SCOPE_EXIT_END
auto guard = make_scope_exit([] { Interest::s_errorIfCanBePrefixUnset = true; });
Interest::s_errorIfCanBePrefixUnset = false;
Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Packet");
@@ -20,12 +20,12 @@
*/
#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
#include "ndn-cxx/util/scope.hpp"
#include "tests/boost-test.hpp"
#include "tests/unit/security/validator-fixture.hpp"
#include <boost/mpl/vector.hpp>
#include <boost/scope_exit.hpp>
namespace ndn {
namespace security {
@@ -38,15 +38,13 @@ BOOST_AUTO_TEST_SUITE(Security)
BOOST_FIXTURE_TEST_SUITE(TestValidationPolicySimpleHierarchy,
HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy>)
typedef boost::mpl::vector<Interest, Data> Packets;
using Packets = boost::mpl::vector<Interest, Data>;
BOOST_AUTO_TEST_CASE_TEMPLATE(Validate, Packet, Packets)
{
// Can't set CanBePrefix on Interests in this test case because of template
// TODO: Remove in #4582
BOOST_SCOPE_EXIT(void) {
Interest::s_errorIfCanBePrefixUnset = true;
} BOOST_SCOPE_EXIT_END
auto guard = make_scope_exit([] { Interest::s_errorIfCanBePrefixUnset = true; });
Interest::s_errorIfCanBePrefixUnset = false;
Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Sub2/Packet");
+3 -6
View File
@@ -24,8 +24,7 @@
#include "ndn-cxx/security/impl/openssl.hpp"
#include "ndn-cxx/util/io.hpp"
#include <boost/scope_exit.hpp>
#include "ndn-cxx/util/scope.hpp"
namespace ndn {
namespace ndnsec {
@@ -42,11 +41,9 @@ ndnsec_export(int argc, char** argv)
std::string output;
std::string password;
// ASan reports a stack-use-after-scope on armhf when
// BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT
BOOST_SCOPE_EXIT(&password) {
auto guard = make_scope_exit([&password] {
OPENSSL_cleanse(&password.front(), password.size());
} BOOST_SCOPE_EXIT_END
});
po::options_description visibleOptDesc(
"Usage: ndnsec export [-h] [-o FILE] [-P PASSPHRASE] [-i|-k|-c] NAME\n"
+3 -6
View File
@@ -24,8 +24,7 @@
#include "ndn-cxx/security/impl/openssl.hpp"
#include "ndn-cxx/util/io.hpp"
#include <boost/scope_exit.hpp>
#include "ndn-cxx/util/scope.hpp"
namespace ndn {
namespace ndnsec {
@@ -38,11 +37,9 @@ ndnsec_import(int argc, char** argv)
std::string input;
std::string password;
// ASan reports a stack-use-after-scope on armhf when
// BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT
BOOST_SCOPE_EXIT(&password) {
auto guard = make_scope_exit([&password] {
OPENSSL_cleanse(&password.front(), password.size());
} BOOST_SCOPE_EXIT_END
});
po::options_description description(
"Usage: ndnsec import [-h] [-P PASSPHRASE] [-i] FILE\n"