diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/scope_exit/example | |
parent | Initial commit. (diff) | |
download | ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/scope_exit/example')
-rw-r--r-- | src/boost/libs/scope_exit/example/Jamfile.v2 | 23 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/nova.hpp | 21 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/scope_guard.cpp | 46 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/scope_guard_seq.cpp | 35 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/scope_guard_seq_nova.cpp | 10 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/try_catch.cpp | 68 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/try_catch_seq.cpp | 61 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/try_catch_seq_nova.cpp | 10 | ||||
-rw-r--r-- | src/boost/libs/scope_exit/example/world_cxx11_lambda.cpp | 57 |
9 files changed, 331 insertions, 0 deletions
diff --git a/src/boost/libs/scope_exit/example/Jamfile.v2 b/src/boost/libs/scope_exit/example/Jamfile.v2 new file mode 100644 index 00000000..82db03a5 --- /dev/null +++ b/src/boost/libs/scope_exit/example/Jamfile.v2 @@ -0,0 +1,23 @@ + +# Copyright (C) 2006-2009, 2012 Alexander Nasonov +# Copyright (C) 2012 Lorenzo Caminiti +# Distributed under the Boost Software License, Version 1.0 +# (see accompanying file LICENSE_1_0.txt or a copy at +# http://www.boost.org/LICENSE_1_0.txt) +# Home at http://www.boost.org/libs/scope_exit + +import testing ; + +# Sun does not automatically detect type-of emulation mode (force it). +project : requirements <toolset>sun:<define>BOOST_TYPEOF_EMULATION ; + +run try_catch.cpp ; +run try_catch_seq.cpp ; +run try_catch_seq_nova.cpp ; + +run scope_guard.cpp ; +run scope_guard_seq.cpp ; +run scope_guard_seq_nova.cpp ; + +run world_cxx11_lambda.cpp ; + diff --git a/src/boost/libs/scope_exit/example/nova.hpp b/src/boost/libs/scope_exit/example/nova.hpp new file mode 100644 index 00000000..ceef87ee --- /dev/null +++ b/src/boost/libs/scope_exit/example/nova.hpp @@ -0,0 +1,21 @@ + +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#ifndef NOVA_HPP_ +#define NOVA_HPP_ + +#include <boost/config.hpp> + +// WARNING: This file must be included first in each compilation unit. + +// Force no variadic macros but avoiding macro redefinition warning/error. +#ifndef BOOST_NO_CXX11_VARIADIC_MACROS +# define BOOST_NO_CXX11_VARIADIC_MACROS +#endif + +#endif // #include guard + diff --git a/src/boost/libs/scope_exit/example/scope_guard.cpp b/src/boost/libs/scope_exit/example/scope_guard.cpp new file mode 100644 index 00000000..1371c5e9 --- /dev/null +++ b/src/boost/libs/scope_exit/example/scope_guard.cpp @@ -0,0 +1,46 @@ + +// Copyright (C) 2006-2009, 2012 Alexander Nasonov +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#include <boost/config.hpp> +#ifdef BOOST_NO_CXX11_VARIADIC_MACROS +# error "variadic macros required" +#else + +#include <boost/scope_exit.hpp> +#include <boost/typeof/std/string.hpp> +#include <boost/typeof/std/map.hpp> +#include <map> +#include <string> +#include <utility> + +int main(void) { + //[scope_guard_decl + bool commit = false; + std::string currency("EUR"); + double rate = 1.3326; + std::map<std::string, double> rates; + bool currency_rate_inserted = + rates.insert(std::make_pair(currency, rate)).second; + // Transaction... + //] + + //[scope_guard_exit + BOOST_SCOPE_EXIT(currency_rate_inserted, &commit, &rates, ¤cy) { + if(currency_rate_inserted && !commit) rates.erase(currency); + } BOOST_SCOPE_EXIT_END + + // ... + + commit = true; + //] + + return 0; +} + +#endif // variadic macros + diff --git a/src/boost/libs/scope_exit/example/scope_guard_seq.cpp b/src/boost/libs/scope_exit/example/scope_guard_seq.cpp new file mode 100644 index 00000000..167cd719 --- /dev/null +++ b/src/boost/libs/scope_exit/example/scope_guard_seq.cpp @@ -0,0 +1,35 @@ + +// Copyright (C) 2006-2009, 2012 Alexander Nasonov +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#include <boost/scope_exit.hpp> +#include <boost/typeof/std/string.hpp> +#include <boost/typeof/std/map.hpp> +#include <map> +#include <string> +#include <utility> + +int main(void) { + bool commit = false; + std::string currency("EUR"); + double rate = 1.3326; + std::map<std::string, double> rates; + bool currency_rate_inserted = + rates.insert(std::make_pair(currency, rate)).second; + + BOOST_SCOPE_EXIT( (currency_rate_inserted) (&commit) (&rates) + (¤cy) ) { + if(currency_rate_inserted && !commit) rates.erase(currency); + } BOOST_SCOPE_EXIT_END + + // ... + + commit = true; + + return 0; +} + diff --git a/src/boost/libs/scope_exit/example/scope_guard_seq_nova.cpp b/src/boost/libs/scope_exit/example/scope_guard_seq_nova.cpp new file mode 100644 index 00000000..2289a932 --- /dev/null +++ b/src/boost/libs/scope_exit/example/scope_guard_seq_nova.cpp @@ -0,0 +1,10 @@ + +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#include "nova.hpp" +#include "scope_guard_seq.cpp" + diff --git a/src/boost/libs/scope_exit/example/try_catch.cpp b/src/boost/libs/scope_exit/example/try_catch.cpp new file mode 100644 index 00000000..4d002328 --- /dev/null +++ b/src/boost/libs/scope_exit/example/try_catch.cpp @@ -0,0 +1,68 @@ + +// Copyright (C) 2006-2009, 2012 Alexander Nasonov +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#include <boost/config.hpp> +#ifdef BOOST_NO_CXX11_VARIADIC_MACROS +# error "variadic macros required" +#else + +#include <boost/scope_exit.hpp> +#include <boost/typeof/typeof.hpp> +#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() +#include <iostream> + +struct file { + file(void) : open_(false) {} + file(char const* path) : open_(false) { open(path); } + + void open(char const* path) { open_ = true; } + void close(void) { open_ = false; } + bool is_open(void) const { return open_; } + +private: + bool open_; +}; +BOOST_TYPEOF_REGISTER_TYPE(file) + +void bad(void) { + //[try_catch_bad + file passwd; + try { + passwd.open("/etc/passwd"); + // ... + passwd.close(); + } catch(...) { + std::clog << "could not get user info" << std::endl; + if(passwd.is_open()) passwd.close(); + throw; + } + //] +} + +void good(void) { + //[try_catch_good + try { + file passwd("/etc/passwd"); + BOOST_SCOPE_EXIT(&passwd) { + passwd.close(); + } BOOST_SCOPE_EXIT_END + } catch(...) { + std::clog << "could not get user info" << std::endl; + throw; + } + //] +} + +int main(void) { + bad(); + good(); + return 0; +} + +#endif // variadic macros + diff --git a/src/boost/libs/scope_exit/example/try_catch_seq.cpp b/src/boost/libs/scope_exit/example/try_catch_seq.cpp new file mode 100644 index 00000000..8ff7ea5f --- /dev/null +++ b/src/boost/libs/scope_exit/example/try_catch_seq.cpp @@ -0,0 +1,61 @@ + +// Copyright (C) 2006-2009, 2012 Alexander Nasonov +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#include <boost/scope_exit.hpp> +#include <boost/typeof/typeof.hpp> +#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() +#include <iostream> + +struct file { + file(void) : open_(false) {} + file(char const* path) : open_(false) { open(path); } + + void open(char const* path) { open_ = true; } + void close(void) { open_ = false; } + bool is_open(void) const { return open_; } + +private: + bool open_; +}; +BOOST_TYPEOF_REGISTER_TYPE(file) + +void bad(void) { + //[try_catch_bad_seq + file passwd; + try { + passwd.open("/etc/passwd"); + // ... + passwd.close(); + } catch(...) { + std::clog << "could not get user info" << std::endl; + if(passwd.is_open()) passwd.close(); + throw; + } + //] +} + +void good(void) { + //[try_catch_good_seq + try { + file passwd("/etc/passwd"); + BOOST_SCOPE_EXIT( (&passwd) ) { + passwd.close(); + } BOOST_SCOPE_EXIT_END + } catch(...) { + std::clog << "could not get user info" << std::endl; + throw; + } + //] +} + +int main(void) { + bad(); + good(); + return 0; +} + diff --git a/src/boost/libs/scope_exit/example/try_catch_seq_nova.cpp b/src/boost/libs/scope_exit/example/try_catch_seq_nova.cpp new file mode 100644 index 00000000..70f0043a --- /dev/null +++ b/src/boost/libs/scope_exit/example/try_catch_seq_nova.cpp @@ -0,0 +1,10 @@ + +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#include "nova.hpp" +#include "try_catch_seq.cpp" + diff --git a/src/boost/libs/scope_exit/example/world_cxx11_lambda.cpp b/src/boost/libs/scope_exit/example/world_cxx11_lambda.cpp new file mode 100644 index 00000000..c8e23d5e --- /dev/null +++ b/src/boost/libs/scope_exit/example/world_cxx11_lambda.cpp @@ -0,0 +1,57 @@ + +// Copyright (C) 2006-2009, 2012 Alexander Nasonov +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit + +#include <boost/config.hpp> +#ifdef BOOST_NO_CXX11_LAMBDAS +# error "lambda functions required" +#else + +#include <boost/detail/lightweight_test.hpp> +#include <vector> + +struct person {}; + +struct world { + void add_person(person const& a_person); + std::vector<person> persons_; +}; + +//[world_cxx11_lambda +#include <functional> + +struct scope_exit { + scope_exit(std::function<void (void)> f) : f_(f) {} + ~scope_exit(void) { f_(); } +private: + std::function<void (void)> f_; +}; + +void world::add_person(person const& a_person) { + bool commit = false; + + persons_.push_back(a_person); + scope_exit on_exit1([&commit, this](void) { // Use C++11 lambda. + if(!commit) persons_.pop_back(); // `persons_` via captured `this`. + }); + + // ... + + commit = true; +} +//] + +int main(void) { + world w; + person p; + w.add_person(p); + BOOST_TEST(w.persons_.size() == 1); + return boost::report_errors(); +} + +#endif // LAMBDAS + |