3 #ifndef INCLUDE_NN_NLIB_MSGPACK_MPWRITER_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_MPWRITER_H_
17 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
18 #undef NLIB_VIS_PUBLIC
19 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
28 bool MpWrite(MpWriter* obj,
const T& v);
36 if (!m_Writer.Flush()) {
40 return this->GetErrorValue() == 0;
44 if (!m_Writer.Close()) {
47 return this->GetErrorValue() == 0;
50 if (m_ErrorValue == 0) m_ErrorValue = e;
62 template <
class T,
size_t n>
63 bool Write(
const T (&vec)[n]);
65 bool Write(
const T& tp);
71 bool WriteRawBody(
const void* p, uint32_t n) NLIB_NOEXCEPT
NLIB_NONNULL;
85 bool Write_(uint8_t v) NLIB_NOEXCEPT {
return this->WriteU8(v); }
86 bool Write_(uint16_t v) NLIB_NOEXCEPT {
return this->WriteU16(v); }
87 bool Write_(uint32_t v) NLIB_NOEXCEPT {
return this->WriteU32(v); }
88 bool Write_(uint64_t v) NLIB_NOEXCEPT {
return this->WriteU64(v); }
89 bool Write_(int8_t v) NLIB_NOEXCEPT {
return this->WriteI8(v); }
90 bool Write_(int16_t v) NLIB_NOEXCEPT {
return this->WriteI16(v); }
91 bool Write_(int32_t v) NLIB_NOEXCEPT {
return this->WriteI32(v); }
92 bool Write_(int64_t v) NLIB_NOEXCEPT {
return this->WriteI64(v); }
95 bool Write_(
bool v) NLIB_NOEXCEPT {
96 return m_Writer.Write<uint8_t>(v ? 0xC3 : 0xC2);
98 bool Write_(nil v) NLIB_NOEXCEPT {
100 return m_Writer.Write<uint8_t>(0xC0);
102 bool Write_(
const std::string& str) NLIB_NOEXCEPT {
return this->
Write(&str[0], str.size()); }
104 bool Write_(
const T& v);
107 BinaryWriter m_Writer;
112 #define NLIB_MPWRITER_CHK_NSTREAM \
117 #define NLIB_MPWRITER_SET_ERROR \
121 template <
class T,
size_t n>
123 if (!this->WriteArrayCount(static_cast<uint32_t>(n)))
return false;
124 for (
size_t i = 0; i < n; ++i) {
125 if (!this->
Write(vec[i])) {
126 NLIB_MPWRITER_SET_ERROR;
134 if (this->Write_(tp)) {
137 NLIB_MPWRITER_SET_ERROR;
140 #undef NLIB_MPWRITER_SET_ERROR
141 #undef NLIB_MPWRITER_CHK_NSTREAM
143 #define NLIB_MPWRITE(tp, coerce) \
144 template<> inline bool MpWrite(MpWriter* obj, const tp& v) { \
145 return obj->Write(static_cast<coerce>(v)); \
148 NLIB_MPWRITE(
char, int32_t)
149 NLIB_MPWRITE(
unsigned char, uint32_t)
150 NLIB_MPWRITE(
int, int32_t)
151 NLIB_MPWRITE(
unsigned int, uint32_t)
152 NLIB_MPWRITE(
short, int32_t)
153 NLIB_MPWRITE(
unsigned short, uint32_t)
154 NLIB_MPWRITE(
long long, int64_t)
155 NLIB_MPWRITE(
unsigned long long, uint64_t)
161 template <
class T1,
class T2>
167 uint32_t n =
static_cast<uint32_t
>(
StrLen(str));
171 template <
class T,
class Alloc>
173 size_t n = vec.size();
175 typename std::vector<T, Alloc>::const_iterator it = vec.begin();
176 typename std::vector<T, Alloc>::const_iterator it_end = vec.end();
177 for (; it != it_end; ++it) {
178 if (!w->
Write(*it))
return false;
183 template <
class T,
class Alloc>
185 size_t n = vec.
size();
189 for (; it != it_end; ++it) {
190 if (!w->
Write(*it))
return false;
195 template <
class K,
class V,
class Pr,
class Alloc>
198 if (!w->
WriteMapCount(static_cast<uint32_t>(n)))
return false;
199 typename std::map<K, V, Pr, Alloc>::const_iterator it = m.begin();
200 typename std::map<K, V, Pr, Alloc>::const_iterator end = m.end();
201 for (; it != end; ++it) {
202 if (!w->
Write(it->first))
return false;
203 if (!w->
Write(it->second))
return false;
209 bool MpWriter::Write_(
const T& v) {
217 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
218 #undef NLIB_VIS_PUBLIC
219 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
222 #endif // INCLUDE_NN_NLIB_MSGPACK_MPWRITER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
bool Write(const char(&str)[n]) noexcept
See Write(const T (&vec)[n]).
#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...
~MpWriter() noexcept
Destructor.
OutputStream * GetStream() noexcept
Gets the base stream specified in Init.
Writes the MessagePack formatted data to the stream.
iterator begin() noexcept
Returns the iterator pointing to the beginning of the container.
bool WriteMapCount(uint32_t n) noexcept
Writes associative array header information.
BaseType::const_iterator const_iterator
Read-only forward iterator.
Object created when MessagePack or JSON is read.
bool Close() noexcept
Closes MpWriter.
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
bool WriteArrayCount(uint32_t n) noexcept
Writes array header information.
Defines the class that resembles std::vector but can store objects that cannot be copied...
bool WriteRawCount(uint32_t n) noexcept
Writes byte array data header information.
size_type size() const noexcept
Returns the number of stored elements.
MpWriter() noexcept
Instantiates the object with default parameters (default constructor).
Object created when MessagePack, JSON, or CSV is read.
bool WriteRawBody(const void *p, uint32_t n) noexcept
Writes byte array data body.
Defines the class for writing binary files to streams.
bool Write(const char *str) noexcept
Writes a text string.
void SetError(errno_t e) noexcept
Sets an error.
A container-like class similar to std::vector that can store objects that do not have copy constructo...
Wraps functions like strlen and strcpy so they can be safely used.
bool Flush() noexcept
Flushes the write to the stream.
bool MpWrite(MpWriter *w, const std::map< K, V, Pr, Alloc > &m)
Writes m via MpWriter.
size_t StrLen(const char *str) noexcept
Wraps the nlib_strlen function.
Defines the base class for output streams.
The base class for output streams. This class cannot be instantiated.
iterator end() noexcept
Returns the iterator pointing to the end of the container.
errno_t GetErrorValue() const noexcept
Gets the error value.