Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdls_tempdirectoryguard
[Package bdls]

Provide a scoped guard that creates a unique temporary directory. More...

Namespaces

namespace  bdls

Detailed Description

Outline
Purpose:
Provide a scoped guard that creates a unique temporary directory.
Classes:
bdls::TempDirectoryGuard guard for creating and removing a temp directory
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:
  void testAlgorithm(const bsl::string &testFileName);
      // Do "algorithm" using the specified 'testFileName' for intermidiate
      // state storage.
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);
  }
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.