
Introspective C++ Hermanpreet Singh Thesis submitted to the Faculty of the Virginia Polytechnic Institute and State University in partial fulfillment of the requirements for the degree of Masters of Science in Computer Science and Applications Denis Gracanin, Chair Shawn Bohner Stephen Edwards August 20, 2004 Blacksburg, Virginia Keywords: Metaprogramming, Templates, C++, Introspection Copyright 2004, Hermanpreet Singh Introspective C++ Hermanpreet Singh (ABSTRACT) Introspection has become a significant language feature to enable new component technolo- gies. It enables such capabilities as runtime component discovery, new levels of component flexibility and change tolerance, dynamic reconfiguration and system self healing. Three levels of introspection are discussed: simple type identification, structural introspection, and behavioral introspection The C++ programming language offers type identification, but neither structural or behav- ioral introspection. Through its use and combination of several language features, C++ has provided a flexible system for achieving some of the features of introspection without explicitly providing it. Features such as templates, operator overloading, polymorphism, and multiple inheritance have allowed software systems in C++ to build flexible components that tolerate change and support dynamic reconfiguration and self healing. The template sys- tem in particular has recently been shown to be more capable than expected, being Turing complete in its own right. Despite their existing capabilities, the language features have their limits and would benefit from an introspective mechanism. Unlike traditional introspective systems that execute solely at run–time, Introspective C++ has chosen a compile–time approach that tightly integrates with the template mechanism. This approach enables interaction with the other language mechanisms during the compilation, enabling the resolution of many introspective questions before the compiled program is ever run. Furthermore, the mechanism can serve as a base for developing run–time introspective systems. Contents 1 Introduction 1 1.1 Introspection . 1 1.1.1 Motivations for Introspection . 2 1.1.2 Current Uses . 4 1.1.3 Classes of Introspection . 7 1.1.4 Usage . 9 1.2 C++ . 11 1.2.1 Generics . 11 1.2.2 RTTI . 12 1.2.3 Limitations . 12 1.3 C++ Benefits from Introspection . 13 1.3.1 Structural Analysis . 13 1.3.2 Template Parameter Analysis . 14 1.3.3 Static Analysis . 14 1.4 Thesis Statement . 14 1.4.1 Goals . 15 1.4.2 Strategy . 15 1.4.3 Improvements over C++ . 16 1.5 Thesis Organization . 16 2 Introspection in Programming Languages 18 iii 2.1 SmallTalk . 18 2.1.1 Compilation . 19 2.1.2 Syntax . 19 2.1.3 Type System . 20 2.1.4 Object Model . 20 2.1.5 Runtime . 21 2.2 Objective-C . 21 2.2.1 Compilation . 22 2.2.2 Syntax . 22 2.2.3 Type System . 23 2.2.4 Runtime . 26 2.2.5 Case Study: Distributed Objects . 27 2.2.6 Case Study: Serialization . 27 2.3 Java ........................................ 28 2.3.1 Compilation . 29 2.3.2 Syntax . 29 2.3.3 Type System . 30 2.3.4 Objects . 31 2.3.5 Runtime . 33 2.3.6 Case Study: Serialization . 34 2.3.7 Case Study: Distributed Objects . 35 2.4 OpenC++ . 35 2.4.1 Compilation . 35 2.4.2 Introspective Abilities . 36 2.4.3 Case Study: Distributed Objects . 36 2.4.4 Case Study: Serialization . 37 3 Introspective C++ 39 3.1 Language Integration . 39 iv 3.1.1 Template Use . 40 3.1.2 Language Features . 42 3.1.3 Techniques . 44 3.2 Type Identification . 49 3.2.1 Provided Type Traits . 50 3.3 Structural Introspection . 53 3.3.1 Methods, Constructors, Destructors, Operators . 53 3.3.2 Overloads . 54 3.3.3 Members . 55 3.3.4 Base Classes . 55 3.3.5 Function Parameters . 55 3.3.6 Template Parameters . 56 4 Implementation 57 4.1 Compiler Evaluation . 57 4.1.1 Microsoft Visual C++ . 57 4.1.2 Comeau C++ . 58 4.1.3 Metrowerks CodeWarrior . 58 4.1.4 OpenWatcom . 58 4.1.5 GNU g++ ................................. 59 4.2 The Gnu Compiler Collection . 59 4.3 Template Instantiation . 60 4.4 Modifications . 62 4.4.1 Detection . 63 4.4.2 Type Analysis . 64 4.4.3 Annotation . 65 5 Case Studies 66 5.1 Container Optimization . 66 v 5.1.1 Multiple Policies . 67 5.1.2 Discrimination . 68 5.1.3 Results . 68 5.2 Runtime Access . 69 5.2.1 Preserved Data . 70 5.2.2 Member and Method Preservation . 71 5.2.3 Class Data Preservation . 73 5.2.4 Usage . 73 5.3 Serialization . 74 5.3.1 Primitive Types . 77 5.3.2 Composite Types . 77 5.3.3 Selection . 78 5.3.4 Execution . 79 6 Related Work 83 6.1 Non-Intrusive Object Introspection in C++ . 83 6.1.1 Comparison . 83 6.2 XVF: eXtensible Visitation Framework . 85 6.2.1 Comparison . 85 6.3 Iguana . 86 6.3.1 Comparison . 86 7 Conclusions 88 7.1 Limitations and Trade Offs . 89 7.1.1 No Dynamic Introspection . 89 7.1.2 A Compile–Time Mechanism . 90 7.2 Future Work . 91 A Compiler Modifications Source 92 A.1 ctti . 92 vi A.2 ctti.c . 94 B Case Studies Source 128 B.1 Serialization Example Source . 128 B.1.1 serializer.h . 128 B.1.2 doserialize.cpp . ..
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages158 Page
-
File Size-