Skip to content

include/tenzr/common/error.h

Namespaces

Name
tenzr
tenzr::detail

Classes

Name
class tenzr::TenzrException
Exception type produced by TENZR_THROW and TENZR_CHECK, carrying a formatted message.

Defines

Name
TENZR_EXCEPTIONS_ENABLED
TENZR_LIKELY(x)
Branch-prediction hints that x is likely (or unlikely) true; no-ops without __builtin_expect.
TENZR_UNLIKELY(x)
TENZR_THROW(msg)
TENZR_CHECK(cond, msg)

Macros Documentation

define TENZR_EXCEPTIONS_ENABLED

#define TENZR_EXCEPTIONS_ENABLED 1

define TENZR_LIKELY

#define TENZR_LIKELY(
    x
)
(x)

Branch-prediction hints that x is likely (or unlikely) true; no-ops without __builtin_expect.

define TENZR_UNLIKELY

#define TENZR_UNLIKELY(
    x
)
(x)

define TENZR_THROW

#define TENZR_THROW(
    msg
)
    do {                                                               \
      std::ostringstream _tenzr_oss;                                   \
      _tenzr_oss << "[" << __FILE__ << ":" << __LINE__ << "] " << msg; \
      ::tenzr::detail::fatal(_tenzr_oss.str());                        \
    } while (0)

Parameters:

  • msg The error message, streamable into an std::ostringstream.

Throws a TenzrException with msg, prefixed by the source file and line. When the project is built without exceptions (TENZR_ENABLE_EXCEPTIONS=OFF), prints the message and aborts instead.

define TENZR_CHECK

#define TENZR_CHECK(
    cond,
    msg
)
  do {                                                \
    if (TENZR_UNLIKELY(!(cond))) [[unlikely]] {       \
      TENZR_THROW("Check failed: " #cond " -- " msg); \
    }                                                 \
  } while (0)

Parameters:

  • cond The condition that must be true.
  • msg Extra context included in the failure message.

Throws a TenzrException unless cond holds, reporting the failed condition and msg.

Source code

// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TENZR_COMMON_ERROR_H_
#define TENZR_COMMON_ERROR_H_

#include <cstdio>
#include <sstream>
#include <string>

// Default to exceptions enabled when the consuming build did not provide the definition (the
// CMake target exports it PUBLIC, but a manual `-Iinclude` consumer would otherwise silently
// compile the abort path).
#ifndef TENZR_EXCEPTIONS_ENABLED
  #define TENZR_EXCEPTIONS_ENABLED 1
#endif

#if !TENZR_EXCEPTIONS_ENABLED
  #include <cstdlib>
  #include <iostream>
#endif

namespace tenzr {

  class TenzrException : public std::runtime_error {
   public:
    explicit TenzrException(const std::string& message) : std::runtime_error(message) {}
  };

#if !TENZR_EXCEPTIONS_ENABLED
  namespace detail {
    [[noreturn]] inline void fatal(const std::string& message) {
      std::cerr << message << std::endl;
      std::abort();
    }
  }  // namespace detail
#endif

#if defined(__GNUC__) || defined(__clang__)
  #define TENZR_LIKELY(x) (__builtin_expect(!!(x), 1))
  #define TENZR_UNLIKELY(x) (__builtin_expect(!!(x), 0))
#else
  #define TENZR_LIKELY(x) (x)
  #define TENZR_UNLIKELY(x) (x)
#endif

#if TENZR_EXCEPTIONS_ENABLED
  #define TENZR_THROW(msg)                                             \
    do {                                                               \
      std::ostringstream _tenzr_oss;                                   \
      _tenzr_oss << "[" << __FILE__ << ":" << __LINE__ << "] " << msg; \
      throw ::tenzr::TenzrException(_tenzr_oss.str());                 \
    } while (0)
#else
  #define TENZR_THROW(msg)                                             \
    do {                                                               \
      std::ostringstream _tenzr_oss;                                   \
      _tenzr_oss << "[" << __FILE__ << ":" << __LINE__ << "] " << msg; \
      ::tenzr::detail::fatal(_tenzr_oss.str());                        \
    } while (0)
#endif

#define TENZR_CHECK(cond, msg)                        \
  do {                                                \
    if (TENZR_UNLIKELY(!(cond))) [[unlikely]] {       \
      TENZR_THROW("Check failed: " #cond " -- " msg); \
    }                                                 \
  } while (0)

}  // namespace tenzr

#endif  // TENZR_COMMON_ERROR_H_