nlib
MpWalker.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_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
If a string is stored, gets the binary (bin format) data.
Definition: MpWalker.h:87
#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:199
std::pair< MpWalker, uint32_t > GetArrayCount() const noexcept
If an array is stored, gets the array size.
Definition: MpWalker.h:106
constexpr MpWalker() noexcept
Instantiates the object with default parameters (default constructor).
Definition: MpWalker.h:38
#define RSIZE_MAX
Defines a value somewhat smaller than the maximum value of size_t.
Definition: Platform.h:219
int64_t nlib_time
The type expressing the time in increments of 100 ns from the zero starting point of 1970-01-01...
Definition: Platform.h:452
size_t GetSize() const noexcept
Gets the size of the MessagePack data.
Definition: MpWalker.h:60
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
MpWalker operator[](int array_idx) const noexcept
Specifies an index and accesses the element in the array.
Definition: MpWalker.h:51
const nlib_byte_t * GetPtr() const noexcept
Gets a pointer to the current location of MessagePack data.
Definition: MpWalker.h:57
std::pair< MpWalker, bool > GetBoolean() const noexcept
If a boolean value is stored, gets the boolean value. The return value, the MpWalker object...
Definition: MpWalker.h:111
std::pair< MpWalker, int64_t > GetInt64() const noexcept
If an integer that can be stored in the int64_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.
Definition: MpWalker.h:121
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:111
A file that contains the configuration information for each development environment.
std::tuple< MpWalker, const nlib_utf8_t *, uint32_t > GetString() const noexcept
If a string is stored, gets the string (str format).
Definition: MpWalker.h:82
std::pair< MpWalker, nlib_time > GetTimestamp() const noexcept
If the timestamp extended type is stored, gets data as the nlib_time type value. The return value...
Definition: MpWalker.h:146
std::pair< MpWalker, uint32_t > GetMapCount() const noexcept
If an associative array is stored, gets the associative array size.
Definition: MpWalker.h:101
Quickly accesses MessagePack expanded in memory.
Definition: MpWalker.h:36
bool Init(const void *p, size_t n) noexcept
Initialize with this function when using the default constructor.
Definition: MpWalker.h:42
constexpr MpWalker(const void *p, size_t n) noexcept
The constructor to initialize.
Definition: MpWalker.h:39
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
unsigned char nlib_byte_t
This type will be defined as std::byte in a typedef of C++17 or later.
Definition: Platform.h:314
std::tuple< MpWalker, int8_t, const nlib_byte_t *, uint32_t > GetExt() const noexcept
If the extended data type is stored, gets extended data type (ext format) data.
Definition: MpWalker.h:93
std::pair< MpWalker, uint64_t > GetUint64() const noexcept
If an integer that can be stored in the uint64_t type is stored, gets the integer value...
Definition: MpWalker.h:131
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
std::pair< MpWalker, double > GetDouble() const noexcept
If a numerical value that can be stored in the double type is stored, gets the numerical value...
Definition: MpWalker.h:141
std::pair< MpWalker, uint32_t > GetUint32() const noexcept
If an integer that can be stored in the uint32_t type is stored, gets the integer value...
Definition: MpWalker.h:126
std::pair< MpWalker, float > GetFloat() const noexcept
If a numerical value that can be stored in the float type is stored, gets the numerical value...
Definition: MpWalker.h:136
char nlib_utf8_t
Defines char with a typedef. Indicates that it is a UTF-8 string.
Definition: Platform.h:303
std::pair< MpWalker, int32_t > GetInt32() const noexcept
If an integer that can be stored in the int32_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.
Definition: MpWalker.h:116