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
|
|
|
|
|
2014-01-19 21:19:15 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2014-08-16 09:46:06 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
|
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)
|
|
|
|
{
|
2016-10-19 07:18:05 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
|
|
|
invariant_access::check_invariant(x);
|
|
|
|
}
|
|
|
|
catch (std::exception const& err)
|
|
|
|
{
|
|
|
|
std::fprintf(stderr, "invariant_check failed with exception: %s"
|
|
|
|
, err.what());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::fprintf(stderr, "invariant_check failed with exception");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
invariant_access::check_invariant(x);
|
|
|
|
#endif
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct invariant_checker {};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct invariant_checker_impl : invariant_checker
|
|
|
|
{
|
2016-10-08 20:07:11 +02:00
|
|
|
explicit invariant_checker_impl(T const& self_)
|
2004-01-25 19:18:36 +01:00
|
|
|
: self(self_)
|
|
|
|
{
|
2016-10-19 07:18:05 +02:00
|
|
|
check_invariant(self);
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
|
2015-04-22 06:24:45 +02:00
|
|
|
invariant_checker_impl(invariant_checker_impl const& rhs)
|
|
|
|
: self(rhs.self) {}
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
~invariant_checker_impl()
|
|
|
|
{
|
2016-10-19 07:18:05 +02:00
|
|
|
check_invariant(self);
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T const& self;
|
2015-04-22 06:24:45 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
invariant_checker_impl& operator=(invariant_checker_impl const&);
|
2004-01-25 19:18:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
invariant_checker_impl<T> make_invariant_checker(T const& x)
|
|
|
|
{
|
|
|
|
return invariant_checker_impl<T>(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INVARIANT_CHECK \
|
2004-04-17 14:29:35 +02:00
|
|
|
invariant_checker const& _invariant_check = make_invariant_checker(*this); \
|
2015-04-27 04:21:12 +02:00
|
|
|
(void)_invariant_check
|
2004-01-25 19:18:36 +01:00
|
|
|
#else
|
2015-04-27 04:21:12 +02:00
|
|
|
#define INVARIANT_CHECK do {} TORRENT_WHILE_0
|
2004-01-25 19:18:36 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // TORRENT_INVARIANT_ACCESS_HPP_INCLUDED
|
2016-06-05 20:07:24 +02:00
|
|
|
|