If your compiler doesn't support templates, or your application can't use them, applying the guard class becomes more difficult, but not impossible. You must provide a class that can be locked for each object. For instance, to support RWDBConnection, you must provide the following class:
class GuardRWDBConnection { RWDBConnection& conn; public: GuardRWDBConnection(const RWDBConnection& conn) : conn_((RWDBConnection&)conn) { conn_.acquire(); } ~GuardRWDBConnection() { conn_.release(); } };
This class can be used like the previous template version. Compare that code with the next example:
void doSomething(const RWDBConnection& conn) { // We're going to use the connection so we'd better // control access GuardRWDBConnection guard(conn); // RWDBConnection supports the // necessary interface. // Do something on the connection. // The mutex is released when the Guard instance goes out of // scope. }
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.