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 #ifdef __cpp_rvalue_references
27 #include <tuple>
28 #endif
29 
30 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
31 #undef NLIB_VIS_PUBLIC
32 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
33 #endif
34 
35 NLIB_NAMESPACE_BEGIN
36 namespace msgpack {
37 namespace jsonrpc {
38 typedef uint32_t reqid_t;
39 
41  public:
42  enum ErrorCode {
43  kOk = 0,
44  kParseError = -32700,
45  kInvalidRequest = -32600,
46  kMethodNotFound = -32601,
47  kInvalidParams = -32602,
48  kInternalError = -32603,
49  kClientAbort = -31000
50  };
52  is_error_(false),
53  error_code_(0),
54  error_msg_(nullptr) {}
56 
57  void MoveResultFrom(MpObject& obj) NLIB_NOEXCEPT;
58 #ifdef __cpp_rvalue_references
59  void MoveResultFrom(MpObject&& obj) NLIB_NOEXCEPT;
60 #endif
61  void SetError(int errcode, const char* msg, MpObject& data) NLIB_NOEXCEPT;
62  void SetError(int errcode, const char* msg) NLIB_NOEXCEPT {
63  MpObject data;
64  this->SetError(errcode, msg, data);
65  }
66 
67  ErrorCode GetError() const NLIB_NOEXCEPT { return static_cast<ErrorCode>(error_code_); }
68  reqid_t GetId() const NLIB_NOEXCEPT { return id_; }
69  const char* GetErrorMessage() const NLIB_NOEXCEPT { return error_msg_ ? error_msg_ : ""; }
70  MpObject& GetMpObject() NLIB_NOEXCEPT { return obj_; }
71  const MpObject& GetMpObject() const NLIB_NOEXCEPT { return obj_; }
72  NLIB_SAFE_BOOL(JsonRpcResponse, !is_error_);
73 
74  private:
75  void SetId(reqid_t id) NLIB_NOEXCEPT { id_ = id; } // from JsonRpcRequest::Init()
76  NLIB_VIS_HIDDEN errno_t Init(MpObject* rhs) NLIB_NOEXCEPT; // from JsonRpcResponseReader
77 
78  private:
79  reqid_t id_;
80  bool is_error_;
81  int error_code_;
82  char* error_msg_;
83  MpObject obj_;
84 
85  friend class JsonRpcResponseReader;
86  friend class JsonRpcRequest;
87  NLIB_DISALLOW_COPY_AND_ASSIGN(JsonRpcResponse);
88 };
89 
91  public:
94  static errno_t ReadResponse(const void* p, size_t n, ListType* result_list) NLIB_NOEXCEPT;
95 
96  private:
98 };
99 
101  public:
103  NLIB_CEXPR JsonRpcResponseWriter() NLIB_NOEXCEPT : prv_(nullptr), is_msgpack_(false) {}
105 #ifdef __cpp_rvalue_references
107  : prv_(rhs.prv_),
108  is_msgpack_(rhs.is_msgpack_) {
109  rhs.prv_ = nullptr;
110  }
112  prv_ = rhs.prv_;
113  is_msgpack_ = rhs.is_msgpack_;
114  rhs.prv_ = nullptr;
115  return *this;
116  }
117 #endif
119  : prv_(rhs.prv_),
120  is_msgpack_(rhs.is_msgpack_) {
121  rhs.prv_ = nullptr;
122  }
123  JsonRpcResponseWriter& assign(JsonRpcResponseWriter& rhs, move_tag) NLIB_NOEXCEPT {
124  prv_ = rhs.prv_;
125  is_msgpack_ = rhs.is_msgpack_;
126  rhs.prv_ = nullptr;
127  return *this;
128  }
129  explicit JsonRpcResponseWriter(bool msgpack) NLIB_NOEXCEPT : prv_(nullptr),
130  is_msgpack_(msgpack) {}
131  errno_t BeginWriteResponse(uint32_t n) NLIB_NOEXCEPT;
132  errno_t WriteResponse(JsonRpcResponse* result) NLIB_NOEXCEPT;
133  errno_t EndWriteResponse(UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
134 #ifdef __cpp_rvalue_references
135  std::tuple<errno_t, UniquePtrType, size_t> EndWriteResponse() NLIB_NOEXCEPT;
136 #endif
137 
138  private:
139  struct JsonRpcResponseWriterPrivate;
140  JsonRpcResponseWriterPrivate* prv_;
141  bool is_msgpack_;
143 };
144 
145 } // namespace jsonrpc
146 } // namespace msgpack
147 NLIB_NAMESPACE_END
148 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::jsonrpc::JsonRpcResponseWriter)
149 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
150 #undef NLIB_VIS_PUBLIC
151 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
152 #endif
153 
154 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONRPC_JSONRPCRESPONSE_H_
JsonRpcResponseWriter(JsonRpcResponseWriter &&rhs) noexcept
ムーブコンストラクタです。
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:183
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:199
JsonRpcResponseWriter(bool msgpack) noexcept
引数にtrueを指定するとmsgpackのバイト列を作成します。
C++11環境(エイリアステンプレートが可能な環境)においてはstd::unique_ptrにエイリアステンプレートされま...
Definition: UniquePtr.h:108
std::unique_ptrに相当するクラスが定義されています。
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:86
JSON-RPCのレスポンスのバイト列を読み込むためのクラスです。
JsonRpcResponseWriter & operator=(JsonRpcResponseWriter &&rhs) noexcept
ムーブ代入演算子です。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
MessagePack又はJSONを読み込むことで作成されるオブジェクトです。
Definition: MpObject.h:96
MpObject & GetMpObject() noexcept
JSON-RPCの戻り値か、エラーの付加情報を返します。
std::vectorに似ていますが、コピーできないオブジェクトを格納可能なクラスが定義されています。 ...
const MpObject & GetMpObject() const noexcept
上記関数のconst修飾付き版です。
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
Definition: Config.h:270
const char * GetErrorMessage() const noexcept
エラーメッセージを返します。
MessagePack, JSON及びCSVを読み込むと作成されるオブジェクトです。
reqid_t GetId() const noexcept
JSON-RPCレスポンスのid を返します。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
ErrorCode
JSON-RPCのレスポンスに含まれる(事前定義の)エラーコードの定義です。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:111
開発環境別の設定が書かれるファイルです。
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:250
JsonRpcResponse() noexcept
デフォルトコンストラクタです。
constexpr JsonRpcResponseWriter() noexcept
デフォルトコンストラクタです。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37