save_restore.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // save_restore.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_SAVE_RESTORE_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_UTILITY_SAVE_RESTORE_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/noncopyable.hpp>
  14. namespace boost { namespace xpressive { namespace detail
  15. {
  16. template<typename T>
  17. struct save_restore
  18. : private noncopyable
  19. {
  20. explicit save_restore(T &t)
  21. : ref(t)
  22. , val(t)
  23. {
  24. }
  25. save_restore(T &t, T const &n)
  26. : ref(t)
  27. , val(t)
  28. {
  29. this->ref = n;
  30. }
  31. ~save_restore()
  32. {
  33. this->ref = this->val;
  34. }
  35. void restore()
  36. {
  37. this->ref = this->val;
  38. }
  39. private:
  40. T &ref;
  41. T const val;
  42. };
  43. }}}
  44. #endif