lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
terminate_guard.h
00001 /*****************************************************/
00002 /* lean Smart                   (c) Tobias Zirr 2011 */
00003 /*****************************************************/
00004 
00005 #ifndef LEAN_SMART_TERMINATE_GUARD
00006 #define LEAN_SMART_TERMINATE_GUARD
00007 
00008 #include "../lean.h"
00009 #include "../tags/noncopyable.h"
00010 #include <exception>
00011 
00012 namespace lean
00013 {
00014 namespace smart
00015 {
00016 
00018 class terminate_guard : public nonassignable
00019 {
00020 private:
00021     mutable bool m_armed;
00022 
00023 public:
00025     LEAN_INLINE terminate_guard()
00026         : m_armed(true) { }
00028     LEAN_INLINE explicit terminate_guard(bool arm)
00029         : m_armed(arm) { }
00031     LEAN_INLINE terminate_guard(const terminate_guard &right)
00032         : m_armed(right.m_armed)
00033     {
00034         right.disarm();
00035     }
00037     LEAN_INLINE ~terminate_guard() throw()
00038     {
00039         if (armed())
00040         {
00041             LEAN_ASSERT_DEBUG(false);
00042             std::unexpected();
00043         }
00044     }
00045 
00047     LEAN_INLINE void armed(bool arm) const { m_armed = arm; }
00049     LEAN_INLINE bool armed() const { return m_armed; }
00050 
00052     LEAN_INLINE void disarm() const { armed(false); }
00054     LEAN_INLINE void arm() const { armed(true); }
00055 };
00056 
00057 } // namespace
00058 
00059 using smart::terminate_guard;
00060 
00061 } // namespace
00062 
00063 #endif