Skip to content

Commit

Permalink
Don't crash when an invalid interval gets constructed in editor builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Jun 6, 2023
1 parent 4324bde commit f74d075
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions util/math/interval.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#include "interval.h"
#include "../string_funcs.h"
#include <sstream>

namespace zylann {
namespace math {

void Interval::_check_range_once() {
static bool once = false;
if (min <= max && once == false) {
once = true;
ZN_PRINT_ERROR(format("Interval constructed with invalid range: min={}, max={}", min, max));
}
}

} // namespace math

std::stringstream &operator<<(std::stringstream &ss, const math::Interval &v) {
ss << "[" << v.min << ", " << v.max << "]";
Expand Down
7 changes: 6 additions & 1 deletion util/math/interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ struct Interval {
inline Interval() : min(0), max(0) {}

inline Interval(real_t p_min, real_t p_max) : min(p_min), max(p_max) {
#if DEBUG_ENABLED
#if DEV_ENABLED
ZN_ASSERT(p_min <= p_max);
#elif DEBUG_ENABLED
// Don't crash but keep signaling
_check_range_once();
#endif
}

void _check_range_once();

inline static Interval from_single_value(real_t p_val) {
return Interval(p_val, p_val);
}
Expand Down

0 comments on commit f74d075

Please sign in to comment.