nlib
MpWalker.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MSGPACK_MPWALKER_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_MPWALKER_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
9 #undef NLIB_VIS_PUBLIC
10 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
11 #endif
12 
13 NLIB_NAMESPACE_BEGIN
14 namespace msgpack {
15 
16 class MpObject;
18  public:
19  MpWalker() NLIB_NOEXCEPT : cur_(NULL), size_(0) {}
21  MpWalker(const void* p, size_t n) NLIB_NOEXCEPT;
22  bool Init(const void* p, size_t n) NLIB_NOEXCEPT NLIB_NONNULL {
23  if (n > RSIZE_MAX) return false;
24  cur_ = reinterpret_cast<const uint8_t*>(p);
25  size_ = n;
26  return true;
27  }
28 
29  MpWalker operator[](size_t array_idx) const NLIB_NOEXCEPT;
30  MpWalker operator[](const char* key) const NLIB_NOEXCEPT NLIB_NONNULL;
31  MpWalker operator[](int array_idx) const NLIB_NOEXCEPT {
32  if (array_idx < 0)
33  return MpWalker();
34  else
35  return operator[](static_cast<size_t>(array_idx));
36  }
37 
38  const void* GetPtr() const NLIB_NOEXCEPT { return cur_; }
39  size_t GetSize() const NLIB_NOEXCEPT { return size_; }
40 
41  MpWalker At(size_t idx) const NLIB_NOEXCEPT;
42  MpWalker At(size_t idx, const char** key, size_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
43  MpWalker Find(const char* key) const NLIB_NOEXCEPT NLIB_NONNULL;
44  MpWalker Skip() const NLIB_NOEXCEPT;
45  MpWalker GetString(const char** str, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
46  MpWalker GetBinary(const void** bin, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
47  MpWalker GetExt(int8_t* tp, const void** bin, uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
48  MpWalker GetMapCount(uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
49  MpWalker GetArrayCount(uint32_t* n) const NLIB_NOEXCEPT NLIB_NONNULL;
50  MpWalker GetNil() const NLIB_NOEXCEPT;
51  MpWalker GetBoolean(bool* val) const NLIB_NOEXCEPT NLIB_NONNULL;
52  MpWalker GetInt(int32_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
53  MpWalker GetInt(int64_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
54  MpWalker GetUint(uint32_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
55  MpWalker GetUint(uint64_t* val) const NLIB_NOEXCEPT NLIB_NONNULL;
56  MpWalker GetFloat(float* val) const NLIB_NOEXCEPT NLIB_NONNULL;
57  MpWalker GetDouble(double* val) const NLIB_NOEXCEPT NLIB_NONNULL;
58  NLIB_SAFE_BOOL(MpWalker, IsOk())
59 
60  private:
61  bool IsOk() const NLIB_NOEXCEPT { return cur_ != NULL && size_ > 0; }
62 
63  const uint8_t* cur_;
64  size_t size_;
65 };
66 
67 } // namespace msgpack
68 NLIB_NAMESPACE_END
69 
70 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
71 #undef NLIB_VIS_PUBLIC
72 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
73 #endif
74 
75 #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:160
MpWalker() noexcept
Instantiates the object with default parameters (default constructor).
Definition: MpWalker.h:19
#define RSIZE_MAX
Defines a value somewhat smaller than the maximum value of size_t.
Definition: Platform.h:541
size_t GetSize() const noexcept
Gets the size of the MessagePack data.
Definition: MpWalker.h:39
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
MpWalker operator[](int array_idx) const noexcept
The same as operator[](size_t array_idx).
Definition: MpWalker.h:31
const void * GetPtr() const noexcept
Gets a pointer to the current location of MessagePack data.
Definition: MpWalker.h:38
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
A file that contains the configuration information for each development environment.
Quickly accesses MessagePack expanded in memory.
Definition: MpWalker.h:17
bool Init(const void *p, size_t n) noexcept
Initialize with this function when using the default constructor.
Definition: MpWalker.h:22
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
~MpWalker() noexcept
Destructor.
Definition: MpWalker.h:20
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:76