git repos / blockattack-game

blame: source/code/Libs/include/cereal/external/rapidjson/encodedstream.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.
6c5f2c01 sago007 2017-03-15 17:56 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
//
6c5f2c01 sago007 2017-03-15 17:56 10
// Unless required by applicable law or agreed to in writing, software distributed 
6c5f2c01 sago007 2017-03-15 17:56 11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
6c5f2c01 sago007 2017-03-15 17:56 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
#ifndef CEREAL_RAPIDJSON_ENCODEDSTREAM_H_
6c5f2c01 sago007 2017-03-15 17:56 16
#define CEREAL_RAPIDJSON_ENCODEDSTREAM_H_
6c5f2c01 sago007 2017-03-15 17:56 17
6c5f2c01 sago007 2017-03-15 17:56 18
#include "stream.h"
6c5f2c01 sago007 2017-03-15 17:56 19
#include "memorystream.h"
6c5f2c01 sago007 2017-03-15 17:56 20
6c5f2c01 sago007 2017-03-15 17:56 21
#ifdef __GNUC__
6c5f2c01 sago007 2017-03-15 17:56 22
CEREAL_RAPIDJSON_DIAG_PUSH
6c5f2c01 sago007 2017-03-15 17:56 23
CEREAL_RAPIDJSON_DIAG_OFF(effc++)
6c5f2c01 sago007 2017-03-15 17:56 24
#endif
6c5f2c01 sago007 2017-03-15 17:56 25
6c5f2c01 sago007 2017-03-15 17:56 26
#ifdef __clang__
6c5f2c01 sago007 2017-03-15 17:56 27
CEREAL_RAPIDJSON_DIAG_PUSH
6c5f2c01 sago007 2017-03-15 17:56 28
CEREAL_RAPIDJSON_DIAG_OFF(padded)
6c5f2c01 sago007 2017-03-15 17:56 29
#endif
6c5f2c01 sago007 2017-03-15 17:56 30
6c5f2c01 sago007 2017-03-15 17:56 31
CEREAL_RAPIDJSON_NAMESPACE_BEGIN
6c5f2c01 sago007 2017-03-15 17:56 32
6c5f2c01 sago007 2017-03-15 17:56 33
//! Input byte stream wrapper with a statically bound encoding.
6c5f2c01 sago007 2017-03-15 17:56 34
/*!
6c5f2c01 sago007 2017-03-15 17:56 35
    \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
6c5f2c01 sago007 2017-03-15 17:56 36
    \tparam InputByteStream Type of input byte stream. For example, FileReadStream.
6c5f2c01 sago007 2017-03-15 17:56 37
*/
6c5f2c01 sago007 2017-03-15 17:56 38
template <typename Encoding, typename InputByteStream>
6c5f2c01 sago007 2017-03-15 17:56 39
class EncodedInputStream {
6c5f2c01 sago007 2017-03-15 17:56 40
    CEREAL_RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
6c5f2c01 sago007 2017-03-15 17:56 41
public:
6c5f2c01 sago007 2017-03-15 17:56 42
    typedef typename Encoding::Ch Ch;
6c5f2c01 sago007 2017-03-15 17:56 43
6c5f2c01 sago007 2017-03-15 17:56 44
    EncodedInputStream(InputByteStream& is) : is_(is) { 
6c5f2c01 sago007 2017-03-15 17:56 45
        current_ = Encoding::TakeBOM(is_);
6c5f2c01 sago007 2017-03-15 17:56 46
    }
6c5f2c01 sago007 2017-03-15 17:56 47
6c5f2c01 sago007 2017-03-15 17:56 48
    Ch Peek() const { return current_; }
6c5f2c01 sago007 2017-03-15 17:56 49
    Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; }
6c5f2c01 sago007 2017-03-15 17:56 50
    size_t Tell() const { return is_.Tell(); }
6c5f2c01 sago007 2017-03-15 17:56 51
6c5f2c01 sago007 2017-03-15 17:56 52
    // Not implemented
6c5f2c01 sago007 2017-03-15 17:56 53
    void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); }
6c5f2c01 sago007 2017-03-15 17:56 54
    void Flush() { CEREAL_RAPIDJSON_ASSERT(false); } 
6c5f2c01 sago007 2017-03-15 17:56 55
    Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 56
    size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 57
6c5f2c01 sago007 2017-03-15 17:56 58
private:
6c5f2c01 sago007 2017-03-15 17:56 59
    EncodedInputStream(const EncodedInputStream&);
6c5f2c01 sago007 2017-03-15 17:56 60
    EncodedInputStream& operator=(const EncodedInputStream&);
6c5f2c01 sago007 2017-03-15 17:56 61
6c5f2c01 sago007 2017-03-15 17:56 62
    InputByteStream& is_;
6c5f2c01 sago007 2017-03-15 17:56 63
    Ch current_;
6c5f2c01 sago007 2017-03-15 17:56 64
};
6c5f2c01 sago007 2017-03-15 17:56 65
6c5f2c01 sago007 2017-03-15 17:56 66
//! Specialized for UTF8 MemoryStream.
6c5f2c01 sago007 2017-03-15 17:56 67
template <>
6c5f2c01 sago007 2017-03-15 17:56 68
class EncodedInputStream<UTF8<>, MemoryStream> {
6c5f2c01 sago007 2017-03-15 17:56 69
public:
6c5f2c01 sago007 2017-03-15 17:56 70
    typedef UTF8<>::Ch Ch;
6c5f2c01 sago007 2017-03-15 17:56 71
6c5f2c01 sago007 2017-03-15 17:56 72
    EncodedInputStream(MemoryStream& is) : is_(is) {
6c5f2c01 sago007 2017-03-15 17:56 73
        if (static_cast<unsigned char>(is_.Peek()) == 0xEFu) is_.Take();
6c5f2c01 sago007 2017-03-15 17:56 74
        if (static_cast<unsigned char>(is_.Peek()) == 0xBBu) is_.Take();
6c5f2c01 sago007 2017-03-15 17:56 75
        if (static_cast<unsigned char>(is_.Peek()) == 0xBFu) is_.Take();
6c5f2c01 sago007 2017-03-15 17:56 76
    }
6c5f2c01 sago007 2017-03-15 17:56 77
    Ch Peek() const { return is_.Peek(); }
6c5f2c01 sago007 2017-03-15 17:56 78
    Ch Take() { return is_.Take(); }
6c5f2c01 sago007 2017-03-15 17:56 79
    size_t Tell() const { return is_.Tell(); }
6c5f2c01 sago007 2017-03-15 17:56 80
6c5f2c01 sago007 2017-03-15 17:56 81
    // Not implemented
6c5f2c01 sago007 2017-03-15 17:56 82
    void Put(Ch) {}
6c5f2c01 sago007 2017-03-15 17:56 83
    void Flush() {} 
6c5f2c01 sago007 2017-03-15 17:56 84
    Ch* PutBegin() { return 0; }
6c5f2c01 sago007 2017-03-15 17:56 85
    size_t PutEnd(Ch*) { return 0; }
6c5f2c01 sago007 2017-03-15 17:56 86
6c5f2c01 sago007 2017-03-15 17:56 87
    MemoryStream& is_;
6c5f2c01 sago007 2017-03-15 17:56 88
6c5f2c01 sago007 2017-03-15 17:56 89
private:
6c5f2c01 sago007 2017-03-15 17:56 90
    EncodedInputStream(const EncodedInputStream&);
6c5f2c01 sago007 2017-03-15 17:56 91
    EncodedInputStream& operator=(const EncodedInputStream&);
6c5f2c01 sago007 2017-03-15 17:56 92
};
6c5f2c01 sago007 2017-03-15 17:56 93
6c5f2c01 sago007 2017-03-15 17:56 94
//! Output byte stream wrapper with statically bound encoding.
6c5f2c01 sago007 2017-03-15 17:56 95
/*!
6c5f2c01 sago007 2017-03-15 17:56 96
    \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
6c5f2c01 sago007 2017-03-15 17:56 97
    \tparam OutputByteStream Type of input byte stream. For example, FileWriteStream.
6c5f2c01 sago007 2017-03-15 17:56 98
*/
6c5f2c01 sago007 2017-03-15 17:56 99
template <typename Encoding, typename OutputByteStream>
6c5f2c01 sago007 2017-03-15 17:56 100
class EncodedOutputStream {
6c5f2c01 sago007 2017-03-15 17:56 101
    CEREAL_RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
6c5f2c01 sago007 2017-03-15 17:56 102
public:
6c5f2c01 sago007 2017-03-15 17:56 103
    typedef typename Encoding::Ch Ch;
6c5f2c01 sago007 2017-03-15 17:56 104
6c5f2c01 sago007 2017-03-15 17:56 105
    EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { 
6c5f2c01 sago007 2017-03-15 17:56 106
        if (putBOM)
6c5f2c01 sago007 2017-03-15 17:56 107
            Encoding::PutBOM(os_);
6c5f2c01 sago007 2017-03-15 17:56 108
    }
6c5f2c01 sago007 2017-03-15 17:56 109
6c5f2c01 sago007 2017-03-15 17:56 110
    void Put(Ch c) { Encoding::Put(os_, c);  }
6c5f2c01 sago007 2017-03-15 17:56 111
    void Flush() { os_.Flush(); }
6c5f2c01 sago007 2017-03-15 17:56 112
6c5f2c01 sago007 2017-03-15 17:56 113
    // Not implemented
6c5f2c01 sago007 2017-03-15 17:56 114
    Ch Peek() const { CEREAL_RAPIDJSON_ASSERT(false); return 0;}
6c5f2c01 sago007 2017-03-15 17:56 115
    Ch Take() { CEREAL_RAPIDJSON_ASSERT(false); return 0;}
6c5f2c01 sago007 2017-03-15 17:56 116
    size_t Tell() const { CEREAL_RAPIDJSON_ASSERT(false);  return 0; }
6c5f2c01 sago007 2017-03-15 17:56 117
    Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 118
    size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 119
6c5f2c01 sago007 2017-03-15 17:56 120
private:
6c5f2c01 sago007 2017-03-15 17:56 121
    EncodedOutputStream(const EncodedOutputStream&);
6c5f2c01 sago007 2017-03-15 17:56 122
    EncodedOutputStream& operator=(const EncodedOutputStream&);
6c5f2c01 sago007 2017-03-15 17:56 123
6c5f2c01 sago007 2017-03-15 17:56 124
    OutputByteStream& os_;
6c5f2c01 sago007 2017-03-15 17:56 125
};
6c5f2c01 sago007 2017-03-15 17:56 126
6c5f2c01 sago007 2017-03-15 17:56 127
#define CEREAL_RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
6c5f2c01 sago007 2017-03-15 17:56 128
6c5f2c01 sago007 2017-03-15 17:56 129
//! Input stream wrapper with dynamically bound encoding and automatic encoding detection.
6c5f2c01 sago007 2017-03-15 17:56 130
/*!
6c5f2c01 sago007 2017-03-15 17:56 131
    \tparam CharType Type of character for reading.
6c5f2c01 sago007 2017-03-15 17:56 132
    \tparam InputByteStream type of input byte stream to be wrapped.
6c5f2c01 sago007 2017-03-15 17:56 133
*/
6c5f2c01 sago007 2017-03-15 17:56 134
template <typename CharType, typename InputByteStream>
6c5f2c01 sago007 2017-03-15 17:56 135
class AutoUTFInputStream {
6c5f2c01 sago007 2017-03-15 17:56 136
    CEREAL_RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
6c5f2c01 sago007 2017-03-15 17:56 137
public:
6c5f2c01 sago007 2017-03-15 17:56 138
    typedef CharType Ch;
6c5f2c01 sago007 2017-03-15 17:56 139
6c5f2c01 sago007 2017-03-15 17:56 140
    //! Constructor.
6c5f2c01 sago007 2017-03-15 17:56 141
    /*!
6c5f2c01 sago007 2017-03-15 17:56 142
        \param is input stream to be wrapped.
6c5f2c01 sago007 2017-03-15 17:56 143
        \param type UTF encoding type if it is not detected from the stream.
6c5f2c01 sago007 2017-03-15 17:56 144
    */
6c5f2c01 sago007 2017-03-15 17:56 145
    AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) {
6c5f2c01 sago007 2017-03-15 17:56 146
        CEREAL_RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);        
6c5f2c01 sago007 2017-03-15 17:56 147
        DetectType();
6c5f2c01 sago007 2017-03-15 17:56 148
        static const TakeFunc f[] = { CEREAL_RAPIDJSON_ENCODINGS_FUNC(Take) };
6c5f2c01 sago007 2017-03-15 17:56 149
        takeFunc_ = f[type_];
6c5f2c01 sago007 2017-03-15 17:56 150
        current_ = takeFunc_(*is_);
6c5f2c01 sago007 2017-03-15 17:56 151
    }
6c5f2c01 sago007 2017-03-15 17:56 152
6c5f2c01 sago007 2017-03-15 17:56 153
    UTFType GetType() const { return type_; }
6c5f2c01 sago007 2017-03-15 17:56 154
    bool HasBOM() const { return hasBOM_; }
6c5f2c01 sago007 2017-03-15 17:56 155
6c5f2c01 sago007 2017-03-15 17:56 156
    Ch Peek() const { return current_; }
6c5f2c01 sago007 2017-03-15 17:56 157
    Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; }
6c5f2c01 sago007 2017-03-15 17:56 158
    size_t Tell() const { return is_->Tell(); }
6c5f2c01 sago007 2017-03-15 17:56 159
6c5f2c01 sago007 2017-03-15 17:56 160
    // Not implemented
6c5f2c01 sago007 2017-03-15 17:56 161
    void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); }
6c5f2c01 sago007 2017-03-15 17:56 162
    void Flush() { CEREAL_RAPIDJSON_ASSERT(false); } 
6c5f2c01 sago007 2017-03-15 17:56 163
    Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 164
    size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 165
6c5f2c01 sago007 2017-03-15 17:56 166
private:
6c5f2c01 sago007 2017-03-15 17:56 167
    AutoUTFInputStream(const AutoUTFInputStream&);
6c5f2c01 sago007 2017-03-15 17:56 168
    AutoUTFInputStream& operator=(const AutoUTFInputStream&);
6c5f2c01 sago007 2017-03-15 17:56 169
6c5f2c01 sago007 2017-03-15 17:56 170
    // Detect encoding type with BOM or RFC 4627
6c5f2c01 sago007 2017-03-15 17:56 171
    void DetectType() {
6c5f2c01 sago007 2017-03-15 17:56 172
        // BOM (Byte Order Mark):
6c5f2c01 sago007 2017-03-15 17:56 173
        // 00 00 FE FF  UTF-32BE
6c5f2c01 sago007 2017-03-15 17:56 174
        // FF FE 00 00  UTF-32LE
6c5f2c01 sago007 2017-03-15 17:56 175
        // FE FF        UTF-16BE
6c5f2c01 sago007 2017-03-15 17:56 176
        // FF FE        UTF-16LE
6c5f2c01 sago007 2017-03-15 17:56 177
        // EF BB BF     UTF-8
6c5f2c01 sago007 2017-03-15 17:56 178
6c5f2c01 sago007 2017-03-15 17:56 179
        const unsigned char* c = reinterpret_cast<const unsigned char *>(is_->Peek4());
6c5f2c01 sago007 2017-03-15 17:56 180
        if (!c)
6c5f2c01 sago007 2017-03-15 17:56 181
            return;
6c5f2c01 sago007 2017-03-15 17:56 182
6c5f2c01 sago007 2017-03-15 17:56 183
        unsigned bom = static_cast<unsigned>(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
6c5f2c01 sago007 2017-03-15 17:56 184
        hasBOM_ = false;
6c5f2c01 sago007 2017-03-15 17:56 185
        if (bom == 0xFFFE0000)                  { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
6c5f2c01 sago007 2017-03-15 17:56 186
        else if (bom == 0x0000FEFF)             { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
6c5f2c01 sago007 2017-03-15 17:56 187
        else if ((bom & 0xFFFF) == 0xFFFE)      { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take();                           }
6c5f2c01 sago007 2017-03-15 17:56 188
        else if ((bom & 0xFFFF) == 0xFEFF)      { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take();                           }
6c5f2c01 sago007 2017-03-15 17:56 189
        else if ((bom & 0xFFFFFF) == 0xBFBBEF)  { type_ = kUTF8;    hasBOM_ = true; is_->Take(); is_->Take(); is_->Take();              }
6c5f2c01 sago007 2017-03-15 17:56 190
6c5f2c01 sago007 2017-03-15 17:56 191
        // RFC 4627: Section 3
6c5f2c01 sago007 2017-03-15 17:56 192
        // "Since the first two characters of a JSON text will always be ASCII
6c5f2c01 sago007 2017-03-15 17:56 193
        // characters [RFC0020], it is possible to determine whether an octet
6c5f2c01 sago007 2017-03-15 17:56 194
        // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
6c5f2c01 sago007 2017-03-15 17:56 195
        // at the pattern of nulls in the first four octets."
6c5f2c01 sago007 2017-03-15 17:56 196
        // 00 00 00 xx  UTF-32BE
6c5f2c01 sago007 2017-03-15 17:56 197
        // 00 xx 00 xx  UTF-16BE
6c5f2c01 sago007 2017-03-15 17:56 198
        // xx 00 00 00  UTF-32LE
6c5f2c01 sago007 2017-03-15 17:56 199
        // xx 00 xx 00  UTF-16LE
6c5f2c01 sago007 2017-03-15 17:56 200
        // xx xx xx xx  UTF-8
6c5f2c01 sago007 2017-03-15 17:56 201
6c5f2c01 sago007 2017-03-15 17:56 202
        if (!hasBOM_) {
8f94a7f5 sago007 2020-05-10 10:26 203
            int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
6c5f2c01 sago007 2017-03-15 17:56 204
            switch (pattern) {
6c5f2c01 sago007 2017-03-15 17:56 205
            case 0x08: type_ = kUTF32BE; break;
6c5f2c01 sago007 2017-03-15 17:56 206
            case 0x0A: type_ = kUTF16BE; break;
6c5f2c01 sago007 2017-03-15 17:56 207
            case 0x01: type_ = kUTF32LE; break;
6c5f2c01 sago007 2017-03-15 17:56 208
            case 0x05: type_ = kUTF16LE; break;
6c5f2c01 sago007 2017-03-15 17:56 209
            case 0x0F: type_ = kUTF8;    break;
6c5f2c01 sago007 2017-03-15 17:56 210
            default: break; // Use type defined by user.
6c5f2c01 sago007 2017-03-15 17:56 211
            }
6c5f2c01 sago007 2017-03-15 17:56 212
        }
6c5f2c01 sago007 2017-03-15 17:56 213
6c5f2c01 sago007 2017-03-15 17:56 214
        // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
6c5f2c01 sago007 2017-03-15 17:56 215
        if (type_ == kUTF16LE || type_ == kUTF16BE) CEREAL_RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
6c5f2c01 sago007 2017-03-15 17:56 216
        if (type_ == kUTF32LE || type_ == kUTF32BE) CEREAL_RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
6c5f2c01 sago007 2017-03-15 17:56 217
    }
6c5f2c01 sago007 2017-03-15 17:56 218
6c5f2c01 sago007 2017-03-15 17:56 219
    typedef Ch (*TakeFunc)(InputByteStream& is);
6c5f2c01 sago007 2017-03-15 17:56 220
    InputByteStream* is_;
6c5f2c01 sago007 2017-03-15 17:56 221
    UTFType type_;
6c5f2c01 sago007 2017-03-15 17:56 222
    Ch current_;
6c5f2c01 sago007 2017-03-15 17:56 223
    TakeFunc takeFunc_;
6c5f2c01 sago007 2017-03-15 17:56 224
    bool hasBOM_;
6c5f2c01 sago007 2017-03-15 17:56 225
};
6c5f2c01 sago007 2017-03-15 17:56 226
6c5f2c01 sago007 2017-03-15 17:56 227
//! Output stream wrapper with dynamically bound encoding and automatic encoding detection.
6c5f2c01 sago007 2017-03-15 17:56 228
/*!
6c5f2c01 sago007 2017-03-15 17:56 229
    \tparam CharType Type of character for writing.
6c5f2c01 sago007 2017-03-15 17:56 230
    \tparam OutputByteStream type of output byte stream to be wrapped.
6c5f2c01 sago007 2017-03-15 17:56 231
*/
6c5f2c01 sago007 2017-03-15 17:56 232
template <typename CharType, typename OutputByteStream>
6c5f2c01 sago007 2017-03-15 17:56 233
class AutoUTFOutputStream {
6c5f2c01 sago007 2017-03-15 17:56 234
    CEREAL_RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
6c5f2c01 sago007 2017-03-15 17:56 235
public:
6c5f2c01 sago007 2017-03-15 17:56 236
    typedef CharType Ch;
6c5f2c01 sago007 2017-03-15 17:56 237
6c5f2c01 sago007 2017-03-15 17:56 238
    //! Constructor.
6c5f2c01 sago007 2017-03-15 17:56 239
    /*!
6c5f2c01 sago007 2017-03-15 17:56 240
        \param os output stream to be wrapped.
6c5f2c01 sago007 2017-03-15 17:56 241
        \param type UTF encoding type.
6c5f2c01 sago007 2017-03-15 17:56 242
        \param putBOM Whether to write BOM at the beginning of the stream.
6c5f2c01 sago007 2017-03-15 17:56 243
    */
6c5f2c01 sago007 2017-03-15 17:56 244
    AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) {
6c5f2c01 sago007 2017-03-15 17:56 245
        CEREAL_RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);
6c5f2c01 sago007 2017-03-15 17:56 246
6c5f2c01 sago007 2017-03-15 17:56 247
        // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
6c5f2c01 sago007 2017-03-15 17:56 248
        if (type_ == kUTF16LE || type_ == kUTF16BE) CEREAL_RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
6c5f2c01 sago007 2017-03-15 17:56 249
        if (type_ == kUTF32LE || type_ == kUTF32BE) CEREAL_RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
6c5f2c01 sago007 2017-03-15 17:56 250
6c5f2c01 sago007 2017-03-15 17:56 251
        static const PutFunc f[] = { CEREAL_RAPIDJSON_ENCODINGS_FUNC(Put) };
6c5f2c01 sago007 2017-03-15 17:56 252
        putFunc_ = f[type_];
6c5f2c01 sago007 2017-03-15 17:56 253
6c5f2c01 sago007 2017-03-15 17:56 254
        if (putBOM)
6c5f2c01 sago007 2017-03-15 17:56 255
            PutBOM();
6c5f2c01 sago007 2017-03-15 17:56 256
    }
6c5f2c01 sago007 2017-03-15 17:56 257
6c5f2c01 sago007 2017-03-15 17:56 258
    UTFType GetType() const { return type_; }
6c5f2c01 sago007 2017-03-15 17:56 259
6c5f2c01 sago007 2017-03-15 17:56 260
    void Put(Ch c) { putFunc_(*os_, c); }
6c5f2c01 sago007 2017-03-15 17:56 261
    void Flush() { os_->Flush(); } 
6c5f2c01 sago007 2017-03-15 17:56 262
6c5f2c01 sago007 2017-03-15 17:56 263
    // Not implemented
6c5f2c01 sago007 2017-03-15 17:56 264
    Ch Peek() const { CEREAL_RAPIDJSON_ASSERT(false); return 0;}
6c5f2c01 sago007 2017-03-15 17:56 265
    Ch Take() { CEREAL_RAPIDJSON_ASSERT(false); return 0;}
6c5f2c01 sago007 2017-03-15 17:56 266
    size_t Tell() const { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 267
    Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 268
    size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 269
6c5f2c01 sago007 2017-03-15 17:56 270
private:
6c5f2c01 sago007 2017-03-15 17:56 271
    AutoUTFOutputStream(const AutoUTFOutputStream&);
6c5f2c01 sago007 2017-03-15 17:56 272
    AutoUTFOutputStream& operator=(const AutoUTFOutputStream&);
6c5f2c01 sago007 2017-03-15 17:56 273
6c5f2c01 sago007 2017-03-15 17:56 274
    void PutBOM() { 
6c5f2c01 sago007 2017-03-15 17:56 275
        typedef void (*PutBOMFunc)(OutputByteStream&);
6c5f2c01 sago007 2017-03-15 17:56 276
        static const PutBOMFunc f[] = { CEREAL_RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
6c5f2c01 sago007 2017-03-15 17:56 277
        f[type_](*os_);
6c5f2c01 sago007 2017-03-15 17:56 278
    }
6c5f2c01 sago007 2017-03-15 17:56 279
6c5f2c01 sago007 2017-03-15 17:56 280
    typedef void (*PutFunc)(OutputByteStream&, Ch);
6c5f2c01 sago007 2017-03-15 17:56 281
6c5f2c01 sago007 2017-03-15 17:56 282
    OutputByteStream* os_;
6c5f2c01 sago007 2017-03-15 17:56 283
    UTFType type_;
6c5f2c01 sago007 2017-03-15 17:56 284
    PutFunc putFunc_;
6c5f2c01 sago007 2017-03-15 17:56 285
};
6c5f2c01 sago007 2017-03-15 17:56 286
6c5f2c01 sago007 2017-03-15 17:56 287
#undef CEREAL_RAPIDJSON_ENCODINGS_FUNC
6c5f2c01 sago007 2017-03-15 17:56 288
6c5f2c01 sago007 2017-03-15 17:56 289
CEREAL_RAPIDJSON_NAMESPACE_END
6c5f2c01 sago007 2017-03-15 17:56 290
6c5f2c01 sago007 2017-03-15 17:56 291
#ifdef __clang__
6c5f2c01 sago007 2017-03-15 17:56 292
CEREAL_RAPIDJSON_DIAG_POP
6c5f2c01 sago007 2017-03-15 17:56 293
#endif
6c5f2c01 sago007 2017-03-15 17:56 294
6c5f2c01 sago007 2017-03-15 17:56 295
#ifdef __GNUC__
6c5f2c01 sago007 2017-03-15 17:56 296
CEREAL_RAPIDJSON_DIAG_POP
6c5f2c01 sago007 2017-03-15 17:56 297
#endif
6c5f2c01 sago007 2017-03-15 17:56 298
6c5f2c01 sago007 2017-03-15 17:56 299
#endif // CEREAL_RAPIDJSON_FILESTREAM_H_
1970-01-01 00:00 300