2017-10-25 19:16:37 +02:00
|
|
|
#ifndef REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_BOOST_HANA_H
|
|
|
|
#define REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_BOOST_HANA_H
|
|
|
|
|
|
|
|
/*!
|
2017-10-28 16:23:39 +02:00
|
|
|
* \file reflector-boosthana.h
|
2017-10-25 19:16:37 +02:00
|
|
|
* \brief Contains generic functions relying on Boost.Hana which can replace the code which would
|
|
|
|
* otherwise had to be generated.
|
2017-10-28 15:50:16 +02:00
|
|
|
* \remarks
|
|
|
|
* These functions use boost::hana::keys() and boost::hana::at_key() rather than the "plain"
|
|
|
|
* for-loop shown in the introspection examples of the Boost.Hana documentation. The reason is that
|
|
|
|
* the "plain" for-loop involves making copies. This costs performance and - more importantly - prevents
|
|
|
|
* modifying the actual object.
|
2017-10-25 19:16:37 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-28 16:23:39 +02:00
|
|
|
#include "./reflector.h"
|
2017-10-25 19:16:37 +02:00
|
|
|
|
2017-11-12 20:13:17 +01:00
|
|
|
#include <boost/hana/adapt_struct.hpp>
|
|
|
|
#include <boost/hana/at_key.hpp>
|
|
|
|
#include <boost/hana/define_struct.hpp>
|
|
|
|
#include <boost/hana/for_each.hpp>
|
|
|
|
#include <boost/hana/intersection.hpp>
|
|
|
|
#include <boost/hana/keys.hpp>
|
2017-10-25 19:16:37 +02:00
|
|
|
|
|
|
|
namespace ReflectiveRapidJSON {
|
2017-11-02 23:35:56 +01:00
|
|
|
namespace JsonReflector {
|
2017-10-25 19:16:37 +02:00
|
|
|
|
2017-11-12 20:13:17 +01:00
|
|
|
// define function to "push" values to a RapidJSON array or object
|
2017-10-25 19:16:37 +02:00
|
|
|
|
2018-06-23 14:35:53 +02:00
|
|
|
template <typename Type, Traits::DisableIf<IsBuiltInType<Type>> *>
|
2017-10-25 19:16:37 +02:00
|
|
|
void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
|
|
|
|
{
|
2017-10-28 15:50:16 +02:00
|
|
|
boost::hana::for_each(boost::hana::keys(reflectable), [&reflectable, &value, &allocator](auto key) {
|
|
|
|
push(boost::hana::at_key(reflectable, key), boost::hana::to<char const *>(key), value, allocator);
|
2017-10-25 19:16:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:13:17 +01:00
|
|
|
// define function to "pull" values from a RapidJSON array or object
|
2017-10-25 19:16:37 +02:00
|
|
|
|
2018-06-23 14:35:53 +02:00
|
|
|
template <typename Type, Traits::DisableIf<IsBuiltInType<Type>> *>
|
2017-10-28 16:23:39 +02:00
|
|
|
void pull(Type &reflectable, const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value,
|
|
|
|
JsonDeserializationErrors *errors)
|
2017-10-25 19:16:37 +02:00
|
|
|
{
|
2017-10-28 15:50:16 +02:00
|
|
|
boost::hana::for_each(boost::hana::keys(reflectable), [&reflectable, &value, &errors](auto key) {
|
|
|
|
pull(boost::hana::at_key(reflectable, key), boost::hana::to<char const *>(key), value, errors);
|
|
|
|
});
|
2017-10-25 19:16:37 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 23:35:56 +01:00
|
|
|
} // namespace JsonReflector
|
2017-10-25 19:16:37 +02:00
|
|
|
} // namespace ReflectiveRapidJSON
|
|
|
|
|
|
|
|
#endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_BOOST_HANA_H
|