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
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
26{
27
31template<typename T>
32struct dest_type_tag {};
33
34template <typename To, typename From>
35concept 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
39namespace detail
40{
41
43{
44 template<typename To, typename 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
52inline constexpr convert_cpo convert_cpo;
53
54} // namespace detail
55
63template<typename To, typename From>
65constexpr std::optional<To> try_to(const From& from) noexcept
66{
67 return detail::convert_cpo.template operator()<To, From>(from);
68}
69
77template<typename To, typename From>
78requires conversion_implementation_exists<To, From>
79To 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
Namespace for type conversion utilities.
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.
To to(const From &from)
Converts a value of type From to type To.
constexpr std::optional< To > try_to(const From &from) noexcept
Tries to convert a value of type From to type To.
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
A tag type used for dispatching convert_impl overloads based on the destination type.
Uninterpretable data error tag.
Definition error_tags.h:88