nlib
MpWalker.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_MPWALKER_H_
17 #define INCLUDE_NN_NLIB_MSGPACK_MPWALKER_H_
18 
19 #include <utility>
20 #include "nn/nlib/Config.h"
21 #include "nn/nlib/Swap.h"
22 
23 #ifdef __cpp_rvalue_references
24 #include <tuple>
25 #endif
26 
27 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
28 #undef NLIB_VIS_PUBLIC
29 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
30 #endif
31 
32 NLIB_NAMESPACE_BEGIN
33 namespace msgpack {
34 
35 class MpObject;
37  public:
38  NLIB_CEXPR MpWalker() NLIB_NOEXCEPT : cur_(nullptr), size_(0) {}
39  NLIB_CEXPR MpWalker(const void* p, size_t n) NLIB_NOEXCEPT
40  : cur_(static_cast<const uint8_t*>(p)),
41  size_(n) {}
42  bool Init(const void* p, size_t n) NLIB_NOEXCEPT {
43  if (n > RSIZE_MAX) return false;
44  cur_ = static_cast<const uint8_t*>(p);
45  size_ = n;
46  return true;
47  }
48 
49  MpWalker operator[](size_t array_idx) const NLIB_NOEXCEPT;
50  MpWalker operator[](const nlib_utf8_t* key) const NLIB_NOEXCEPT NLIB_NONNULL;
51  MpWalker operator[](int array_idx) const NLIB_NOEXCEPT {
52  if (array_idx < 0)
53  return MpWalker();
54  else
55  return operator[](static_cast<size_t>(array_idx));
56  }
57  const nlib_byte_t* GetPtr() const NLIB_NOEXCEPT {
58  return reinterpret_cast<const nlib_byte_t*>(cur_);
59  }
60  size_t GetSize() const NLIB_NOEXCEPT { return size_; }
61 
62  MpWalker At(size_t idx) const NLIB_NOEXCEPT;
63  MpWalker At(size_t idx, const nlib_utf8_t** key, size_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
64  MpWalker Find(const nlib_utf8_t* key) const NLIB_NOEXCEPT NLIB_NONNULL;
65  MpWalker Skip() const NLIB_NOEXCEPT;
66  MpWalker GetString(const nlib_utf8_t** str, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
67  MpWalker GetBinary(const void** bin, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
68  MpWalker GetExt(int8_t* tp, const void** bin, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
69  MpWalker GetMapCount(uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
70  MpWalker GetArrayCount(uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
71  MpWalker GetNil() const NLIB_NOEXCEPT;
72  MpWalker GetBoolean(bool* val) const NLIB_NOEXCEPT NLIB_NONNULL;
73  MpWalker GetInt(int32_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
74  MpWalker GetInt(int64_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
75  MpWalker GetUint(uint32_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
76  MpWalker GetUint(uint64_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
77  MpWalker GetFloat(float* val) const NLIB_NOEXCEPT NLIB_NONNULL;
78  MpWalker GetDouble(double* val) const NLIB_NOEXCEPT NLIB_NONNULL;
79  MpWalker GetTimestamp(nlib_time* val) const NLIB_NOEXCEPT NLIB_NONNULL;
80 
81 #ifdef __cpp_rvalue_references
82  std::tuple<MpWalker, const nlib_utf8_t*, uint32_t> GetString() const NLIB_NOEXCEPT {
83  std::tuple<MpWalker, const nlib_utf8_t*, uint32_t> rval;
84  std::get<0>(rval) = GetString(&std::get<1>(rval), &std::get<2>(rval));
85  return rval;
86  }
87  std::tuple<MpWalker, const nlib_byte_t*, uint32_t> GetBinary() const NLIB_NOEXCEPT {
88  std::tuple<MpWalker, const nlib_byte_t*, uint32_t> rval;
89  std::get<0>(rval) =
90  GetBinary(reinterpret_cast<const void**>(&std::get<1>(rval)), &std::get<2>(rval));
91  return rval;
92  }
93  std::tuple<MpWalker, int8_t, const nlib_byte_t*, uint32_t> GetExt() const NLIB_NOEXCEPT {
94  std::tuple<MpWalker, int8_t, const nlib_byte_t*, uint32_t> rval;
95  std::get<0>(rval) = GetExt(&std::get<1>(rval),
96  reinterpret_cast<const void**>(&std::get<2>(rval)),
97  &std::get<3>(rval));
98  return rval;
99  }
100 #endif
101  std::pair<MpWalker, uint32_t> GetMapCount() const NLIB_NOEXCEPT {
102  std::pair<MpWalker, uint32_t> rval;
103  rval.first = GetMapCount(&rval.second);
104  return rval;
105  }
106  std::pair<MpWalker, uint32_t> GetArrayCount() const NLIB_NOEXCEPT {
107  std::pair<MpWalker, uint32_t> rval;
108  rval.first = GetArrayCount(&rval.second);
109  return rval;
110  }
111  std::pair<MpWalker, bool> GetBoolean() const NLIB_NOEXCEPT {
112  std::pair<MpWalker, bool> rval;
113  rval.first = GetBoolean(&rval.second);
114  return rval;
115  }
116  std::pair<MpWalker, int32_t> GetInt32() const NLIB_NOEXCEPT {
117  std::pair<MpWalker, int32_t> rval;
118  rval.first = GetInt(&rval.second);
119  return rval;
120  }
121  std::pair<MpWalker, int64_t> GetInt64() const NLIB_NOEXCEPT {
122  std::pair<MpWalker, int64_t> rval;
123  rval.first = GetInt(&rval.second);
124  return rval;
125  }
126  std::pair<MpWalker, uint32_t> GetUint32() const NLIB_NOEXCEPT {
127  std::pair<MpWalker, uint32_t> rval;
128  rval.first = GetUint(&rval.second);
129  return rval;
130  }
131  std::pair<MpWalker, uint64_t> GetUint64() const NLIB_NOEXCEPT {
132  std::pair<MpWalker, uint64_t> rval;
133  rval.first = GetUint(&rval.second);
134  return rval;
135  }
136  std::pair<MpWalker, float> GetFloat() const NLIB_NOEXCEPT {
137  std::pair<MpWalker, float> rval;
138  rval.first = GetFloat(&rval.second);
139  return rval;
140  }
141  std::pair<MpWalker, double> GetDouble() const NLIB_NOEXCEPT {
142  std::pair<MpWalker, double> rval;
143  rval.first = GetDouble(&rval.second);
144  return rval;
145  }
146  std::pair<MpWalker, nlib_time> GetTimestamp() const NLIB_NOEXCEPT {
147  std::pair<MpWalker, nlib_time> rval;
148  rval.first = GetTimestamp(&rval.second);
149  return rval;
150  }
151  NLIB_SAFE_BOOL(MpWalker, IsOk())
152 
153  private:
154  bool IsOk() const NLIB_NOEXCEPT { return cur_ != nullptr; }
155 
156  const uint8_t* cur_;
157  size_t size_;
158 };
159 
160 } // namespace msgpack
161 NLIB_NAMESPACE_END
162 
163 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
164 #undef NLIB_VIS_PUBLIC
165 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
166 #endif
167 
168 #endif // INCLUDE_NN_NLIB_MSGPACK_MPWALKER_H_
std::tuple< MpWalker, const nlib_byte_t *, uint32_t > GetBinary() const noexcept
文字列が格納されている場合、バイナリ(bin format)のデータを取得します。
Definition: MpWalker.h:87
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:199
std::pair< MpWalker, uint32_t > GetArrayCount() const noexcept
配列が格納されている場合、配列のサイズを取得します。
Definition: MpWalker.h:106
constexpr MpWalker() noexcept
デフォルトコンストラクタです。
Definition: MpWalker.h:38
#define RSIZE_MAX
size_tの最大値よりいくらか小さい値が定義されています。
Definition: Platform.h:219
int64_t nlib_time
1970/01/01を起点(0)としてから100ns刻みで時刻を表現する型です。64bit符号付き整数です。 ...
Definition: Platform.h:452
size_t GetSize() const noexcept
MessagePackデータのサイズを取得します。
Definition: MpWalker.h:60
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
MpWalker operator[](int array_idx) const noexcept
インデックスを指定して配列の要素にアクセスします。
Definition: MpWalker.h:51
const nlib_byte_t * GetPtr() const noexcept
MessagePackデータの現在位置へのポインタを取得します。
Definition: MpWalker.h:57
std::pair< MpWalker, bool > GetBoolean() const noexcept
ブール値が格納されている場合、ブール値を取得します。戻り値のMpWalkerオブジェクトは、成功した場合次の...
Definition: MpWalker.h:111
std::pair< MpWalker, int64_t > GetInt64() const noexcept
int64_t型に格納できる整数値が格納されている場合、整数値を取得します。戻り値のMpWalkerオブジェクトは、...
Definition: MpWalker.h:121
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:111
開発環境別の設定が書かれるファイルです。
std::tuple< MpWalker, const nlib_utf8_t *, uint32_t > GetString() const noexcept
文字列が格納されている場合、文字列(str format)を取得します。
Definition: MpWalker.h:82
std::pair< MpWalker, nlib_time > GetTimestamp() const noexcept
タイムスタンプ拡張型がが格納されている場合、nlib_time型の値として取得します。戻り値のMpWalkerオブジェ...
Definition: MpWalker.h:146
std::pair< MpWalker, uint32_t > GetMapCount() const noexcept
連想配列が格納されている場合、連想配列のサイズを取得します。
Definition: MpWalker.h:101
メモリ上に展開されたMessagePackのデータに高速にアクセスします。
Definition: MpWalker.h:36
bool Init(const void *p, size_t n) noexcept
デフォルトコンストラクタを利用した場合はこの関数で初期化します。
Definition: MpWalker.h:42
constexpr MpWalker(const void *p, size_t n) noexcept
初期化を行うコンストラクタです。
Definition: MpWalker.h:39
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:250
unsigned char nlib_byte_t
C++17以降でstd::byteにtypedefされる型です。
Definition: Platform.h:314
std::tuple< MpWalker, int8_t, const nlib_byte_t *, uint32_t > GetExt() const noexcept
拡張データ型が格納されている場合、拡張データ型(ext format)のデータを取得します。
Definition: MpWalker.h:93
std::pair< MpWalker, uint64_t > GetUint64() const noexcept
uint64_t型に格納できる整数値が格納されている場合、整数値を取得します。戻り値のMpWalkerオブジェクトは...
Definition: MpWalker.h:131
#define NLIB_NONNULL
全ての引数にNULLを指定することができないことを示します。
std::pair< MpWalker, double > GetDouble() const noexcept
double型に格納できる数値が格納されている場合、数値を取得します。戻り値のMpWalkerオブジェクトは、成功...
Definition: MpWalker.h:141
std::pair< MpWalker, uint32_t > GetUint32() const noexcept
uint32_t型に格納できる整数値が格納されている場合、整数値を取得します。戻り値のMpWalkerオブジェクトは...
Definition: MpWalker.h:126
std::pair< MpWalker, float > GetFloat() const noexcept
float型に格納できる数値が格納されている場合、数値を取得します。戻り値のMpWalkerオブジェクトは、成功し...
Definition: MpWalker.h:136
char nlib_utf8_t
charのtypedefです。文字列がUTF-8であることを示します。
Definition: Platform.h:303
std::pair< MpWalker, int32_t > GetInt32() const noexcept
int32_t型に格納できる整数値が格納されている場合、整数値を取得します。戻り値のMpWalkerオブジェクトは、...
Definition: MpWalker.h:116