BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdls_tempdirectoryguard

Detailed Description

Outline

Purpose

Provide a scoped guard that creates a unique temporary directory.

Classes

Description

This component provides a scoped guard bdls::TempDirectoryGuard, intended primarily for testing, which creates a uniquely named temporary directory. If possible, this is located in the system temp directory, otherwise it is created within the current working directory.

As this class is primarily intended for testing, any failures to build the directory name or create the temporary directory will be fatal.

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage

Suppose an algorithm requires writing data to a temporary file on disk during processing:

/// Do "algorithm" using the specified `testFileName` for intermidiate state
/// storage.
void testAlgorithm(const bsl::string &testFileName);
Definition bslstl_string.h:1281

A function looking to use this algorithm can obtain a directory in which to put this file, guaranteed to not be used by other processes and to be cleaned up on normal exit, using an instance of bdls::TempDirectoryGuard:

void usesTestAlgorithm()
{
bdls::TempDirectoryGuard tempDirGuard("my_algo_");
bsl::string tmpFileName(tempDirGuard.getTempDirName());
bdls::PathUtil::appendRaw(&tmpFileName,"algorithm.scratch");
testAlgorithm(tmpFileName);
}
Definition bdls_tempdirectoryguard.h:113
static void appendRaw(bsl::string *path, const char *filename, int length=-1, int rootEnd=-1)

After exiting, the scratch file (named "algorithm.scratch") and the temporary directory (with an unspecified name starting with "my_algo_"), possibly in the system temp directory or the current working directory, will be removed.