3 #ifndef INCLUDE_NN_NLIB_MSGPACK_MPREADER_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_MPREADER_H_
15 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
16 #undef NLIB_VIS_PUBLIC
17 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
25 bool MpRead(MpReader* obj, T* v);
35 : maxArraySize(max_array_size_),
36 maxMapSize(max_map_size_),
37 maxRawSize(max_raw_size_) {}
40 maxRawSize(1024 * 512) {}
60 static bool Read(
MpObject* obj,
const void* p,
size_t n) NLIB_NOEXCEPT NLIB_NONNULL {
64 if (m_ErrorValue == 0) m_ErrorValue = e;
75 return this->Convert_(v);
85 bool Read(
char (&v)[n]) NLIB_NOEXCEPT {
86 return this->
Read(&v[0], n);
88 template <
class T,
size_t n>
90 return this->
Read(&v[0], n);
98 return this->Read_(&obj);
102 NLIB_VIS_PUBLIC bool CheckString(const
char* key,
size_t keylen) NLIB_NOEXCEPT NLIB_NONNULL;
103 bool CheckString(const
char* key) NLIB_NOEXCEPT NLIB_NONNULL {
127 #define NLIB_EXEC_READ return GetNextValue() ? (void)this->Convert(v), true : false
128 NLIB_VIS_PUBLIC bool Read_(int8_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
129 NLIB_VIS_PUBLIC bool Read_(int16_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
130 NLIB_VIS_PUBLIC bool Read_(int32_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
131 NLIB_VIS_PUBLIC bool Read_(int64_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
132 NLIB_VIS_PUBLIC bool Read_(uint8_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
133 NLIB_VIS_PUBLIC bool Read_(uint16_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
134 NLIB_VIS_PUBLIC bool Read_(uint32_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
135 NLIB_VIS_PUBLIC bool Read_(uint64_t* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
137 NLIB_VIS_PUBLIC bool Read_(
double* v) NLIB_NOEXCEPT { NLIB_EXEC_READ; }
144 #undef NLIB_EXEC_READ
172 ValueObj() NLIB_NOEXCEPT : category(CAT_NIL) {}
174 size_t m_MaxArraySize;
186 if (!this->GetNextArrayNum(&num))
return false;
192 for (i = 0; i < num; ++i) {
193 if (!this->
Read(&v[i]))
return false;
202 template <
class T1,
class T2>
210 if (!r->
Read(&v->first))
return false;
211 if (!r->
Read(&v->second))
return false;
215 template <
class T,
class Alloc>
220 for (
size_t i = 0; i < num; ++i) {
221 if (!r->
Read(&(*v)[i]))
return false;
226 template <
class T,
class Alloc>
231 if (!v->
reserve(num))
return false;
232 for (
size_t i = 0; i < num; ++i) {
234 if (!r->
Read(item))
return false;
239 template <
class K,
class V,
class Pr,
class Alloc>
245 for (
size_t i = 0; i < num; ++i) {
246 if (!r->
Read(&key))
return false;
247 if (!r->
Read(&value))
return false;
254 bool MpReader::Read_(T* v) {
258 #define NLIB_MPREADER_READ_(tp, coerce) \
260 template<> inline bool MpReader::Read_<tp>(tp * v) { \
262 if (!Read_(&vv)) return false; \
263 *v = static_cast<tp>(vv); \
268 NLIB_MPREADER_READ_(
char, int8_t)
269 NLIB_MPREADER_READ_(
unsigned char, uint8_t)
270 NLIB_MPREADER_READ_(
int, int32_t)
271 NLIB_MPREADER_READ_(
unsigned int, uint32_t)
272 NLIB_MPREADER_READ_(
short, int16_t)
273 NLIB_MPREADER_READ_(
unsigned short, uint16_t)
276 NLIB_MPREADER_READ_(
long long, int64_t)
277 NLIB_MPREADER_READ_(
unsigned long long, uint64_t)
279 #undef NLIB_MPREADER_READ_
281 #define NLIB_MPREADER_CONVERT_(tp, coerce) \
283 template<> inline errno_t MpReader::Convert_<tp>(tp * v) { \
284 return this->Convert_(reinterpret_cast<coerce*>(v)); \
288 NLIB_MPREADER_CONVERT_(
char, int8_t)
289 NLIB_MPREADER_CONVERT_(
unsigned char, uint8_t)
290 NLIB_MPREADER_CONVERT_(
int, int32_t)
291 NLIB_MPREADER_CONVERT_(
unsigned int, uint32_t)
292 NLIB_MPREADER_CONVERT_(
short, int16_t)
293 NLIB_MPREADER_CONVERT_(
unsigned short, uint16_t)
296 NLIB_MPREADER_CONVERT_(
long long, int64_t)
297 NLIB_MPREADER_CONVERT_(
unsigned long long, uint64_t)
299 #undef NLIB_MPREADER_CONVERT_
304 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
305 #undef NLIB_VIS_PUBLIC
306 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
309 #endif // INCLUDE_NN_NLIB_MSGPACK_MPREADER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
bool Read(char(&v)[n]) noexcept
See Read(T (&vec)[n]).
bool Read(T(&v)[n])
Reads text strings or arrays.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#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...
Reads the MessagePack-formatted data from the stream.
errno_t GetErrorValue() const noexcept
Gets the error that occurred.
static bool Read(MpObject *obj, const void *p, size_t n, const MpReaderSettings &settings) noexcept
Constructs an MpReader object, and sets MpObject from the p parameter.
void SetError(errno_t e) noexcept
Sets an error.
bool Close() noexcept
Closes MpReader.
Object created when MessagePack or JSON is read.
size_t maxMapSize
Specifies the maximum associative array size. The default is 1024.
size_t maxArraySize
Specifies the maximum array size. The default is 65536.
pointer push_back()
Adds an element to the end and initializes it with the default constructor.
bool GetNextArrayNum(size_t *num) noexcept
Reads the array size data.
bool GetNextMapNum(size_t *num) noexcept
Reads the associative array size data.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Defines the class that resembles std::vector but can store objects that cannot be copied...
static bool Read(MpObject *obj, const void *p, size_t n) noexcept
Runs Read(MpObject* obj, const void* p, size_t n, const MpReaderSettings& settings) with settings set...
bool Init(InputStream *istr) noexcept
Initializes an object.
void clear() noexcept
Clears the container.
bool MpRead(MpReader *r, std::map< K, V, Pr, Alloc > *m)
Reads the data into map.
Object created when MessagePack, JSON, or CSV is read.
Data structure used to store the MpReader settings parameters.
bool Read(MpObject *obj) noexcept
See Read(T v).
bool reserve(size_type n) noexcept
Allocates memory for n number of elements.
A container-like class similar to std::vector that can store objects that do not have copy constructo...
constexpr MpReaderSettings(size_t max_array_size_, size_t max_map_size_, size_t max_raw_size_) noexcept
Sets the various data members.
nlib_i64_t i64
nlib_i64_t is defined using typedef.
constexpr MpReaderSettings() noexcept
Instantiates the object with default parameters (default constructor). Sets each data member to the d...
bool Read(T v)
Reads data from the stream, and sets the v parameter to it.
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
size_t maxRawSize
Specifies the maximum byte array size. The default is 512 KB.
Class that corresponds to nil in MessagePack and null in JSON.
bool ReadArray(BinaryReader *r, T *x, size_t n)
You can read to user-defined class objects by specializing this function template.