xrpld
Loading...
Searching...
No Matches
contract.h
1#pragma once
2
3#include <xrpl/basics/sanitizers.h>
4#include <xrpl/beast/type_name.h>
5
6#include <exception>
7#include <string>
8#include <utility>
9
10namespace xrpl {
11
12/* Programming By Contract
13
14 This routines are used when checking
15 preconditions, postconditions, and invariants.
16*/
17
21void
22logThrow(std::string const& title);
23
35[[noreturn]] XRPL_NO_SANITIZE_ADDRESS inline void
37{
38 logThrow("Re-throwing exception");
39 throw;
40}
41
42/*
43 Logs and throws an exception of type E.
44
45 ASAN can't handle sudden jumps in control flow very well. This
46 function is marked as XRPL_NO_SANITIZE_ADDRESS to prevent it from
47 triggering false positives, since it throws.
48*/
49
50template <class E, class... Args>
51[[noreturn]] XRPL_NO_SANITIZE_ADDRESS inline void
52Throw(Args&&... args)
53{
54 static_assert(
55 std::is_convertible_v<E*, std::exception*>, "Exception must derive from std::exception.");
56
57 E e(std::forward<Args>(args)...);
58 logThrow(std::string("Throwing exception of type " + beast::typeName<E>() + ": ") + e.what());
59 throw std::move(e);
60}
61
65[[noreturn]] void
66logicError(std::string const& how) noexcept;
67
68} // namespace xrpl
T forward(T... args)
T is_convertible_v
std::string typeName()
Definition type_name.h:16
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
XRPL_NO_SANITIZE_ADDRESS void rethrow()
Rethrow the exception currently being handled.
Definition contract.h:36
void logThrow(std::string const &title)
Generates and logs a call stack.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52