Scope Guard Classes

Introduction

To support better resource control, the Tools Module includes scope guard classes that implement the C++ idiom “Resource Acquisition Is Initialization” (RAII). These classes provide a simple mechanism to allow an object to take ownership of a resource and to release that resource, thus avoiding potentially unsafe or verbose error handing code.

The RAII idiom is useful when writing exception-safe code, but also simplifies resource cleanup when control flow gets complicated. The C++ Standard Library class template std::auto_ptr<> is another implementation of this same idiom written to manage the cleanup of a heap-allocated object.

The basic concept is to acquire a resource in the constructor, and to leverage the C++ language’s guarantee that the destructor of a successfully-constructed object will be called.

Using the RAII idiom, the program performs an action when control flow leaves the current scope. Typically, the action to be performed is to undo a previous action in the event that an exception occurs.

Scope guard classes lists classes and functions in this group.

Table 57. Scope guard classes

Class

Description

RWScopeGuard

Typedef for RWScopeGuardImp.

RWTRefHolder

Reference wrapper object used with scope guards.

RWScopeGuardImp

This class and its derived classes implement the scope guard.

In this section: