Deep exploration intelligent pointer (Smart Pointer)
Subject index:
First, analyze C standard library smart pointer (std :: auto_ptr) 1.Do you smart pointer? 2.std :: auto_ptr Design principle 3.std :: auto_ptr Advanced User Guide 4. Do you think std :: auto_ptr is not enough? perfect?
Second, C conditions, find a strategy for constructing more powerful smart pointers 1. Support for a number of design strategies for reference score 2. Support to process multiple resources 3. Support SubClassing 4. Support for multithreading conditions, thread security A variety of design strategies 5. Other special requirements, re-construct
Third, Generic Programming Basic Technology and Smart Pointer 1. Looking back to Traits Technology in Resources 2. Looking back over multithreaded support
Fourth, COM implementation, Smart Pointer design principle
V. Status of Smart Pointer in the famous C library (standard and non-standard)
-------------------------------------------------- -------------------
Second, C conditions, find a strategy for constructing more powerful smart pointers 1. Support a variety of design strategies to reference a number of design strategies, have you heard of COM and its famous IUNKNOWN interface? What is iUnknown? I want to tell you In the three function signatures of the IUNKNOWN interface, two are used to manage objects (Coclass Object, component class objects) to control its life cycle. In practice, our object is not only once, only allowed A referenced.
So, who is to manage its life cycle? Our strategy is: Reference record. When the reference number of the object is zero, it is destroyed. In fact, the destruction of the target is often AUTO_PTR. In COM, destroying objects is object yourself. In fact, it and our smart pointer is not a level of concept. Our smart pointer is responsible for the object level reference. And COM is based on interface reference as core To ensure the interface operation, the interface references are automatically managed. Oh! Yes! So how do we give auto_PTR plus object reference modes?
Strategy 1: An object corresponds to a reference count object. The smart pointer is a proxy with a count object. Imagine, this is also returned to the classic "adding intermediate layer" solution. # Core 1: We add a "reference) "It". Its duty has two: a. Maintenance of the reference number of the object. b. Maintain the object's pointer. Structure is as follows: Template
In Smart Pointer, we will involve a large number of _m_objrefCounted operations. The following is a process, in detail, designed. For example, when you assign an object to Smart Pointer to build an auxiliary reference Hosting object, at this time, m_uicounted is 1, m_obj_delegate_ptr is assigned to object pointer, if I now give Smart Pointer to another SmartPointer2, then smartpointer2 call _m_objrefcounted-> releaseref (); reduce the number of objects for maintenance, will Your own _m_objrefcounted is set to SmartPointer2, then call _m_objrefcounted-> addref (); ok! That's. Policy 2. Maintain an object pointer and a pointer to a reference value in each smart pointer Here, the focus here is to maintain a pointer to a reference value, which makes the uniform count value between the Smart Pointer becomes possible. Structure is as follows: Template
Below we will discuss this problem:
Policy 1. With a function pointer to support the processing of multiple resources. Our smart pointer will be designed with a template class with two parameters. The first parameter indication: the second parameter of the resource is indicated: the function type structure of the resource Drawing is as follows:
Typedef void freeeresourcefunction (void * p); void deaRray (void * p); void dealfile (void * p); // // Declaration of a function pointer to special resource Declaration // Template
Inline void dealarray (void * p) {if (p) delete [] p;} inline void dealfile (void * p) {if (p) p-> close ();} // Add to handle for special resources //
OK! But when we use this strategy, we must pay attention to that the passing pointer cannot be wrong. Of course, it is also necessary to retrofit the above structure, so that it has stronger identification bad ability. .
3. Support Subclassing Subclassing in the smart pointer, what is it? Let's first look at the one-rule fragment: class baseclass {}; class derived: public baseclass {}; auto_ptr
Seeing the # 1 above, you think in auto_ptr, can it be executed in Auto_PTR? No. Why? It is essentially, quite with this: baseclass * m_baseclass; m_baseclass = new derivedClass (Inparam); This is obviously illegal. Only we used the AUTO_PTR to a class with virtual characteristics, but also virtuality.
However, it does not access the inherited data, and it is not true Subclassing.
Then, we will implement such a function. Policy 1. In the SmartPoint described in the above reference count, we make the following operations: Template
Strategy 2. About the second method, this is no longer detailed. It involves too many details, the peak back is difficult to say. Generally, it is a target pointer maintained in the reference count object to void * and in specific The call is converted via static_cast or reinterpret_cast. In short, the so-called subclassing technique is inseparable from transformation.
4. Support for multi-threading conditions, multiple design strategies for thread safety are not very concerned for standard C . The reason is that the standard library does not support multithreading. Policy 1: First of all, we think: Access synchronization. So we have two options: a. Create a critical area object. Pass the execution of the object to the critical area. To ensure security. B. Use temporary object to complete the task, and leave the critical responsibility to be used Object. The following analysis: Programme1: Class Widget {... void Lock (); // Enter the critical area void unlock (); // Exit critical area}; Programme2: Template
Programmme4. Smartptr
Below, we simulate, execute the process. ## 1 Execute, build a temporary object LockingProxy
-------------------------------------------------- ------------ to be continued
Third, Generic Programming Basic Technology and Smart Pointer 1. Looking back to Traits Technology in Resources 2. Looking back over multithreaded support
Fourth, COM implementation, Smart Pointer design principle
V. Status of Smart Pointer in the famous C library (standard and non-standard)
-------------------------------------------------- ----------------
-------------------------------------------------- ------------ Solemn statement: Allow copy, modification, delivery or other behavior but is not allowed for any commercial use. Writing 20/3/2003 Last modified: 20/3/2003 by redstar81 81_redstar@163.com--------------------------------------------- ----------------