cphot 0.1
A C++ tool for computing photometry from spectra.
memorystream.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_MEMORYSTREAM_H_
16 #define RAPIDJSON_MEMORYSTREAM_H_
17 
18 #include "stream.h"
19 
20 #ifdef __clang__
21 RAPIDJSON_DIAG_PUSH
22 RAPIDJSON_DIAG_OFF(unreachable-code)
23 RAPIDJSON_DIAG_OFF(missing-noreturn)
24 #endif
25 
27 
28 //! Represents an in-memory input byte stream.
29 /*!
30  This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream.
31 
32  It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file.
33 
34  Differences between MemoryStream and StringStream:
35  1. StringStream has encoding but MemoryStream is a byte stream.
36  2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source.
37  3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4().
38  \note implements Stream concept
39 */
40 struct MemoryStream {
41  typedef char Ch; // byte
42 
43  MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
44 
45  Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; }
46  Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; }
47  size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
48 
49  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
50  void Put(Ch) { RAPIDJSON_ASSERT(false); }
51  void Flush() { RAPIDJSON_ASSERT(false); }
52  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
53 
54  // For encoding detection only.
55  const Ch* Peek4() const {
56  return Tell() + 4 <= size_ ? src_ : 0;
57  }
58 
59  const Ch* src_; //!< Current read position.
60  const Ch* begin_; //!< Original head of the string.
61  const Ch* end_; //!< End of stream.
62  size_t size_; //!< Size of the stream.
63 };
64 
66 
67 #ifdef __clang__
68 RAPIDJSON_DIAG_POP
69 #endif
70 
71 #endif // RAPIDJSON_MEMORYBUFFER_H_
MemoryStream::src_
const Ch * src_
Current read position.
Definition: memorystream.h:59
RAPIDJSON_NAMESPACE_END
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
RAPIDJSON_NAMESPACE_BEGIN
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
MemoryStream::size_
size_t size_
Size of the stream.
Definition: memorystream.h:62
MemoryStream::end_
const Ch * end_
End of stream.
Definition: memorystream.h:61
MemoryStream
Represents an in-memory input byte stream.
Definition: memorystream.h:40
MemoryStream::Put
void Put(Ch)
Definition: memorystream.h:50
MemoryStream::Tell
size_t Tell() const
Definition: memorystream.h:47
MemoryStream::Peek
Ch Peek() const
Definition: memorystream.h:45
RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
MemoryStream::PutEnd
size_t PutEnd(Ch *)
Definition: memorystream.h:52
stream.h
MemoryStream::Peek4
const Ch * Peek4() const
Definition: memorystream.h:55
MemoryStream::Ch
char Ch
Definition: memorystream.h:41
MemoryStream::begin_
const Ch * begin_
Original head of the string.
Definition: memorystream.h:60
MemoryStream::PutBegin
Ch * PutBegin()
Definition: memorystream.h:49
MemoryStream::Flush
void Flush()
Definition: memorystream.h:51
MemoryStream::MemoryStream
MemoryStream(const Ch *src, size_t size)
Definition: memorystream.h:43
MemoryStream::Take
Ch Take()
Definition: memorystream.h:46
RAPIDJSON_UNLIKELY
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:468