From f6d0c4bea758388b413732d7d35602a38a6d6626 Mon Sep 17 00:00:00 2001 From: Alexander Suprunenko Date: Tue, 12 Mar 2019 00:19:30 +0200 Subject: [PATCH] Some #include improvements --- include/lib/graft/inout.h | 74 +---------------- include/lib/graft/jsonrpc.h | 1 + include/lib/graft/requests/health_check.h | 1 + include/lib/graft/requests/requestdefines.h | 2 +- include/lib/graft/serialize.h | 79 +++++++++++++++++++ include/supernode/requests/authorize_rta_tx.h | 1 + include/supernode/requests/pay_status.h | 1 + include/supernode/requests/reject_pay.h | 1 + include/supernode/requests/reject_sale.h | 1 + include/supernode/requests/sale_details.h | 2 +- src/lib/graft/sys_info_request.cpp | 2 +- 11 files changed, 90 insertions(+), 75 deletions(-) create mode 100644 include/lib/graft/serialize.h diff --git a/include/lib/graft/inout.h b/include/lib/graft/inout.h index bb81034c..a116c243 100644 --- a/include/lib/graft/inout.h +++ b/include/lib/graft/inout.h @@ -1,94 +1,24 @@ #pragma once -#include "lib/graft/graft_macros.h" #include "lib/graft/common/utils.h" -#include "lib/graft/reflective-rapidjson/reflector-boosthana.h" -#include "lib/graft/reflective-rapidjson/serializable.h" -#include "lib/graft/reflective-rapidjson/types.h" - #include #include #include #include #include -#include struct http_message; //from mongoose.h struct mg_str; //from mongoose.h -#define GRAFT_DEFINE_IO_STRUCT(__S__, ...) \ - struct __S__ : public ReflectiveRapidJSON::JsonSerializable<__S__> { \ - BOOST_HANA_DEFINE_STRUCT(__S__, __VA_ARGS__); \ - } - -#define GRAFT_DEFINE_IO_STRUCT_INITED(__S__, ...) \ - struct __S__ : public ReflectiveRapidJSON::JsonSerializable<__S__> { \ - __S__() : INIT_PAIRS(__VA_ARGS__) {} \ - BOOST_HANA_DEFINE_STRUCT(__S__, TN_PAIRS(__VA_ARGS__)); \ - } - -/* - * Mapping of supported C++ types to supported JSON types - * ======================================================================== - * C++ type | JSON type - * ---------------------------------------------------------+-------------- - * custom structures/classes | object - * bool | true/false - * signed and unsigned integral types | number - * float and double | number - * enum and enum class | number - * std::string | string - * const char * | string - * iteratable lists (std::vector, std::list, ...) | array - * sets (std::set, std::unordered_set, std::multiset, ...) | array - * std::tuple | array - * std::unique_ptr, std::shared_ptr | depends/null - * std::map, std::unordered_map | object - * JsonSerializable | object - * ---------------------------------------------------------+-------------- - * - * Example of structure definitions: - * ================================= - * - * GRAFT_DEFINE_IO_STRUCT(Payment, - * (uint64, amount), - * (uint32, block_height), - * (std::string, payment_id), - * (std::string, tx_hash), - * (uint32, unlock_time) - * ); - * - * or initialized with default values - * - * GRAFT_DEFINE_IO_STRUCT_INITED(Payment, - * (uint64, amount, 999), - * (uint32, block_height, 10000), - * (std::string, payment_id, "abc"), - * (std::string, tx_hash, "def"), - * (uint32, unlock_time, 555555) - * ); - * - * GRAFT_DEFINE_IO_STRUCT(Payments, - * (std::vector, payments) - * ); - */ +namespace rapidjson { class ParseResult; } namespace graft { namespace serializer { - class JsonParseError : public std::runtime_error - { - public: - JsonParseError(const rapidjson::ParseResult &pr) - : std::runtime_error( std::string("Json parse error, code: ") + std::to_string(pr.Code()) - + ", offset: " + std::to_string(pr.Offset())) - { - } - }; - + class JsonParseError; template struct JSON diff --git a/include/lib/graft/jsonrpc.h b/include/lib/graft/jsonrpc.h index 8d7fa707..6797f6e4 100644 --- a/include/lib/graft/jsonrpc.h +++ b/include/lib/graft/jsonrpc.h @@ -29,6 +29,7 @@ #pragma once +#include "lib/graft/serialize.h" #include "lib/graft/inout.h" #include diff --git a/include/lib/graft/requests/health_check.h b/include/lib/graft/requests/health_check.h index 3f0ec3e8..f7e08287 100644 --- a/include/lib/graft/requests/health_check.h +++ b/include/lib/graft/requests/health_check.h @@ -1,6 +1,7 @@ #pragma once +#include "lib/graft/serialize.h" #include "lib/graft/router.h" namespace graft::request { diff --git a/include/lib/graft/requests/requestdefines.h b/include/lib/graft/requests/requestdefines.h index c89aeb11..714658c0 100644 --- a/include/lib/graft/requests/requestdefines.h +++ b/include/lib/graft/requests/requestdefines.h @@ -2,7 +2,7 @@ #pragma once #include "lib/graft/graft_constants.h" -#include "lib/graft/inout.h" +#include "lib/graft/serialize.h" #include "lib/graft/router.h" #include "rta/supernode.h" diff --git a/include/lib/graft/serialize.h b/include/lib/graft/serialize.h new file mode 100644 index 00000000..a6ea5248 --- /dev/null +++ b/include/lib/graft/serialize.h @@ -0,0 +1,79 @@ + +#pragma once + +#include "lib/graft/graft_macros.h" + +#include "lib/graft/reflective-rapidjson/reflector-boosthana.h" +#include "lib/graft/reflective-rapidjson/serializable.h" +#include "lib/graft/reflective-rapidjson/types.h" + +#include + +#define GRAFT_DEFINE_IO_STRUCT(__S__, ...) \ + struct __S__ : public ReflectiveRapidJSON::JsonSerializable<__S__> { \ + BOOST_HANA_DEFINE_STRUCT(__S__, __VA_ARGS__); \ + } + +#define GRAFT_DEFINE_IO_STRUCT_INITED(__S__, ...) \ + struct __S__ : public ReflectiveRapidJSON::JsonSerializable<__S__> { \ + __S__() : INIT_PAIRS(__VA_ARGS__) {} \ + BOOST_HANA_DEFINE_STRUCT(__S__, TN_PAIRS(__VA_ARGS__)); \ + } + +/* + * Mapping of supported C++ types to supported JSON types + * ======================================================================== + * C++ type | JSON type + * ---------------------------------------------------------+-------------- + * custom structures/classes | object + * bool | true/false + * signed and unsigned integral types | number + * float and double | number + * enum and enum class | number + * std::string | string + * const char * | string + * iteratable lists (std::vector, std::list, ...) | array + * sets (std::set, std::unordered_set, std::multiset, ...) | array + * std::tuple | array + * std::unique_ptr, std::shared_ptr | depends/null + * std::map, std::unordered_map | object + * JsonSerializable | object + * ---------------------------------------------------------+-------------- + * + * Example of structure definitions: + * ================================= + * + * GRAFT_DEFINE_IO_STRUCT(Payment, + * (uint64, amount), + * (uint32, block_height), + * (std::string, payment_id), + * (std::string, tx_hash), + * (uint32, unlock_time) + * ); + * + * or initialized with default values + * + * GRAFT_DEFINE_IO_STRUCT_INITED(Payment, + * (uint64, amount, 999), + * (uint32, block_height, 10000), + * (std::string, payment_id, "abc"), + * (std::string, tx_hash, "def"), + * (uint32, unlock_time, 555555) + * ); + * + * GRAFT_DEFINE_IO_STRUCT(Payments, + * (std::vector, payments) + * ); + */ + +namespace graft::serializer +{ + class JsonParseError : public std::runtime_error + { + public: + JsonParseError(const rapidjson::ParseResult &pr) + : std::runtime_error( std::string("Json parse error, code: ") + std::to_string(pr.Code()) + + ", offset: " + std::to_string(pr.Offset())) + { } + }; +} //namespace graft::serializer diff --git a/include/supernode/requests/authorize_rta_tx.h b/include/supernode/requests/authorize_rta_tx.h index e1ee1e89..02d7b6d8 100644 --- a/include/supernode/requests/authorize_rta_tx.h +++ b/include/supernode/requests/authorize_rta_tx.h @@ -1,6 +1,7 @@ #pragma once +#include "lib/graft/serialize.h" #include "lib/graft/router.h" namespace graft::supernode::request { diff --git a/include/supernode/requests/pay_status.h b/include/supernode/requests/pay_status.h index b071c0f2..65652893 100644 --- a/include/supernode/requests/pay_status.h +++ b/include/supernode/requests/pay_status.h @@ -1,6 +1,7 @@ #pragma once +#include "lib/graft/serialize.h" #include "lib/graft/router.h" namespace graft::supernode::request { diff --git a/include/supernode/requests/reject_pay.h b/include/supernode/requests/reject_pay.h index 35ef63be..92b069ce 100644 --- a/include/supernode/requests/reject_pay.h +++ b/include/supernode/requests/reject_pay.h @@ -1,6 +1,7 @@ #pragma once +#include "lib/graft/serialize.h" #include "lib/graft/router.h" namespace graft::supernode::request { diff --git a/include/supernode/requests/reject_sale.h b/include/supernode/requests/reject_sale.h index c32aecc3..e7f38115 100644 --- a/include/supernode/requests/reject_sale.h +++ b/include/supernode/requests/reject_sale.h @@ -1,6 +1,7 @@ #pragma once +#include "lib/graft/serialize.h" #include "lib/graft/router.h" namespace graft::supernode::request { diff --git a/include/supernode/requests/sale_details.h b/include/supernode/requests/sale_details.h index d12b0381..66cd8705 100644 --- a/include/supernode/requests/sale_details.h +++ b/include/supernode/requests/sale_details.h @@ -1,7 +1,7 @@ #pragma once -#include "lib/graft/inout.h" +#include "lib/graft/serialize.h" #include "lib/graft/router.h" #include diff --git a/src/lib/graft/sys_info_request.cpp b/src/lib/graft/sys_info_request.cpp index 5cffdd14..d4d2330e 100644 --- a/src/lib/graft/sys_info_request.cpp +++ b/src/lib/graft/sys_info_request.cpp @@ -2,7 +2,7 @@ #include "lib/graft/sys_info_request.h" #include "lib/graft/context.h" -#include "lib/graft/connection.h" +#include "lib/graft/task.h" #include "lib/graft/inout.h" #include "lib/graft/router.h" #include "lib/graft/sys_info.h"