The Compound Document framework provides an easy way for you to ensure the model is properly locked and unlocked. You can use the template class TModelPointerTo<AModel> to create a smart pointer to a model. A smart pointer locks and unlocks the target object automatically when you instantiate and destroy the smart pointer object. By creating a const
TModelPointerTo<AModel>, you get read-only access to the model--to get write access, create a non-const
pointer. You instantiate this class with a TModelReference, and you get an object that acts just like a pointer to a TModel.
For example, the TTilesView DrawContents function uses this class to handle locking the model, as follows:
const
pointer because it needs only read-access to the model (it doesn't modify the data--it only displays it). TTilesView gets the correct TModelReference using the TGUIBundle mechanism--see "Using GUI bundles" on page 18.If you use TModelPointerTo<AModel> to access a model, you don't have to explicitly lock and unlock the model because the pointer object does it for you. However, you should keep in mind that a model must be locked before you can access it and unlocked when you're finished with it, so you must make sure you have a locking mechanism in place before accessing a model. If you don't lock a model before accessing it, your program will probably generate exceptions or freeze.
This diagram shows how the TTilesView DrawContents function uses TModelPointerTo<TTilesModel> to lock the model.