|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "../utility/polymorphy.h" |
| 4 | +#include "../utility/property_tree_probe.h" |
| 5 | + |
| 6 | +#include <type_traits> |
| 7 | + |
| 8 | +namespace JSON |
| 9 | +{ |
| 10 | + namespace internal |
| 11 | + { |
| 12 | + template <typename T, bool ClassType, bool Enabler> |
| 13 | + struct smart_pointer_parser |
| 14 | + { |
| 15 | + }; |
| 16 | + |
| 17 | + template <typename T> |
| 18 | + struct smart_pointer_parser <T, true, true> |
| 19 | + { |
| 20 | + static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options) |
| 21 | + { |
| 22 | + value.reset(new typename std::decay<T>::type::element_type{}); |
| 23 | + value->parse(name, object, options); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + template <typename T> |
| 28 | + struct smart_pointer_parser <T, false, true> |
| 29 | + { |
| 30 | + static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options) |
| 31 | + { |
| 32 | + using element_type = typename std::decay<T>::type::element_type; |
| 33 | + value.reset(new element_type{}); |
| 34 | + GET_VALUE(element_type, name, *value, {}); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + template <typename T> |
| 39 | + struct smart_pointer_parser <T, true, false> |
| 40 | + { |
| 41 | + static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options) |
| 42 | + { |
| 43 | + using polydecl_type = polydecls <typename T::element_type>; |
| 44 | + |
| 45 | + try |
| 46 | + { |
| 47 | + // causes exceptions for proper error. |
| 48 | + object.tree.get <tree_probe> (name); |
| 49 | + |
| 50 | + // expects type name |
| 51 | + auto type_name = object.tree.get_optional <std::string> (name + ".__cxx_type"); |
| 52 | + if (!type_name) |
| 53 | + throw std::runtime_error ( |
| 54 | + std::string("object with name '") + |
| 55 | + name + |
| 56 | + "' and base type '" + |
| 57 | + boost::typeindex::type_id_with_cvr<typename T::element_type>().pretty_name() + |
| 58 | + "' is declared polymorphic, but does not come with a type definition __cxx_type" |
| 59 | + ); |
| 60 | + |
| 61 | + // read smart pointer contents. |
| 62 | + polydecl_type::smart_pointer_get(value, type_name.get(), name, object, options); |
| 63 | + } |
| 64 | + catch (boost::property_tree::ptree_bad_data& exc) |
| 65 | + { |
| 66 | + DEFAULT_PROPERTY_ERROR_HANDLER(T(), T()); |
| 67 | + } |
| 68 | + catch (boost::property_tree::ptree_bad_path& exc) |
| 69 | + { |
| 70 | + DEFAULT_PATH_ERROR_HANDLER(T(), T()); |
| 71 | + } |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + template <typename T> |
| 76 | + struct smart_pointer_parser <T, false, false> |
| 77 | + { |
| 78 | + static void get(T& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options) |
| 79 | + { |
| 80 | + // static_assert(false, "Not a class but polymorphic? you did something wrong!"); |
| 81 | + // poly, but not a class |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + template <typename T> |
| 86 | + void parse_smart_ptr(T& value, std::string const& name, |
| 87 | + PropertyTree const& object, ParsingOptions const& options = {}) |
| 88 | + { |
| 89 | + |
| 90 | + using element_type = typename std::decay<decltype(value)>::type::element_type; |
| 91 | + using polydecl_type = polydecls <element_type>; |
| 92 | + |
| 93 | + internal::smart_pointer_parser <T, |
| 94 | + std::is_class <element_type>::value, |
| 95 | + std::is_same <typename polydecl_type::type, no_poly>::value>::get(value, |
| 96 | + name, |
| 97 | + object, |
| 98 | + options); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments