libcruft-util/debug/warn.hpp

95 lines
4.0 KiB
C++
Raw Normal View History

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2010-2021 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "./assert.hpp"
#include <iostream>
#define WARN(C) do { \
DEBUG_ONLY ( \
if (C) { \
std::cerr << __FILE__ \
<< ":" << __func__ \
<< ":" << __LINE__ \
<< ", " << #C \
<< '\n'; \
} \
); \
} while (0)
2021-12-21 14:55:55 +11:00
#define RETURN_FALSE_UNLESS(CONDITION) do { \
if (const auto &__return_false_unless = (CONDITION); !__return_false_unless) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return false; \
} \
} while (0)
#define WARN_RETURN(CONDITION,VALUE) do { \
if (const auto& __warn_return = (CONDITION); !!__warn_return) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
} while (0)
#define WARN_AND_RETURN_IF(COND, VALUE) WARN_RETURN((COND), (VALUE))
#define RETURN_UNLESS(VALUE,CONDITION) do { \
if (const auto &__return_unless = (CONDITION); !__return_unless) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
} while (0)
///////////////////////////////////////////////////////////////////////////////
#include <string>
#include <string_view>
void warn (void);
void warn (std::string const&);
void warn (std::string_view);
void warn (const char *);
template <typename ValueT>
decltype(auto)
warn_return (
char const *message,
ValueT &&value
) {
warn (message);
return std::forward<ValueT> (value);
}