Move valptridx error reporting out of main header

This commit is contained in:
Kp 2015-10-13 02:43:25 +00:00
parent 2a1e07adfa
commit 5f29170702
3 changed files with 88 additions and 86 deletions

View file

@ -52,62 +52,8 @@
#define DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT
#endif
class valptridxutil_untyped_base
{
public:
class index_mismatch_exception;
class index_range_exception;
class null_pointer_exception;
};
class valptridxutil_untyped_base::index_mismatch_exception
{
protected:
#define REPORT_STANDARD_FORMAT " base=%p size=%lu"
#define REPORT_STANDARD_ARGUMENTS array_base, static_cast<unsigned long>(array_size)
#define REPORT_STANDARD_SIZE (sizeof(REPORT_FORMAT_STRING) + sizeof("0x0000000000000000") + sizeof("18446744073709551615"))
#define REPORT_FORMAT_STRING "pointer/index mismatch:" REPORT_STANDARD_FORMAT " index=%li expected=%p actual=%p"
static constexpr std::size_t report_buffer_size = REPORT_STANDARD_SIZE + (sizeof("0x0000000000000000") * 2) + sizeof("18446744073709551615");
__attribute_cold
static void prepare_report(char (&buf)[report_buffer_size], const void *const array_base, const std::size_t array_size, const long supplied_index, const void *const expected_pointer, const void *const actual_pointer)
{
snprintf(buf, sizeof(buf), REPORT_FORMAT_STRING, REPORT_STANDARD_ARGUMENTS, supplied_index, expected_pointer, actual_pointer);
}
#undef REPORT_FORMAT_STRING
};
class valptridxutil_untyped_base::index_range_exception
{
protected:
#define REPORT_FORMAT_STRING "invalid index used in array subscript:" REPORT_STANDARD_FORMAT " index=%li"
static constexpr std::size_t report_buffer_size = REPORT_STANDARD_SIZE + sizeof("18446744073709551615");
__attribute_cold
static void prepare_report(char (&buf)[report_buffer_size], const void *const array_base, const std::size_t array_size, const long supplied_index)
{
snprintf(buf, sizeof(buf), REPORT_FORMAT_STRING, REPORT_STANDARD_ARGUMENTS, supplied_index);
}
#undef REPORT_FORMAT_STRING
};
class valptridxutil_untyped_base::null_pointer_exception
{
protected:
#define REPORT_FORMAT_STRING "NULL pointer used:" REPORT_STANDARD_FORMAT
static constexpr std::size_t report_buffer_size = REPORT_STANDARD_SIZE;
__attribute_cold
static void prepare_report(char (&buf)[report_buffer_size], const void *const array_base, const std::size_t array_size)
{
snprintf(buf, sizeof(buf), REPORT_FORMAT_STRING, REPORT_STANDARD_ARGUMENTS);
}
#undef REPORT_FORMAT_STRING
#undef REPORT_STANDARD_SIZE
#undef REPORT_STANDARD_ARGUMENTS
#undef REPORT_STANDARD_FORMAT
};
template <typename P>
class valptridx<P>::index_mismatch_exception :
valptridxutil_untyped_base::index_mismatch_exception,
public std::logic_error
{
DXX_INHERIT_CONSTRUCTORS(index_mismatch_exception, logic_error);
@ -120,7 +66,6 @@ public:
template <typename P>
class valptridx<P>::index_range_exception :
valptridxutil_untyped_base::index_range_exception,
public std::out_of_range
{
DXX_INHERIT_CONSTRUCTORS(index_range_exception, out_of_range);
@ -133,7 +78,6 @@ public:
template <typename P>
class valptridx<P>::null_pointer_exception :
valptridxutil_untyped_base::null_pointer_exception,
public std::logic_error
{
DXX_INHERIT_CONSTRUCTORS(null_pointer_exception, logic_error);
@ -148,36 +92,6 @@ public:
static void report(const array_managed_type &);
};
template <typename managed_type>
void valptridx<managed_type>::index_mismatch_exception::report(const array_managed_type &array, const index_type supplied_index, const const_pointer_type expected_pointer, const const_pointer_type actual_pointer)
{
char buf[report_buffer_size];
prepare_report(buf, &array[0], array.size(), supplied_index, expected_pointer, actual_pointer);
throw index_mismatch_exception(buf);
}
template <typename managed_type>
void valptridx<managed_type>::index_range_exception::report(const array_managed_type &array, const long supplied_index)
{
char buf[report_buffer_size];
prepare_report(buf, &array[0], array.size(), supplied_index);
throw index_range_exception(buf);
}
template <typename managed_type>
void valptridx<managed_type>::null_pointer_exception::report()
{
throw null_pointer_exception("NULL pointer converted");
}
template <typename managed_type>
void valptridx<managed_type>::null_pointer_exception::report(const array_managed_type &array)
{
char buf[report_buffer_size];
prepare_report(buf, &array[0], array.size());
throw null_pointer_exception(buf);
}
template <typename managed_type>
void valptridx<managed_type>::check_index_match(const managed_type &r, index_type i, const array_managed_type &a)
{

84
common/main/valptridx.tcc Normal file
View file

@ -0,0 +1,84 @@
/*
* This file is part of the DXX-Rebirth project <http://www.dxx-rebirth.com/>.
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
#include <cstdio>
#include "valptridx.h"
namespace untyped_index_mismatch_exception
{
#define REPORT_STANDARD_FORMAT " base=%p size=%lu"
#define REPORT_STANDARD_ARGUMENTS array_base, static_cast<unsigned long>(array_size)
#define REPORT_STANDARD_SIZE (sizeof(REPORT_FORMAT_STRING) + sizeof("0x0000000000000000") + sizeof("18446744073709551615"))
#define REPORT_FORMAT_STRING "pointer/index mismatch:" REPORT_STANDARD_FORMAT " index=%li expected=%p actual=%p"
static constexpr std::size_t report_buffer_size = REPORT_STANDARD_SIZE + (sizeof("0x0000000000000000") * 2) + sizeof("18446744073709551615");
__attribute_cold
static void prepare_report(char (&buf)[report_buffer_size], const void *const array_base, const std::size_t array_size, const long supplied_index, const void *const expected_pointer, const void *const actual_pointer)
{
snprintf(buf, sizeof(buf), REPORT_FORMAT_STRING, REPORT_STANDARD_ARGUMENTS, supplied_index, expected_pointer, actual_pointer);
}
#undef REPORT_FORMAT_STRING
};
namespace untyped_index_range_exception
{
#define REPORT_FORMAT_STRING "invalid index used in array subscript:" REPORT_STANDARD_FORMAT " index=%li"
static constexpr std::size_t report_buffer_size = REPORT_STANDARD_SIZE + sizeof("18446744073709551615");
__attribute_cold
static void prepare_report(char (&buf)[report_buffer_size], const void *const array_base, const std::size_t array_size, const long supplied_index)
{
snprintf(buf, sizeof(buf), REPORT_FORMAT_STRING, REPORT_STANDARD_ARGUMENTS, supplied_index);
}
#undef REPORT_FORMAT_STRING
};
namespace untyped_null_pointer_exception
{
#define REPORT_FORMAT_STRING "NULL pointer used:" REPORT_STANDARD_FORMAT
static constexpr std::size_t report_buffer_size = REPORT_STANDARD_SIZE;
__attribute_cold
static void prepare_report(char (&buf)[report_buffer_size], const void *const array_base, const std::size_t array_size)
{
snprintf(buf, sizeof(buf), REPORT_FORMAT_STRING, REPORT_STANDARD_ARGUMENTS);
}
#undef REPORT_FORMAT_STRING
#undef REPORT_STANDARD_SIZE
#undef REPORT_STANDARD_ARGUMENTS
#undef REPORT_STANDARD_FORMAT
};
template <typename managed_type>
void valptridx<managed_type>::index_mismatch_exception::report(const array_managed_type &array, const index_type supplied_index, const const_pointer_type expected_pointer, const const_pointer_type actual_pointer)
{
using namespace untyped_index_mismatch_exception;
char buf[report_buffer_size];
prepare_report(buf, &array[0], array.size(), supplied_index, expected_pointer, actual_pointer);
throw index_mismatch_exception(buf);
}
template <typename managed_type>
void valptridx<managed_type>::index_range_exception::report(const array_managed_type &array, const long supplied_index)
{
using namespace untyped_index_range_exception;
char buf[report_buffer_size];
prepare_report(buf, &array[0], array.size(), supplied_index);
throw index_range_exception(buf);
}
template <typename managed_type>
void valptridx<managed_type>::null_pointer_exception::report()
{
throw null_pointer_exception("NULL pointer converted");
}
template <typename managed_type>
void valptridx<managed_type>::null_pointer_exception::report(const array_managed_type &array)
{
using namespace untyped_null_pointer_exception;
char buf[report_buffer_size];
prepare_report(buf, &array[0], array.size());
throw null_pointer_exception(buf);
}

View file

@ -32,6 +32,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include "3d.h"
#include "game.h"
#include "textures.h"
#include "valptridx.tcc"
// Global array of vertices, common to one mine.
array<vertex, MAX_VERTICES> Vertices;
@ -98,3 +99,6 @@ valptridx<managed_type>::array_managed_type::array_managed_type()
valptridx<object>::array_managed_type Objects;
valptridx<segment>::array_managed_type Segments;
template class valptridx<object>;
template class valptridx<segment>;