|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "../parse/jsd_object.hpp" |
| 4 | +#include "../utility/optional_info.hpp" |
| 5 | + |
| 6 | +#include <type_traits> |
| 7 | +#include <boost/optional.hpp> |
| 8 | + |
| 9 | +#include <boost/fusion/sequence/intrinsic/at.hpp> |
| 10 | +#include <boost/fusion/include/at.hpp> |
| 11 | +#include <boost/fusion/adapted.hpp> |
| 12 | +#include <boost/fusion/include/at.hpp> |
| 13 | +#include <boost/mpl/range_c.hpp> |
| 14 | +#include <boost/mpl/for_each.hpp> |
| 15 | +#include <boost/fusion/include/size.hpp> |
| 16 | + |
| 17 | +namespace JSON |
| 18 | +{ |
| 19 | + template <typename T> |
| 20 | + void fill_missing(std::string const& name, PropertyTree& tree); |
| 21 | + |
| 22 | + template <typename T> |
| 23 | + class Filler |
| 24 | + { |
| 25 | +public: |
| 26 | + template <typename U = T> |
| 27 | + typename std::enable_if <Internal::isParsable<U>::value, void>::type |
| 28 | + operator()(std::string const& name, PropertyTree& tree) const |
| 29 | + { |
| 30 | + //! If you get an Error here, you likely forgot to use BOOST_FUSION_ADAPT_STRUCT ! |
| 31 | + |
| 32 | + typedef boost::mpl::range_c< |
| 33 | + int, |
| 34 | + 0, |
| 35 | + boost::fusion::result_of::size<T>::type::value |
| 36 | + > range; |
| 37 | + |
| 38 | + std::stringstream sstr; |
| 39 | + boost::mpl::for_each<range>(std::bind<void> |
| 40 | + ( |
| 41 | + _helper(boost::fusion::result_of::size<T>::type::value), |
| 42 | + std::placeholders::_1, |
| 43 | + std::ref(name), |
| 44 | + std::ref(tree) |
| 45 | + )); |
| 46 | + } |
| 47 | + |
| 48 | + template <typename U = T> |
| 49 | + typename std::enable_if <!Internal::isParsable<U>::value, void>::type |
| 50 | + operator()(std::string const& name, PropertyTree& tree) const |
| 51 | + { |
| 52 | + //std::cout << "endpoint: " << name << "\n"; |
| 53 | + default_initialize <U>(name, tree); |
| 54 | + } |
| 55 | + |
| 56 | + private: |
| 57 | + class _helper |
| 58 | + { |
| 59 | + public: |
| 60 | + template<class Index> |
| 61 | + void operator()(Index, std::string const& name, PropertyTree& tree) const |
| 62 | + { |
| 63 | + using type = typename boost::fusion::result_of::at_c<T, Index::value>::type; |
| 64 | + |
| 65 | + if (!name.empty()) |
| 66 | + default_initialize <type>(name, tree); |
| 67 | + |
| 68 | + std::string deeperName; |
| 69 | + if (name.empty()) |
| 70 | + deeperName = boost::fusion::extension::struct_member_name<T, Index::value>::call(); |
| 71 | + else |
| 72 | + deeperName = name + "." + boost::fusion::extension::struct_member_name<T, Index::value>::call(); |
| 73 | + |
| 74 | + std::cout << deeperName << "\n"; |
| 75 | + fill_missing <type>(deeperName, tree); |
| 76 | + } |
| 77 | + _helper(int len) : len(len) {} |
| 78 | + private: |
| 79 | + int len; |
| 80 | + }; |
| 81 | + |
| 82 | + template <typename U> |
| 83 | + typename std::enable_if <!Internal::is_optional <U>::value, void>::type |
| 84 | + static default_initialize(std::string const& name, PropertyTree& tree) |
| 85 | + { |
| 86 | + //using raw = typename std::decay<U>::type; |
| 87 | + auto opt = tree.tree.get_optional <std::string>(name); |
| 88 | + if (!opt) |
| 89 | + tree.tree.put(name, std::string{}); |
| 90 | + } |
| 91 | + |
| 92 | + template <typename U> |
| 93 | + typename std::enable_if <Internal::is_optional <U>::value, void>::type |
| 94 | + static default_initialize(std::string const& name, PropertyTree& tree) |
| 95 | + { |
| 96 | + // do nothing |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + template <typename T> |
| 101 | + void fill_missing(std::string const& name, PropertyTree& tree) |
| 102 | + { |
| 103 | + Filler <typename std::decay<T>::type> filler; |
| 104 | + filler(name, tree); |
| 105 | + } |
| 106 | +} |
0 commit comments