I/O in Plywood

Jeff Preshing · 2020年7月8日

AI 摘要 AI 生成,可能有误

本文介绍了开源 C++ 框架 Plywood 的 I/O 系统,并将其定位为已有数十年历史的 C 和 C++ 标准运行时 I/O 库的现代替代方案。文章通过代码示例讲解了诸如带缓冲的 OutStream 和 StringWriter 类、基于适配器的链式处理(用于换行符转换和字符编码,例如从 UTF-8 输入写入 UTF-16 文件),以及使用 InStream 和 StringReader 进行带缓冲的文本读取等核心概念。作者还着重指出了若干设计决策,例如 OutStream 对象并非线程安全,需要通过 StdOut::binary() 或 StdOut::text() 显式创建,以及 Plywood 总体上倾向于在内部使用 UTF-8 文本。

Plywood is an open-source C++ framework I released a few weeks ago. It includes, among other things, a runtime module that exposes a cross-platform API for I/O, memory, threads, process management and more.

This post is about the I/O part. For those who don’t know, I/O stands for input/output, and refers to the part of a computer system that either writes serialized data to or reads serialized data from an external interface. The external interface could be a storage device, pipe, network connection or any other type of communication channel.

Typically, it’s the operating system’s responsibility to provide low-level I/O services to an application. But there’s still plenty of work that needs to happen at the application level, such as buffering, data conversion, performance tuning and exposing an interface that makes life easier on application programmers. That’s where Plywood’s I/O system comes in.

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Preshing on Programming

How C++ Resolves a Function Call

AI 摘要The article explains the hidden compile-time algorithm that every C++ compiler uses to resolve a function call, which the author was motivated to understand after repeatedly encountering confusing errors about 'overloads with similar conversions.' It walks through the three main phases of the process—name lookup (including argument-dependent lookup), template argument deduction and substitution (introducing SFINAE), and overload resolution—using a concrete example to show how candidates are found and narrowed down. The author argues that understanding how these steps fit together is especially valuable for programmers developing libraries who combine multiple C++ features.

Flap Hero Code Review

AI 摘要This post reviews the source code of Flap Hero, a small C++ game built without an existing game engine, explaining how it is organized using the Plywood framework and how it differs from larger game projects. The author evaluates the deliberate omission of typical engine subsystems—abstract scene representation, a shader manager, a physics engine, and an asset pipeline—rating each choice as OK, Good, or Bad based on the trade-offs involved. Overall, the article suggests Flap Hero is a valuable learning resource for novice and intermediate game developers, though some simplifications (like hand-managed shaders and no asset pipeline) proved disadvantageous.

A Small Open Source Game In C++

AI 摘要The author has released Flap Hero, a free, open source Flappy Bird clone written entirely in C++, with MIT-licensed source code and public domain assets available on GitHub. The game was created to promote Plywood, a module-oriented open source C++ framework (not a game engine) that integrates third-party libraries like Assimp, SoLoud, stb, and GLFW through its workspace and built-in modules. Flap Hero exemplifies the lean, 'handmade' software style Plywood is designed to support, and the author plans future work such as a GUI build manager, with a source code review to follow in the next blog post.

Automatically Detecting Text Encodings in C++

AI 摘要The article describes how the open-source Plywood C++ framework automatically detects text file formats, addressing the inherent ambiguity of encodings like UTF-8, UTF-16 (with or without BOM), and Windows-1252, along with varying line endings. Plywood examines the first 4KB of a file, checks for valid UTF-8 decoding, and scores alternative encodings using heuristics based on whitespace, ASCII characters, and decoding errors. The author benchmarks this approach against a 50-file test suite, noting that editors like Notepad++, Sublime Text, VS Code, and Windows Notepad achieve comparable detection rates, and invites contributions to improve support for formats like Shift JIS on GitHub.

A New Cross-Platform Open Source C++ Framework

AI 摘要The author has released Plywood, a cross-platform open source C++ framework extracted from their proprietary Arc80 game engine, which builds and runs on Windows, Linux, and macOS. Plywood is not a game engine itself but a general framework featuring a workspace for sharing code across applications, 36 built-in modules for tasks like platform abstraction and JSON parsing, and a runtime reflection and serialization system. The author plans future improvements such as simplifying module creation, extending the built-in C++ parser, and improving the webserver, and welcomes community feedback.