Defining Handle Classes that Automatically Create Bodies
Whenever possible, implement your handles to automatically create the appropriate body instance. This is easiest for users of your handle class. Sometimes there is a one-to-one correspondence between your handle and body classes. In those cases, it is trivial to create the right body to bind to the handle. Other times the choice of body depends on some context or the current environment. If so, you’ll have to add some intelligence to your handle or use an external factory object.
For the body class:
1. Derive your own body class from
RWBodyBase. Add a suffix to it, to differentiate its name from the handle class name. (The convention for the Threads Module classes is that all body classes end in
Imp.)
2. To enforce that the bodies are created only by handles, make the body class’s constructors protected and grant friendship to the handle class.
For the handle class:
1. Derive your own handle class from
RWHandleBase. In your class, replicate the interface of the body, taking into consideration the way handles and bodies work together:
Handle constructors create body implementations on the heap.
All other methods forward their calls to the corresponding body implementation methods.
2. Override the body() method to return a reference to the most derived body.