nlib
JsonRpcResponse.h
[詳解]
1 
2 /*---------------------------------------------------------------------------*
3 
4  Project: CrossRoad
5  Copyright (C)2012-2016 Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12 
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"
24 
25 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
26 #undef NLIB_VIS_PUBLIC
27 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
28 #endif
29 
30 NLIB_NAMESPACE_BEGIN
31 namespace msgpack {
32 namespace jsonrpc {
33 typedef uint32_t reqid_t;
34 
36  public:
37  enum ErrorCode {
38  OK = 0,
39  PARSE_ERROR = -32700,
40  INVALID_REQUEST = -32600,
41  METHOD_NOT_FOUND = -32601,
42  INVALID_PARAMS = -32602,
43  INTERNAL_ERROR = -32603,
44  CLIENT_ABORT = -31000
45  };
47  : id_(0), is_error_(false), error_code_(0), error_msg_(NULL) {}
49 
50  void MoveResultFrom(MpObject& obj) NLIB_NOEXCEPT; // NOLINT
51  void SetError(int errcode, const char* msg, MpObject& data) NLIB_NOEXCEPT; // NOLINT
52  void SetError(int errcode, const char* msg) NLIB_NOEXCEPT {
53  MpObject data;
54  this->SetError(errcode, msg, data);
55  }
56 
57  ErrorCode GetError() const NLIB_NOEXCEPT {
58  return static_cast<ErrorCode>(error_code_);
59  }
60  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
61  const char* GetErrorMessage() const NLIB_NOEXCEPT {
62  return error_msg_ ? error_msg_ : "";
63  }
64  MpObject& GetMpObject() NLIB_NOEXCEPT { return obj_; }
65  const MpObject& GetMpObject() const NLIB_NOEXCEPT { return obj_; }
66  NLIB_SAFE_BOOL(JsonRpcResponse, !is_error_);
67 
68  private:
69  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; } // from JsonRpcRequest::Init()
70  NLIB_VIS_HIDDEN errno_t Init(MpObject* rhs) NLIB_NOEXCEPT; // from JsonRpcResponseReader
71 
72  private:
73  reqid_t id_;
74  bool is_error_;
75  int error_code_;
76  char* error_msg_;
77  MpObject obj_;
78 
79  friend class JsonRpcResponseReader;
80  friend class JsonRpcRequest;
82 };
83 
85  public:
88  static errno_t ReadResponse(const void* p, size_t n, ListType* result_list) NLIB_NOEXCEPT;
89 };
90 
92  public:
93  JsonRpcResponseWriter() NLIB_NOEXCEPT : prv_(NULL), is_msgpack_(false) {}
95  explicit JsonRpcResponseWriter(bool msgpack) NLIB_NOEXCEPT
96  : prv_(NULL), is_msgpack_(msgpack) {}
97  errno_t BeginWriteResponse(uint32_t n) NLIB_NOEXCEPT;
98  errno_t WriteResponse(JsonRpcResponse* result) NLIB_NOEXCEPT;
99  errno_t EndWriteResponse(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
100 
101  private:
102  struct JsonRpcResponseWriterPrivate;
103  JsonRpcResponseWriterPrivate* prv_;
104  bool is_msgpack_;
106 };
107 
108 } // namespace jsonrpc
109 } // namespace msgpack
110 NLIB_NAMESPACE_END
111 
112 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
113 #undef NLIB_VIS_PUBLIC
114 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
115 #endif
116 
117 #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:158
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:173
JsonRpcResponseWriter(bool msgpack) noexcept
引数にtrueを指定するとmsgpackのバイト列を作成します。
UniquePtrはポインタの所有権を保持し、UniquePtrがスコープから出るときにデストラクタでポインタをDELで指...
Definition: UniquePtr.h:109
std::unique_ptrに相当するクラスが定義されています。
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:86
JSON-RPCのレスポンスのバイト列を読み込むためのクラスです。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
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:224
JsonRpcResponse() noexcept
デフォルトコンストラクタです。
JsonRpcResponseWriter() noexcept
デフォルトコンストラクタです。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37