git repos / blockattack-game

blame: source/code/Libs/include/cereal/external/rapidjson/istreamwrapper.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_ISTREAMWRAPPER_H_
6c5f2c01 sago007 2017-03-15 17:56 16
#define CEREAL_RAPIDJSON_ISTREAMWRAPPER_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 <iosfwd>
6c5f2c01 sago007 2017-03-15 17:56 20
6c5f2c01 sago007 2017-03-15 17:56 21
#ifdef __clang__
6c5f2c01 sago007 2017-03-15 17:56 22
CEREAL_RAPIDJSON_DIAG_PUSH
6c5f2c01 sago007 2017-03-15 17:56 23
CEREAL_RAPIDJSON_DIAG_OFF(padded)
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 _MSC_VER
6c5f2c01 sago007 2017-03-15 17:56 27
CEREAL_RAPIDJSON_DIAG_PUSH
6c5f2c01 sago007 2017-03-15 17:56 28
CEREAL_RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
6c5f2c01 sago007 2017-03-15 17:56 29
CEREAL_RAPIDJSON_DIAG_OFF(4127) // ignore assert(false) for triggering exception
6c5f2c01 sago007 2017-03-15 17:56 30
#endif
6c5f2c01 sago007 2017-03-15 17:56 31
6c5f2c01 sago007 2017-03-15 17:56 32
CEREAL_RAPIDJSON_NAMESPACE_BEGIN
6c5f2c01 sago007 2017-03-15 17:56 33
6c5f2c01 sago007 2017-03-15 17:56 34
//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
6c5f2c01 sago007 2017-03-15 17:56 35
/*!
6c5f2c01 sago007 2017-03-15 17:56 36
    The classes can be wrapped including but not limited to:
6c5f2c01 sago007 2017-03-15 17:56 37
6c5f2c01 sago007 2017-03-15 17:56 38
    - \c std::istringstream
6c5f2c01 sago007 2017-03-15 17:56 39
    - \c std::stringstream
6c5f2c01 sago007 2017-03-15 17:56 40
    - \c std::wistringstream
6c5f2c01 sago007 2017-03-15 17:56 41
    - \c std::wstringstream
6c5f2c01 sago007 2017-03-15 17:56 42
    - \c std::ifstream
6c5f2c01 sago007 2017-03-15 17:56 43
    - \c std::fstream
6c5f2c01 sago007 2017-03-15 17:56 44
    - \c std::wifstream
6c5f2c01 sago007 2017-03-15 17:56 45
    - \c std::wfstream
6c5f2c01 sago007 2017-03-15 17:56 46
6c5f2c01 sago007 2017-03-15 17:56 47
    \tparam StreamType Class derived from \c std::basic_istream.
6c5f2c01 sago007 2017-03-15 17:56 48
*/
6c5f2c01 sago007 2017-03-15 17:56 49
6c5f2c01 sago007 2017-03-15 17:56 50
template <typename StreamType>
6c5f2c01 sago007 2017-03-15 17:56 51
class BasicIStreamWrapper {
6c5f2c01 sago007 2017-03-15 17:56 52
public:
6c5f2c01 sago007 2017-03-15 17:56 53
    typedef typename StreamType::char_type Ch;
6c5f2c01 sago007 2017-03-15 17:56 54
    BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
6c5f2c01 sago007 2017-03-15 17:56 55
6c5f2c01 sago007 2017-03-15 17:56 56
    Ch Peek() const {
6c5f2c01 sago007 2017-03-15 17:56 57
        typename StreamType::int_type c = stream_.peek();
6c5f2c01 sago007 2017-03-15 17:56 58
        return CEREAL_RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : '\0';
6c5f2c01 sago007 2017-03-15 17:56 59
    }
6c5f2c01 sago007 2017-03-15 17:56 60
6c5f2c01 sago007 2017-03-15 17:56 61
    Ch Take() {
6c5f2c01 sago007 2017-03-15 17:56 62
        typename StreamType::int_type c = stream_.get();
6c5f2c01 sago007 2017-03-15 17:56 63
        if (CEREAL_RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
6c5f2c01 sago007 2017-03-15 17:56 64
            count_++;
6c5f2c01 sago007 2017-03-15 17:56 65
            return static_cast<Ch>(c);
6c5f2c01 sago007 2017-03-15 17:56 66
        }
6c5f2c01 sago007 2017-03-15 17:56 67
        else
6c5f2c01 sago007 2017-03-15 17:56 68
            return '\0';
6c5f2c01 sago007 2017-03-15 17:56 69
    }
6c5f2c01 sago007 2017-03-15 17:56 70
6c5f2c01 sago007 2017-03-15 17:56 71
    // tellg() may return -1 when failed. So we count by ourself.
6c5f2c01 sago007 2017-03-15 17:56 72
    size_t Tell() const { return count_; }
6c5f2c01 sago007 2017-03-15 17:56 73
6c5f2c01 sago007 2017-03-15 17:56 74
    Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 75
    void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); }
6c5f2c01 sago007 2017-03-15 17:56 76
    void Flush() { CEREAL_RAPIDJSON_ASSERT(false); }
6c5f2c01 sago007 2017-03-15 17:56 77
    size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 78
6c5f2c01 sago007 2017-03-15 17:56 79
    // For encoding detection only.
6c5f2c01 sago007 2017-03-15 17:56 80
    const Ch* Peek4() const {
6c5f2c01 sago007 2017-03-15 17:56 81
        CEREAL_RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
6c5f2c01 sago007 2017-03-15 17:56 82
        int i;
6c5f2c01 sago007 2017-03-15 17:56 83
        bool hasError = false;
6c5f2c01 sago007 2017-03-15 17:56 84
        for (i = 0; i < 4; ++i) {
6c5f2c01 sago007 2017-03-15 17:56 85
            typename StreamType::int_type c = stream_.get();
6c5f2c01 sago007 2017-03-15 17:56 86
            if (c == StreamType::traits_type::eof()) {
6c5f2c01 sago007 2017-03-15 17:56 87
                hasError = true;
6c5f2c01 sago007 2017-03-15 17:56 88
                stream_.clear();
6c5f2c01 sago007 2017-03-15 17:56 89
                break;
6c5f2c01 sago007 2017-03-15 17:56 90
            }
6c5f2c01 sago007 2017-03-15 17:56 91
            peekBuffer_[i] = static_cast<Ch>(c);
6c5f2c01 sago007 2017-03-15 17:56 92
        }
6c5f2c01 sago007 2017-03-15 17:56 93
        for (--i; i >= 0; --i)
6c5f2c01 sago007 2017-03-15 17:56 94
            stream_.putback(peekBuffer_[i]);
6c5f2c01 sago007 2017-03-15 17:56 95
        return !hasError ? peekBuffer_ : 0;
6c5f2c01 sago007 2017-03-15 17:56 96
    }
6c5f2c01 sago007 2017-03-15 17:56 97
6c5f2c01 sago007 2017-03-15 17:56 98
private:
6c5f2c01 sago007 2017-03-15 17:56 99
    BasicIStreamWrapper(const BasicIStreamWrapper&);
6c5f2c01 sago007 2017-03-15 17:56 100
    BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
6c5f2c01 sago007 2017-03-15 17:56 101
6c5f2c01 sago007 2017-03-15 17:56 102
    StreamType& stream_;
6c5f2c01 sago007 2017-03-15 17:56 103
    size_t count_;  //!< Number of characters read. Note:
6c5f2c01 sago007 2017-03-15 17:56 104
    mutable Ch peekBuffer_[4];
6c5f2c01 sago007 2017-03-15 17:56 105
};
6c5f2c01 sago007 2017-03-15 17:56 106
6c5f2c01 sago007 2017-03-15 17:56 107
typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
6c5f2c01 sago007 2017-03-15 17:56 108
typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
6c5f2c01 sago007 2017-03-15 17:56 109
6c5f2c01 sago007 2017-03-15 17:56 110
#if defined(__clang__) || defined(_MSC_VER)
6c5f2c01 sago007 2017-03-15 17:56 111
CEREAL_RAPIDJSON_DIAG_POP
6c5f2c01 sago007 2017-03-15 17:56 112
#endif
6c5f2c01 sago007 2017-03-15 17:56 113
6c5f2c01 sago007 2017-03-15 17:56 114
CEREAL_RAPIDJSON_NAMESPACE_END
6c5f2c01 sago007 2017-03-15 17:56 115
6c5f2c01 sago007 2017-03-15 17:56 116
#endif // CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_
1970-01-01 00:00 117