nlib
MpObject.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_MPOBJECT_H_
17 #define INCLUDE_NN_NLIB_MSGPACK_MPOBJECT_H_
18 
19 #include <map>
20 #include <utility>
21 #include <vector>
22 
23 #include "nn/nlib/Config.h"
24 #include "nn/nlib/Swap.h"
25 #include "nn/nlib/Cstring.h"
26 #include "nn/nlib/Nlist.h"
27 
28 #ifdef NLIB_CXX11_STDLIB_ARRAY
29 #include <array> // NOLINT
30 #endif
31 
32 #ifdef NLIB_CXX11_STDLIB_UNORDERED
33 #include <unordered_map> // NOLINT
34 #endif
35 
36 #ifdef NLIB_CXX11_STDLIB_TUPLE
37 #include <tuple> // NOLINT
38 #endif
39 
40 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
41 #undef NLIB_VIS_PUBLIC
42 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
43 #endif
44 
45 NLIB_NAMESPACE_BEGIN
46 namespace msgpack {
47 
48 class MpObject;
49 
50 template <class T>
51 errno_t Box(MpObject* obj, const T& v);
52 
53 template <class T>
54 errno_t Unbox(const MpObject* obj, T* v);
55 
56 struct nil {};
57 
58 template <class T>
59 inline bool operator==(const nil& lhs, const T& rhs) NLIB_NOEXCEPT {
60  NLIB_UNUSED(lhs);
61  NLIB_UNUSED(rhs);
62  return false;
63 }
64 template <class T>
65 inline bool operator==(const T& lhs, const nil& rhs) NLIB_NOEXCEPT {
66  NLIB_UNUSED(lhs);
67  NLIB_UNUSED(rhs);
68  return false;
69 }
70 inline bool operator==(const nil& lhs, const nil& rhs) NLIB_NOEXCEPT {
71  NLIB_UNUSED(lhs);
72  NLIB_UNUSED(rhs);
73  return true;
74 }
75 template <class T>
76 inline bool operator!=(const nil& rhs, const T& lhs) NLIB_NOEXCEPT {
77  NLIB_UNUSED(lhs);
78  NLIB_UNUSED(rhs);
79  return true;
80 }
81 template <class T>
82 inline bool operator!=(const T& rhs, const nil& lhs) NLIB_NOEXCEPT {
83  NLIB_UNUSED(lhs);
84  NLIB_UNUSED(rhs);
85  return true;
86 }
87 inline bool operator!=(const nil& rhs, const nil& lhs) NLIB_NOEXCEPT {
88  NLIB_UNUSED(lhs);
89  NLIB_UNUSED(rhs);
90  return false;
91 }
92 
93 struct MpObjectKv;
94 
96  private:
97  union UnionType {
98  uint64_t u64;
99  int64_t i64;
100  float f32;
101  double f64;
102  Nlist<MpObject>* ary;
103  Nlist<MpObjectKv>* mp;
104  nlib_utf8_t* str;
105  void* binary;
106  };
107 
108  public:
109  static bool IsJsonPointer(const nlib_utf8_t* first, const nlib_utf8_t* last) NLIB_NOEXCEPT;
110  static bool IsJsonPointer(const nlib_utf8_t* str) NLIB_NOEXCEPT {
111  return IsJsonPointer(str, str + nlib_strlen(str));
112  }
113  static errno_t ResolveJsonPointer(MpObject** result, MpObject* root,
114  const nlib_utf8_t* json_pointer) NLIB_NOEXCEPT;
115  static errno_t DigByJsonPointer(MpObject** result, MpObject* root,
116  const nlib_utf8_t* json_pointer) NLIB_NOEXCEPT;
117  static errno_t RemoveByJsonPointer(MpObjectKv* removed, MpObject* root,
118  const nlib_utf8_t* json_pointer) NLIB_NOEXCEPT;
119  MpObject* GetArrayItem(size_t n) NLIB_NOEXCEPT {
120  MpObject* obj;
121  return NLIB_LIKELY(0 == this->GetArrayItem(n, &obj)) ? obj : nullptr;
122  }
123  const MpObject* GetArrayItem(size_t n) const NLIB_NOEXCEPT {
124  const MpObject* obj;
125  return NLIB_LIKELY(0 == this->GetArrayItem(n, &obj)) ? obj : nullptr;
126  }
127  MpObject& operator[](size_t n) {
128  NLIB_ASSERT(this->IsArray());
129  NLIB_ASSERT(n < size_);
130  return (*via_.ary)[n];
131  }
132  const MpObject* GetMapItem(const nlib_utf8_t* str) const NLIB_NOEXCEPT;
133  MpObject* GetMapItem(const nlib_utf8_t* str) NLIB_NOEXCEPT;
134  template<size_t N>
135  MpObject* GetMapItem(const nlib_utf8_t (&str)[N]) NLIB_NOEXCEPT {
136  return GetMapItem(&str[0]);
137  }
138  template<size_t N>
139  const MpObject* GetMapItem(const nlib_utf8_t (&str)[N]) const NLIB_NOEXCEPT {
140  return GetMapItem(&str[0]);
141  }
142  template<class STDSTRING>
143  const MpObject* GetMapItem(const STDSTRING& str) const NLIB_NOEXCEPT {
144  return GetMapItem(&str[0], str.size());
145  }
146  template<class STDSTRING>
147  MpObject* GetMapItem(const STDSTRING& str) NLIB_NOEXCEPT {
148  return GetMapItem(&str[0], str.size());
149  }
150  errno_t Resize(uint32_t n);
151  NLIB_DEPRECATED MpObject* SwapArrayItem(size_t n, MpObject* obj) NLIB_NOEXCEPT;
152  template<class STDSTRING>
153  NLIB_DEPRECATED MpObject* SwapMapItem(const STDSTRING& key, MpObject* obj) NLIB_NOEXCEPT;
154  template<size_t N>
155  NLIB_DEPRECATED MpObject* SwapMapItem(const nlib_utf8_t(&key)[N],
156  MpObject* obj) NLIB_NOEXCEPT;
157  NLIB_DEPRECATED MpObject* SwapMapItem(const nlib_utf8_t* key, MpObject* obj) NLIB_NOEXCEPT;
158  MpObject* AppendArrayItem() NLIB_NOEXCEPT;
159  MpObject* InsertArrayItem(size_t n) NLIB_NOEXCEPT;
160  MpObjectKv* AppendMapItem() NLIB_NOEXCEPT;
161  NLIB_OVERRIDE_NEW;
162 
163  public:
164  MpObject() NLIB_NOEXCEPT : type_(kNil) {} // only do minimum memory access
165  ~MpObject() NLIB_NOEXCEPT { ReleaseIfNeeded(); }
166 #ifdef __cpp_rvalue_references
167  MpObject(MpObject&& rhs) NLIB_NOEXCEPT {
168  nlib_memcpy(this, sizeof(*this), &rhs, sizeof(rhs));
169  rhs.type_ = kNil;
170  }
171  MpObject& operator=(MpObject&& rhs) NLIB_NOEXCEPT {
172  nlib_memcpy(this, sizeof(*this), &rhs, sizeof(rhs));
173  rhs.type_ = kNil;
174  return *this;
175  }
176 #endif
177  MpObject(MpObject& rhs, move_tag) NLIB_NOEXCEPT { // NOLINT
178  nlib_memcpy(this, sizeof(*this), &rhs, sizeof(rhs));
179  rhs.type_ = kNil;
180  }
181  MpObject& assign(MpObject& rhs, move_tag) NLIB_NOEXCEPT { // NOLINT
182  nlib_memcpy(this, sizeof(*this), &rhs, sizeof(rhs));
183  rhs.type_ = kNil;
184  return *this;
185  }
186  NLIB_DEPRECATED void swap(MpObject& rhs) NLIB_NOEXCEPT { // NOLINT
187  using std::swap;
188  swap(type_, rhs.type_);
189  swap(raw14size_, rhs.raw14size_);
190  swap(raw14data_, rhs.raw14data_);
191  swap(_, rhs._);
192  swap(size_, rhs.size_);
193  swap(via_.u64, rhs.via_.u64);
194  }
195 
196  // NOTE: implicit call was error in gcc or clang....
197  template <class T>
198  explicit MpObject(const T& x) {
199  MyBox<T, typename IsIntegral<T>::type, typename IsSigned<T>::type>::Construct(this, x);
200  }
201  errno_t GetArrayItem(size_t n, MpObject** obj) NLIB_NOEXCEPT NLIB_NONNULL;
202  errno_t GetArrayItem(size_t n, const MpObject** obj) const NLIB_NOEXCEPT NLIB_NONNULL;
203  errno_t GetMapItem(size_t n, MpObjectKv** obj) NLIB_NOEXCEPT NLIB_NONNULL;
204  errno_t GetMapItem(size_t n, const MpObjectKv** obj) const NLIB_NOEXCEPT NLIB_NONNULL;
205  errno_t GetMapItem(const nlib_utf8_t* str, size_t n,
206  MpObjectKv** obj) NLIB_NOEXCEPT NLIB_NONNULL {
207  const MpObjectKv* kv;
208  errno_t e = this->GetMapItem(str, n, &kv);
209  if (e != 0) return e;
210  *obj = const_cast<MpObjectKv*>(kv);
211  return 0;
212  }
213  errno_t GetMapItem(const nlib_utf8_t* str, size_t n,
214  const MpObjectKv** obj) const NLIB_NOEXCEPT NLIB_NONNULL;
215  nlib_utf8_t* GetString() NLIB_NOEXCEPT {
216  if (type_ == kString) return via_.str;
217  if (NLIB_LIKELY(type_ == kStringInline))
218  return reinterpret_cast<nlib_utf8_t*>(&raw14data_);
219  return nullptr;
220  }
221  errno_t RemoveArrayItem(size_t n, MpObject* obj) NLIB_NOEXCEPT;
222  errno_t RemoveMapItem(size_t n, MpObjectKv* kv) NLIB_NOEXCEPT;
223  errno_t RemoveMapItem(const nlib_utf8_t* key, MpObjectKv* kv) NLIB_NOEXCEPT;
224  template<class STDSTRING>
225  errno_t RemoveMapItem(const STDSTRING& str, MpObjectKv* kv) NLIB_NOEXCEPT {
226  return RemoveMapItem(str.c_str(), kv);
227  }
228 
229  const nlib_utf8_t* GetString() const NLIB_NOEXCEPT {
230  return const_cast<MpObject*>(this)->GetString();
231  }
232  void* GetBinary(uint32_t* n) NLIB_NOEXCEPT {
233  if (type_ == kBinary) {
234  *n = size_;
235  return via_.binary;
236  }
237  if (NLIB_LIKELY(type_ == kBinaryInline)) {
238  *n = raw14size_;
239  return &raw14data_;
240  }
241  return nullptr;
242  }
243  const void* GetBinary(uint32_t* n) const NLIB_NOEXCEPT {
244  return const_cast<MpObject*>(this)->GetBinary(n);
245  }
246  void* GetExt(int8_t* tp, uint32_t* n) NLIB_NOEXCEPT {
247  if (type_ == kExt) {
248  *n = size_;
249  *tp = static_cast<int8_t>(raw14data_);
250  return via_.binary;
251  } else {
252  return nullptr;
253  }
254  }
255  const void* GetExt(int8_t* tp, uint32_t* n) const NLIB_NOEXCEPT {
256  return const_cast<MpObject*>(this)->GetExt(tp, n);
257  }
258 
259  // I/F which returns std::pair, std::tuple
260 #ifdef NLIB_CXX11_STDLIB_TUPLE
261  std::tuple<errno_t, const nlib_byte_t*, uint32_t>
262  GetBinary() const NLIB_NOEXCEPT {
263  if (type_ == kBinary) {
264  return std::make_tuple(0, static_cast<const nlib_byte_t*>(via_.binary), size_);
265  } else if (NLIB_LIKELY(type_ == kBinaryInline)) {
266  return std::make_tuple(
267  0, reinterpret_cast<const nlib_byte_t*>(&raw14data_), raw14size_);
268  }
269  return std::make_tuple(EACCES, nullptr, 0);
270  }
271  std::tuple<errno_t, int8_t, const nlib_byte_t*, uint32_t>
272  GetExt() const NLIB_NOEXCEPT {
273  if (type_ != kExt)
274  return std::make_tuple(EACCES, static_cast<int8_t>(0), nullptr, 0);
275  return std::make_tuple(0, static_cast<int8_t>(raw14data_),
276  static_cast<const nlib_byte_t*>(via_.binary), size_);
277  }
278  // std::tuple<errno_t, p, n> GetStringEx()
279 #endif
280 #ifdef __cpp_rvalue_references
281  std::pair<errno_t, MpObject>
282  RemoveArrayItem(size_t n) NLIB_NOEXCEPT {
283  std::pair<errno_t, MpObject> rval;
284  rval.first = RemoveArrayItem(n, &rval.second);
285  return rval;
286  }
287  std::pair<errno_t, MpObjectKv>
288  RemoveMapItem(size_t n) NLIB_NOEXCEPT;
289 #endif
290  std::pair<errno_t, MpObjectKv*>
291  GetMapItem(const nlib_utf8_t* str, size_t n) NLIB_NOEXCEPT {
292  MpObjectKv* kv;
293  return std::make_pair(GetMapItem(str, n, &kv), kv);
294  }
295  std::pair<errno_t, const MpObjectKv*>
296  GetMapItem(const nlib_utf8_t* str, size_t n) const NLIB_NOEXCEPT {
297  const MpObjectKv* kv;
298  return std::make_pair(GetMapItem(str, n, &kv), kv);
299  }
300  // std::pair<errno_t, int32_t> GetInt32, GetInt64, GetUint32, GetUint64,
301  // GetBoolean(), GetFloat(), GetDouble()
302  std::pair<errno_t, nlib_time> GetTimestamp() const NLIB_NOEXCEPT;
303 
304  errno_t InitArray(uint32_t n) NLIB_NOEXCEPT;
305  errno_t InitMap(uint32_t n) NLIB_NOEXCEPT;
306  errno_t InitString(uint32_t n) NLIB_NOEXCEPT;
307  errno_t InitString(const nlib_utf8_t* str, uint32_t n) NLIB_NOEXCEPT;
308  errno_t InitString(const nlib_utf8_t* str) NLIB_NOEXCEPT {
309  uint32_t n = static_cast<uint32_t>(nlib_strlen(str));
310  return InitString(str, n);
311  }
312  errno_t InitBinary(uint32_t n) NLIB_NOEXCEPT;
313  errno_t InitBinary(const void* p, uint32_t n) NLIB_NOEXCEPT;
314  errno_t InitExt(int8_t tp, uint32_t n) NLIB_NOEXCEPT;
315  errno_t InitExt(int8_t tp, const void* p, uint32_t n) NLIB_NOEXCEPT;
316  errno_t InitTimestamp(nlib_time t) NLIB_NOEXCEPT;
317 
318  public: // Box
319  errno_t Box(const nlib_utf8_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
320  template <uint32_t n>
321  errno_t Box(const nlib_utf8_t (&str)[n]) NLIB_NOEXCEPT;
322  template <class T, uint32_t n>
323  errno_t Box(const T (&vec)[n]);
324 #ifdef NLIB_CXX11_STDLIB_ARRAY
325  template <class T, size_t n>
326  errno_t Box(const std::array<T, n>& vec);
327 #endif
328  template <class T>
329  errno_t Box(const T& v) {
330  return MyBox<T, typename IsIntegral<T>::type, typename IsSigned<T>::type>::Box(this, v);
331  }
332  // Use BoxBinary(p, n) or Box(reinterpret_cast<const nlib_utf8_t*>(p)) instead
333  errno_t BoxBinary(const void* p, uint32_t n) NLIB_NOEXCEPT NLIB_NONNULL;
334  errno_t BoxExt(int8_t tp, const void* p, uint32_t n) NLIB_NOEXCEPT NLIB_NONNULL;
335 
336  public: // Unbox
337  template <class T, size_t n>
338  errno_t Unbox(T (&a)[n]) const {
339  return this->Unbox(&a[0], n);
340  }
341 #ifdef NLIB_CXX11_STDLIB_ARRAY
342  template <class T, size_t n>
343  errno_t Unbox(std::array<T, n>& a) { // NOLINT
344  return this->Unbox(reinterpret_cast<T*>(&*a.begin()), n);
345  }
346 #endif
347  template <size_t n>
348  errno_t Unbox(nlib_utf8_t (&str)[n]) const NLIB_NOEXCEPT {
349  return this->Unbox(&str[0], n);
350  }
351  template <class T>
352  errno_t Unbox(T* a, size_t n) const NLIB_NONNULL;
353  errno_t Unbox(nlib_utf8_t* str, size_t n) const NLIB_NOEXCEPT NLIB_NONNULL;
354  errno_t UnboxBinary(void* p, size_t n) const NLIB_NOEXCEPT NLIB_NONNULL;
355  errno_t UnboxExt(int8_t* tp, void* p, size_t n) const NLIB_NOEXCEPT NLIB_NONNULL;
356  template <class T>
357  errno_t Unbox(T v) const {
358  // T cannot be T* though v is only pointer type
359  // because of conflict against array types.
360  return MyBox<typename RemovePtr<T>::type,
361  typename IsIntegral<typename RemovePtr<T>::type>::type,
362  typename IsSigned<typename RemovePtr<T>::type>::type>::Unbox(this, v);
363  }
364 
365  public:
366  // You can use operator=() only for basic types(never fails).
367  // Use Box() otherwise.
368  template <class T>
369  MpObject& operator=(const T& x) {
370  MyBox<T, typename IsIntegral<T>::type, typename IsSigned<T>::type>::Assign(this, x);
371  return *this;
372  }
373 
374  MpObject* Clone() const NLIB_NOEXCEPT;
375 
376  public:
377  enum ObjectType {
378  kNil = 0,
387  kBinary,
388  kExt,
389  kStringInline = 0x80 | kString,
390  kBinaryInline = 0x80 | kBinary,
391  kBooleanFalse = 0x00 | kBoolean,
392  kBooleanTrue = 0x80 | kBoolean,
393  NIL = kNil,
394  BOOLEAN = kBoolean,
395  UINT64 = kUint64,
396  INT64 = kInt64,
397  FLOAT = kFloat,
398  DOUBLE = kDouble,
399  STRING = kString,
400  ARRAY = kArray,
401  MAP = kMap,
402  BINARY = kBinary,
403  EXT = kExt,
404  RAW = STRING,
405  STRING_INLINE = kStringInline,
406  BINARY_INLINE = kBinaryInline,
407  RAW_INLINE = 0x80 | RAW,
408  BOOLEAN_FALSE = kBooleanFalse,
409  BOOLEAN_TRUE = kBooleanTrue
410  };
411  ObjectType GetType() const NLIB_NOEXCEPT {
412  return static_cast<ObjectType>(type_ & 0x7F);
413  }
414  uint32_t GetSize() const NLIB_NOEXCEPT {
415  if (type_ == kStringInline || type_ == kBinaryInline)
416  return raw14size_;
417  else
418  return size_;
419  }
420  bool IsNil() const NLIB_NOEXCEPT { return GetType() == kNil; }
421  bool IsBoolean() const NLIB_NOEXCEPT { return GetType() == kBoolean; }
422  bool IsInteger() const NLIB_NOEXCEPT {
423  ObjectType tp = GetType();
424  return tp == kUint64 || tp == kInt64;
425  }
426  bool IsFloat() const NLIB_NOEXCEPT { return GetType() == kFloat; }
427  bool IsDouble() const NLIB_NOEXCEPT { return GetType() == kDouble; }
428  bool IsString() const NLIB_NOEXCEPT { return GetType() == kString; }
429  bool IsBinary() const NLIB_NOEXCEPT { return GetType() == kBinary; }
430  bool IsExt() const NLIB_NOEXCEPT { return GetType() == kExt; }
431  bool IsArray() const NLIB_NOEXCEPT { return GetType() == kArray; }
432  bool IsMap() const NLIB_NOEXCEPT { return GetType() == kMap; }
433 
434  private:
435  errno_t Clone(MpObject* p) const NLIB_NOEXCEPT;
436  template <class T, class IS_INTEGRAL, class IS_SIGNED>
437  struct MyBox {
438  static errno_t Box(MpObject* This, const T& v) { return ::nlib_ns::msgpack::Box(This, v); }
439  static errno_t Unbox(const MpObject* This, T* v) {
440  if (!v) return EINVAL;
442  }
443  static void Assign(MpObject* This, const T& x);
444  static void Construct(MpObject* This, const T& x);
445  };
446  void ReleaseIfNeeded() NLIB_NOEXCEPT {
447  if (type_ >= kString && type_ <= kExt) ReleaseIfNeeded_();
448  }
449  void ReleaseIfNeeded_() NLIB_NOEXCEPT;
450 
451  private:
452  uint8_t type_;
453  uint8_t raw14size_;
454  uint8_t raw14data_;
455  uint8_t _;
456  uint32_t size_;
457  UnionType via_;
459  friend NLIB_VIS_PUBLIC bool operator==(const MpObject& lhs, const MpObject& rhs);
460 };
461 
465 
466  public:
467  MpObjectKv() NLIB_NOEXCEPT {}
468  ~MpObjectKv() NLIB_NOEXCEPT {}
469 #ifdef __cpp_rvalue_references
470 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS
471  MpObjectKv(MpObjectKv&& rhs) = default;
472  MpObjectKv& operator=(MpObjectKv&& rhs) = default;
473 #else
475  : first(std::move(rhs.first)), second(std::move(rhs.second)) {}
476  MpObjectKv& operator=(MpObjectKv&& rhs) NLIB_NOEXCEPT {
477  first = std::move(rhs.first);
478  second = std::move(rhs.second);
479  return *this;
480  }
481 #endif
482 #endif
483  MpObjectKv(MpObjectKv& rhs, move_tag) NLIB_NOEXCEPT // NOLINT
484  : first(rhs.first, move_tag()), second(rhs.second, move_tag()) {}
485  MpObjectKv& assign(MpObjectKv& rhs, move_tag) NLIB_NOEXCEPT { // NOLINT
486  first.assign(rhs.first, move_tag());
487  second.assign(rhs.second, move_tag());
488  return *this;
489  }
490  NLIB_DEPRECATED void swap(MpObjectKv& rhs) NLIB_NOEXCEPT; // NOLINT
491 
492  private:
494 };
495 
496 #ifdef __cpp_rvalue_references
497 inline std::pair<errno_t, MpObjectKv>
498 MpObject::RemoveMapItem(size_t n) NLIB_NOEXCEPT {
499  std::pair<errno_t, MpObjectKv> rval;
500  rval.first = RemoveMapItem(n, &rval.second);
501  return rval;
502 }
503 #endif
504 
505 template <>
506 struct MpObject::MyBox<nil, FalseType, FalseType> {
507  static errno_t Box(MpObject* This, nil v) NLIB_NOEXCEPT {
508  NLIB_UNUSED(v);
509  This->ReleaseIfNeeded();
510  This->type_ = kNil;
511  return 0;
512  }
513  static void Assign(MpObject* This, nil x) NLIB_NOEXCEPT { This->Box(x); }
514  static void Construct(MpObject* This, nil) NLIB_NOEXCEPT { This->type_ = kNil; }
515 };
516 
517 template <>
518 struct MpObject::MyBox<bool, TrueType, FalseType> {
519  static errno_t Box(MpObject* This, bool v) NLIB_NOEXCEPT {
520  This->ReleaseIfNeeded();
521  This->type_ = v ? static_cast<uint8_t>(kBooleanTrue) : static_cast<uint8_t>(kBooleanFalse);
522  return 0;
523  }
524  static errno_t Unbox(const MpObject* This, bool* v) NLIB_NOEXCEPT {
525  if (This->type_ == kBooleanFalse) {
526  *v = false;
527  return 0;
528  } else if (NLIB_LIKELY(This->type_ == kBooleanTrue)) {
529  *v = true;
530  return 0;
531  }
532  *v = false; // to suppress warnings
533  return EACCES;
534  }
535  static void Assign(MpObject* This, bool x) NLIB_NOEXCEPT { This->Box(x); }
536  static void Construct(MpObject* This, bool x) NLIB_NOEXCEPT {
537  This->type_ = x ? static_cast<uint8_t>(kBooleanTrue) : static_cast<uint8_t>(kBooleanFalse);
538  }
539 };
540 
541 template <class T>
542 struct MpObject::MyBox<T, TrueType, TrueType> {
543  static errno_t Box(MpObject* This, T v) NLIB_NOEXCEPT {
544  This->ReleaseIfNeeded();
545  This->type_ = kInt64;
546  This->via_.i64 = v;
547  return 0;
548  }
549  static errno_t Unbox(const MpObject* This, T* v) NLIB_NOEXCEPT {
550  if (NLIB_UNLIKELY(!v)) return EINVAL;
551  if (This->type_ == kInt64) {
552  *v = static_cast<T>(This->via_.i64);
553  return 0;
554  } else if (NLIB_LIKELY(This->type_ == kUint64)) {
555  *v = static_cast<T>(This->via_.u64);
556  return 0;
557  }
558  *v = 0; // to suppress warnings
559  return EACCES;
560  }
561  static void Assign(MpObject* This, T x) NLIB_NOEXCEPT { This->Box(x); }
562  static void Construct(MpObject* This, T x) NLIB_NOEXCEPT {
563  This->type_ = kInt64;
564  This->via_.i64 = x;
565  }
566 };
567 
568 template <class T>
569 struct MpObject::MyBox<T, TrueType, FalseType> {
570  static errno_t Box(MpObject* This, T v) NLIB_NOEXCEPT {
571  This->ReleaseIfNeeded();
572  This->type_ = kUint64;
573  This->via_.u64 = v;
574  return 0;
575  }
576  static errno_t Unbox(const MpObject* This, T* v) NLIB_NOEXCEPT {
577  if (NLIB_UNLIKELY(!v)) return EINVAL;
578  if (This->type_ == kInt64) {
579  *v = static_cast<T>(This->via_.i64);
580  return 0;
581  } else if (NLIB_LIKELY(This->type_ == kUint64)) {
582  *v = static_cast<T>(This->via_.u64);
583  return 0;
584  }
585  *v = 0; // to suppress warnings
586  return EACCES;
587  }
588  static void Assign(MpObject* This, T x) NLIB_NOEXCEPT { This->Box(x); }
589  static void Construct(MpObject* This, T x) NLIB_NOEXCEPT {
590  This->type_ = kUint64;
591  This->via_.u64 = x;
592  }
593 };
594 
595 template <class TR1_WORKAROUND>
596 struct MpObject::MyBox<float, FalseType, TR1_WORKAROUND> {
597  static errno_t Box(MpObject* This, float v) NLIB_NOEXCEPT {
598  This->ReleaseIfNeeded();
599  This->type_ = kFloat;
600  This->via_.f32 = v;
601  return 0;
602  }
603  static errno_t Unbox(const MpObject* This, float* v) NLIB_NOEXCEPT {
604  if (NLIB_UNLIKELY(!v)) return EINVAL;
605  switch (This->type_) {
606  case kFloat:
607  *v = This->via_.f32;
608  break;
609  case kDouble:
610  *v = static_cast<float>(This->via_.f64);
611  break;
612  default:
613  *v = 0; // to suppress warnings
614  return EACCES;
615  }
616  return 0;
617  }
618  static void Assign(MpObject* This, float x) NLIB_NOEXCEPT { This->Box(x); }
619  static void Construct(MpObject* This, float x) NLIB_NOEXCEPT {
620  This->type_ = kFloat;
621  This->via_.f32 = x;
622  }
623 };
624 
625 template <class TR1_WORKAROUND>
626 struct MpObject::MyBox<double, FalseType, TR1_WORKAROUND> {
627  static errno_t Box(MpObject* This, double v) NLIB_NOEXCEPT {
628  This->ReleaseIfNeeded();
629  This->type_ = kDouble;
630  This->via_.f64 = v;
631  return 0;
632  }
633  static errno_t Unbox(const MpObject* This, double* v) NLIB_NOEXCEPT {
634  if (NLIB_UNLIKELY(!v)) return EINVAL;
635  switch (This->type_) {
636  case kFloat:
637  *v = This->via_.f32;
638  break;
639  case kDouble:
640  *v = This->via_.f64;
641  break;
642  default:
643  *v = 0; // to suppress warnings
644  return EACCES;
645  }
646  return 0;
647  }
648  static void Assign(MpObject* This, double x) NLIB_NOEXCEPT { This->Box(x); }
649  static void Construct(MpObject* This, double x) NLIB_NOEXCEPT {
650  This->type_ = kDouble;
651  This->via_.f64 = x;
652  }
653 };
654 
655 template <class STDSTRING>
656 struct MpObject::MyBox<STDSTRING, FalseType, FalseType> {
657  static errno_t Box(MpObject* This, const STDSTRING& str) NLIB_NOEXCEPT {
658  uint32_t n = static_cast<uint32_t>(str.size());
659  ErrnoT e = This->InitString(n);
660  if (NLIB_UNLIKELY(e != 0)) return e;
661  nlib_memcpy(static_cast<void*>(This->GetString()),
662  n,
663  static_cast<const void*>(&str[0]),
664  n);
665  return 0;
666  }
667  static errno_t Unbox(const MpObject* This, STDSTRING* str) NLIB_NOEXCEPT {
668  if (NLIB_UNLIKELY(!str)) return EINVAL;
669  if (NLIB_UNLIKELY(This->type_ != kString) && NLIB_UNLIKELY(This->type_ != kStringInline))
670  return EACCES;
671  const nlib_utf8_t* chr = This->GetString();
672  NLIB_TRY { (*str).assign(chr, This->GetSize()); }
673  NLIB_CATCH(std::bad_alloc&) { return ENOMEM; }
674  return 0;
675  }
676 };
677 
678 template <class T, class ALLOC>
679 struct MpObject::MyBox<std::vector<T, ALLOC>, FalseType, FalseType> {
680  static errno_t Box(MpObject* This, const std::vector<T, ALLOC>& vec) {
681  uint32_t n = static_cast<uint32_t>(vec.size());
682  MpObject tmp;
683  ErrnoT e = tmp.InitArray(n);
684  if (NLIB_UNLIKELY(e != 0)) return e;
685 
686  for (uint32_t i = 0; i < n; ++i) {
687  e = (*tmp.via_.ary)[i].Box(vec[i]);
688  if (NLIB_UNLIKELY(e != 0)) return e;
689  }
690  This->assign(tmp, move_tag());
691  return 0;
692  }
693  static errno_t Unbox(const MpObject* This, std::vector<T, ALLOC>* vec) {
694  if (NLIB_UNLIKELY(!vec)) return EINVAL;
695  if (NLIB_UNLIKELY(This->type_ != kArray)) return EACCES;
696  NLIB_TRY { vec->resize(This->size_); }
697  NLIB_CATCH(std::bad_alloc&) { return ENOMEM; }
698  for (uint32_t i = 0; i < This->size_; ++i) {
699  ErrnoT e = (*This->via_.ary)[i].Unbox(&(*vec)[i]);
700  if (NLIB_UNLIKELY(e != 0)) return e; // *vec NOT RESET
701  }
702  return 0;
703  }
704 };
705 
706 template <class T, class ALLOC>
707 struct MpObject::MyBox<Nlist<T, ALLOC>, FalseType, FalseType> {
708  static errno_t Box(MpObject* This, const Nlist<T, ALLOC>& vec) {
709  size_t n = vec.size();
710  MpObject tmp;
711  ErrnoT e = tmp.InitArray(static_cast<uint32_t>(n));
712  if (NLIB_UNLIKELY(e != 0)) return e;
713  typename Nlist<T, ALLOC>::const_iterator it = vec.begin();
714  for (size_t i = 0; i < n; ++it, ++i) {
715  e = (*tmp.via_.ary)[i].Box(*it);
716  if (NLIB_UNLIKELY(e != 0)) return e;
717  }
718  This->assign(tmp, move_tag());
719  return 0;
720  }
721  static errno_t Unbox(const MpObject* This, Nlist<T, ALLOC>* vec) {
722  if (NLIB_UNLIKELY(!vec)) return EINVAL;
723  if (NLIB_UNLIKELY(This->type_ != kArray)) return EACCES;
724  vec->clear();
725  if (NLIB_UNLIKELY(!vec->reserve(This->size_))) return ENOMEM;
726  for (uint32_t i = 0; i < This->size_; ++i) {
727  T* item = vec->push_back();
728  ErrnoT e = (*This->via_.ary)[i].Unbox(item);
729  if (NLIB_UNLIKELY(e != 0)) return e; // *vec NOT RESET
730  }
731  return 0;
732  }
733 };
734 
735 template <class T1, class T2>
736 struct MpObject::MyBox<std::pair<T1, T2>, FalseType, FalseType> {
737  static errno_t Box(MpObject* This, const std::pair<T1, T2>& p) {
738  MpObject tmp;
739  ErrnoT e = tmp.InitArray(2);
740  if (NLIB_UNLIKELY(e != 0)) return e;
741  e = (*tmp.via_.ary)[0].Box(p.first);
742  if (NLIB_UNLIKELY(e != 0)) return e;
743  e = (*tmp.via_.ary)[1].Box(p.second);
744  if (NLIB_UNLIKELY(e != 0)) return e;
745  This->assign(tmp, move_tag());
746  return 0;
747  }
748  static errno_t Unbox(const MpObject* This, std::pair<T1, T2>* p) {
749  if (NLIB_UNLIKELY(!p)) return EINVAL;
750  if (NLIB_UNLIKELY(This->type_ != kArray)) return EACCES;
751  if (NLIB_UNLIKELY(This->size_ != 2)) return EACCES;
752  ErrnoT e;
753  e = (*This->via_.ary)[0].Unbox(&p->first);
754  if (NLIB_UNLIKELY(e != 0)) return e; // *p NOT RESET
755  e = (*This->via_.ary)[1].Unbox(&p->second);
756  if (NLIB_UNLIKELY(e != 0)) return e; // *p NOT RESET
757  return 0;
758  }
759 };
760 
761 #if defined(NLIB_CXX11_STDLIB_TUPLE) && defined(__cpp_variadic_templates)
762 template <class ...Args>
763 struct MpObject::MyBox<std::tuple<Args...>, FalseType, FalseType > {
764  private:
765  template<size_t N, class HEAD>
766  static errno_t BoxHelper(MpObject* tmp, const HEAD& head) {
767  return (*tmp->via_.ary)[N].Box(head);
768  }
769  template<size_t N, class HEAD, class ...TAIL>
770  static errno_t BoxHelper(MpObject* tmp, const HEAD& head, TAIL&... tail) {
771  ErrnoT e = (*tmp->via_.ary)[N].Box(head);
772  if (NLIB_UNLIKELY(e != 0)) return e;
773  return BoxHelper<N + 1, TAIL...>(tmp, tail...);
774  }
775  template<size_t N, class HEAD>
776  static errno_t UnboxHelper(const MpObject* This, HEAD& head) { // NOLINT
777  return (*This->via_.ary)[N].Unbox(&head);
778  }
779  template<size_t N, class HEAD, class ...TAIL>
780  static errno_t UnboxHelper(const MpObject* This, HEAD& head, TAIL&... tail) { // NOLINT
781  ErrnoT e = (*This->via_.ary)[N].Unbox(&head);
782  if (NLIB_UNLIKELY(e != 0)) return e;
783  return UnboxHelper<N + 1, TAIL...>(This, tail...);
784  }
785  template<int ...> struct seq {};
786  template<int N, int ...S> struct gens : gens<N - 1, N - 1, S...> {};
787  template<int ...S>
788  struct gens<0, S...> {
789  typedef seq<S...> type;
790  };
791  template<int ...S>
792  static errno_t Box(MpObject* tmp, seq<S...>, const std::tuple<Args...>& p) {
793  return BoxHelper<0>(tmp, std::get<S>(p)...);
794  }
795  template<int ...S>
796  static errno_t Unbox(const MpObject* This, seq<S...>, std::tuple<Args...>& p) { // NOLINT
797  return UnboxHelper<0>(This, std::get<S>(p)...);
798  }
799 
800  public:
801  static errno_t Box(MpObject* This, const std::tuple<Args...>& p) {
802  size_t count = std::tuple_size<std::tuple<Args...> >::value;
803  MpObject tmp;
804  ErrnoT e = tmp.InitArray(static_cast<uint32_t>(count));
805  if (NLIB_UNLIKELY(e != 0)) return e;
806  typedef typename gens<sizeof...(Args)>::type MySeq;
807  e = Box(&tmp, MySeq(), p);
808  if (NLIB_UNLIKELY(e != 0)) return e;
809  This->assign(tmp, move_tag());
810  return 0;
811  }
812  static errno_t Unbox(const MpObject* This, std::tuple<Args...>* p) {
813  if (NLIB_UNLIKELY(!p)) return EINVAL;
814  if (NLIB_UNLIKELY(This->type_ != kArray)) return EACCES;
815  if (NLIB_UNLIKELY(This->size_ != std::tuple_size<std::tuple<Args...> >::value))
816  return EACCES;
817  typedef typename gens<sizeof...(Args)>::type MySeq;
818  return Unbox(This, MySeq(), *p);
819  }
820 };
821 #endif
822 
823 template <class K, class V, class Pr, class Alloc>
824 struct MpObject::MyBox<std::map<K, V, Pr, Alloc>, FalseType, FalseType> {
825  static errno_t Box(MpObject* This, const std::map<K, V, Pr, Alloc>& m) {
826  typedef typename std::map<K, V, Pr, Alloc> maptype;
827  uint32_t n = static_cast<uint32_t>(m.size());
828  MpObject tmp;
829  ErrnoT e = tmp.InitMap(n);
830  if (NLIB_UNLIKELY(e != 0)) return e;
831 
832  typename maptype::const_iterator it;
833  typename maptype::const_iterator it_end = m.end();
834  uint32_t i;
835  for (i = 0, it = m.begin(); it != it_end; ++it, ++i) {
836  MpObjectKv& kv = (*tmp.via_.mp)[i];
837  e = kv.first.Box(it->first);
838  if (NLIB_UNLIKELY(e != 0)) return e;
839  e = kv.second.Box(it->second);
840  if (NLIB_UNLIKELY(e != 0)) return e;
841  }
842  This->assign(tmp, move_tag());
843  return 0;
844  }
845  static errno_t Unbox(const MpObject* This, std::map<K, V, Pr, Alloc>* m) {
846  if (NLIB_UNLIKELY(!m)) return EINVAL;
847  if (NLIB_UNLIKELY(This->type_ != kMap)) return EACCES;
848  m->clear();
849  for (uint32_t i = 0; i < This->size_; ++i) {
850  K key;
851  V value;
852  const MpObjectKv& kv = (*This->via_.mp)[i];
853  ErrnoT e;
854  e = kv.first.Unbox(&key);
855  if (NLIB_UNLIKELY(e != 0)) return e; // *m NOT RESET
856  e = kv.second.Unbox(&value);
857  if (NLIB_UNLIKELY(e != 0)) return e; // *m NOT RESET
858  NLIB_TRY { (*m)[NLIB_MOVE(key)] = NLIB_MOVE(value); }
859  NLIB_CATCH(...) { return ENOMEM; }
860  }
861  return 0;
862  }
863 };
864 
865 #ifdef NLIB_CXX11_STDLIB_UNORDERED
866 template <class K, class V, class Hash, class Pr, class Alloc>
867 struct MpObject::MyBox<std::unordered_map<K, V, Hash, Pr, Alloc>, FalseType, FalseType > {
868  static errno_t Box(MpObject* This, const std::unordered_map<K, V, Hash, Pr, Alloc>& m) {
869  typedef typename std::unordered_map<K, V, Hash, Pr, Alloc> maptype;
870  uint32_t n = static_cast<uint32_t>(m.size());
871  MpObject tmp;
872  ErrnoT e = tmp.InitMap(n);
873  if (NLIB_UNLIKELY(e != 0)) return e;
874 
875  typename maptype::const_iterator it;
876  typename maptype::const_iterator it_end = m.end();
877  uint32_t i;
878  for (i = 0, it = m.begin(); it != it_end; ++it, ++i) {
879  MpObjectKv& kv = (*tmp.via_.mp)[i];
880  e = kv.first.Box(it->first);
881  if (NLIB_UNLIKELY(e != 0)) return e;
882  e = kv.second.Box(it->second);
883  if (NLIB_UNLIKELY(e != 0)) return e;
884  }
885  This->assign(tmp, move_tag());
886  return 0;
887  }
888  static errno_t Unbox(const MpObject* This, std::unordered_map<K, V, Hash, Pr, Alloc>* m) {
889  if (NLIB_UNLIKELY(!m)) return EINVAL;
890  if (NLIB_UNLIKELY(This->type_ != kMap)) return EACCES;
891  m->clear();
892  for (uint32_t i = 0; i < This->size_; ++i) {
893  K key;
894  V value;
895  const MpObjectKv& kv = (*This->via_.mp)[i];
896  ErrnoT e;
897  e = kv.first.Unbox(&key);
898  if (NLIB_UNLIKELY(e != 0)) return e; // *m NOT RESET
899  e = kv.second.Unbox(&value);
900  if (NLIB_UNLIKELY(e != 0)) return e; // *m NOT RESET
901  NLIB_TRY { (*m)[NLIB_MOVE(key)] = NLIB_MOVE(value); }
902  NLIB_CATCH(...) { return ENOMEM; }
903  }
904  return 0;
905  }
906 };
907 #endif
908 
909 NLIB_VIS_PUBLIC bool operator==(const MpObject& lhs, const MpObject& rhs);
910 inline bool operator==(const MpObjectKv& lhs, const MpObjectKv& rhs) NLIB_NOEXCEPT {
911  return lhs.first == rhs.first && lhs.second == rhs.second;
912 }
913 inline bool operator!=(const MpObject& lhs, const MpObject& rhs) NLIB_NOEXCEPT {
914  return !(lhs == rhs);
915 }
916 inline bool operator!=(const MpObjectKv& lhs, const MpObjectKv& rhs) NLIB_NOEXCEPT {
917  return !(lhs == rhs);
918 }
919 
920 template <uint32_t n>
921 errno_t MpObject::Box(const nlib_utf8_t (&str)[n]) NLIB_NOEXCEPT {
922  uint32_t nn = static_cast<uint32_t>(nlib_strlen(str));
923  ErrnoT e = this->InitString(nn);
924  if (NLIB_UNLIKELY(e != 0)) return e;
925  nlib_memcpy(this->GetString(), nn, static_cast<const void*>(&str[0]), nn);
926  return 0;
927 }
928 
929 template <class T, uint32_t n>
930 errno_t MpObject::Box(const T (&vec)[n]) {
931  MpObject tmp;
932  ErrnoT e = tmp.InitArray(n);
933  if (NLIB_UNLIKELY(e != 0)) return e;
934  for (uint32_t i = 0; i < n; ++i) {
935  e = (*tmp.via_.ary)[i].Box(vec[i]);
936  if (NLIB_UNLIKELY(e != 0)) return e;
937  }
938  this->assign(tmp, move_tag());
939  return 0;
940 }
941 
942 #ifdef NLIB_CXX11_STDLIB_ARRAY
943 template <class T, size_t n>
944 errno_t MpObject::Box(const std::array<T, n>& vec) {
945  MpObject tmp;
946  ErrnoT e = tmp.InitArray(n);
947  if (NLIB_UNLIKELY(e != 0)) return e;
948  for (uint32_t i = 0; i < n; ++i) {
949  e = (*tmp.via_.ary)[i].Box(vec[i]);
950  if (NLIB_UNLIKELY(e != 0)) return e;
951  }
952  this->assign(tmp, move_tag());
953  return 0;
954 }
955 #endif
956 
957 template <class T>
958 errno_t MpObject::Unbox(T* a, size_t n) const {
959  NLIB_EINVAL_IFNULL(a);
960  if (NLIB_UNLIKELY(n == 0)) return EINVAL;
961  if (NLIB_UNLIKELY(type_ != kArray)) return EACCES;
962  if (NLIB_UNLIKELY(n != size_)) return ERANGE;
963  for (uint32_t i = 0; i < size_; ++i) {
964  ErrnoT e = (*via_.ary)[i].Unbox(&a[i]);
965  if (NLIB_UNLIKELY(e != 0)) return e;
966  }
967  return 0;
968 }
969 
970 } // namespace msgpack
971 NLIB_NAMESPACE_END
972 
973 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::MpObject)
974 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::MpObjectKv)
975 
976 NLIB_NAMESPACE_BEGIN
977 namespace msgpack {
978 inline MpObject* MpObject::SwapArrayItem(size_t n, MpObject* obj) NLIB_NOEXCEPT {
979  MpObject* p = this->GetArrayItem(n);
980  if (NLIB_LIKELY(p)) {
981  using std::swap;
982  swap(*p, *obj);
983  }
984  return p;
985 }
986 template<class STDSTRING>
987 inline MpObject* MpObject::SwapMapItem(const STDSTRING& key, MpObject* obj) NLIB_NOEXCEPT {
988  MpObject* p = this->GetMapItem(key);
989  if (NLIB_LIKELY(p)) {
990  using std::swap;
991  swap(*p, *obj);
992  }
993  return p;
994 }
995 template<size_t N>
996 inline MpObject* MpObject::SwapMapItem(const nlib_utf8_t (&key)[N],
997  MpObject* obj) NLIB_NOEXCEPT {
998  MpObject* p = this->GetMapItem(key);
999  if (NLIB_LIKELY(p)) {
1000  using std::swap;
1001  swap(*p, *obj);
1002  }
1003  return p;
1004 }
1005 inline MpObject* MpObject::SwapMapItem(const nlib_utf8_t* key, MpObject* obj) NLIB_NOEXCEPT {
1006  MpObject* p = this->GetMapItem(key);
1007  if (NLIB_LIKELY(p)) {
1008  using std::swap;
1009  swap(*p, *obj);
1010  }
1011  return p;
1012 }
1013 
1014 inline void MpObjectKv::swap(MpObjectKv& rhs) NLIB_NOEXCEPT { // NOLINT
1015  using std::swap;
1016  swap(first, rhs.first);
1017  swap(second, rhs.second);
1018 }
1019 } // namespace msgpack
1020 NLIB_NAMESPACE_END
1021 
1022 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
1023 #undef NLIB_VIS_PUBLIC
1024 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
1025 #endif
1026 
1027 #endif // INCLUDE_NN_NLIB_MSGPACK_MPOBJECT_H_
nlib_utf8_t * GetString() noexcept
Gets a string from an object.
Definition: MpObject.h:215
errno_t Unbox(T v) const
Unboxes the object.
Definition: MpObject.h:357
bool IsString() const noexcept
Determines whether the stored value is a string.
Definition: MpObject.h:428
bool IsMap() const noexcept
Determines whether the stored value is an associative array.
Definition: MpObject.h:432
MpObject & operator[](size_t n)
For an array, returns a reference to an element of the array.
Definition: MpObject.h:127
MpObjectKv() noexcept
Instantiates the object with default parameters (default constructor).
Definition: MpObject.h:467
#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
Represents an unsigned integer type. The internal representation is uint64_t.
Definition: MpObject.h:380
Represents an integer type. The internal representation is int64_t.
Definition: MpObject.h:381
Definition: Base64.h:25
MpObject() noexcept
Instantiates the object with default parameters (default constructor). Set to the nil type...
Definition: MpObject.h:164
~MpObject() noexcept
Destructor.
Definition: MpObject.h:165
const void * GetExt(int8_t *tp, uint32_t *n) const noexcept
Gets an extended data type from an object.
Definition: MpObject.h:255
Represents an array. The internal representation is an array of MpObject.
Definition: MpObject.h:385
iterator begin() noexcept
Returns the iterator pointing to the beginning of the container.
Definition: Nlist.h:562
const MpObject * GetArrayItem(size_t n) const noexcept
The same as GetArrayItem(size_t n).
Definition: MpObject.h:123
const void * GetBinary(uint32_t *n) const noexcept
Gets binary from an object.
Definition: MpObject.h:243
#define NLIB_UNLIKELY(x)
Indicates to the compiler that condition x is likely to be false.
const nlib_utf8_t * GetString() const noexcept
Gets a string from an object.
Definition: MpObject.h:229
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
Definition: NMalloc.h:149
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
Definition: NMalloc.h:154
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
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:109
errno_t Box(const T &v)
Boxes the object.
Definition: MpObject.h:329
BaseType::const_iterator const_iterator
Read-only forward iterator.
Definition: Nlist.h:457
#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
Represents a floating point type. The internal representation is float.
Definition: MpObject.h:382
pointer push_back()
Adds an element to the end and initializes it with the default constructor.
Definition: Nlist.h:507
MpObjectKv & assign(MpObjectKv &rhs, move_tag) noexcept
Corresponds to a move assignment operator.
Definition: MpObject.h:485
MpObject(MpObject &rhs, move_tag) noexcept
Corresponds to a move constructor.
Definition: MpObject.h:177
MpObject(MpObject &&rhs) noexcept
Instantiates the object (move constructor). This function is useful when using C++11.
Definition: MpObject.h:167
MpObject & operator=(const T &x)
The value is passed into MpObject.
Definition: MpObject.h:369
MpObject second
Object used as a value.
Definition: MpObject.h:464
Defines the class that resembles std::vector but can store objects that cannot be copied...
ObjectType
Data type of the object stored in MpObject.
Definition: MpObject.h:377
An empty structure indicating that an argument to a function needs to be moved.
Definition: Config.h:265
bool IsExt() const noexcept
Determines whether the stored value is extended data.
Definition: MpObject.h:430
MpObject & operator=(MpObject &&rhs) noexcept
Move assignment operator. This function is useful when using C++11.
Definition: MpObject.h:171
errno_t InitArray(uint32_t n) noexcept
Initializes as an object with an n element array.
void clear() noexcept
Clears the container.
Definition: Nlist.h:536
Represents a floating point type. The internal representation is double.
Definition: MpObject.h:383
errno_t Unbox(const MpObject *obj, T *v)
This function template can be specialized to define unboxing the user type.
bool IsNil() const noexcept
Determines whether the stored value is nil.
Definition: MpObject.h:420
void swap(MpObject &rhs) noexcept
Swaps the content of the object.
Definition: MpObject.h:186
#define NLIB_LIKELY(x)
Indicates to the compiler that condition x is likely to be true.
Definition: Platform_unix.h:99
#define NLIB_CATCH(x)
Defines catch(x) if exceptions are enabled. If not, defines if (true).
Definition: Config.h:143
errno_t InitString(uint32_t n) noexcept
Initializes the object as a string.
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
Definition: Config.h:406
static errno_t nlib_memcpy(void *s1, size_t s1max, const void *s2, size_t n)
An implementation corresponding to N1078 memcpy_s.
Definition: Platform.h:2437
errno_t Box(const nlib_utf8_t *str) noexcept
Boxes the string.
#define NLIB_TRY
Defines try if exceptions are enabled. If not, defines if (true).
Definition: Config.h:142
ObjectType GetType() const noexcept
Returns the object type.
Definition: MpObject.h:411
bool IsArray() const noexcept
Determines whether the stored value is an array.
Definition: MpObject.h:431
MpObject(const T &x)
The constructor for boxing a T-type object.
Definition: MpObject.h:198
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
errno_t InitString(const nlib_utf8_t *str) noexcept
Initializes the object as a string.
Definition: MpObject.h:308
errno_t Unbox(nlib_utf8_t(&str)[n]) const noexcept
See Unbox(T (&a)[n]).
Definition: MpObject.h:348
const MpObject * GetMapItem(const STDSTRING &str) const noexcept
The same as GetMapItem(const STDSTRING& str).
Definition: MpObject.h:143
A file that contains the configuration information for each development environment.
~MpObjectKv() noexcept
Destructor.
Definition: MpObject.h:468
Represents a boolean type. The internal representation is a bool.
Definition: MpObject.h:379
bool reserve(size_type n) noexcept
Allocates memory for n number of elements.
Definition: Nlist.h:782
uint32_t GetSize() const noexcept
Returns the size of the array, associative array, string, or binary.
Definition: MpObject.h:414
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Definition: Nlist.h:32
errno_t RemoveMapItem(const STDSTRING &str, MpObjectKv *kv) noexcept
Specifies a key and deletes the corresponding key and object in the associative array.
Definition: MpObject.h:225
MpObject & assign(MpObject &rhs, move_tag) noexcept
Assigns the object by using swap for a move.
Definition: MpObject.h:181
size_type size() const noexcept
Returns the number of stored elements.
Definition: Nlist.h:495
Respresents a byte array (string).
Definition: MpObject.h:384
static bool IsJsonPointer(const nlib_utf8_t *str) noexcept
This function is equivalent to IsJsonPointer(str, str + nlib_strlen(str)).
Definition: MpObject.h:110
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
bool IsBoolean() const noexcept
Determines whether the stored value is a boolean.
Definition: MpObject.h:421
void * GetBinary(uint32_t *n) noexcept
Gets binary from an object.
Definition: MpObject.h:232
Wraps functions like strlen and strcpy so they can be safely used.
void * GetExt(int8_t *tp, uint32_t *n) noexcept
Gets an extended data type from an object.
Definition: MpObject.h:246
bool IsFloat() const noexcept
Determines whether the stored value is a single precision float.
Definition: MpObject.h:426
MpObject first
Object used as a key.
Definition: MpObject.h:463
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
MpObject * GetMapItem(const STDSTRING &str) noexcept
Specifies a string and gets the object in the associative array.
Definition: MpObject.h:147
errno_t InitMap(uint32_t n) noexcept
Initializes the object as an n element associative array.
MpObject * GetArrayItem(size_t n) noexcept
Specifies an index and gets the object in the array.
Definition: MpObject.h:119
bool IsDouble() const noexcept
Determines whether the stored value is a double.
Definition: MpObject.h:427
A pair consisting of an MpObject-type key and value. Used to store an associative array...
Definition: MpObject.h:462
errno_t GetMapItem(const nlib_utf8_t *str, size_t n, MpObjectKv **obj) noexcept
Obtains the pair of a key and value from the associative array by specifying a non-null terminated st...
Definition: MpObject.h:205
Class that corresponds to nil in MessagePack and null in JSON.
Definition: MpObject.h:56
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
bool IsInteger() const noexcept
Determines whether the stored value is an integer.
Definition: MpObject.h:422
bool IsBinary() const noexcept
Determines whether the stored value is binary.
Definition: MpObject.h:429
Represents an associative array. The internal representation is an array of MpObject pairs...
Definition: MpObject.h:386
MpObjectKv(MpObjectKv &rhs, move_tag) noexcept
Corresponds to a move constructor.
Definition: MpObject.h:483
errno_t Unbox(T(&a)[n]) const
Unboxes the object value.
Definition: MpObject.h:338
errno_t Box(MpObject *obj, const T &v)
This function template can be specialized to define boxing the user type.
char nlib_utf8_t
Defines char with a typedef. Indicates that it is a UTF-8 string.
Definition: Platform.h:308
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37