nlib
JsonRpcResponse.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
17 #define INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/UniquePtr.h"
21 #include "nn/nlib/Nlist.h"
22 #include "nn/nlib/Swap.h"
25 
26 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
27 #undef NLIB_VIS_PUBLIC
28 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
29 #endif
30 
31 NLIB_NAMESPACE_BEGIN
32 namespace msgpack {
33 namespace jsonrpc {
34 typedef uint32_t reqid_t;
35 
37  public:
38  enum ErrorCode {
39  kOk = 0,
40  kParseError = -32700,
41  kInvalidRequest = -32600,
42  kMethodNotFound = -32601,
43  kInvalidParams = -32602,
44  kInternalError = -32603,
45  kClientAbort = -31000,
46  OK = kOk,
47  PARSE_ERROR = kParseError,
48  INVALID_REQUEST = kInvalidRequest,
49  METHOD_NOT_FOUND = kMethodNotFound,
50  INVALID_PARAMS = kInvalidParams,
51  INTERNAL_ERROR = kInternalError,
52  CLIENT_ABORT = kClientAbort
53  };
55  : id_(0), is_error_(false), error_code_(0), error_msg_(NULL) {}
57 
58  void MoveResultFrom(MpObject& obj) NLIB_NOEXCEPT; // NOLINT
59  void SetError(int errcode, const char* msg, MpObject& data) NLIB_NOEXCEPT; // NOLINT
60  void SetError(int errcode, const char* msg) NLIB_NOEXCEPT {
61  MpObject data;
62  this->SetError(errcode, msg, data);
63  }
64 
65  ErrorCode GetError() const NLIB_NOEXCEPT {
66  return static_cast<ErrorCode>(error_code_);
67  }
68  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
69  const char* GetErrorMessage() const NLIB_NOEXCEPT {
70  return error_msg_ ? error_msg_ : "";
71  }
72  MpObject& GetMpObject() NLIB_NOEXCEPT { return obj_; }
73  const MpObject& GetMpObject() const NLIB_NOEXCEPT { return obj_; }
74  NLIB_SAFE_BOOL(JsonRpcResponse, !is_error_);
75 
76  private:
77  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; } // from JsonRpcRequest::Init()
78  NLIB_VIS_HIDDEN errno_t Init(MpObject* rhs) NLIB_NOEXCEPT; // from JsonRpcResponseReader
79 
80  private:
81  reqid_t id_;
82  bool is_error_;
83  int error_code_;
84  char* error_msg_;
85  MpObject obj_;
86 
87  friend class JsonRpcResponseReader;
88  friend class JsonRpcRequest;
90 };
91 
93  public:
96  static errno_t ReadResponse(const void* p, size_t n, ListType* result_list) NLIB_NOEXCEPT;
97 };
98 
100  public:
101  JsonRpcResponseWriter() NLIB_NOEXCEPT : prv_(NULL), is_msgpack_(false) {}
103  NLIB_MOVE_MEMBER_HELPER_1(JsonRpcResponseWriter, prv_);
104  explicit JsonRpcResponseWriter(bool msgpack) NLIB_NOEXCEPT
105  : prv_(NULL), is_msgpack_(msgpack) {}
106  errno_t BeginWriteResponse(uint32_t n) NLIB_NOEXCEPT;
107  errno_t WriteResponse(JsonRpcResponse* result) NLIB_NOEXCEPT;
108  errno_t EndWriteResponse(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
109  void swap(JsonRpcResponseWriter& rhs) NLIB_NOEXCEPT {
110  using std::swap;
111  swap(prv_, rhs.prv_);
112  swap(is_msgpack_, rhs.is_msgpack_);
113  }
114 
115  private:
116  struct JsonRpcResponseWriterPrivate;
117  JsonRpcResponseWriterPrivate* prv_;
118  bool is_msgpack_;
120 };
121 
122 } // namespace jsonrpc
123 } // namespace msgpack
124 NLIB_NAMESPACE_END
125 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::jsonrpc::JsonRpcResponseWriter)
126 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
127 #undef NLIB_VIS_PUBLIC
128 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
129 #endif
130 
131 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
UniquePtr< JsonRpcResponse > ValueType
UniquePtr経由でJSON-RPCのレスポンスを格納します。
void SetError(int errcode, const char *msg) noexcept
JSON-RPC関数のエラーを設定します。
JSON-RPCのレスポンスを表すクラスです。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:163
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:178
JsonRpcResponseWriter(bool msgpack) noexcept
引数にtrueを指定するとmsgpackのバイト列を作成します。
UniquePtrはポインタの所有権を保持し、UniquePtrがスコープから出るときにデストラクタでポインタをDELで指...
Definition: UniquePtr.h:109
std::unique_ptrに相当するクラスが定義されています。
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:88
JSON-RPCのレスポンスのバイト列を読み込むためのクラスです。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
MessagePack又はJSONを読み込むことで作成されるオブジェクトです。
Definition: MpObject.h:95
MpObject & GetMpObject() noexcept
JSON-RPCの戻り値か、エラーの付加情報を返します。
std::vectorに似ていますが、コピーできないオブジェクトを格納可能なクラスが定義されています。 ...
const MpObject & GetMpObject() const noexcept
JSON-RPCの戻り値か、エラーの付加情報を返します。
const char * GetErrorMessage() const noexcept
エラーメッセージを返します。
JSON-RPCのリクエストを表すクラスです。
MessagePack, JSON及びCSVを読み込むと作成されるオブジェクトです。
reqid_t GetId() const noexcept
JSON-RPCレスポンスのid を返します。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
ErrorCode
JSON-RPCのレスポンスに含まれる(事前定義の)エラーコードの定義です。
開発環境別の設定が書かれるファイルです。
uint32_t reqid_t
JSON-RPCのリクエストに付与されるidの型です。
Definition: JsonRpcClient.h:32
std::vectorに似た、コピーコンストラクタを持たないオブジェクトを格納可能なコンテナ類似クラスです。 ...
Definition: Nlist.h:32
JSON-RPCのレスポンスのバイト列を書きこむためのクラスです。
ErrorCode GetError() const noexcept
エラーコードを返します。
Nlist< ValueType > ListType
JSON-RPCのレスポンスのシーケンスを格納する型です。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
JsonRpcResponse() noexcept
デフォルトコンストラクタです。
JsonRpcResponseWriter() noexcept
デフォルトコンストラクタです。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37