JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
function_adapter.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/function_adapter.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 "adapter_for.hpp"
16
17namespace jsonv
18{
19
20/// \addtogroup Serialization
21/// \{
22
23template <typename T, typename FExtract, typename FToJson>
25 public adapter_for<T>
26{
27public:
28 template <typename FUExtract, typename FUToJson>
30 _extract(std::forward<FUExtract>(extract_)),
31 _to_json(std::forward<FUToJson>(to_json_))
32 { }
33
34protected:
35 virtual T create(const extraction_context& context, const value& from) const override
36 {
37 return create_impl(_extract, context, from);
38 }
39
40 virtual value to_json(const serialization_context& context, const T& from) const override
41 {
42 return to_json_impl(_to_json, context, from);
43 }
44
45private:
46 template <typename FUExtract>
47 static auto create_impl(const FUExtract& func, const extraction_context& context, const value& from)
48 -> decltype(func(context, from))
49 {
50 return func(context, from);
51 }
52
53 template <typename FUExtract, typename = void>
54 static auto create_impl(const FUExtract& func, const extraction_context&, const value& from)
55 -> decltype(func(from))
56 {
57 return func(from);
58 }
59
60 template <typename FUToJson>
61 static auto to_json_impl(const FUToJson& func, const serialization_context& context, const T& from)
62 -> decltype(func(context, from))
63 {
64 return func(context, from);
65 }
66
67 template <typename FUToJson, typename = void>
68 static auto to_json_impl(const FUToJson& func, const serialization_context&, const T& from)
69 -> decltype(func(from))
70 {
71 return func(from);
72 }
73
74private:
75 FExtract _extract;
76 FToJson _to_json;
77};
78
79template <typename FExtract, typename FToJson>
80auto make_adapter(FExtract extract, FToJson to_json_)
84 >
85{
89 >
90 (std::move(extract), std::move(to_json_));
91}
92
93template <typename FExtract, typename FToJson, typename = void>
94auto make_adapter(FExtract extract, FToJson to_json_)
95 -> function_adapter<decltype(extract(std::declval<const value&>())),
96 FExtract,
97 FToJson
98 >
99{
100 return function_adapter<decltype(extract(std::declval<const value&>())),
101 FExtract,
102 FToJson
103 >
104 (std::move(extract), std::move(to_json_));
105}
106
107/// \}
108
109}
An adapter for the type T.
An adapter for enumeration types.
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.
T extract(const value &from, const formats &fmts)
Extract a C++ value from from using the provided fmts.
Conversion between C++ types and JSON values.