nlib
JsonRpcResponse.h
Go to the documentation of this file.
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_(nullptr) {}
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  private:
99 };
100 
102  public:
103  NLIB_CEXPR JsonRpcResponseWriter() NLIB_NOEXCEPT : prv_(nullptr), is_msgpack_(false) {}
105 #ifdef __cpp_rvalue_references
107  : prv_(rhs.prv_), is_msgpack_(rhs.is_msgpack_) {
108  rhs.prv_ = nullptr;
109  }
111  prv_ = rhs.prv_;
112  is_msgpack_ = rhs.is_msgpack_;
113  rhs.prv_ = nullptr;
114  return *this;
115  }
116 #endif
118  : prv_(rhs.prv_), is_msgpack_(rhs.is_msgpack_) {
119  rhs.prv_ = nullptr;
120  }
122  prv_ = rhs.prv_;
123  is_msgpack_ = rhs.is_msgpack_;
124  rhs.prv_ = nullptr;
125  return *this;
126  }
128  using std::swap;
129  swap(prv_, rhs.prv_);
130  swap(is_msgpack_, rhs.is_msgpack_);
131  }
132  explicit JsonRpcResponseWriter(bool msgpack) NLIB_NOEXCEPT
133  : prv_(nullptr), is_msgpack_(msgpack) {}
134  errno_t BeginWriteResponse(uint32_t n) NLIB_NOEXCEPT;
135  errno_t WriteResponse(JsonRpcResponse* result) NLIB_NOEXCEPT;
136  errno_t EndWriteResponse(ReallocOutputStream::UniquePtrType* ptr, size_t* n) NLIB_NOEXCEPT;
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_
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:179
#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:194
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:109
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:88
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:109
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:89
Object created when MessagePack or JSON is read.
Definition: MpObject.h:95
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.
An empty structure indicating that an argument to a function needs to be moved.
Definition: Config.h:265
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:105
ErrorCode
Definition of the predefined error code contained in the JSON-RPC response.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:107
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:32
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Definition: Nlist.h:32
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:245
JsonRpcResponse() noexcept
Instantiates the object with default parameters (default constructor).
constexpr 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:37