00001 // Copyright Daniel Wallin 2004. Use, modification and distribution is 00002 // subject to the Boost Software License, Version 1.0. (See accompanying 00003 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00004 00005 #ifndef TORRENT_INVARIANT_ACCESS_HPP_INCLUDED 00006 #define TORRENT_INVARIANT_ACCESS_HPP_INCLUDED 00007 00008 #include <cassert> 00009 00010 namespace libtorrent 00011 { 00012 00013 class invariant_access 00014 { 00015 public: 00016 template<class T> 00017 static void check_invariant(T const& self) 00018 { 00019 self.check_invariant(); 00020 } 00021 }; 00022 00023 template<class T> 00024 void check_invariant(T const& x) 00025 { 00026 invariant_access::check_invariant(x); 00027 } 00028 00029 struct invariant_checker {}; 00030 00031 template<class T> 00032 struct invariant_checker_impl : invariant_checker 00033 { 00034 invariant_checker_impl(T const& self_) 00035 : self(self_) 00036 { 00037 try 00038 { 00039 check_invariant(self); 00040 } 00041 catch (...) 00042 { 00043 assert(false); 00044 } 00045 } 00046 00047 ~invariant_checker_impl() 00048 { 00049 try 00050 { 00051 check_invariant(self); 00052 } 00053 catch (...) 00054 { 00055 assert(false); 00056 } 00057 } 00058 00059 T const& self; 00060 }; 00061 00062 template<class T> 00063 invariant_checker_impl<T> make_invariant_checker(T const& x) 00064 { 00065 return invariant_checker_impl<T>(x); 00066 } 00067 } 00068 00069 #ifndef NDEBUG 00070 #define INVARIANT_CHECK \ 00071 invariant_checker const& _invariant_check = make_invariant_checker(*this); \ 00072 (void)_invariant_check; \ 00073 do {} while (false) 00074 #else 00075 #define INVARIANT_CHECK do {} while (false) 00076 #endif 00077 00078 #endif // TORRENT_INVARIANT_ACCESS_HPP_INCLUDED
1.5.6