Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some #include improvements #229

Open
wants to merge 1 commit into
base: feature/dht2-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 2 additions & 72 deletions include/lib/graft/inout.h
Original file line number Diff line number Diff line change
@@ -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 <utility>
#include <string>
#include <vector>
#include <tuple>
#include <unordered_map>
#include <boost/hana.hpp>

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<Payment>, 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<typename T>
struct JSON
Expand Down
1 change: 1 addition & 0 deletions include/lib/graft/jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#pragma once

#include "lib/graft/serialize.h"
#include "lib/graft/inout.h"

#include <string>
Expand Down
1 change: 1 addition & 0 deletions include/lib/graft/requests/health_check.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include "lib/graft/serialize.h"
#include "lib/graft/router.h"

namespace graft::request {
Expand Down
2 changes: 1 addition & 1 deletion include/lib/graft/requests/requestdefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
79 changes: 79 additions & 0 deletions include/lib/graft/serialize.h
Original file line number Diff line number Diff line change
@@ -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 <boost/hana.hpp>

#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<Payment>, 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
1 change: 1 addition & 0 deletions include/supernode/requests/authorize_rta_tx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include "lib/graft/serialize.h"
#include "lib/graft/router.h"

namespace graft::supernode::request {
Expand Down
1 change: 1 addition & 0 deletions include/supernode/requests/pay_status.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include "lib/graft/serialize.h"
#include "lib/graft/router.h"

namespace graft::supernode::request {
Expand Down
1 change: 1 addition & 0 deletions include/supernode/requests/reject_pay.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include "lib/graft/serialize.h"
#include "lib/graft/router.h"

namespace graft::supernode::request {
Expand Down
1 change: 1 addition & 0 deletions include/supernode/requests/reject_sale.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include "lib/graft/serialize.h"
#include "lib/graft/router.h"

namespace graft::supernode::request {
Expand Down
2 changes: 1 addition & 1 deletion include/supernode/requests/sale_details.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#pragma once

#include "lib/graft/inout.h"
#include "lib/graft/serialize.h"
#include "lib/graft/router.h"

#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/graft/sys_info_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down