nlib
JsonRpcResponse.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/UniquePtr.h"
8 #include "nn/nlib/Nlist.h"
11 
12 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
13 #undef NLIB_VIS_PUBLIC
14 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
15 #endif
16 
17 NLIB_NAMESPACE_BEGIN
18 namespace msgpack {
19 namespace jsonrpc {
20 typedef uint32_t reqid_t;
21 
23  public:
24  enum ErrorCode {
25  OK = 0,
26  PARSE_ERROR = -32700,
27  INVALID_REQUEST = -32600,
28  METHOD_NOT_FOUND = -32601,
29  INVALID_PARAMS = -32602,
30  INTERNAL_ERROR = -32603,
31  CLIENT_ABORT = -31000
32  };
34  : id_(0), is_error_(false), error_code_(0), error_msg_(NULL) {}
36 
37  void MoveResultFrom(MpObject& obj) NLIB_NOEXCEPT; // NOLINT
38  void SetError(int errcode, const char* msg, MpObject& data) NLIB_NOEXCEPT; // NOLINT
39  void SetError(int errcode, const char* msg) NLIB_NOEXCEPT {
40  MpObject data;
41  this->SetError(errcode, msg, data);
42  }
43 
44  ErrorCode GetError() const NLIB_NOEXCEPT {
45  return static_cast<ErrorCode>(error_code_);
46  }
47  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
48  const char* GetErrorMessage() const NLIB_NOEXCEPT {
49  return error_msg_ ? error_msg_ : "";
50  }
51  MpObject& GetMpObject() NLIB_NOEXCEPT { return obj_; }
52  const MpObject& GetMpObject() const NLIB_NOEXCEPT { return obj_; }
53  NLIB_SAFE_BOOL(JsonRpcResponse, !is_error_);
54 
55  private:
56  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; } // from JsonRpcRequest::Init()
57  NLIB_VIS_HIDDEN errno_t Init(MpObject* rhs) NLIB_NOEXCEPT; // from JsonRpcResponseReader
58 
59  private:
60  reqid_t id_;
61  bool is_error_;
62  int error_code_;
63  char* error_msg_;
64  MpObject obj_;
65 
66  friend class JsonRpcResponseReader;
67  friend class JsonRpcRequest;
69 };
70 
72  public:
75  static errno_t ReadResponse(const void* p, size_t n, ListType* result_list) NLIB_NOEXCEPT;
76 };
77 
79  public:
80  JsonRpcResponseWriter() NLIB_NOEXCEPT : prv_(NULL), is_msgpack_(false) {}
82  explicit JsonRpcResponseWriter(bool msgpack) NLIB_NOEXCEPT
83  : prv_(NULL), is_msgpack_(msgpack) {}
84  errno_t BeginWriteResponse(uint32_t n) NLIB_NOEXCEPT;
85  errno_t WriteResponse(JsonRpcResponse* result) NLIB_NOEXCEPT;
86  errno_t EndWriteResponse(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
87 
88  private:
89  struct JsonRpcResponseWriterPrivate;
90  JsonRpcResponseWriterPrivate* prv_;
91  bool is_msgpack_;
93 };
94 
95 } // namespace jsonrpc
96 } // namespace msgpack
97 NLIB_NAMESPACE_END
98 
99 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
100 #undef NLIB_VIS_PUBLIC
101 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
102 #endif
103 
104 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
UniquePtr< JsonRpcResponse > ValueType
Stores the JSON-RPC response via UniquePtr.
void SetError(int errcode, const char *msg) noexcept
Sets the JSON-RPC function error.
Class for representing the JSON-RPC response.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:145
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:160
JsonRpcResponseWriter(bool msgpack) noexcept
Creates a msgpack byte string when true is specified as the argument.
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:96
Defines that class that is corresponding to std::unique_ptr.
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
Class that reads the JSON-RPC response byte string.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
Object created when MessagePack or JSON is read.
Definition: MpObject.h:82
MpObject & GetMpObject() noexcept
Returns the JSON-RPC return value, or additional information about an error.
Defines the class that resembles std::vector but can store objects that cannot be copied...
const MpObject & GetMpObject() const noexcept
Returns the JSON-RPC return value, or additional information about an error.
const char * GetErrorMessage() const noexcept
Returns an error message.
Class for representing a JSON-RPC request.
Object created when MessagePack, JSON, or CSV is read.
reqid_t GetId() const noexcept
Returns the ID of the JSON-RPC response.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
ErrorCode
Definition of the predefined error code contained in the JSON-RPC response.
A file that contains the configuration information for each development environment.
uint32_t reqid_t
id type for the JSON-RPC request.
Definition: JsonRpcClient.h:19
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Definition: Nlist.h:19
Class that writes the JSON-RPC response byte string.
ErrorCode GetError() const noexcept
Returns an error code.
Nlist< ValueType > ListType
Type that stores the JSON-RPC response sequence.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
JsonRpcResponse() noexcept
Instantiates the object with default parameters (default constructor).
JsonRpcResponseWriter() noexcept
Instantiates the object with default parameters (default constructor).
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24