git repos / blockattack-game

blame: source/code/Libs/include/cereal/external/rapidjson/stream.h

normal view · raw

6c5f2c01 sago007 2017-03-15 17:56 1
// Tencent is pleased to support the open source community by making RapidJSON available.
8f94a7f5 sago007 2020-05-10 10:26 2
//
6c5f2c01 sago007 2017-03-15 17:56 3
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
6c5f2c01 sago007 2017-03-15 17:56 4
//
6c5f2c01 sago007 2017-03-15 17:56 5
// Licensed under the MIT License (the "License"); you may not use this file except
6c5f2c01 sago007 2017-03-15 17:56 6
// in compliance with the License. You may obtain a copy of the License at
6c5f2c01 sago007 2017-03-15 17:56 7
//
6c5f2c01 sago007 2017-03-15 17:56 8
// http://opensource.org/licenses/MIT
6c5f2c01 sago007 2017-03-15 17:56 9
//
8f94a7f5 sago007 2020-05-10 10:26 10
// Unless required by applicable law or agreed to in writing, software distributed
8f94a7f5 sago007 2020-05-10 10:26 11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8f94a7f5 sago007 2020-05-10 10:26 12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
6c5f2c01 sago007 2017-03-15 17:56 13
// specific language governing permissions and limitations under the License.
6c5f2c01 sago007 2017-03-15 17:56 14
6c5f2c01 sago007 2017-03-15 17:56 15
#include "rapidjson.h"
6c5f2c01 sago007 2017-03-15 17:56 16
6c5f2c01 sago007 2017-03-15 17:56 17
#ifndef CEREAL_RAPIDJSON_STREAM_H_
6c5f2c01 sago007 2017-03-15 17:56 18
#define CEREAL_RAPIDJSON_STREAM_H_
6c5f2c01 sago007 2017-03-15 17:56 19
6c5f2c01 sago007 2017-03-15 17:56 20
#include "encodings.h"
6c5f2c01 sago007 2017-03-15 17:56 21
6c5f2c01 sago007 2017-03-15 17:56 22
CEREAL_RAPIDJSON_NAMESPACE_BEGIN
6c5f2c01 sago007 2017-03-15 17:56 23
6c5f2c01 sago007 2017-03-15 17:56 24
///////////////////////////////////////////////////////////////////////////////
6c5f2c01 sago007 2017-03-15 17:56 25
//  Stream
6c5f2c01 sago007 2017-03-15 17:56 26
6c5f2c01 sago007 2017-03-15 17:56 27
/*! \class rapidjson::Stream
6c5f2c01 sago007 2017-03-15 17:56 28
    \brief Concept for reading and writing characters.
6c5f2c01 sago007 2017-03-15 17:56 29
6c5f2c01 sago007 2017-03-15 17:56 30
    For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd().
6c5f2c01 sago007 2017-03-15 17:56 31
6c5f2c01 sago007 2017-03-15 17:56 32
    For write-only stream, only need to implement Put() and Flush().
6c5f2c01 sago007 2017-03-15 17:56 33
6c5f2c01 sago007 2017-03-15 17:56 34
\code
6c5f2c01 sago007 2017-03-15 17:56 35
concept Stream {
6c5f2c01 sago007 2017-03-15 17:56 36
    typename Ch;    //!< Character type of the stream.
6c5f2c01 sago007 2017-03-15 17:56 37
6c5f2c01 sago007 2017-03-15 17:56 38
    //! Read the current character from stream without moving the read cursor.
6c5f2c01 sago007 2017-03-15 17:56 39
    Ch Peek() const;
6c5f2c01 sago007 2017-03-15 17:56 40
6c5f2c01 sago007 2017-03-15 17:56 41
    //! Read the current character from stream and moving the read cursor to next character.
6c5f2c01 sago007 2017-03-15 17:56 42
    Ch Take();
6c5f2c01 sago007 2017-03-15 17:56 43
6c5f2c01 sago007 2017-03-15 17:56 44
    //! Get the current read cursor.
6c5f2c01 sago007 2017-03-15 17:56 45
    //! \return Number of characters read from start.
6c5f2c01 sago007 2017-03-15 17:56 46
    size_t Tell();
6c5f2c01 sago007 2017-03-15 17:56 47
6c5f2c01 sago007 2017-03-15 17:56 48
    //! Begin writing operation at the current read pointer.
6c5f2c01 sago007 2017-03-15 17:56 49
    //! \return The begin writer pointer.
6c5f2c01 sago007 2017-03-15 17:56 50
    Ch* PutBegin();
6c5f2c01 sago007 2017-03-15 17:56 51
6c5f2c01 sago007 2017-03-15 17:56 52
    //! Write a character.
6c5f2c01 sago007 2017-03-15 17:56 53
    void Put(Ch c);
6c5f2c01 sago007 2017-03-15 17:56 54
6c5f2c01 sago007 2017-03-15 17:56 55
    //! Flush the buffer.
6c5f2c01 sago007 2017-03-15 17:56 56
    void Flush();
6c5f2c01 sago007 2017-03-15 17:56 57
6c5f2c01 sago007 2017-03-15 17:56 58
    //! End the writing operation.
6c5f2c01 sago007 2017-03-15 17:56 59
    //! \param begin The begin write pointer returned by PutBegin().
6c5f2c01 sago007 2017-03-15 17:56 60
    //! \return Number of characters written.
6c5f2c01 sago007 2017-03-15 17:56 61
    size_t PutEnd(Ch* begin);
6c5f2c01 sago007 2017-03-15 17:56 62
}
6c5f2c01 sago007 2017-03-15 17:56 63
\endcode
6c5f2c01 sago007 2017-03-15 17:56 64
*/
6c5f2c01 sago007 2017-03-15 17:56 65
6c5f2c01 sago007 2017-03-15 17:56 66
//! Provides additional information for stream.
6c5f2c01 sago007 2017-03-15 17:56 67
/*!
6c5f2c01 sago007 2017-03-15 17:56 68
    By using traits pattern, this type provides a default configuration for stream.
6c5f2c01 sago007 2017-03-15 17:56 69
    For custom stream, this type can be specialized for other configuration.
6c5f2c01 sago007 2017-03-15 17:56 70
    See TEST(Reader, CustomStringStream) in readertest.cpp for example.
6c5f2c01 sago007 2017-03-15 17:56 71
*/
6c5f2c01 sago007 2017-03-15 17:56 72
template<typename Stream>
6c5f2c01 sago007 2017-03-15 17:56 73
struct StreamTraits {
6c5f2c01 sago007 2017-03-15 17:56 74
    //! Whether to make local copy of stream for optimization during parsing.
6c5f2c01 sago007 2017-03-15 17:56 75
    /*!
6c5f2c01 sago007 2017-03-15 17:56 76
        By default, for safety, streams do not use local copy optimization.
6c5f2c01 sago007 2017-03-15 17:56 77
        Stream that can be copied fast should specialize this, like StreamTraits<StringStream>.
6c5f2c01 sago007 2017-03-15 17:56 78
    */
6c5f2c01 sago007 2017-03-15 17:56 79
    enum { copyOptimization = 0 };
6c5f2c01 sago007 2017-03-15 17:56 80
};
6c5f2c01 sago007 2017-03-15 17:56 81
6c5f2c01 sago007 2017-03-15 17:56 82
//! Reserve n characters for writing to a stream.
6c5f2c01 sago007 2017-03-15 17:56 83
template<typename Stream>
6c5f2c01 sago007 2017-03-15 17:56 84
inline void PutReserve(Stream& stream, size_t count) {
6c5f2c01 sago007 2017-03-15 17:56 85
    (void)stream;
6c5f2c01 sago007 2017-03-15 17:56 86
    (void)count;
6c5f2c01 sago007 2017-03-15 17:56 87
}
6c5f2c01 sago007 2017-03-15 17:56 88
6c5f2c01 sago007 2017-03-15 17:56 89
//! Write character to a stream, presuming buffer is reserved.
6c5f2c01 sago007 2017-03-15 17:56 90
template<typename Stream>
6c5f2c01 sago007 2017-03-15 17:56 91
inline void PutUnsafe(Stream& stream, typename Stream::Ch c) {
6c5f2c01 sago007 2017-03-15 17:56 92
    stream.Put(c);
6c5f2c01 sago007 2017-03-15 17:56 93
}
6c5f2c01 sago007 2017-03-15 17:56 94
6c5f2c01 sago007 2017-03-15 17:56 95
//! Put N copies of a character to a stream.
6c5f2c01 sago007 2017-03-15 17:56 96
template<typename Stream, typename Ch>
6c5f2c01 sago007 2017-03-15 17:56 97
inline void PutN(Stream& stream, Ch c, size_t n) {
6c5f2c01 sago007 2017-03-15 17:56 98
    PutReserve(stream, n);
6c5f2c01 sago007 2017-03-15 17:56 99
    for (size_t i = 0; i < n; i++)
6c5f2c01 sago007 2017-03-15 17:56 100
        PutUnsafe(stream, c);
6c5f2c01 sago007 2017-03-15 17:56 101
}
6c5f2c01 sago007 2017-03-15 17:56 102
6c5f2c01 sago007 2017-03-15 17:56 103
///////////////////////////////////////////////////////////////////////////////
8f94a7f5 sago007 2020-05-10 10:26 104
// GenericStreamWrapper
8f94a7f5 sago007 2020-05-10 10:26 105
8f94a7f5 sago007 2020-05-10 10:26 106
//! A Stream Wrapper
8f94a7f5 sago007 2020-05-10 10:26 107
/*! \tThis string stream is a wrapper for any stream by just forwarding any
8f94a7f5 sago007 2020-05-10 10:26 108
    \treceived message to the origin stream.
8f94a7f5 sago007 2020-05-10 10:26 109
    \note implements Stream concept
8f94a7f5 sago007 2020-05-10 10:26 110
*/
8f94a7f5 sago007 2020-05-10 10:26 111
8f94a7f5 sago007 2020-05-10 10:26 112
#if defined(_MSC_VER) && _MSC_VER <= 1800
8f94a7f5 sago007 2020-05-10 10:26 113
CEREAL_RAPIDJSON_DIAG_PUSH
8f94a7f5 sago007 2020-05-10 10:26 114
CEREAL_RAPIDJSON_DIAG_OFF(4702)  // unreachable code
8f94a7f5 sago007 2020-05-10 10:26 115
CEREAL_RAPIDJSON_DIAG_OFF(4512)  // assignment operator could not be generated
8f94a7f5 sago007 2020-05-10 10:26 116
#endif
8f94a7f5 sago007 2020-05-10 10:26 117
8f94a7f5 sago007 2020-05-10 10:26 118
template <typename InputStream, typename Encoding = UTF8<> >
8f94a7f5 sago007 2020-05-10 10:26 119
class GenericStreamWrapper {
8f94a7f5 sago007 2020-05-10 10:26 120
public:
8f94a7f5 sago007 2020-05-10 10:26 121
    typedef typename Encoding::Ch Ch;
8f94a7f5 sago007 2020-05-10 10:26 122
    GenericStreamWrapper(InputStream& is): is_(is) {}
8f94a7f5 sago007 2020-05-10 10:26 123
8f94a7f5 sago007 2020-05-10 10:26 124
    Ch Peek() const { return is_.Peek(); }
8f94a7f5 sago007 2020-05-10 10:26 125
    Ch Take() { return is_.Take(); }
8f94a7f5 sago007 2020-05-10 10:26 126
    size_t Tell() { return is_.Tell(); }
8f94a7f5 sago007 2020-05-10 10:26 127
    Ch* PutBegin() { return is_.PutBegin(); }
8f94a7f5 sago007 2020-05-10 10:26 128
    void Put(Ch ch) { is_.Put(ch); }
8f94a7f5 sago007 2020-05-10 10:26 129
    void Flush() { is_.Flush(); }
8f94a7f5 sago007 2020-05-10 10:26 130
    size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); }
8f94a7f5 sago007 2020-05-10 10:26 131
8f94a7f5 sago007 2020-05-10 10:26 132
    // wrapper for MemoryStream
8f94a7f5 sago007 2020-05-10 10:26 133
    const Ch* Peek4() const { return is_.Peek4(); }
8f94a7f5 sago007 2020-05-10 10:26 134
8f94a7f5 sago007 2020-05-10 10:26 135
    // wrapper for AutoUTFInputStream
8f94a7f5 sago007 2020-05-10 10:26 136
    UTFType GetType() const { return is_.GetType(); }
8f94a7f5 sago007 2020-05-10 10:26 137
    bool HasBOM() const { return is_.HasBOM(); }
8f94a7f5 sago007 2020-05-10 10:26 138
8f94a7f5 sago007 2020-05-10 10:26 139
protected:
8f94a7f5 sago007 2020-05-10 10:26 140
    InputStream& is_;
8f94a7f5 sago007 2020-05-10 10:26 141
};
8f94a7f5 sago007 2020-05-10 10:26 142
8f94a7f5 sago007 2020-05-10 10:26 143
#if defined(_MSC_VER) && _MSC_VER <= 1800
8f94a7f5 sago007 2020-05-10 10:26 144
CEREAL_RAPIDJSON_DIAG_POP
8f94a7f5 sago007 2020-05-10 10:26 145
#endif
8f94a7f5 sago007 2020-05-10 10:26 146
8f94a7f5 sago007 2020-05-10 10:26 147
///////////////////////////////////////////////////////////////////////////////
6c5f2c01 sago007 2017-03-15 17:56 148
// StringStream
6c5f2c01 sago007 2017-03-15 17:56 149
6c5f2c01 sago007 2017-03-15 17:56 150
//! Read-only string stream.
6c5f2c01 sago007 2017-03-15 17:56 151
/*! \note implements Stream concept
6c5f2c01 sago007 2017-03-15 17:56 152
*/
6c5f2c01 sago007 2017-03-15 17:56 153
template <typename Encoding>
6c5f2c01 sago007 2017-03-15 17:56 154
struct GenericStringStream {
6c5f2c01 sago007 2017-03-15 17:56 155
    typedef typename Encoding::Ch Ch;
6c5f2c01 sago007 2017-03-15 17:56 156
6c5f2c01 sago007 2017-03-15 17:56 157
    GenericStringStream(const Ch *src) : src_(src), head_(src) {}
6c5f2c01 sago007 2017-03-15 17:56 158
6c5f2c01 sago007 2017-03-15 17:56 159
    Ch Peek() const { return *src_; }
6c5f2c01 sago007 2017-03-15 17:56 160
    Ch Take() { return *src_++; }
6c5f2c01 sago007 2017-03-15 17:56 161
    size_t Tell() const { return static_cast<size_t>(src_ - head_); }
6c5f2c01 sago007 2017-03-15 17:56 162
6c5f2c01 sago007 2017-03-15 17:56 163
    Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 164
    void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); }
6c5f2c01 sago007 2017-03-15 17:56 165
    void Flush() { CEREAL_RAPIDJSON_ASSERT(false); }
6c5f2c01 sago007 2017-03-15 17:56 166
    size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 167
6c5f2c01 sago007 2017-03-15 17:56 168
    const Ch* src_;     //!< Current read position.
6c5f2c01 sago007 2017-03-15 17:56 169
    const Ch* head_;    //!< Original head of the string.
6c5f2c01 sago007 2017-03-15 17:56 170
};
6c5f2c01 sago007 2017-03-15 17:56 171
6c5f2c01 sago007 2017-03-15 17:56 172
template <typename Encoding>
6c5f2c01 sago007 2017-03-15 17:56 173
struct StreamTraits<GenericStringStream<Encoding> > {
6c5f2c01 sago007 2017-03-15 17:56 174
    enum { copyOptimization = 1 };
6c5f2c01 sago007 2017-03-15 17:56 175
};
6c5f2c01 sago007 2017-03-15 17:56 176
6c5f2c01 sago007 2017-03-15 17:56 177
//! String stream with UTF8 encoding.
6c5f2c01 sago007 2017-03-15 17:56 178
typedef GenericStringStream<UTF8<> > StringStream;
6c5f2c01 sago007 2017-03-15 17:56 179
6c5f2c01 sago007 2017-03-15 17:56 180
///////////////////////////////////////////////////////////////////////////////
6c5f2c01 sago007 2017-03-15 17:56 181
// InsituStringStream
6c5f2c01 sago007 2017-03-15 17:56 182
6c5f2c01 sago007 2017-03-15 17:56 183
//! A read-write string stream.
6c5f2c01 sago007 2017-03-15 17:56 184
/*! This string stream is particularly designed for in-situ parsing.
6c5f2c01 sago007 2017-03-15 17:56 185
    \note implements Stream concept
6c5f2c01 sago007 2017-03-15 17:56 186
*/
6c5f2c01 sago007 2017-03-15 17:56 187
template <typename Encoding>
6c5f2c01 sago007 2017-03-15 17:56 188
struct GenericInsituStringStream {
6c5f2c01 sago007 2017-03-15 17:56 189
    typedef typename Encoding::Ch Ch;
6c5f2c01 sago007 2017-03-15 17:56 190
6c5f2c01 sago007 2017-03-15 17:56 191
    GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
6c5f2c01 sago007 2017-03-15 17:56 192
6c5f2c01 sago007 2017-03-15 17:56 193
    // Read
6c5f2c01 sago007 2017-03-15 17:56 194
    Ch Peek() { return *src_; }
6c5f2c01 sago007 2017-03-15 17:56 195
    Ch Take() { return *src_++; }
6c5f2c01 sago007 2017-03-15 17:56 196
    size_t Tell() { return static_cast<size_t>(src_ - head_); }
6c5f2c01 sago007 2017-03-15 17:56 197
6c5f2c01 sago007 2017-03-15 17:56 198
    // Write
6c5f2c01 sago007 2017-03-15 17:56 199
    void Put(Ch c) { CEREAL_RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
6c5f2c01 sago007 2017-03-15 17:56 200
6c5f2c01 sago007 2017-03-15 17:56 201
    Ch* PutBegin() { return dst_ = src_; }
6c5f2c01 sago007 2017-03-15 17:56 202
    size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
6c5f2c01 sago007 2017-03-15 17:56 203
    void Flush() {}
6c5f2c01 sago007 2017-03-15 17:56 204
6c5f2c01 sago007 2017-03-15 17:56 205
    Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
6c5f2c01 sago007 2017-03-15 17:56 206
    void Pop(size_t count) { dst_ -= count; }
6c5f2c01 sago007 2017-03-15 17:56 207
6c5f2c01 sago007 2017-03-15 17:56 208
    Ch* src_;
6c5f2c01 sago007 2017-03-15 17:56 209
    Ch* dst_;
6c5f2c01 sago007 2017-03-15 17:56 210
    Ch* head_;
6c5f2c01 sago007 2017-03-15 17:56 211
};
6c5f2c01 sago007 2017-03-15 17:56 212
6c5f2c01 sago007 2017-03-15 17:56 213
template <typename Encoding>
6c5f2c01 sago007 2017-03-15 17:56 214
struct StreamTraits<GenericInsituStringStream<Encoding> > {
6c5f2c01 sago007 2017-03-15 17:56 215
    enum { copyOptimization = 1 };
6c5f2c01 sago007 2017-03-15 17:56 216
};
6c5f2c01 sago007 2017-03-15 17:56 217
6c5f2c01 sago007 2017-03-15 17:56 218
//! Insitu string stream with UTF8 encoding.
6c5f2c01 sago007 2017-03-15 17:56 219
typedef GenericInsituStringStream<UTF8<> > InsituStringStream;
6c5f2c01 sago007 2017-03-15 17:56 220
6c5f2c01 sago007 2017-03-15 17:56 221
CEREAL_RAPIDJSON_NAMESPACE_END
6c5f2c01 sago007 2017-03-15 17:56 222
6c5f2c01 sago007 2017-03-15 17:56 223
#endif // CEREAL_RAPIDJSON_STREAM_H_
1970-01-01 00:00 224