OpenRCT2/src/openrct2/core/Guard.hpp

80 lines
2.7 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2016-01-04 18:24:12 +01:00
#pragma once
#include <memory>
#include <optional>
#include <stdarg.h>
#include <stdbool.h>
#include <string>
2018-06-22 22:58:39 +02:00
void openrct2_assert_fwd(bool expression, const char* message, ...);
2018-06-22 22:58:39 +02:00
#define openrct2_assert(expr, msg, ...) \
if (!(expr)) \
{ \
openrct2_assert_fwd((expr), msg, ##__VA_ARGS__); \
}
enum class ASSERT_BEHAVIOUR
{
ABORT,
CASSERT,
MESSAGE_BOX,
};
2016-01-04 18:24:12 +01:00
/**
* Utility methods for asserting function parameters.
*/
namespace Guard
{
ASSERT_BEHAVIOUR GetAssertBehaviour();
void SetAssertBehaviour(ASSERT_BEHAVIOUR behaviour);
2018-06-22 22:58:39 +02:00
void Assert(bool expression, const char* message = nullptr, ...);
void Assert_VA(bool expression, const char* message, va_list args);
void Fail(const char* message = nullptr, ...);
void Fail_VA(const char* message, va_list args);
std::optional<std::string> GetLastAssertMessage();
2018-06-22 22:58:39 +02:00
template<typename T> static void ArgumentNotNull(T* argument, const char* message = nullptr, ...)
{
2016-07-16 15:12:16 +02:00
va_list args;
va_start(args, message);
Assert_VA(argument != nullptr, message, args);
va_end(args);
}
2018-06-22 22:58:39 +02:00
template<typename T> static void ArgumentNotNull(const std::shared_ptr<T>& argument, const char* message = nullptr, ...)
{
va_list args;
va_start(args, message);
Assert_VA(argument != nullptr, message, args);
va_end(args);
}
2018-06-22 22:58:39 +02:00
template<typename T> static void ArgumentInRange(T argument, T min, T max, const char* message = nullptr, ...)
{
2016-07-16 15:12:16 +02:00
va_list args;
va_start(args, message);
Assert(argument >= min && argument <= max, message, args);
va_end(args);
}
2020-08-10 15:35:54 +02:00
template<typename T> static void IndexInRange(size_t index, const T& container)
{
2020-08-11 20:29:18 +02:00
Guard::Assert(index < container.size(), "Index %zu out of bounds (%zu)", index, container.size());
2020-08-10 15:35:54 +02:00
}
2018-05-04 22:40:09 +02:00
} // namespace Guard
2016-07-16 15:12:16 +02:00
#define GUARD_LINE "Location: %s:%d", __func__, __LINE__