Single piece mode C ++ template implementation

zhaozj2021-02-08  251

What is the use of single-piece mode, please check the relevant books yourself:

/ * $ Log: E / VC / TEST / TESTCONSOLE / JYSINGETON2.H $ * $ source: /ye/vc/test/testconsole/jysingleton2.h * /// $ /// $ jysingleton2.h // $ / / $ Author: "Jasper Yeh" // $ version: 1.00.000 // $ CREATE TIME: TESDAY, JULY 31, 200113: 59 // $ TIME-stamp: // $ descriptions: (Jysingleton2.h file function description) / / $ // $ IF This Code Works, IT Was Written By Jasper yehicket yehrest, i don't know who wrote it.//$ / $ / / / @ $ under the Terms of the gnu lesser gpl witense.//$ Lesser GPL IS Specially at you can reserve your change, no public necessary, but you could be // $ please to notify me by maililerest to notify me by mail // $ Go to http: //www.gnu .org / copyleft / lesser.html for LGPL / / / $ / $ // # IFNDEF __JYSINGLETON2_H # Define __jysingleton2_h

#include #if! defined (jytrace) #if Defined (_console) #define Jytrace_tprintf # Elif Defined (_MFC_VER) #define Jytrace TRACE # Else # Define Jytrace # Endif # ENDIF

// is still a C solution that is not full, but finally templates one piece.

/// / The principle used here is a static member variable technology in the dynamic construction template class: // 1. The code of the template is dynamically generated during the first instantiation; // 2. Template class itself The determination of the name is also really meaningful in the first instantiation; // 3. Place the static member variable of the template class into the member function body, solve the embarrassment of the declaration in the template class. // For example, such a code will be difficult to use // template // Class SG {// public: // SG () {} // ~ SG ()} // static std :: auto_ptr _TDATA; //}; // The similar situation is mainly: // a. A static member variable // b. This variable is dependent on the passing parameterized name (ie T) // In line with the above situation, it will encounter a problem that cannot be written in the statement of the static variable. // In the above example, in order to use the SG template class, you have to write: // class yy; // typedef sg sgyy; // std :: auto_ptr sgy :: _ tdata; // horrible !! // sgyy * someptr = new sgy; // where YY is a class that has been defined. / (Note that this is different from the usage of the standard template: if you need template second classes, you still need to write: // class zz; // typedef SG sgzz; // std :: Auto_ptr sgzz :: _ tdata; // horrible !! // sgzz * SomePtr = New SGZZ; // Investigate the second sentence of the above two codes, you may have already understood where the problem is. Yes, whenever you need to // parameterize a class, you need to manually make the static member variable of a template class SG for this class. // If you don't care about the overhead of such a sentence, you can use the above examples, which have proven to be effective. / / But if you feel more important for the meaning of the package, or when your template class needs multiple static variables, you may need to refer to the implementation below. /// This implementation is typically used in single-piece mode, especially important, to use this approach, your template class // will be single-style. // By modifying // static std :: auto_ptr; ////, you can actually jump off the above limit, allowing the container to accommodate multiple real // examples (for example , Declare it into a simple linked list). // Natural, the instance of the instance () function itself also needs to be adjusted to accommodate a number of instances. ///// Due to the function of the template class has evolved into pointers that are inclusive, one or more parameter classes, so we use the names such as Jysingletncontainer to emphasize his container characteristics. //// By the way, Auto_Ptr > _Container; syntax is not easy to pass.

Template Class Jysingleton {// In order to use Jysingleton , you need: // Cannot be directly constructed from the parameter class, can only achieve and // using JYSINGLETON :: instance () to acquire and / / use parameters An example of a class. The standard instance is as follows: // class yy; // typedef jysingleton cyy; // yy * pyy = * cyy :: instance (); // delegated pointer mechanism makes CYY instance pointers do not have to be paid attention to jysingleton () {_ptr = NULL; JYTRACE (_T ("Jysingleton Class Initializing At% P / N"), this);} public: ~ jysingleton () {release (); Jytrace (_t ("Jysingleton Class AT% P } public: typedef jysingleton jysingletoncontainer; // **************************************** ************************************ // can only use this interface to get the pointer of the instance of T / As for the unique instance of the JYSINGLETON container class itself, it is not necessary to preferently //. Static Jysingleton * Instance () {// // Singleton Class Me (IE Jysingleton ) // Error Syntax: // StateTic st: auto_ptr > _Container; // static std :: Auto_PTR _Container;

IF (_Container.get () == null) {JYSINGLETONCONTAINER * SG = New JysingletonTainer (); SG -> _ PTR = New T;

_Container = std :: auto_ptr (sg);

Return_Container.get ();

PUBLIC: Operator T * () {return get ();}

T * Operator -> () {return get ();} t & operator * () {return * get ();} t * get () {return _ptr;}

Void release () {if (_ptr) {delete _ptr; _ptr = null;}} T * DETACH () {t * p = _ptr; _ptr = null; return p;}

Private: T *_ptr; // T class a copy of a work so that customers can use. No matter that case, a copy. }

#ENDIF / / __ JYSINGLETON2_H

The next article will tell the traditional single piece in C is optimal.

转载请注明原文地址:https://www.9cbs.com/read-1772.html

New Post(0)