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
19void
20logThrow(std::string const& title);
21
32[[noreturn]] XRPL_NO_SANITIZE_ADDRESS inline void
34{
35 logThrow("Re-throwing exception");
36 throw;
37}
38
39/*
40 Logs and throws an exception of type E.
41
42 ASAN can't handle sudden jumps in control flow very well. This
43 function is marked as XRPL_NO_SANITIZE_ADDRESS to prevent it from
44 triggering false positives, since it throws.
45*/
46
47template <class E, class... Args>
48[[noreturn]] XRPL_NO_SANITIZE_ADDRESS inline void
49Throw(Args&&... args)
50{
51 static_assert(
52 std::is_convertible_v<E*, std::exception*>, "Exception must derive from std::exception.");
53
54 E e(std::forward<Args>(args)...);
55 logThrow(std::string("Throwing exception of type " + beast::typeName<E>() + ": ") + e.what());
56 throw std::move(e);
57}
58
60[[noreturn]] void
61logicError(std::string const& how) noexcept;
62
63} // 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:33
void logThrow(std::string const &title)
Generates and logs a call stack.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49