added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / invariant_check.hpp
blobc687b6a63b99a6cb962afa3e6ad02a09468a5a4f
1 // Copyright Daniel Wallin 2004. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef TORRENT_INVARIANT_ACCESS_HPP_INCLUDED
6 #define TORRENT_INVARIANT_ACCESS_HPP_INCLUDED
8 #include "libtorrent/assert.hpp"
10 namespace libtorrent
13 class invariant_access
15 public:
16 template<class T>
17 static void check_invariant(T const& self)
19 self.check_invariant();
23 template<class T>
24 void check_invariant(T const& x)
26 invariant_access::check_invariant(x);
29 struct invariant_checker {};
31 template<class T>
32 struct invariant_checker_impl : invariant_checker
34 invariant_checker_impl(T const& self_)
35 : self(self_)
37 try
39 check_invariant(self);
41 catch (...)
43 TORRENT_ASSERT(false);
47 ~invariant_checker_impl()
49 try
51 check_invariant(self);
53 catch (...)
55 TORRENT_ASSERT(false);
59 T const& self;
62 template<class T>
63 invariant_checker_impl<T> make_invariant_checker(T const& x)
65 return invariant_checker_impl<T>(x);
69 #if !defined NDEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
70 #define INVARIANT_CHECK \
71 invariant_checker const& _invariant_check = make_invariant_checker(*this); \
72 (void)_invariant_check; \
73 do {} while (false)
74 #else
75 #define INVARIANT_CHECK do {} while (false)
76 #endif
78 #endif // TORRENT_INVARIANT_ACCESS_HPP_INCLUDED