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 example below.
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 © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.