mmcfilters
Public API documentation
Loading...
Searching...
No Matches
Contract.hpp
Go to the documentation of this file.
1#pragma once
2
15#define MMCFILTERS_CONTRACT_UNCHECKED 0
16
18#define MMCFILTERS_CONTRACT_CHECKED 1
19
20#ifndef MMCFILTERS_CONTRACT_MODE
22#define MMCFILTERS_CONTRACT_MODE MMCFILTERS_CONTRACT_CHECKED
23#endif
24
25#if MMCFILTERS_CONTRACT_MODE != MMCFILTERS_CONTRACT_UNCHECKED && MMCFILTERS_CONTRACT_MODE != MMCFILTERS_CONTRACT_CHECKED
26#error "MMCFILTERS_CONTRACT_MODE must be MMCFILTERS_CONTRACT_CHECKED or MMCFILTERS_CONTRACT_UNCHECKED."
27#endif
28
29namespace mmcfilters::contract {
30
38
40inline constexpr Mode mode = static_cast<Mode>(MMCFILTERS_CONTRACT_MODE);
41
43inline constexpr bool validationsEnabled = mode == Mode::Checked;
44
45} // namespace mmcfilters::contract
46
53#define MMCFILTERS_CONTRACT_REQUIRE(condition, ...) \
54 do { \
55 if constexpr (::mmcfilters::contract::validationsEnabled) { \
56 if (!(condition)) { \
57 __VA_ARGS__; \
58 } \
59 } \
60 } while (false)
61
67#define MMCFILTERS_CONTRACT_CHECKED_ONLY(...) \
68 do { \
69 if constexpr (::mmcfilters::contract::validationsEnabled) { \
70 __VA_ARGS__; \
71 } \
72 } while (false)
constexpr bool validationsEnabled
Whether defensive API-boundary validation is enabled in this build.
Definition Contract.hpp:43
#define MMCFILTERS_CONTRACT_CHECKED
Compile-time value selecting a build with defensive boundary validation.
Definition Contract.hpp:18
#define MMCFILTERS_CONTRACT_MODE
Contract mode selected for the current translation unit.
Definition Contract.hpp:22
constexpr Mode mode
Contract policy selected when this translation unit was compiled.
Definition Contract.hpp:40
Mode
Available compile-time policies for defensive API-boundary validation.
Definition Contract.hpp:32
@ Checked
Public boundaries validate caller-provided preconditions.
@ Unchecked
Caller-established preconditions are trusted without defensive checks.
#define MMCFILTERS_CONTRACT_UNCHECKED
Compile-time value selecting a build without defensive boundary validation.
Definition Contract.hpp:15