JSON RPC
JSON-RPC 2.0 library for C++
export.h
Go to the documentation of this file.
1#ifndef E66C1505_D447_4384_BB35_55B23FF31F0A
2#define E66C1505_D447_4384_BB35_55B23FF31F0A
3
4/**
5 * @file
6 * @brief Defines macros for exporting and importing symbols in the JSON RPC library.
7 *
8 * This file contains macros that control the visibility of symbols in the JSON RPC library.
9 * It handles the differences between static and dynamic linking, as well as platform-specific
10 * visibility attributes for Windows and Unix-like systems.
11 */
12
13/**
14 * @def WWA_JSONRPC_EXPORT
15 * @brief Macro for exporting symbols when building the library dynamically or importing symbols when using the library dynamically.
16 */
17
18/**
19 * @def WWA_JSONRPC_NO_EXPORT
20 * @brief Macro for hiding symbols.
21 */
22
23#ifdef WWA_JSONRPC_STATIC_DEFINE
24# define WWA_JSONRPC_EXPORT
25# define WWA_JSONRPC_NO_EXPORT
26#else
27# ifdef wwa_jsonrpc_EXPORTS
28/* We are building this library; export */
29# if defined _WIN32 || defined __CYGWIN__
30# define WWA_JSONRPC_EXPORT __declspec(dllexport)
31# define WWA_JSONRPC_NO_EXPORT
32# else
33# define WWA_JSONRPC_EXPORT [[gnu::visibility("default")]]
34# define WWA_JSONRPC_NO_EXPORT [[gnu::visibility("hidden")]]
35# endif
36# else
37/* We are using this library; import */
38# if defined _WIN32 || defined __CYGWIN__
39# define WWA_JSONRPC_EXPORT __declspec(dllimport)
40# define WWA_JSONRPC_NO_EXPORT
41# else
42# define WWA_JSONRPC_EXPORT [[gnu::visibility("default")]]
43# define WWA_JSONRPC_NO_EXPORT [[gnu::visibility("hidden")]]
44# endif
45# endif
46#endif
47
48#endif /* E66C1505_D447_4384_BB35_55B23FF31F0A */
Private implementation of the JSON RPC dispatcher class.
A class that manages JSON RPC method handlers and processes JSON RPC requests.
Definition dispatcher.h:77
dispatcher & operator=(dispatcher &&rhs)=default
Move assignment operator.
dispatcher(dispatcher &&rhs)=default
Move constructor.
virtual void on_request(const nlohmann::json &extra)
Invoked when a request is received.
void add_ex(std::string_view method, F &&f, C instance)
Adds a method handler with an extra parameter and a class instance.
Definition dispatcher.h:277
std::string parse_and_process_request(const std::string &request, const nlohmann::json &extra=nlohmann::json::object())
Parses and processes a JSON RPC request.
constexpr auto create_closure(C inst, F &&f) const
Creates a closure for invoking a member function with JSON parameters.
Definition dispatcher.h:455
std::string process_request(const nlohmann::json &request, const nlohmann::json &extra=nlohmann::json::object())
Processes a JSON RPC request.
virtual void on_request_processed(const std::string &method, int code, const nlohmann::json &extra)
Invoked after the method handler is called.
void add_internal_method(std::string_view method, handler_t &&handler)
Adds a method handler for the specified method.
void add(std::string_view method, F &&f)
Adds a method handler f for the method method.
Definition dispatcher.h:192
virtual void on_method(const std::string &method, const nlohmann::json &extra)
Invoked right before the method handler is called.
void add(std::string_view method, F &&f, C instance)
Adds a method to the dispatcher with the specified instance and function.
Definition dispatcher.h:210
dispatcher()
Class constructor.
dispatcher(const dispatcher &)=delete
dispatcher & operator=(const dispatcher &)=delete
virtual ~dispatcher()
Class destructor.
void add_ex(std::string_view method, F &&f)
Adds a method handler with an extra parameter.
Definition dispatcher.h:254
std::unique_ptr< dispatcher_private > d_ptr
Pointer to the implementation (Pimpl idiom).
Definition dispatcher.h:413
JSON RPC Exception class.
Definition exception.h:79
exception(const exception &)=default
Default copy constructor.
~exception() override
Default destructor.
nlohmann::json m_data
Custom data associated with the error.
Definition exception.h:224
std::string m_message
Error message.
Definition exception.h:223
int code() const noexcept
Returns the error code.
Definition exception.h:176
const std::string & message() const noexcept
Returns the error message.
Definition exception.h:183
exception(exception &&)=default
Default move constructor.
exception(int code, std::string_view message)
Construct a new exception object.
Definition exception.h:138
exception & operator=(const exception &rhs)=default
Default copy assignment operator.
const nlohmann::json & data() const noexcept
Returns custom data associated with the error.
Definition exception.h:190
exception & operator=(exception &&rhs)=default
Default move assignment operator.
const char * what() const noexcept override
Returns the error message.
Definition exception.h:200
nlohmann::json to_json() const
Returns the error message as an Error Object.
Definition exception.h:208
exception(int code, std::string_view message, const T &data)
Construct a new exception object with additional data.
Definition exception.h:127
int m_code
Error code.
Definition exception.h:225
#define WWA_JSONRPC_EXPORT
Macro for exporting symbols when building the library dynamically or importing symbols when using the...
Definition export.h:42
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
Definition exception.h:108
static constexpr int INTERNAL_ERROR
Internal JSON-RPC error.
Definition exception.h:113
static constexpr int METHOD_NOT_FOUND
The method does not exist or is not available.
Definition exception.h:103
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
Definition exception.h:98
static constexpr int PARSE_ERROR
Invalid JSON was received by the server.
Definition exception.h:93
static constexpr std::string_view err_method_not_found
Error message for when the method is not found.
Definition exception.h:43
static constexpr std::string_view err_not_jsonrpc_2_0_request
Error message for when the request is not a JSON-RPC 2.0 request.
Definition exception.h:31
static constexpr std::string_view err_empty_batch
Error message for when the batch request is empty.
Definition exception.h:70
static constexpr std::string_view err_empty_method
Error message for when the method is empty.
Definition exception.h:49
static constexpr std::string_view err_bad_params_type
Error message for when the parameters are not an array or an object.
Definition exception.h:55
static constexpr std::string_view err_invalid_params_passed_to_method
Error message for when the parameters passed to the method are not correct.
Definition exception.h:37
static constexpr std::string_view err_bad_id_type
Error message for when the ID is not a number, a string, or null.
Definition exception.h:63
constexpr auto convert_args(const nlohmann::json &params, std::index_sequence< Indices... >)
Converts JSON parameters to a tuple of arguments based on the specified types.
Definition details.h:346
constexpr auto make_inst_tuple(C inst)
Creates a tuple containing the instance if it is a class pointer, or an empty tuple if it is a null p...
Definition details.h:239
Contains the implementation details of the JSON RPC library.
Definition details.h:31
Primary template for function traits.
Definition details.h:172