Non-In trusiv e Ob je t In trosp e tion in C++: Ar hite ture and Appli ation T yng-Ruey Ch uang Institute of Information S ien e A ademia Sini a Nank ang, T aip ei 115, T aiw an Join t w ork with Chin-Ch uan Hsu, Ch uan-Chieh Jung, W en-Min Kuan, Y. S. Kuo, W ei-Hsueh Lai, and Chien-Min W ang. 1
Outline Ob je t in trosp e tion � The problems with C++ � Non-in trusiv e ob je t in trosp e tion in C++ � Implemen tation issues � Appli ations � 2
Dynami Appli ations They deal with ob je ts whose t yp es are kno wn only at � run-time. Examples in lude dynami library loaders, program debuggers, � ob je t serialization routines, et . They mak e external ob je ts \ ome aliv e" whenev er they are � needed, and no matter what they are. Sev eral OO languages ha v e builtin supp orts, su h as � late-binding, for dev eloping dynami appli ations. CLOS, Ja v a, Ob je tiv e C, and Smalltalk, for example, all allo w � late-binding metho d alls to ob je ts. 3
Ob je t In trosp e tion It is the abilit y to query and op erate an ob je t without � kno wing its t yp e stati ally . In trosp e tiv e op erations are still t yp e- he k ed, ho w ev er, to ex lude illegal a esses. It greatly helps the dev elopmen t of dynami appli ations. � Example: Ja v a ore re�e tion API. � No builtin supp ort for ob je t in trosp e tion in C++. C++'s � virtual fun tion and Run-Time T yp e Information (R TTI) me hanism an b e to o restri tiv e. 4
Example C++ ob je ts: � BSTree *p = new BSTree; p->insert("Sini a "); In trosp e tiv e C++ ob je ts: � void *p; Klass bstree = getClass("BSTree" ); p = bstree.new(); Method insert = getMethod(bstree , "insert"); void* argv[℄ = {"Sini a", 0}; bstree.invoke(p, insert, argv); 5
Curren t C++ Solutions Appli ation framew ork: lasses deriv ed from pre-de�ned � framew ork re eiv e additional in trosp e tiv e supp orts. Examples: System Ob je t Mo del (SOM) from IBM, and Common Ob je t Mo del (COM) from Mi rosoft. New language seman ti s and implemen tation: Mak e all ob je ts � in trosp e tiv e b y default. Systemati augmen tation to lass de�nitions: Instan es of the � augmen ted lasses b e ome in trosp e tiv e. 6
Issues of In terop erabilit y T ypi al problem: Ob je ts pro du ed b y third-part y lass library � need in trosp e tion as w ell. Third-part y library ma y ha v e its o wn lass hierar hi al � stru ture (i.e., framew ork). It is diÆ ult to �t it in to another appli ation framew ork. The library is dev elop ed using old seman ti s and � implemen tation. New C++ seman ti s and implemen tation do not help. The augmen ted lass de�nitions will pro du e ob je ts � in ompatible with the existing library (a \bla k b o x"). Summary: urren t solutions are in trusiv e to existing lass � de�nitions. 7
Goals: Non-In trusiv e Ob je t In trosp e tiv e in C++ Mak e ob je ts in trosp e tiv e, as long as one has a ess to their � lass de�nitions in adv an e. Non-in trusiv e. F or an y giv en lass, its in trosp e tiv e instan es � are ompatible with non-in trosp e tiv e instan es. Easy to use. Automati to ols are used to mak e instan es from � a lass in trosp e tiv e. Comprehensiv e supp orts. The to ol should o v er as man y C++ � lass features as p ossible. 8
A Meta Ob je t Ar hite ture (I) F or ea h lass, a orresp onding meta ob je t is reated at � run-time with all ne essary information ab out the lass. In trosp e tiv e a esses to instan e of the lass go through the � meta ob je t for help. The information stored at the meta ob je ts is olle ted b y � parsing the lass de�nitions in ada v an e. 9
Comp onen ts of the Ar hite ture (I I) Meta lass supp orting library: It is not sp e i� to an y lass, � and is used in user programming. Meta ob je t generation routines: They are sp e i� to giv en � lasses, and ma y b e in v ok ed in t w o stages at ompile-time and/or run-time. Tigh tly- oupled mo del: In trosp e tiv e lasses are kno wn at � ompile-time, but ob je ts ma y from an y of these lasses. Lo osely- oupled mo del: In trosp e tiv e lasses are kno wn only at � run-time. Meta ob je t generations are in v ok ed at di�eren t stages for the � t w o di�eren t mo dels. 10
Tigh tly-Coupled Mo del application meta class meta object class source declarations code generator declarations Compile-Time application meta class code to generate class libraries binary supporting libraries meta objects ("black boxes") Link-Time application meta class meta objects class libraries binary supporting libraries (one for each class) ("black boxes") Introspective Run-Time Environment Stand-alone Executable Run-Time Example: Appli ations that needs C++ ob je t input/output. 11
Lo osely-Coupled Mo del application meta class meta object class code generator source declarations declarations Compile-Time application meta class code to generate class libraries binary supporting libraries meta objects ("black boxes") Link-Time Script (Meta Object Generation) application meta class meta objects class libraries binary supporting libraries (one for each class) ("black boxes") Executable (Client) Dynamically Loaded Libraries (Servers) Run-Time Example: Bro wsers that load and exer ise C++ lasses on demand. 12
Meta Class Supp orting Library Meta lasses: Klass , Method , et . for represen ting fun tionalit y � pro vided b y an y C++ lass and metho d (e.g., ea h meta ob je t an instan e of Klass ). Most metho ds in the meta lasses are virtual fun tions. P olymorphi ob je t referen es: In trosp e tiv e ob je ts all ha v e � stati t yp e void* , ho w do es one dis o v er their dynami t yp es? W e need virtual onst har onst *dynami Type (void onst *self) { return typeid(*(B *) self).name(); } in the meta ob je t for lass B . Similar problems with on v ersion routines b et w een base and � deriv ed lasses. 13
Meta Ob je t Generation Routines They are automati ally generated b y parsing the lass � de larations. They ma y b e applied in t w o stages: the generator exe ute at � appli ation dev elopmen t time, but the o de so generated exe ute at appli ation run-time. Hen e the need for o de lik e offset_of_a_in_A = ( har *) &(((A *) 64)->a) - ( har *) ((A *) 64) for prop er memory la y out of lass A . Ob je t ompatibilit y: W e ma y need to augmen t a lass � de laration to gain a ess, but nev er augmen t it in w a ys that will hange its ob je t la y out. F or example, inserting friend fun tions is OK, but adding new virtual fun tions is not. 14
Tw o In trosp e tiv e Appli ations am : A soft w are to ol for pro viding automati I/O Obje tStr e � supp orts for omplex C++ ob je ts. Tigh tly- oupled mo del. unClass : An appli ation for in tera tiv e exe ution of R � dynami ally loaded C++ lass library . Lo osely- oupled mo del. Ob je tStream helps sa v e dev elopmen t time and a v oid man ual � o ding error. RunClass helps p erform lass-based soft w are testing, and helps � one familiarize with new lass library . 15
RunClass It allo ws users to reate ob je ts for giv en lasses, exe ute � metho ds up on sp e i� ob je ts, and examine their on ten ts in tera tiv ely . It has easy-to-use graphi user in terfa e. � A user's ommand sequen e, and its e�e t, an b e logged, and � repla y ed later for regression testing on new er v ersions of a lass library . 16
RunClass S reen Shot Note: Being sho wn to exer ise Mi rosoft F oundation Classes. 17
Ob je tStream C++ I/O op erators lik e and are made to w ork on << >> � instan es of an y user-de�ned lass. In header �le of the supp orting library: template < lass T> Uostream &operator<<(Uostre am &os, T &obj) { Uwrite(os, (void*) &obj, typeid(T).name()); return os; } template < lass T> Uistream &operator>>(Uistre am &is, T *&obj){ Uread (is, (void *) obj, typeid(T).name()); return is; } Ob je tStream is used to onstru t an in tera tiv e bro wser for � ob je ts stored in external media/format. It is also in orp orated in to RunClass for ob je t equalit y test. 18
Recommend
More recommend