DocWire SDK
DocWire SDK: Deterministic, auditable, and secure data processing in C++20. On-premise by default. No compromises on computational speed.
Loading...
Searching...
No Matches
enforce.h
1/*********************************************************************************************************************************************/
2/* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3/* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4/* document analysis. Offline processing possible for security and confidentiality */
5/* */
6/* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7/* Project homepage: https://github.com/docwire/docwire */
8/* */
9/* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10/*********************************************************************************************************************************************/
11
12#ifndef DOCWIRE_ENFORCE_H
13#define DOCWIRE_ENFORCE_H
14
15#include "safety_policy.h"
16#include "with_source_location.h"
17#include "make_error.h"
18#include "error_tags.h"
19#include "debug_assert.h"
20
21namespace docwire
22{
23
37template<safety_policy safety_level = default_safety_level, typename... Context>
38void enforce(detail::with_source_location<bool> condition, Context&&... context)
39{
40 if (!condition.value)
41 {
42 if constexpr (safety_level == strict) {
43 auto context_tuple = std::make_tuple(std::forward<Context>(context)..., errors::program_logic{});
44 throw errors::make_error_from_tuple(condition.location, std::move(context_tuple));
45 } else { // relaxed
46 debug_assert(condition, "Contract violation", std::forward<Context>(context)..., errors::program_logic{});
47 }
48 }
49}
50
51}
52
53#endif // DOCWIRE_ENFORCE_H
auto make_error_from_tuple(const source_location &location, std::tuple< Context... > &&context_tuple)
Creates an error object from a tuple of context information.
Definition make_error.h:30
The main namespace for the DocWire SDK.
Definition ai_elements.h:19
void enforce(detail::with_source_location< bool > condition, Context &&... context)
Enforces a condition based on the safety policy.
Definition enforce.h:38
void debug_assert(detail::with_source_location< bool > condition, Context &&... context)
Asserts a condition in debug builds.
safety_policy
Defines the safety policy for operations.
@ strict
Perform runtime checks and throw exceptions on violations.
Helper struct to capture the source location of a call site. This is implicitly constructed from a va...
Program logic failure error tag.
Definition error_tags.h:40