-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsd_smart_ptr.hpp
More file actions
106 lines (93 loc) · 4.15 KB
/
Copy pathjsd_smart_ptr.hpp
File metadata and controls
106 lines (93 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include "../utility/polymorphy.hpp"
#include "../utility/property_tree_probe.hpp"
#include <type_traits>
namespace JSON
{
namespace internal
{
template <typename T, bool ClassType, bool Poly>
struct smart_pointer_parser
{
};
template <typename T>
struct smart_pointer_parser <T, true, false>
{
static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options)
{
//! Trying to create abstract class here?
//! Your polydecls are not correct!
value.reset(new typename std::decay<T>::type::element_type{});
value->parse(name, object, options);
}
};
template <typename T>
struct smart_pointer_parser <T, false, false>
{
static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options)
{
using element_type = typename std::decay<T>::type::element_type;
value.reset(new element_type{});
SJSON_GET_VALUE(element_type, name, *value, {});
}
};
template <typename T>
struct smart_pointer_parser <T, true, true>
{
static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options)
{
using polydecl_type = polydecls <typename T::element_type>;
try
{
// causes exceptions for proper error.
object.tree.get <tree_probe> (name);
// expects type name
boost::optional <std::string> type_name;
if (!name.empty())
type_name = object.tree.get_optional <std::string> (name + ".__cxx_type");
else
type_name = object.tree.get_optional <std::string> (name + "__cxx_type");
if (!type_name)
throw std::runtime_error (
std::string("object with name '") +
name +
"' and base type '" +
boost::typeindex::type_id_with_cvr<typename T::element_type>().pretty_name() +
"' is declared polymorphic, but does not come with a type definition __cxx_type"
);
// read smart pointer contents.
polydecl_type::smart_pointer_get(value, type_name.get(), name, object, options);
}
catch (boost::property_tree::ptree_bad_data& exc)
{
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER({},{});
}
catch (boost::property_tree::ptree_bad_path& exc)
{
SJSON_DEFAULT_PATH_ERROR_HANDLER({},{});
}
}
};
template <typename T>
struct smart_pointer_parser <T, false, true>
{
static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options)
{
// static_assert(false, "Not a class but polymorphic? you did something wrong!");
// poly, but not a class
}
};
template <typename T>
void parse_smart_ptr(T& value, std::string const& name,
PropertyTree const& object, ParsingOptions const& options = {})
{
using element_type = typename std::decay<decltype(value)>::type::element_type;
internal::smart_pointer_parser <T,
std::is_class <element_type>::value,
is_polydecl <element_type>::value>::get(value,
name,
object,
options);
}
}
}