summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/scope_exit/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/scope_exit/example')
-rw-r--r--src/boost/libs/scope_exit/example/Jamfile.v223
-rw-r--r--src/boost/libs/scope_exit/example/nova.hpp21
-rw-r--r--src/boost/libs/scope_exit/example/scope_guard.cpp46
-rw-r--r--src/boost/libs/scope_exit/example/scope_guard_seq.cpp35
-rw-r--r--src/boost/libs/scope_exit/example/scope_guard_seq_nova.cpp10
-rw-r--r--src/boost/libs/scope_exit/example/try_catch.cpp68
-rw-r--r--src/boost/libs/scope_exit/example/try_catch_seq.cpp61
-rw-r--r--src/boost/libs/scope_exit/example/try_catch_seq_nova.cpp10
-rw-r--r--src/boost/libs/scope_exit/example/world_cxx11_lambda.cpp57
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, &currency) {
+ 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)
+ (&currency) ) {
+ 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
+