2004-01-25 19:18:36 +01:00
|
|
|
// Copyright Daniel Wallin 2004. Use, modification and distribution is
|
|
|
|
// subject to the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
#ifndef TORRENT_INVARIANT_ACCESS_HPP_INCLUDED
|
|
|
|
#define TORRENT_INVARIANT_ACCESS_HPP_INCLUDED
|
|
|
|
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2004-01-25 19:18:36 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
class invariant_access
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<class T>
|
|
|
|
static void check_invariant(T const& self)
|
|
|
|
{
|
|
|
|
self.check_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void check_invariant(T const& x)
|
|
|
|
{
|
|
|
|
invariant_access::check_invariant(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct invariant_checker {};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct invariant_checker_impl : invariant_checker
|
|
|
|
{
|
|
|
|
invariant_checker_impl(T const& self_)
|
|
|
|
: self(self_)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
check_invariant(self);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~invariant_checker_impl()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
check_invariant(self);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T const& self;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
invariant_checker_impl<T> make_invariant_checker(T const& x)
|
|
|
|
{
|
|
|
|
return invariant_checker_impl<T>(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-13 07:17:56 +01:00
|
|
|
#if !defined NDEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
|
2004-01-25 19:18:36 +01:00
|
|
|
#define INVARIANT_CHECK \
|
2004-04-17 14:29:35 +02:00
|
|
|
invariant_checker const& _invariant_check = make_invariant_checker(*this); \
|
|
|
|
(void)_invariant_check; \
|
|
|
|
do {} while (false)
|
2004-01-25 19:18:36 +01:00
|
|
|
#else
|
|
|
|
#define INVARIANT_CHECK do {} while (false)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // TORRENT_INVARIANT_ACCESS_HPP_INCLUDED
|