LOKI library reading 4: hierarchygenerators.h read
Every line of code in this is understanding, using it, I have seen it, but I still don't understand how to use this. I also hope that someone can answer.
// the loki library
// Copyright (c) 2001 by Andrei Alexandrescu
// this code Accompanies The Book:
// Alexandrescu, Andrei. "Modern C Design: Generic Programming and Design
// Patterns Applied ". CopyRight (c) 2001. Addison-Wesley.
// permission to use, copy, modify, distribute and sell this software for any
// Purpose Is hereby granted without fee, provided that the Above copyright
// NOTICE APPEAR in All Copies and this Both That Copyright NOTICE AND THIS
// Permission Notice APPEAR in Supporting Documentation.
// the author or addison-welsey longman make no representation about the @ r
// suitability of this software for any purpose. It is provided "as IS"
// WITHOUT EXPRESS or Implied Warranty.
// Last Update: March 05, 2001
#ifndef hierarchygerators_inc_
#define hierarchygenerators_inc_
#include "typelist.h"
#include "typetraits.h"
#include "emptytype.h"
Namespace loki
{
// Class Template GenScatterHierarchy
// generates a scattered hierarchy starting from a typelist and a template
// invocation (Tlist is a Typelist, Model IS A Template of One Arg):
// genscatterhierarchy
// the generated class inherits all classes generated by instantiarating the
// template 'model' with the Types Contained in TLIST
// WQ Note: Multi-inheritance from all types in Typelist, which is an inclusive derived (Unit
If there is a type repeated in the list, or there is a Typelist (tree) to find trouble.
Template
Class genscatterhierarchy;
// WQ Note: It is not consistent with the predetermination, parameters, and implementation, to indicate the design intent. Template
Class GenScatterHierarchy
: Public GenScatterHierarchy
, Public GenScatterHierarchy
// WQ Note: If you use Typelist
{
PUBLIC:
Typedef Typelist
Typedef genscatterhierarchy
Typedef genscatterhierarchy
// WQ Note: Typelist's Head, Tail is similar.
Template
{
Typedef Unit
}; // WQ Note: It does not require T in Typelist. But according to other code, T must be in Typelist. Other parts of the LOKI library is to perform such a compile period check.
// WQ Note: Pay attention to the details of the "Detective class with the same name to cover the foundation version".
}
Template
Class GenScatterHierarchy: Public Unit
// WQ Note: This is the implementation of the template, but only the situation of traditional types.
{
TypedEf Unit
Template
{
Typedef Unit
}
}
Template Class Unit>
Class GenScatterHierarchy
// WQ Note: Don't forget this boundary point.
{
Template
{
Typedef Unit
}
}
// Function Template Field
// Accesses a field in an Object of a type generated with genscatterhierarchy
// invocation (Obj is an Object of a type h generated with genscatterhierarchy,
// t is a type in the Typelist Used to Generate H):
// Field
// Returns a Reference to Unit
// WQ Note: Pay attention to the above instructions, don't use it wrong. Template
Typename H :: rebind
{// WQ Note: When typelist, Unit
Return Obj;
}
Template
Const Typename H :: Rebind
{
Return Obj;
}
// Function Template TUPLEUNIT
// the building block of tuples
// WQ Note: It is just a simple bag container, and its offset is more useful.
Template
Struct TupleUnit
{
T value_;
Operator T & () {Return Value_;
Operator const t & () const {return value_;
}
// Class Template Tuple
//Mplements a tuple class what holds a number of values and provides field
// Access to Them Via the Field Function (Below)
Template
Struct Tuple: Public GenScatterHierarchy
{
}
// WQ Note: A structure as shown below!
// Helper Class Template FieldHelper
// See Field BELOW
// WQ Note: Compiling errors will occur when I am. Refer to Typelist :: typeat ().
Template
Template
Struct FieldHelper
// Specialized 0, is to judge RightBase or LeftBase
{
TypeDef Typename H :: Tlist :: Head ElementType;
Typedef Typename H :: rebind
ENUM
{
ISTUPLE = Conversion
// WQ Note: This sentence determines that H is generated with TUPLEUNIT because this class relies on H to T must have a conversion relationship. If you use a user-defined UNIT class, it does not provide this feature, you can ....
Isconst = Typetraits
// WQ Note: Loki's extraction capacity on the type is also extremely approached. In Typetraits.h, the code is relatively simple, almost look at it, just lamenting "only if you can't think, no."
}
Typedef constlyetame H :: Leftbase constleftbase; typef type constleftbase; typef type constleftbase; typef type
Typename H :: Leftbase> :: results;
TypeDef TypeName Select UnitType> :: results. R r; // WQ Note: In order to reduce calls to user-defined Operator T & (), because C specifies "custom type conversion can only be invoked once", so it is necessary to reserve as far as possible. TypedEf TypeName Select UNQUALIFIEDRESULTTYPE> :: Result results; Static ResultType & Do (H & Obj) { Leftbase & leftbase = obj; // wq Note: Explicitly completed type conversion. The reason is also the same. Return Leftbase; } } Template Struct FieldHelper { TypedEf Typename TL :: Typeat // WQ Note: This information can be obtained directly, and all do not reclaim. Typedef Typename H :: rebind ENUM { ISTUPLE = Conversion Isconst = Typetraits } Typedef const typeename h :: rightbase constructbase; TypedEf TypeName Select Typename H :: RightBase> :: results rightbase; TypeDef TypeName Select UnitType> :: results. R r; TypedEf TypeName Select UNQUALIFIEDRESULTTYPE> :: Result results; Static ResultType & Do (H & Obj) { Rightbase & rightbase = OBJ; RTURN FieldHelper // WQ Note: When the type is converted, it can not be done directly. } } // Function Template Field // Accesses a field in an Object of a type generated with genscatterhierarchy // invocation (Obj is an Object of a type h generated with genscatterhierarchy, // i is the index of a type in the Typelist buy to generate h): // field (obj) // Returns a Reference to Unit // and t is the I-th Type in the Typelist // WQ Note: Convert to the i-th base class, pay attention to the above description. Template TypeName FieldHelper Field (H & Obj) { Return FieldHelper } // Template // const typeAme FieldHelper // Field (const h & obj) // { // Return FieldHelper //} // Class Template GenlineArhirarchy // generates a linear hierarchy starting from a typelist and a template // invocation (Tlist is a Typelist, Model Is A Template of Two ARGS): // genscatterhierarchy // WQ Note: Pay attention to the implied role of the name of the parameter in the declaration! Template < Class TLIST, / / WQ Note: You must pass a Typelist. Template Class root = Emptytype > Class GenlineArchirarchy; Template < Class T1, Class T2, Template Class root > Class GenlineArchy : Public Unit { } // WQ Note: Due to the recursive relationship of Typelist itself, the following figure structure: Template < Class T, Template Class root > Class GenlineArchy : Public Unit { } // WQ Note: It is pointed out that the completeness should be added based on the completeness, and it should be increased. However, this class clearly explains that its interface is required to pass a Typelist, so this offset demand is not considered. } // Namespace Loki // Change log: // June 20, 2001: Ported by Nick Thurn to GCC 2.95.3. Kudos, Nick !!! #ENDIF // hierarchygenerators_inc_