Skip to content

Commit

Permalink
Create README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er authored Jan 29, 2024
1 parent 0ecef09 commit 9209c76
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## tmp

RAII-wrappers for unique temporary files and directories that are deleted automatically for C++17 and later.

Currently only works on UNIX-like systems because it uses functions similar to mktemp. Tested on linux and macOS systems.

## Examples

### tmp::directory
tmp::directory is a smart handle that owns and manages a temporary directory and deletes it recursively when this handle goes out of scope. This can be useful for setting up a directory to later use as a PWD for some process. For example,

```cpp
auto tmpdir = tmp::directory("org.example.product");
fs::copy(file, tmpdir);

boost::process::child child {
...
boost::process::start_dir = tmpdir->native(),
...
};

auto output = tmpdir / "output_file.txt";
```
### tmp::file
tmp::file, on the other hand, is a smart handle for a temporary file. It is also deleted if the handle goes out of scope. This can be useful, for example, for accepting a file from a socket and testing it before saving in a long-term storage:
```cpp
auto tmpfile = tmp::file("org.example.product");
tmpfile.write(data);
if (validate_metadata(tmpfile)) {
fs::copy(tmpfile, storage);
} else {
throw InvalidRequestError();
}
```

## Integration

### CMake integration
You can also use the CMake interface target `tmp::tmp` and described below:

#### Embedded

To embed the library into your existing CMake project, place the entire source tree in a subdirectory (for example, using `git submodule` commands) and call `add_subdirectory()` in your `CMakeLists.txt` file:
```cmake
add_subdirectory(tmp)
...
add_library(foo ...)
target_link_libraries(foo PUBLIC tmp::tmp)
```

#### FetchContent

You can also use the FetchContent functions to automatically download a dependency. Put this in your `CMakeLists.txt` file:
```cmake
include(FetchContent)
FetchContent_Declare(tmp URL https://github.com/bugdea1er/defer/releases/download/<version>/defer.tar.xz)
FetchContent_MakeAvailable(tmp)
target_link_libraries(foo PUBLIC tmp::tmp)
```

## Current plans
- Provide install targets https://github.com/bugdea1er/tmp/issues/11
- Make this work on Windows https://github.com/bugdea1er/tmp/issues/3

## Contributing
Contributions are always welcome!

0 comments on commit 9209c76

Please sign in to comment.