Skip to content

Commit 808f569

Browse files
authored
Rewrite documentation (#196)
Rewrite documentation and sync examples in the public headers and README
1 parent 4fc6726 commit 808f569

File tree

3 files changed

+71
-78
lines changed

3 files changed

+71
-78
lines changed

README.md

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This library provides functionality for efficient management of temporary files
77
![Static Badge](https://img.shields.io/badge/C%2B%2B-17%2B-blue)
88
![GitHub Tag](https://img.shields.io/github/v/release/bugdea1er/tmp)
99

10+
1011
## Overview
1112

1213
When developing applications with long uptimes, such as server software, the management of temporary files and directories can become crucial. While the system may eventually clean up temporary files, this process may not occur frequently enough to meet the needs of applications with extended operational periods. Relying solely on the system for this task may lead to an accumulation of unnecessary temporary data on the device.
@@ -26,68 +27,54 @@ The location of temporary files generated by this library is determined by the `
2627

2728
### Temporary directories
2829

29-
`tmp::directory` is a RAII-handle for unique temporary directories. This can be useful for:
30-
31-
1. **Batch Processing:** Temporary directories can be used during batch processing tasks where multiple files need to be processed in a specific order. For example, a script that processes a large number of images may create temporary directories to organize and manage the input and output files efficiently.
32-
33-
2. **Software Installation:** Temporary directories are commonly used during software installation processes to store temporary files needed for installation. For instance, when installing a new application, temporary directories can be used to extract installation packages and store temporary configuration files.
30+
`tmp::directory` is a smart handle that manages a temporary directory, ensuring its recursive deletion when the handle goes out of scope. When a `tmp::directory` object is created, it generates a unique temporary directory in the current user's temporary directory.
3431

35-
3. **Data Backup:** Temporary directories can be utilized for creating temporary backups of important data before performing critical operations. For example, before making changes to a database, a temporary directory can be used to store a backup copy of the database to ensure data integrity.
32+
The temporary directory is deleted when either of the following happens:
33+
- the `tmp::directory` object is destroyed
34+
- the `tmp::directory` object is assigned another directory using `operator=`
3635

37-
4. **File Compression:** Temporary directories are often used during file compression tasks where multiple files need to be compressed into a single archive. For instance, when creating a zip file containing multiple documents, a temporary directory can be used to store the compressed files before creating the final archive.
38-
39-
5. **Data Migration:** Temporary directories can be helpful during data migration processes where data needs to be transferred from one system to another. For example, when migrating data from a legacy system to a new system, temporary directories can be used to stage and process the data before final migration.
36+
The example below demonstrates a usage of a `tmp::directory` object to run a subprocess within it and archive its logs; when the function returns, the temporary directory is recursively deleted:
4037

4138
```cpp
4239
#include <tmp/directory>
43-
...
44-
{
45-
// Create a unique temporary directory
46-
auto tmpdir = tmp::directory("org.example.product");
47-
48-
// Populate this directory
49-
fs::copy(file1, tmpdir);
50-
fs::copy(file2, tmpdir);
51-
52-
// Make an archive from this directory
53-
archiver::zip(tmpdir);
54-
} // The directory will be deleted when exiting the scope
55-
```
5640

57-
### Temporary files
41+
auto func() {
42+
auto tmpdir = tmp::directory("org.example.product");
43+
process::exec(executable, args, tmpdir);
5844

59-
`tmp::file` is a RAII-handle for unique temporary files. This can be useful for:
45+
return archive::glob(tmpdir, "*.log");
6046

61-
1. **Caching:** Temporary files can be used to cache data that needs to be accessed frequently but doesn't need to be stored permanently. For example, a web application can store pre-rendered HTML pages in temporary files to improve performance by serving them quickly without re-generating them each time.
47+
// The temporary directory is deleted recursively when the
48+
// tmp::directory object goes out of scope
49+
}
50+
```
6251

63-
2. **Data Processing:** Temporary files are often used during data processing tasks where intermediate results need to be stored temporarily before further processing. For instance, a script that manipulates a large dataset may write intermediate results to temporary files to avoid memory constraints.
52+
### Temporary files
6453

65-
3. **File Conversion:** Temporary files can be utilized during file format conversions. For instance, when converting a video file from one format to another, temporary files can be used to store the partially converted data before completing the conversion process.
54+
`tmp::file` is a smart handle that manages a temporary file, ensuring its deletion when the handle goes out of scope. Upon creation, a `tmp::file` object generates a unique temporary file in the current user's temporary directory, opening it for both reading and writing.
6655

67-
4. **Testing:** Temporary files can be handy during testing scenarios where you need to create mock data or simulate certain conditions. For example, a unit test for a file upload feature might create a temporary file to mimic the behavior of an uploaded file without actually saving it permanently.
56+
The temporary file is deleted of when either of the following happens:
57+
- the `tmp::file` object is destroyed
58+
- the `tmp::file` object is assigned another file using `operator=`
6859

69-
5. **Logging:** Temporary files can also be used for logging purposes, where log data is written to a temporary file before being processed and stored in a more permanent location. This can be helpful for managing log data efficiently in high-traffic systems.
60+
`tmp::file` inherits from the `std::iostream` class, allowing it to be used seamlessly with standard input/output operations and simplifying file handling while maintaining the flexibility of stream operations.
7061

71-
`tmp::file` extends the `std::iostream` class and uses an instance of `std::filebuf` internally. It is essentially `std::fstream` without open/close methods
62+
The example below demonstrates a usage of a tmp::file object to validate a request content and then move it to persistent storage; note that after the `move()` call, the temporary file will not be deleted, as its ownership is transferred to the specified location:
7263

7364
```cpp
7465
#include <tmp/file>
75-
...
76-
{
77-
// Create a unique temporary file
78-
auto tmpfile = tmp::file(std::ios::binary);
79-
80-
// Write its contents and metadata
81-
tmpfile << contents << metadata;
82-
83-
// Validate the file with an external validator
84-
if (validate(tmpfile)) {
85-
// Save the file to the persistent storage
86-
tmpfile.move(storage / "new_file");
87-
} else {
88-
throw InvalidRequestError();
89-
}
90-
} // The file will be deleted when exiting the scope
66+
67+
auto func(std::string_view content) {
68+
auto tmpfile = tmp::file(std::ios::binary);
69+
tmpfile << contents << std::flush;
70+
if (validate(tmpfile)) {
71+
// Save the file to the persistent storage
72+
tmpfile.move(storage / "new_file");
73+
} else {
74+
// The file is deleted automatically
75+
throw InvalidRequestError();
76+
}
77+
}
9178
```
9279
9380
## Building

include/tmp/directory

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,31 @@
99

1010
namespace tmp {
1111

12-
/// `tmp::directory` is a smart handle that owns and manages a temporary
13-
/// directory and deletes it recursively when this handle goes out of scope
12+
/// tmp::directory is a smart handle that manages a temporary directory,
13+
/// ensuring its recursive deletion when the handle goes out of scope
1414
///
15-
/// When a `tmp::directory` object is created, it creates a unique temporary
15+
/// When a tmp::directory object is created, it generates a unique temporary
1616
/// directory in the current user's temporary directory
1717
///
18-
/// The managed directory is deleted of when either of the following happens:
19-
/// - the managing `tmp::directory` object is destroyed
20-
/// - the managing `tmp::directory` object is assigned another path
21-
/// via `operator=`
18+
/// The temporary directory is deleted when either of the following happens:
19+
/// - the tmp::directory object is destroyed
20+
/// - the tmp::directory object is assigned another directory using operator=
2221
///
23-
/// The following example uses a `tmp::directory` object to create a temporary
24-
/// directory; when the function returns, the `tmp::directory` object goes out
25-
/// of scope and the temporary directory is recursively deleted:
22+
/// The example below demonstrates a usage of a tmp::directory object to run
23+
/// a subprocess within it and archive its logs; when the function
24+
/// returns, the temporary directory is recursively deleted:
2625
///
2726
/// @code{.cpp}
2827
/// #include <tmp/directory>
2928
///
3029
/// auto func() {
3130
/// auto tmpdir = tmp::directory("org.example.product");
32-
/// std::ostream(tmpdir / "file.txt") << "Hello, world!";
31+
/// process::exec(executable, args, tmpdir);
3332
///
34-
/// // the temporary directory is deleted recursively when the
35-
/// // `tmp::directory` object goes out of scope and is destroyed
33+
/// return archive::glob(tmpdir, "*.log");
34+
///
35+
/// // The temporary directory is deleted recursively when the
36+
/// // tmp::directory object goes out of scope
3637
/// }
3738
/// @endcode
3839
class directory {

include/tmp/file

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,39 @@
1313

1414
namespace tmp {
1515

16-
/// `tmp::file` is a smart handle that owns and manages a temporary file and
17-
/// deletes it when this handle goes out of scope
16+
/// tmp::file is a smart handle that manages a temporary file, ensuring
17+
/// its deletion when the handle goes out of scope
1818
///
19-
/// When a `tmp::file` object is created, it creates a unique temporary file
20-
/// in the current user's temporary directory and opens it with
21-
/// for reading and writing
19+
/// Upon creation, a tmp::file object generates a unique temporary file
20+
/// in the current user's temporary directory, opening it for both
21+
/// reading and writing
2222
///
23-
/// The managed file is deleted of when either of the following happens:
24-
/// - the managing `tmp::file` object is destroyed
25-
/// - the managing `tmp::file` object is assigned another path via `operator=`
23+
/// The temporary file is deleted of when either of the following happens:
24+
/// - the tmp::file object is destroyed
25+
/// - the tmp::file object is assigned another file using operator=
2626
///
27-
/// `tmp::file` extends the `std::iostream` class and uses an instance
28-
/// of `std::filebuf` internally. It is essentially `std::fstream`
29-
/// without open/close methods
27+
/// tmp::file inherits from the std::iostream class, allowing it to be
28+
/// used seamlessly with standard input/output operations and simplifying file
29+
/// handling while maintaining the flexibility of stream operations
3030
///
31-
/// The following example uses a `tmp::file` object to create a temporary file
32-
/// and write a string to it; when the function returns, the `tmp::file` object
33-
/// goes out of scope and the temporary file is deleted:
31+
/// The example below demonstrates a usage of a tmp::file object to validate
32+
/// a request content and then move it to persistent storage; note that after
33+
/// the move() call, the temporary file will not be deleted, as its ownership
34+
/// is transferred to the specified location:
3435
///
3536
/// @code{.cpp}
3637
/// #include <tmp/file>
3738
///
3839
/// auto func(std::string_view content) {
39-
/// auto tmpfile = tmp::file(std::ios::bin);
40-
/// tmpfile << content << std::flush;
41-
///
42-
/// // the temporary file is deleted recursively when the
43-
/// // `tmp::file` object goes out of scope and is destroyed
40+
/// auto tmpfile = tmp::file(std::ios::binary);
41+
/// tmpfile << contents << std::flush;
42+
/// if (validate(tmpfile)) {
43+
/// // Save the file to the persistent storage
44+
/// tmpfile.move(storage / "new_file");
45+
/// } else {
46+
/// // The file is deleted automatically
47+
/// throw InvalidRequestError();
48+
/// }
4449
/// }
4550
/// @endcode
4651
class file : public std::iostream {

0 commit comments

Comments
 (0)