com-intro

download com-intro

of 27

Transcript of com-intro

  • 8/17/2019 com-intro

    1/27

     

    COM: A Brief Introduction

    Dan Berger [email protected]

    mailto:[email protected]:[email protected]

  • 8/17/2019 com-intro

    2/27

  • 8/17/2019 com-intro

    3/27

     

    A Brief History of COM  $%e Brain C%ild of Ant%ony &illiams +

    outlined in t'o !internal# M, aers: Object Arc%itecture: Dealing 'it% t%e

    -nno'n or $ye ,afety in a Dynamically/0tensible Class !1233# On In%eritance: &%at It Means and Ho' $o

    -se it !1224#

  • 8/17/2019 com-intro

    4/27

     

    History !Cont.# Origins of COM 'ere O5/ !Object 5ining

    and /mbedding# 1 t%at s%ied 'it%&indo's 6.1 !1227#

     $%e *rst ublic version of COM s%iedin O5/ 7.4 !1226#. DCOM !Distributed COM# 'as released

    in 1228 in ans'er to CO(BA. !&e9ll

    ignore it.# COM" 'as released along 'it% &indo's7444 and 'as rimarily concerned 'it%M$,. $%e DCOM monier 'as droed.

  • 8/17/2019 com-intro

    5/27

     

    Objects vs. Comonents Object Oriented ;rogramming <

    ;olymor%ism " !,ome# 5ate Binding "!,ome# /ncasulation " In%eritance

    Comonent Oriented ;rogramming <;olymor%ism " !(eally# 5ate Binding "!(eal= /nforced# /ncasulation "Interface In%eritance " Binary (euse>

    C%arlie ?indel COM uy> Microsoft Cor. 22

  • 8/17/2019 com-intro

    6/27

     

    COM as !C""#"" !Adated rom7E#

    If you get> t%is= it9s all do'n %ill from%ere.

    In C""= in articular= t%e linage model

    maes binary distribution and reusediFcult.

    Consider: Gou9ve 'ritten a class t%at9sart of a C"" class library.

  • 8/17/2019 com-intro

    7/27 

    C%allenges of Distribution Imagine 'e distribute t%e source for t%e

    class !as is common in C"" class libraries#. If eac% alication t%at uses it statically lins it=

    it gets dulicated !'aste# and is imossible to

    udate*0 in t%e *eld 'it%out redistributing ane' alication.

    If it9s acaged as a s%ared libraryobject= t%elac of binary standardiation moves t%e

    roblem to one of interoeration.  $%e D55 model !but not t%e .so model# can actually

    deal 'it% t%is lac of standardiation= but it9s notretty.

  • 8/17/2019 com-intro

    8/27 

    C%allenges of /ncasulation Assume 'e side)ste t%e comilerliner

    trouble. $%e coast still isn9t clear. $%eC"" standard also lacs any standard

    de*nition for binary encasulation. ,o c%anges to t%e internals of an object

    t%at don’t change it’s interface can!and do# brea code t%at uses t%e

    object. Consider adding rivate member variables.

  • 8/17/2019 com-intro

    9/27

  • 8/17/2019 com-intro

    10/27 

    Interface v. Imlementation C"" suorts searation of interface and

    imlementation at t%e synta0 level + notat t%e binary level. ,o c%anges of t%e imlementation are seen>

    by clients. &e could %ide t%e actual imlementing

    class be%ind an oaJue ointer in t%einterface e0osed to t%e client and

    delegate interface calls t%roug% t%isointer to t%e real> object. /asy for simle= cumbersome for comle0

    interfaces

  • 8/17/2019 com-intro

    11/27 

    Abstract Classes as

    Interfaces &it% t%ree assumtions= 'e canuse abstract classes to solve t%eseroblems:

    1. C)style structs are reresentedidentically across !C""# comilers.

    7. All comilers can be forced to usecommon call conventions.

    6. All comilers on a latform useequivalent virtual callimlementations.

  • 8/17/2019 com-intro

    12/27 

    vtbls and vtrs Assumtion 6 is critical= and turns

    out to be not unfounded= as nearlyall C"" comilers use vtrs and

    vtbls. or eac% class t%e comiler

    generates a !static# array of funcointers to it9s members !it9s vtbl#.

    /ac% instance of eac% class %as a!%idden# member t%at oints to t%evtbl !it9s vrt#.

  • 8/17/2019 com-intro

    13/27 Isearc%able,tring

    /0amle

    class I,earc%able,tring N

      ublic:

      virtual int 5engt%!void# const < 4  virtual int ind!const c%ar Ps# < 4

    Q

    vtr

    vtbl

    5engt% !null#

    ind !null#

  • 8/17/2019 com-intro

    14/27 

    /0amle !cont.#

    class ,,tring : ublicI,earc%able,tring N  ublic:

      ,earc%able,tring!const c%ar Ps#

      R,earc%able,tring!void#  int 5engt%!void# const

      int ind!const c%ar Ps#Q

    ,,tring

    vtr

    vtbl

    ,,tring::5engt%

    ,,tring::ind

  • 8/17/2019 com-intro

    15/27 

    Instantiating an Abstract

    Class Clearly t%e client can9t instantiatean I,earc%able,tring + it9s ureabstract= nor do 'e 'ant t%em

    instantiating a ,,tring + t%atbreas !binary# encasulation.

    ,o 'e need a factory met%od + and

    'e can force it !using e0tern C>#to be accessible to all clients.

  • 8/17/2019 com-intro

    16/27 

    irtual Destructors -nfortunately= t%ere9s a roblem + our

    class lacs a virtual d9tor + so calls todelete 'ill use t%e !default# d9tor on t%e

    I,earc%able,tring class. &e can9t add a virtual d9tor to t%e

    abstract class because dierentcomilers ut dtor9s in dierent laces

    in t%e vtbl. !blec%# ,o 'e add a virtual Delete> met%od to

    t%e abstract class.

  • 8/17/2019 com-intro

    17/27 

    &%at is COM A substrate> for building re)usable

    comonents.

    5anguage neutral it9s easier to use in C""= but can be used from any

    language t%at can generategro vtbl9s and vtrs. Interfaces are de*ned in COM ID5 !ID5"COM

    e0tensions for in%eritance and olymor%ism#

    O, Leutral

    ∃ commercial -ni0 imlementations= and M,suorts COM on Mac ,ystem !O, ST#

    -sing only on t%e COM sec= 'e !OM?$# rolled ouro'n.

  • 8/17/2019 com-intro

    18/27 

    Interfaces Interfaces are uniJuely identi*ed by

    --ID9s !often called -ID9s + t%e termsare eJuivalent# called t%eir IID 

    !interface ID#. Imlementers of an interface are

    uniJuely identi*ed by a --ID calledt%eir CLSID !class ID#.

    All COM objects imlement t%eIUnknown interface.

  • 8/17/2019 com-intro

    19/27 

    I-nno'n ;rovides t%ree met%ods:

    H(/,-5$ UueryInterface!IID iid= void PPv# -5OL Add(ef!void#

    -5OL (elease!void# Add(ef and (elease are for resource

    management !reference counting#.&e9ll mostly ignore t%em.

  • 8/17/2019 com-intro

    20/27 

    UueryInterface UueryInterface is essentially a run)time

    cast + it allo's you to as a comonentif it imlements a seci*c interface.

    If it does= it returns a ointer to t%atinterface ointer in v.  $%in of it as a comilerlanguage neutral

    dynamicVcast oeration.

  • 8/17/2019 com-intro

    21/27

     

    H(/,-5$  $%is is language neutral + so no

    e0cetions. H(/,-5$, are a aced bit*eld return value used all over COM.

    Honestly it9s one of t%e ugliest arts of COM.  $%e most used return value is de*ned as

    ,VO? !success= o#= t%e ot%er is /VAI5!error= failure# but t%ere are ot%ers.

     $%ere are macros ,-CC//D/D!# andAI5/D!# t%at tae an H(/,-5$ andreort success or failure.

  • 8/17/2019 com-intro

    22/27

     

    Instantiating Objects ,o as develoers= 'e %ave interfaces

    !de*ned in ID5# for t%e comonentsavailable in a libraryon t%e system.

    Ho' do 'e actually obtain aninstance of an object 'e 'ant to useT In COM t%is is termed Activation + t%ere

    are t%ree basic tyes= and eac% involvest%e ,CM !service control manager#.

  • 8/17/2019 com-intro

    23/27

  • 8/17/2019 com-intro

    24/27

     

    Activation !cont.# ,ometimes you 'ant an

    imlementation of t%e follo'inginterface t%at meets some set of

    constraints> enter category IDs !CA$IDs#

     Gou can de*ne a set of categories=

    and eac% COM class can advertiset%e categories it imlements.

  • 8/17/2019 com-intro

    25/27

     

    COM as CO(BA)lig%t COM rovides a very eFcient in)rocess

    comonent model. Once ast t%e initial COCreateInstance and

    UueryInterface calls= eac% met%od call issimly a call)by)func)ointer call= essentiallyfree.

    Instantiating a comonent doesn9t

    reJuire any out of rocess= or s%aredmemory oerations + it9s all D55 !or,%ared Object# magic.

  • 8/17/2019 com-intro

    26/27

  • 8/17/2019 com-intro

    27/27

     $%e De*nitive (eferences

    1E $%e Comonent Object Model,eci*cation Microsoft and Digital /Juiment Cor= 1227)

    122W '''.microsoft.comcomresourcescomdocs

    .as

    7E /ssential COM

    Don Bo0= Addison &esley I,BL 4)741)86XX8)W

    http://www.microsoft.com/com/resources/comdocs.asphttp://www.microsoft.com/com/resources/comdocs.asphttp://www.microsoft.com/com/resources/comdocs.asphttp://www.microsoft.com/com/resources/comdocs.asp