DocWire SDK
DocWire SDK: Deterministic, auditable, and secure data processing in C++20. On-premise by default. No compromises on computational speed.
Toggle main menu visibility
Loading...
Searching...
No Matches
convert_base.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_CONVERT_BASE_H
13
#define DOCWIRE_CONVERT_BASE_H
14
15
#include <optional>
16
#include <tuple>
17
#include "error_tags.h"
18
#include "make_error.h"
19
#include "source_location.h"
20
#include "type_name.h"
21
25
namespace
docwire::convert
26
{
27
31
template
<
typename
T>
32
struct
dest_type_tag
{};
33
34
template
<
typename
To,
typename
From>
35
concept
conversion_implementation_exists
=
requires
(
const
From& from) {
36
{
convert_impl
(from,
convert::dest_type_tag<To>
{}) }
noexcept
-> std::same_as<std::optional<To>>;
37
};
38
39
namespace
detail
40
{
41
42
struct
convert_cpo
43
{
44
template
<
typename
To,
typename
From>
45
requires
conversion_implementation_exists<To, From>
46
constexpr
std::optional<To> operator()(
const
From& from)
const
noexcept
47
{
48
return
convert_impl
(from,
dest_type_tag<To>
{});
49
}
50
};
51
52
inline
constexpr
convert_cpo
convert_cpo
;
53
54
}
// namespace detail
55
63
template
<
typename
To,
typename
From>
64
requires
conversion_implementation_exists<To, From>
65
constexpr
std::optional<To>
try_to
(
const
From& from)
noexcept
66
{
67
return
detail::convert_cpo
.template operator()<To, From>(from);
68
}
69
77
template
<
typename
To,
typename
From>
78
requires
conversion_implementation_exists<To, From>
79
To
to
(
const
From& from)
80
{
81
auto
result =
try_to<To>
(from);
82
if
(!result.has_value())
83
{
84
auto
context = std::make_tuple(
"Failed to convert value"
,
"from_type"
_v = type_name::pretty<From>(),
"to_type"
_v = type_name::pretty<To>(),
errors::uninterpretable_data
{});
85
throw
errors::make_error_from_tuple
(source_location::current(), std::move(context));
86
}
87
return
*result;
88
}
89
90
}
// namespace docwire::convert
91
92
#endif
// DOCWIRE_CONVERT_BASE_H
docwire::convert::conversion_implementation_exists
Definition
convert_base.h:35
docwire::convert
Namespace for type conversion utilities.
Definition
convert_base.h:26
docwire::convert::convert_impl
std::optional< std::chrono::sys_seconds > convert_impl(with::date_format::iso8601 s, dest_type_tag< std::chrono::sys_seconds >) noexcept
Converts an ISO 8601 date string to sys_seconds.
Definition
convert_chrono.h:103
docwire::convert::to
To to(const From &from)
Converts a value of type From to type To.
Definition
convert_base.h:79
docwire::convert::try_to
constexpr std::optional< To > try_to(const From &from) noexcept
Tries to convert a value of type From to type To.
Definition
convert_base.h:65
docwire::errors::make_error_from_tuple
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
docwire::convert::dest_type_tag
A tag type used for dispatching convert_impl overloads based on the destination type.
Definition
convert_base.h:32
docwire::convert::detail::convert_cpo
Definition
convert_base.h:43
docwire::errors::uninterpretable_data
Uninterpretable data error tag.
Definition
error_tags.h:88
convert_base.h
Generated by
1.18.0