JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
function_extractor.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/function_extractor.hpp
2///
3/// Copyright (c) 2015-2026 by Travis Gockel. All rights reserved.
4///
5/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
6/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
7/// version.
8///
9/// \author Travis Gockel (travis@gockelhut.com)
10#pragma once
11
12#include <jsonv/config.hpp>
14
15#include "extractor_for.hpp"
16
17#include <concepts>
18#include <expected>
19#include <type_traits>
20#include <utility>
21
22namespace jsonv
23{
24
25/// \addtogroup Serialization
26/// \{
27
28/// An \c extractor which calls a function to perform extraction.
29///
30/// The function may take any of the shapes \c detail::invoke_extract accepts and may return either a \c T or a
31/// \c std::expected<T, ast_node_type>.
32template <typename T, typename FExtract>
34 public extractor_for<T>
35{
36public:
37 // Constrained so that copying from a non-const lvalue reaches the copy constructor, which this would otherwise
38 // beat as the better match.
39 template <typename FUExtract>
40 requires (!std::same_as<std::remove_cvref_t<FUExtract>, function_extractor>)
41 explicit function_extractor(FUExtract&& func) :
42 _func(std::forward<FUExtract>(func))
43 { }
44
45protected:
47 virtual std::expected<T, ast_node_type> create(extraction_context& context, reader& from) const override
48 {
49 return detail::invoke_extract<T>(_func, context, from);
50 }
51
52private:
53 FExtract _func;
54};
55
56/// Create an \c extractor from \a func, deducing what it extracts from its return type.
57template <typename FExtract>
64
65/// \}
66
67}
Provides extra information to routines used for extraction and serialization.
Definition context.hpp:26
Provides extra information to routines used for extraction, collects the problems they encounter,...
Definition extract.hpp:391
An extractor for type T.
An extractor which calls a function to perform extraction.
virtual std::expected< T, ast_node_type > create(extraction_context &context, reader &from) const override
Create an instance of T by reading from.
A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON...
Definition reader.hpp:104
Copyright (c) 2014-2020 by Travis Gockel.
Extraction of C++ types from a JSON AST.
Copyright (c) 2015-2026 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:132
auto make_extractor(FExtract func) -> function_extractor< detail::extract_function_result_t< FExtract >, FExtract >
Create an extractor from func, deducing what it extracts from its return type.