JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
extractor_for.hpp
Go to the documentation of this file.
1
/// \file jsonv/serialization/extractor_for.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
>
13
#include <
jsonv/serialization/extract.hpp
>
14
15
#include <expected>
16
#include <new>
17
#include <utility>
18
19
namespace
jsonv
20
{
21
22
/// \addtogroup Serialization
23
/// \{
24
25
/// An \c extractor for type \c T. This is a utility class which converts the `void*` used in the \c extractor
26
/// interface into the more-friendly \c T.
27
///
28
/// \see adapter_for
29
template
<
typename
T>
30
class
extractor_for
:
31
public
extractor
32
{
33
public
:
34
/// \see extractor::get_type
35
JSONV_NODISCARD
36
virtual
const
std::type_info&
get_type
() const noexcept
override
37
{
38
return
typeid
(T);
39
}
40
41
/// \see extractor::extract
42
JSONV_NODISCARD
43
virtual
std::expected<void, ast_node_type>
44
extract
(
extraction_context
&
context
,
reader
& from,
void
* into)
const override
45
{
46
if
(
auto
created =
create
(
context
, from))
47
{
48
try
49
{
50
new
(into) T(std::move(created).value());
51
}
52
catch
(...)
53
{
54
// `create` succeeded, so the cursor is already one past the value this was built from. Moving it
55
// into place is the last thing which can fail, and `T` is the caller's type; a loop recovering from
56
// that failure must not step over the value a second time.
57
context
.note_value_consumed(from);
58
throw
;
59
}
60
return
{};
61
}
62
else
63
{
64
return
std::unexpected(created.error());
65
}
66
}
67
68
protected
:
69
/// Create an instance of \c T by reading \a from.
70
///
71
/// \see adapter_for::create
72
JSONV_NODISCARD
73
virtual
std::expected<T, ast_node_type>
create
(
extraction_context
&
context
,
reader
& from)
const
= 0;
74
};
75
76
/// \}
77
78
}
jsonv::context
Provides extra information to routines used for extraction and serialization.
Definition
context.hpp:26
jsonv::extraction_context
Provides extra information to routines used for extraction, collects the problems they encounter,...
Definition
extract.hpp:391
jsonv::extractor_for
An extractor for type T.
Definition
extractor_for.hpp:32
jsonv::extractor_for::get_type
virtual const std::type_info & get_type() const noexcept override
Definition
extractor_for.hpp:36
jsonv::extractor_for::create
virtual std::expected< T, ast_node_type > create(extraction_context &context, reader &from) const =0
Create an instance of T by reading from.
jsonv::extractor_for::extract
virtual std::expected< void, ast_node_type > extract(extraction_context &context, reader &from, void *into) const override
Definition
extractor_for.hpp:44
jsonv::extractor
An extractor holds the method for converting JSON source into an arbitrary C++ type.
Definition
extract.hpp:345
jsonv::reader
A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON...
Definition
reader.hpp:104
config.hpp
Copyright (c) 2014-2020 by Travis Gockel.
extract.hpp
Extraction of C++ types from a JSON AST.
JSONV_NODISCARD
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition
config.hpp:132
jsonv
serialization
extractor_for.hpp
Generated by
1.9.8