Boost logo

Boost :

Subject: Re: [boost] File Handle and FILE* wrappers?
From: Josh Juran (jjuran_at_[hidden])
Date: 2014-11-29 12:17:21


On Nov 29, 2014, at 6:16 AM, Olaf van der Spek <ml_at_[hidden]> wrote:

> On Wed, Nov 26, 2014 at 9:28 PM, Josh Juran <jjuran_at_[hidden]> wrote:
>
>> I use POSIX I/O calls frequently. I adapted Lisa Lippincott and Marshall Clow's work on Nitrogen to create a library called poseven, based on the same 'nucleus' core library. p7::fd_t is an enum type for a file descriptor, and n::owned<p7::fd_t> and n::shared<p7::fd_t> are smart resources that work like unique_ptr and shared_ptr (loosely speaking) respectively. Much of it is about a decade old, early in my exposure to generic programming, though just recently I added p7::coprocess.
>
> Interesting. Why are you using an enum to store the fd?

For type-safety and efficiency. I don't use int, because that would allow any constant to be misinterpreted as a file descriptor. Nitrogen used to use complicated wrapper classes, but these had several defects: (a) they were slow to compile in the compiler used at the time (and which I still use for classic Mac OS development[1]), (b) the same compiler wouldn't place their values in registers, (c) their values couldn't be used in switch statements, and (d) creating constants for them was problematic, typically resulting in many `static const Foo x = Foo( ::x );` in headers.

Using an enum addressed all of these issues (with (d) being at least a partial improvement).

One of the downsides of C-style enums is non-portable memory layout. You can ensure an enum is large enough my declaring max_Foo = uint32_t( -1 ), but there's no guarantee that enum Boolean { False, True } occupies a single byte instead of, say, four. I'm new to C++11, but if I understand correctly, its enum classes address this very issue.

> Do you think it'd be useful to move parts of this functionality into Boost?

Quite possibly. Poseven is useful if you want to add type-safety and exception-safety to a C-style POSIX codebase (or subset thereof) without rewriting it to use higher-level semantics like ASIO. In debug mode, it throws exceptions containing a backtrace. A key question is how to approach the overlap of poseven with existing Boost libraries like Filesystem and Thread. I think the first step toward adoption would be for me to familiarize myself with Boost.System.

Josh

[1] Classic Mac OS is relevant in that POSIX programs are supported through MacRelix. The latest compiler to fully support classic Mac OS is Metrowerks C++ 2.4.1 from CodeWarrior Pro 6.3, released in summer 2000.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk