I l@ve RuBoard |
![]() ![]() |
7.14 Putting It All TogetherNot much to go! Here comes the fun part. So far we have treated each issue in isolation. It's now time to collect all the decisions into a unique SmartPtr implementation. The strategy we'll use is the one described in Chapter 1: policy-based class design. Each design aspect that doesn't have a unique solution migrates to a policy. The SmartPtr class template accepts each policy as a separate template parameter. SmartPtr inherits all these template parameters, allowing the corresponding policies to store state. Let's recap the previous sections by enumerating the variation points of SmartPtr. Each variation point translates into a policy.
Other issues are not worth dedicating separate policies to them or have an optimal solution:
The presentation of the design issues surrounding smart pointers made these issues easier to understand and more manageable because each issue was discussed in isolation. It would be very helpful, then, if the implementation could decompose and treat issues in isolation instead of fighting with all the complexity at once. Divide et Impera— this old principle coined by Julius Caesar can be of help even today with smart pointers. (I'd bet money he didn't predict that.) We break the problem into small component classes, called policies. Each policy class deals with exactly one issue. SmartPtr inherits all these classes, thus inheriting all their features. It's that simple—yet incredibly flexible, as you will soon see. Each policy is also a template parameter, which means you can mix and match existing stock policy classes or build your own. The pointed-to type comes first, followed by each of the policies. Here is the resulting declaration of SmartPtr: template < typename T, template <class> class OwnershipPolicy = RefCounted, class ConversionPolicy = DisallowConversion, template <class> class CheckingPolicy = AssertCheck, template <class> class StoragePolicy = DefaultSPStorage > class SmartPtr; The order in which the policies appear in SmartPtr's declaration puts the ones that you customize most often at the top. The following four subsections discuss the requirements of the four policies we have defined. A rule for all policies is that they must have value semantics; that is, they must define a proper copy constructor and assignment operator. 7.14.1 The Storage PolicyThe Storage policy abstracts the structure of the smart pointer. It provides type definitions and stores the actual pointee_ object. If StorageImpl is an implementation of the Storage policy and storageImpl is an object of type StorageImpl<T>, then the constructs in Table 7.1 apply. Here is the default Storage policy implementation: template <class T> class DefaultSPStorage { protected: typedef T* StoredType; //the type of the pointee_ object typedef T* PointerType; //type returned by operator-> typedef T& ReferenceType; //type returned by operator* public: DefaultSPStorage() : pointee_(Default()) {} DefaultSPStorage(const StoredType& p): pointee_(p) {} PointerType operator->() const { return pointee_; } ReferenceType operator*() const { return *pointee_; } friend inline PointerType GetImpl(const DefaultSPStorage& sp) { return sp.pointee_; } friend inline const StoredType& GetImplRef( const DefaultSPStorage& sp) { return sp.pointee_; } friend inline StoredType& GetImplRef(DefaultSPStorage& sp) { return sp.pointee_; } protected: void Destroy() { delete pointee_; } static StoredType Default() { return 0; } private: StoredType pointee_; }; In addition to DefaultSPStorage, Loki also defines the following:
7.14.2 The Ownership PolicyThe Ownership policy must support intrusive as well as nonintrusive reference counting. Therefore, it uses explicit function calls rather than constructor/destructor techniques, as Koenig (1996) does. The reason is that you can call member functions at any time, whereas constructors and destructors are called automatically and only at specific times. The Ownership policy implementation takes one template parameter, which is the corresponding pointer type. SmartPtr passes StoragePolicy<T>::PointerType to OwnershipPolicy. Note that OwnershipPolicy's template parameter is a pointer type, not an object type. If OwnershipImpl is an implementation of Ownership and ownershipImpl is an object of type OwnershipImpl<P>, then the constructs in Table 7.2 apply.
An implementation of Ownership that supports reference counting is shown in the following: template <class P> class RefCounted { unsigned int* pCount_; protected: RefCounted() : pCount_(new unsigned int(1)) {} P Clone(const P & val) { ++*pCount_; return val; } bool Release(const P&) { if (!--*pCount_) { delete pCount_; return true; } return false; } enum { destructiveCopy = false }; // see below {; Implementing a policy for other schemes of reference counting is very easy. Let's write an Ownership policy implementation for COM objects. COM objects have two functions: AddRef and Release. Upon the last Release call, the object destroys itself. You need only direct Clone to AddRef and Release to COM's Release: template <class P> class COMRefCounted { public: static P Clone(const P& val) { val->AddRef(); return val; } static bool Release(const P& val) { val->Release(); return false; } enum { destructiveCopy = false }; // see below }; Loki defines the following Ownership implementations:
7.14.3 The Conversion PolicyConversion is a simple policy: It defines a Boolean compile-time constant that says whether or not SmartPtr allows implicit conversion to the underlying pointer type. If ConversionImpl is an implementation of Conversion, then the construct in Table 7.3 applies. The underlying pointer type of SmartPtr is dictated by its Storage policy and is StorageImpl<T>::PointerType. As you would expect, Loki defines precisely two Conversion implementations:
7.14.4 The Checking PolicyAs discussed in Section 7.10, there are two main places to check a SmartPtr object for consistency: during initialization and before dereference. The checks themselves might use assert, exceptions, or lazy initialization or not do anything at all. The Checking policy operates on the StoredType of the Storage policy, not on the PointerType. (See Section 7.14.1 for the definition of Storage.) If S is the stored type as defined by the Storage policy implementation, and if CheckingImpl is an implementation of Checking, and if checkingImpl is an object of type CheckingImpl<S>, then the constructs in Table 7.4 apply. Loki defines the following implementations of Checking:
![]() |
I l@ve RuBoard |
![]() ![]() |