JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
function_extractor.hpp
1/// \file jsonv/serialization/extractor_for.hpp
2///
3/// Copyright (c) 2015-2020 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
17namespace jsonv
18{
19
20/// \addtogroup Serialization
21/// \{
22
23/// An \c extractor which calls a function to perform extraction.
24template <typename T, typename FExtract>
26 public extractor_for<T>
27{
28public:
29 template <typename FUExtract>
31 _func(std::forward<FUExtract>(func))
32 { }
33
34protected:
35 virtual T create(const extraction_context& context, const value& from) const override
36 {
37 return create_impl(_func, context, from);
38 }
39
40private:
41 template <typename FUExtract>
42 static auto create_impl(const FUExtract& func, const extraction_context& context, const value& from)
43 -> decltype(func(context, from))
44 {
45 return func(context, from);
46 }
47
48 template <typename FUExtract, typename = void>
49 static auto create_impl(const FUExtract& func, const extraction_context&, const value& from)
50 -> decltype(func(from))
51 {
52 return func(from);
53 }
54
55private:
56 FExtract _func;
57};
58
59template <typename FExtract>
60auto make_extractor(FExtract func)
63 >
64{
67 >
68 (std::move(func));
69}
70
71template <typename FExtract, typename = void>
72auto make_extractor(FExtract func)
73 -> function_extractor<decltype(func(std::declval<const value&>())),
74 FExtract
75 >
76{
77 return function_extractor<decltype(func(std::declval<const value&>())), FExtract>
78 (std::move(func));
79}
80
81/// \}
82
83}
An adapter for enumeration types.
An extractor for the type T.
An extractor which calls a function to perform extraction.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:107
Copyright (c) 2014-2020 by Travis Gockel.
Copyright (c) 2015-2020 by Travis Gockel.
Conversion between C++ types and JSON values.