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 NLIB_CXX11_STDLIB_TUPLE
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)), size_(n) {}
41  bool Init(const void* p, size_t n) NLIB_NOEXCEPT {
42  if (n > RSIZE_MAX) return false;
43  cur_ = static_cast<const uint8_t*>(p);
44  size_ = n;
45  return true;
46  }
47 
48  MpWalker operator[](size_t array_idx) const NLIB_NOEXCEPT;
49  MpWalker operator[](const nlib_utf8_t* key) const NLIB_NOEXCEPT NLIB_NONNULL;
50  MpWalker operator[](int array_idx) const NLIB_NOEXCEPT {
51  if (array_idx < 0)
52  return MpWalker();
53  else
54  return operator[](static_cast<size_t>(array_idx));
55  }
56  const nlib_byte_t* GetPtr() const NLIB_NOEXCEPT {
57  return reinterpret_cast<const nlib_byte_t*>(cur_);
58  }
59  size_t GetSize() const NLIB_NOEXCEPT { return size_; }
60 
61  MpWalker At(size_t idx) const NLIB_NOEXCEPT;
62  MpWalker At(size_t idx, const nlib_utf8_t** key, size_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
63  MpWalker Find(const nlib_utf8_t* key) const NLIB_NOEXCEPT NLIB_NONNULL;
64  MpWalker Skip() const NLIB_NOEXCEPT;
65  MpWalker GetString(const nlib_utf8_t** str, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
66  MpWalker GetBinary(const void** bin, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
67  MpWalker GetExt(int8_t* tp, const void** bin, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
68  MpWalker GetMapCount(uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
69  MpWalker GetArrayCount(uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
70  MpWalker GetNil() const NLIB_NOEXCEPT;
71  MpWalker GetBoolean(bool* val) const NLIB_NOEXCEPT NLIB_NONNULL;
72  MpWalker GetInt(int32_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
73  MpWalker GetInt(int64_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
74  MpWalker GetUint(uint32_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
75  MpWalker GetUint(uint64_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
76  MpWalker GetFloat(float* val) const NLIB_NOEXCEPT NLIB_NONNULL;
77  MpWalker GetDouble(double* val) const NLIB_NOEXCEPT NLIB_NONNULL;
78  MpWalker GetTimestamp(nlib_time* val) const NLIB_NOEXCEPT NLIB_NONNULL;
79 
80 #ifdef NLIB_CXX11_STDLIB_TUPLE
81  std::tuple<MpWalker, const nlib_utf8_t*, uint32_t>
82  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>
88  GetBinary() const NLIB_NOEXCEPT {
89  std::tuple<MpWalker, const nlib_byte_t*, uint32_t> rval;
90  std::get<0>(rval) = GetBinary(reinterpret_cast<const void**>(&std::get<1>(rval)),
91  &std::get<2>(rval));
92  return rval;
93  }
94  std::tuple<MpWalker, int8_t, const nlib_byte_t*, uint32_t>
95  GetExt() const NLIB_NOEXCEPT {
96  std::tuple<MpWalker, int8_t, const nlib_byte_t*, uint32_t> rval;
97  std::get<0>(rval) = GetExt(&std::get<1>(rval),
98  reinterpret_cast<const void**>(&std::get<2>(rval)),
99  &std::get<3>(rval));
100  return rval;
101  }
102 #endif
103  std::pair<MpWalker, uint32_t>
104  GetMapCount() const NLIB_NOEXCEPT {
105  std::pair<MpWalker, uint32_t> rval;
106  rval.first = GetMapCount(&rval.second);
107  return rval;
108  }
109  std::pair<MpWalker, uint32_t>
110  GetArrayCount() const NLIB_NOEXCEPT {
111  std::pair<MpWalker, uint32_t> rval;
112  rval.first = GetArrayCount(&rval.second);
113  return rval;
114  }
115  std::pair<MpWalker, bool>
116  GetBoolean() const NLIB_NOEXCEPT {
117  std::pair<MpWalker, bool> rval;
118  rval.first = GetBoolean(&rval.second);
119  return rval;
120  }
121  std::pair<MpWalker, int32_t>
122  GetInt32() const NLIB_NOEXCEPT {
123  std::pair<MpWalker, int32_t> rval;
124  rval.first = GetInt(&rval.second);
125  return rval;
126  }
127  std::pair<MpWalker, int64_t>
128  GetInt64() const NLIB_NOEXCEPT {
129  std::pair<MpWalker, int64_t> rval;
130  rval.first = GetInt(&rval.second);
131  return rval;
132  }
133  std::pair<MpWalker, uint32_t>
134  GetUint32() const NLIB_NOEXCEPT {
135  std::pair<MpWalker, uint32_t> rval;
136  rval.first = GetUint(&rval.second);
137  return rval;
138  }
139  std::pair<MpWalker, uint64_t>
140  GetUint64() const NLIB_NOEXCEPT {
141  std::pair<MpWalker, uint64_t> rval;
142  rval.first = GetUint(&rval.second);
143  return rval;
144  }
145  std::pair<MpWalker, float>
146  GetFloat() const NLIB_NOEXCEPT {
147  std::pair<MpWalker, float> rval;
148  rval.first = GetFloat(&rval.second);
149  return rval;
150  }
151  std::pair<MpWalker, double>
152  GetDouble() const NLIB_NOEXCEPT {
153  std::pair<MpWalker, double> rval;
154  rval.first = GetDouble(&rval.second);
155  return rval;
156  }
157  std::pair<MpWalker, nlib_time>
158  GetTimestamp() const NLIB_NOEXCEPT {
159  std::pair<MpWalker, nlib_time> rval;
160  rval.first = GetTimestamp(&rval.second);
161  return rval;
162  }
163  NLIB_SAFE_BOOL(MpWalker, IsOk())
164 
165  private:
166  bool IsOk() const NLIB_NOEXCEPT { return cur_ != nullptr; }
167 
168  const uint8_t* cur_;
169  size_t size_;
170 };
171 
172 } // namespace msgpack
173 NLIB_NAMESPACE_END
174 
175 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
176 #undef NLIB_VIS_PUBLIC
177 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
178 #endif
179 
180 #endif // INCLUDE_NN_NLIB_MSGPACK_MPWALKER_H_
#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
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:224
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:457
size_t GetSize() const noexcept
Gets the size of the MessagePack data.
Definition: MpWalker.h:59
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
MpWalker operator[](int array_idx) const noexcept
The same as operator[](size_t array_idx).
Definition: MpWalker.h:50
const nlib_byte_t * GetPtr() const noexcept
Gets a pointer to the current location of MessagePack data.
Definition: MpWalker.h:56
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
#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.
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:41
constexpr MpWalker(const void *p, size_t n) noexcept
Instantiates the object.
Definition: MpWalker.h:39
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
unsigned char nlib_byte_t
This type will be defined as std::byte in a typedef of C++17 or later.
Definition: Platform.h:319
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
char nlib_utf8_t
Defines char with a typedef. Indicates that it is a UTF-8 string.
Definition: Platform.h:308