"MODERN C ++ Design" LOKI reservoir source code reading

zhaozj2021-02-16  50

"MODERN C Design" LOKI reservoir source code reading

The "MODERN C Design" of Big Bull Andrei Alexandrescu discussed the cutting edge of C languages: Generative Programing. This book is estimated to be available for half a year, so it can only be used by its attached source code to spherling Generative Programming.

At present, I just read the source code for about half, and I have finished reading, I will release my read solution. Now, talk about my feelings.

I have a little more. C has two huge advantages: and C compatible, free; there are two huge disadvantages: and C compatible, complex. C is extremely complicated, it is difficult to master, and this is exactly the price of "freedom". The C language is a multi-programming language, which supports processization, object-oriented, generic, and generates five programming ideas, with extremely powerful expressive capabilities, which can easily transform various design.

Generic Programming is based on interface programming (with respect to OOP, the base class needed to even polymorphism), its technical starting point is that the choice, the core technology is: type derivation, type extraction, specialization / off The results are STL libraries: a set of universal containers and a common algorithm operated on a general container.

The essence of GeneATIVE Programming is based on policy programming (the compiler automatically generates the required code according to the policy. Due to higher abstraction, the code reset is higher), in the implementation of the Loki library, currently only uses recursive Strategy, its technical starting point is Typelist, core technology is: type derivation, type extraction, specialization / offset, multi-inheritance, and type to coupling, its achievement is the LOKI library: package in design mode.

Typelist is a trick to store and manage the type itself. Its source code has been passed, and I also have an annotation, and I will not talk about it here.

This is another large use of multiple inheritance after COM. Multiple inheritance is extremely easy to occur, so the Loki library uses a type to decoupled technology to avoid:

Template

Struct Type2Type

{

Typedef t OriginalType;

}

After such a layer of conversion, various conversion relationships between the original type T (especially inheritance / derived relationship) have no longer exist, and the diamond flaws will not happen again.

The specific implementation of the Loki library is quite technical, design it is very difficult (difficulty is much greater than the STL library, and there is a Boost library). But it is very easy to use it, and it is convenient. Since the Loki library provides a package of design patterns, it has greatly enriched the expression of C languages, making your design more easily converted to implementation.

Currently, the Loki library only provides packages for plant models and Visitor mode, which is still in the early stages of development.

I take Visitor model as an example to explain the convenience of the Loki library.

Visitor mode has four implementations: 1 string RTTI type determination; 2 secondary scheduling (Double Dispatch); 3 Establish a corresponding table of type and processing function; 4 Non-loop Visitor (Acyclic Visitor). The first three implementations were discussed in "More Effective C " Item 31 (although its example itself is not a Visitor mode). The fourth way is explained in the "Http://www-900.ibm.com/dpstruct/part2/index.shtml) in" Http://www-900.ibm.com/developruct/part2/index.shtml). Prototype

In this prototype, the user needs to achieve a lot of code, and because of the use of multiple inheritance technology, it will have problems when there is inheritance relationship between the specific HOST type.

LOKI library implementation

Using the LOKI library, users only need to implement the Visitor mode: Let the Host class inherit from BasevisITable and add a define_visitable () macro in all derived classes, so that all Visitor classes are double inherited from the Basevisitor class and the VisitorIMPL class (and can only Provides the processing function of the HOST class interested in you, not interested in the processing function). The user's workload is very small, very simple, and designers can distingumects for implementation.

More importantly, due to the use of type-between decoupling technology, there is no problem with multiple Host, there is no problem (which is more complicated, so I made fuzzy processing on the UML map, did not show it, Leave the Loki reservoir source code read solution).

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

New Post(0)