Reference-counting and the Handle-Body Implementation
This software uses reference-counting and the handle-body (interface-implementation) idiom, which decouples the interface from its implementation. These techniques simplify memory management, allow efficient body object sharing, and provide automatic object life-cycle control.
The handle class is an interface to an internal object representation or body. The operations included in an internal representation are accessed by calling member functions supplied by the handle class. These functions forward control to the corresponding function in the body class instance. For example, an RWFtpAgent object is a handle to an RWFtpAgentImp instance that performs the protocol interaction.
Figure 2 shows the architecture of RWFtpAgent and its related class. RWPop3Agent and RWSmtpAgent use the same design.
Figure 2 – The architecture of RWFtpAgent and its related class
Similarly, the RWFtpClient object is a handle to an RWFtpClientImp instance that performs the protocol interaction. Figure 3 shows the relationship between RWFtpClient and its related class RWFtpClientImp. The other client classes—RWHttpClient, RWPop3Client, and RWSmtpClient—use the same design.
NOTE: The classes RWFtpAgentImp and RWFtpClientImp are discussed here for demonstration purposes only and should not be referenced directly or used by application code.
Figure 3 – The relationship between RWFtpClient and its related class
Reference-counting allows multiple handles to refer to the same underlying body object. A handle instance is created by binding to a body instance, located on the heap. When a handle-body binding occurs, the reference counter is incremented. When a handle is detached from its body, the body’s reference counter is decremented. When all the handles that refer to a particular body object have gone out of scope, the body object’s destructor is invoked and the body object is deleted from the heap.