Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/stream.h
1 /* 2 * Copyright 2016 WebAssembly Community Group participants 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WABT_STREAM_H_ 18 #define WABT_STREAM_H_ 19 20 #include <cassert> 21 #include <memory> 22 #include <vector> 23 24 #include "wabt/common.h" 25 26 namespace wabt { 27 28 /* whether to display the ASCII characters in the debug output for 29 * write_memory */ 30 enum class PrintChars { 31 No = 0, 32 Yes = 1, 33 }; 34 35 class Stream { 36 public: 37 explicit Stream(Stream* log_stream = nullptr); 38 virtual ~Stream() = default; 39 40 size_t offset() { return offset_; } 41 Result result() { return result_; } 42 43 void set_log_stream(Stream* stream) { 44 assert(stream); 45 log_stream_ = stream; 46 } 47 48 Stream& log_stream() { 49 assert(log_stream_); 50 return *log_stream_; 51 } 52 53 bool has_log_stream() const { return log_stream_ != nullptr; } 54 55 void ClearOffset() { offset_ = 0; } 56 void AddOffset(ssize_t delta); 57 58 void WriteData(const void* src, 59 size_t size, 60 const char* desc = nullptr, 61 PrintChars = PrintChars::No); 62 63 template <typename T> 64 void WriteData(const std::vector<T> src, 65 const char* desc, 66 PrintChars print_chars = PrintChars::No) { 67 if (!src.empty()) { 68 WriteData(src.data(), src.size() * sizeof(T), desc, print_chars); 69 } 70 } 71 72 void WriteDataAt(size_t offset, 73 const void* src, 74 size_t size, 75 const char* desc = nullptr, 76 PrintChars = PrintChars::No); 77 78 void MoveData(size_t dst_offset, size_t src_offset, size_t size); 79 80 void Truncate(size_t size); 81 82 void WABT_PRINTF_FORMAT(2, 3) Writef(const char* format, ...); 83 84 // Specified as uint32_t instead of uint8_t so we can check if the value 85 // given is in range before wrapping. 86 void WriteU8(uint32_t value, 87 const char* desc = nullptr, 88 PrintChars print_chars = PrintChars::No) { 89 assert(value <= UINT8_MAX); 90 Write(static_cast<uint8_t>(value), desc, print_chars); 91 } 92 void WriteU32(uint32_t value, 93 const char* desc = nullptr, 94 PrintChars print_chars = PrintChars::No) { 95 Write(value, desc, print_chars); 96 } 97 void WriteU64(uint64_t value, 98 const char* desc = nullptr, 99 PrintChars print_chars = PrintChars::No) { 100 Write(value, desc, print_chars); 101 } 102 void WriteU128(v128 value, 103 const char* desc = nullptr, 104 PrintChars print_chars = PrintChars::No) { 105 Write(value, desc, print_chars); 106 } 107 108 void WriteChar(char c, 109 const char* desc = nullptr, 110 PrintChars print_chars = PrintChars::No) { 111 WriteU8(static_cast<unsigned char>(c), desc, print_chars); 112 } 113 114 // Dump memory as text, similar to the xxd format. 115 void WriteMemoryDump(const void* start, 116 size_t size, 117 size_t offset = 0, 118 PrintChars print_chars = PrintChars::No, 119 const char* prefix = nullptr, 120 const char* desc = nullptr); 121 122 // Convenience functions for writing enums. 123 template <typename T> 124 void WriteU8Enum(T value, 125 const char* desc = nullptr, 126 PrintChars print_chars = PrintChars::No) { 127 WriteU8(static_cast<uint32_t>(value), desc, print_chars); 128 } 129 130 virtual void Flush() {} 131 132 protected: 133 virtual Result WriteDataImpl(size_t offset, 134 const void* data, 135 size_t size) = 0; 136 virtual Result MoveDataImpl(size_t dst_offset, 137 size_t src_offset, 138 size_t size) = 0; 139 virtual Result TruncateImpl(size_t size) = 0; 140 141 private: 142 template <typename T> 143 void Write(const T& data, const char* desc, PrintChars print_chars) { 144 #if WABT_BIG_ENDIAN 145 char tmp[sizeof(T)]; 146 memcpy(tmp, &data, sizeof(tmp)); 147 SwapBytesSized(tmp, sizeof(tmp)); 148 WriteData(tmp, sizeof(tmp), desc, print_chars); 149 #else 150 WriteData(&data, sizeof(data), desc, print_chars); 151 #endif 152 } 153 154 size_t offset_; 155 Result result_; 156 // Not owned. If non-null, log all writes to this stream. 157 Stream* log_stream_; 158 }; 159 160 struct OutputBuffer { 161 Result WriteToFile(std::string_view filename) const; 162 Result WriteToStdout() const; 163 164 void clear() { data.clear(); } 165 size_t size() const { return data.size(); } 166 167 std::vector<uint8_t> data; 168 }; 169 170 class MemoryStream : public Stream { 171 public: 172 WABT_DISALLOW_COPY_AND_ASSIGN(MemoryStream); 173 MemoryStream(MemoryStream&&) = default; 174 explicit MemoryStream(Stream* log_stream = nullptr); 175 explicit MemoryStream(std::unique_ptr<OutputBuffer>&&, 176 Stream* log_stream = nullptr); 177 178 OutputBuffer& output_buffer() { return *buf_; } 179 std::unique_ptr<OutputBuffer> ReleaseOutputBuffer(); 180 181 void Clear(); 182 183 Result WriteToFile(std::string_view filename) { 184 return buf_->WriteToFile(filename); 185 } 186 187 protected: 188 Result WriteDataImpl(size_t offset, const void* data, size_t size) override; 189 Result MoveDataImpl(size_t dst_offset, 190 size_t src_offset, 191 size_t size) override; 192 Result TruncateImpl(size_t size) override; 193 194 private: 195 std::unique_ptr<OutputBuffer> buf_; 196 }; 197 198 class FileStream : public Stream { 199 public: 200 WABT_DISALLOW_COPY_AND_ASSIGN(FileStream); 201 explicit FileStream(std::string_view filename, Stream* log_stream = nullptr); 202 explicit FileStream(FILE*, Stream* log_stream = nullptr); 203 FileStream(FileStream&&); 204 FileStream& operator=(FileStream&&); 205 ~FileStream() override; 206 207 static std::unique_ptr<FileStream> CreateStdout(); 208 static std::unique_ptr<FileStream> CreateStderr(); 209 210 bool is_open() const { return file_ != nullptr; } 211 212 void Flush() override; 213 214 protected: 215 Result WriteDataImpl(size_t offset, const void* data, size_t size) override; 216 Result MoveDataImpl(size_t dst_offset, 217 size_t src_offset, 218 size_t size) override; 219 Result TruncateImpl(size_t size) override; 220 221 private: 222 FILE* file_; 223 size_t offset_; 224 bool should_close_; 225 }; 226 227 } // namespace wabt 228 229 #endif /* WABT_STREAM_H_ */