|
| 1 | +[](https://opensource.org/licenses/MIT) |
| 2 | +[](https://github.com/dvnrrs/libcppwrap/actions/workflows/build.yml) |
| 3 | +[](https://github.com/dvnrrs/libcppwrap/actions/workflows/doxygen.yml) |
| 4 | + |
| 5 | +# libcppwrap<br><small><sub>A collection of C++ wrappers for native APIs</sub></small> |
| 6 | + |
| 7 | +`libcppwrap` is a collection of low-level C++ wrappers around native C APIs such as POSIX |
| 8 | +functions and BSD sockets. It provides the following features on top of the native functions, |
| 9 | +while duplicating the function signatures as closely as possible: |
| 10 | + |
| 11 | +* Errors are thrown as `std::system_error` exceptions instead of returning error codes. |
| 12 | +* Since return values are no longer used for error reporting, output parameters can be returned as |
| 13 | + values instead, wherever practical. |
| 14 | +* The [RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) pattern is |
| 15 | + applied to resources such as file handles, preventing leaks, using a sophisticated and flexible |
| 16 | + `w::handle` type. |
| 17 | +* Functions such as `fcntl()` and `ioctl()`, which can take input and/or output parameters of |
| 18 | + arbitrary type, are implemented as type-safe templates. |
| 19 | +* Derived POD structures for types such as `sockaddr_in` are provided with useful constructors for |
| 20 | + initialization. Use of the derived structures is always optional. |
| 21 | + |
| 22 | +The philosophy of the library is to stay as faithful as possible to the native API, while ensuring |
| 23 | +type- and exception-safety and resource management as robustly as possible. Some helper functions |
| 24 | +which deviate from the native APIs or add missing functionality are implemented in a separate, |
| 25 | +optional namespace, which can be disabled. |
| 26 | + |
| 27 | +The library is currently very POSIX-centric, being developed for and tested on Linux. It could and |
| 28 | +may eventually be extended to include native APIs for other platforms like Windows. |
| 29 | + |
| 30 | +# Usage |
| 31 | + |
| 32 | +`libcppwrap` exposes two namespaces, `w` and `wx`. The `w` namespace contains the core wrappers, |
| 33 | +which follow the native APIs as closely as possible. The `wx` namespace contains useful extensions |
| 34 | +which deviate from the native API but provide low-level functionality that would not necessarily |
| 35 | +justify a separate library. |
| 36 | + |
| 37 | + #include <w/posix.hpp> |
| 38 | + |
| 39 | + auto fd = w::open("file.txt", "at"); |
| 40 | + w::write(fd, "hello", 5); |
| 41 | + // fd is automatically closed when it goes out of scope |
| 42 | + |
| 43 | +See the **[Doxygen API reference](https://dvnrrs.github.io/libcppwrap/)** for details. |
| 44 | + |
| 45 | +# Building |
| 46 | + |
| 47 | +`libcppwrap` has no dependencies, requiring only a C++ compiler that supports C++17. |
| 48 | + |
| 49 | +It can be built easily with [CMake](https://cmake.org/): |
| 50 | + |
| 51 | + mkdir build |
| 52 | + cd build |
| 53 | + cmake .. |
| 54 | + make |
| 55 | + sudo make install |
| 56 | + |
| 57 | +# License |
| 58 | + |
| 59 | +`libcppwrap` is published under the [MIT License](https://opensource.org/license/mit/). See |
| 60 | +[LICENSE.txt](LICENSE.txt) for the full license text. |
0 commit comments