1 #ifndef BOOST_SERIALIZATION_STATE_SAVER_HPP
2 #define BOOST_SERIALIZATION_STATE_SAVER_HPP
4 // MS compatible compilers support #pragma once
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
12 // (C) Copyright 2003-4 Pavel Vozenilek and Robert Ramey - http://www.rrsd.com.
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 // See http://www.boost.org/libs/serialization for updates, documentation, and revision history.
19 // Inspired by Daryle Walker's iostate_saver concept. This saves the original
20 // value of a variable when a state_saver is constructed and restores
21 // upon destruction. Useful for being sure that state is restored to
22 // variables upon exit from scope.
25 #include <boost/config.hpp>
26 #ifndef BOOST_NO_EXCEPTIONS
30 #include <boost/call_traits.hpp>
31 #include <boost/noncopyable.hpp>
32 #include <boost/type_traits/has_nothrow_copy.hpp>
33 #include <boost/core/no_exceptions_support.hpp>
35 #include <boost/mpl/eval_if.hpp>
36 #include <boost/mpl/identity.hpp>
39 namespace serialization {
43 // - POD or object semantic (cannot be reference, function, ...)
45 // - operator = (no-throw one preferred)
46 class state_saver : private boost::noncopyable
49 const T previous_value;
53 static void invoke(T & previous_ref, const T & previous_value){
54 previous_ref = previous_value; // won't throw
58 struct restore_with_exception {
59 static void invoke(T & previous_ref, const T & previous_value){
61 previous_ref = previous_value;
63 BOOST_CATCH(::std::exception &) {
64 // we must ignore it - we are in destructor
74 previous_value(object),
79 #ifndef BOOST_NO_EXCEPTIONS
80 typedef typename mpl::eval_if<
81 has_nothrow_copy< T >,
82 mpl::identity<restore>,
83 mpl::identity<restore_with_exception>
85 typex::invoke(previous_ref, previous_value);
87 previous_ref = previous_value;
96 #endif //BOOST_SERIALIZATION_STATE_SAVER_HPP