|
| 1 | +#ifndef JSD_FUSION_ADAPTED_STRUCT_H_INCLUDED |
| 2 | +#define JSD_FUSION_ADAPTED_STRUCT_H_INCLUDED |
| 3 | + |
| 4 | +#include "jsd_core.hpp" |
| 5 | +#include "jsd_object.hpp" |
| 6 | + |
| 7 | +#include <boost/fusion/mpl.hpp> |
| 8 | +#include <boost/fusion/adapted.hpp> |
| 9 | +#include <boost/fusion/include/at.hpp> |
| 10 | +#include <boost/fusion/include/for_each.hpp> |
| 11 | +#include <boost/mpl/range_c.hpp> |
| 12 | +#include <boost/mpl/for_each.hpp> |
| 13 | +#include <boost/phoenix/phoenix.hpp> |
| 14 | +#include <boost/fusion/include/size.hpp> |
| 15 | + |
| 16 | +#include <iostream> |
| 17 | +#include <functional> |
| 18 | +#include <type_traits> |
| 19 | + |
| 20 | +namespace JSON |
| 21 | +{ |
| 22 | + template <typename T> |
| 23 | + class AdaptedParser |
| 24 | + { |
| 25 | + public: |
| 26 | + void operator()(T& object, std::string const& name, PropertyTree const& tree, ParsingOptions const& options) const |
| 27 | + { |
| 28 | + //! If you get an Error here, you likely forgot to use BOOST_FUSION_ADAPT_STRUCT ! |
| 29 | + |
| 30 | + typedef boost::mpl::range_c< |
| 31 | + int, |
| 32 | + 0, |
| 33 | + boost::fusion::result_of::size<T>::type::value |
| 34 | + > range; |
| 35 | + |
| 36 | + std::stringstream sstr; |
| 37 | + boost::mpl::for_each<range>(std::bind<void> |
| 38 | + ( |
| 39 | + _helper(boost::fusion::result_of::size<T>::type::value), |
| 40 | + std::placeholders::_1, |
| 41 | + std::ref(object), |
| 42 | + std::ref(name), |
| 43 | + std::ref(tree), |
| 44 | + std::ref(options) |
| 45 | + )); |
| 46 | + } |
| 47 | + private: |
| 48 | + class _helper |
| 49 | + { |
| 50 | + public: |
| 51 | + template<class Index> |
| 52 | + void operator()(Index, T& object, std::string const& name, PropertyTree const& tree, ParsingOptions const& options) const |
| 53 | + { |
| 54 | + if (name.empty()) |
| 55 | + parse(boost::fusion::at<Index>(object), |
| 56 | + boost::fusion::extension::struct_member_name<T, Index::value>::call(), |
| 57 | + tree, options); |
| 58 | + else |
| 59 | + parse(boost::fusion::at<Index>(object), |
| 60 | + name + "." + boost::fusion::extension::struct_member_name<T, Index::value>::call(), |
| 61 | + tree, options); |
| 62 | + } |
| 63 | + _helper(int len) : len(len) {} |
| 64 | + private: |
| 65 | + int len; |
| 66 | + }; |
| 67 | + }; |
| 68 | + |
| 69 | + template <typename Derived> |
| 70 | + struct Parsable |
| 71 | + { |
| 72 | + void parse(std::string const& name, PropertyTree const& tree, ParsingOptions const& options = {}) |
| 73 | + { |
| 74 | + AdaptedParser<Derived> parser; |
| 75 | + parser(*static_cast <Derived*> (this), name, tree, options); |
| 76 | + } |
| 77 | + virtual ~Parsable() = default; |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +#endif // JSS_FUSION_ADAPTED_STRUCT_H_INCLUDED |
0 commit comments