Docsity

COM in CMSC 433: Interfaces, QueryInterface, & Reference Counting - Prof. Alan L. Sussman, Study notes of Programming Languages

Toggle sidebar
bg1
CMSC 433, Alan Sussman, U. Maryland (via
Bill Pugh) 1
CMSC433, Fall 2001
COM -Component Object Model
Alan Sussman
December 4, 2001
CMCS 433, Fall 2001 -Alan Sussman 2
Administrivia
•Project 6 due Monday, 12/10, and commentary for
Project 5
–grading guidelines posted
•No office hours tomorrow
–Thursday 2-4
•Classes/Interfaces to look at for Project 6
–java.awt.Toolkit
–javax.swing.{JFrame, JMenu, JPanel}
–java.awt.{Component, Graphics} –repaint()
–java.awt.event.{ActionListener, MouseListener,
MouseAdapter}
CMCS 433, Fall 2001 -Alan Sussman 3
Component Object Model
•Language independent
•OS independent (in theory)
•Way to allow components to be designed,
deployed, upgraded
–Need to interact with code written after you
were deployed
CMCS 433, Fall 2001 -Alan Sussman 4
Immutable interfaces
•Interact through interfaces
–No direct access to fields
–Interfaces must never be changed
–Interfaces assigned a GUID
•avoid name clashes
•allow versioning by assigning a new GUID
CMCS 433, Fall 2001 -Alan Sussman 5
COM
•A binary compatibility standard
–interface pointers
Client IStream Method 1
Method 2
Method 3
Method 4
CMCS 433, Fall 2001 -Alan Sussman 6
Multiple interfaces
•Components can implement multiple
interfaces
•Different interfaces may correspond to
different entry points to object
–C++ multiple inheritance
–adaptors
pf3
pf4
pf5

Partial preview of the text

Download COM in CMSC 433: Interfaces, QueryInterface, & Reference Counting - Prof. Alan L. Sussman and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Alan Sussman, U. Maryland (via

CMSC433, Fall 2001

COM - Component Object Model

Alan Sussman December 4, 2001

CMCS 433, Fall 2001 - Alan Sussman 2

Administrivia

  • Project 6 due Monday, 12/10, and commentary for Project 5 - grading guidelines posted
  • No office hours tomorrow
    • Thursday 2 -
  • Classes/Interfaces to look at for Project 6
    • java.awt.Toolkit
    • javax.swing.{JFrame, JMenu, JPanel}
    • java.awt.{Component, Graphics} – repaint()
    • java.awt.event.{ActionListener, MouseListener, MouseAdapter}

CMCS 433, Fall 2001 - Alan Sussman 3

Component Object Model

  • Language independent
  • OS independent (in theory)
  • Way to allow components to be designed, deployed, upgraded - Need to interact with code written after you were deployed

CMCS 433, Fall 2001 - Alan Sussman 4

Immutable interfaces

  • Interact through interfaces
    • No direct access to fields
    • Interfaces must never be changed
    • Interfaces assigned a GUID
      • avoid name clashes
      • allow versioning by assigning a new GUID

CMCS 433, Fall 2001 - Alan Sussman 5

COM

  • A binary compatibility standard
    • interface pointers

Client IStream (^) Method 1 Method 2 Method 3 Method 4

CMCS 433, Fall 2001 - Alan Sussman 6

Multiple interfaces

  • Components can implement multiple interfaces
  • Different interfaces may correspond to different entry points to object - C++ multiple inheritance - adaptors

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Fall 2001 - Alan Sussman 7

Multiple interfaces

Client IStream^ Method 1 Method 2 Method 3 Method 4 IPrint (^) Method 1 Method 2 Method 3 Method 4 (^) CMCS 433, Fall 2001 - Alan Sussman 8

Interfaces in COM

  • Similar to interfaces in Java
    • no variables
  • Interfaces have a 128 bit Unique ID
    • immutable, never changed, no collisions
  • In writing COM code, always use pointers/references to interfaces

CMCS 433, Fall 2001 - Alan Sussman 9

Reference counting

  • COM objects are reference counted
    • each object keeps track of the number of pointers to it
  • When ref count goes to zero, element deletes itself
  • Cycles can be a problem
  • Remembering where to put all increments and decrements can be a problem

CMCS 433, Fall 2001 - Alan Sussman 10

Each interface counted separately

  • Each entry point/interface to a COM object is ref counted separately - allows an adaptor to be garbage collected

CMCS 433, Fall 2001 - Alan Sussman 11

IUnknown

  • All COM interfaces must extend interface IUnknown , with methods: - HRESULT QueryInterface(const IID& iid, void ** ppv) - ULONG AddRef() // inc ref count - ULONG Release() // dec ref count

CMCS 433, Fall 2001 - Alan Sussman 12

QueryInterface

  • Like a C++ dynamic cast
    • Does the component support this interface?
    • If so, gives back a pointer of that kind
      • incrementing the ref count for that interface
    • Else, signal failure

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Fall 2001 - Alan Sussman 19

Implementing QueryInterface

STDMETHODIMP QueryInterface(const IID& iid, void **ppv) { if (iid == IID_IUnknown || iid == IID_IFoo) ppv = static_cast< Foo>(this); else if ( iid == IID_IBar) *ppv = static_cast<Bar *>(this); else { *ppv = null; return E_NOINTERFACE; } reinterpret_cast<IUnknown >(ppv)->AddRef(); return S_OK; }

CMCS 433, Fall 2001 - Alan Sussman 20

Things to note

  • If you support 100 interfaces, cascaded if statements are going to get expensive - can’t use case statements (UIID’s aren’t ints) - could use custom hashtable
  • Separate ref counts
    • could put call to AddRef in each branch
    • would eliminate reinterpret_cast
    • but would increase code size

CMCS 433, Fall 2001 - Alan Sussman 21

I want everything

  • Why can’t I ask
    • what is the list of all of the interfaces you support?
  • What would you do with the list of all interfaces a component supports?
  • Can use component categories

CMCS 433, Fall 2001 - Alan Sussman 22

Component categories

  • Assigned a GUID
  • Corresponds to a set of interfaces
    • If a component is registered as member of a category
    • instances of that component support all of those interfaces
    • will still need to use QueryInterface to move between interfaces

CMCS 433, Fall 2001 - Alan Sussman 23

Categories in Java

  • Just define a Mega-interface
    • An interface that extends all of the interfaces in the category
    • Ask if class/component implements that
    • Can use reference to Mega-interface type to invoke all methods from any interface in category
    • No casting needed

CMCS 433, Fall 2001 - Alan Sussman 24

Component reuse

  • How to reuse components?
    • Base class (implementation inheritance)
    • Containment (have as a member)
      • Delegation - Some methods get directly forwarded
      • Adaptor - Some methods get translated
    • Aggregation

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Fall 2001 - Alan Sussman 25

Aggregation

  • Say I have a component Bar
    • which uses a component Foo
  • Foo implements the IFoo interface
  • Bar also implements the IFoo interface
    • by handing things off to its Foo
  • Could handle by delegation
    • but that adds an additional level of indirection

CMCS 433, Fall 2001 - Alan Sussman 26

Using Aggregation

  • When someone asks a Bar for its IFoo interface - just hand them a reference to your Foo - handles all IFoo function calls
  • But what if you invoke QueryInterface on the IFoo reference and ask for an IBar interface?

CMCS 433, Fall 2001 - Alan Sussman 27

Delegation/Forwarding

IBar

Bar

Foo

IUnknown

IFoo

IUnknown

inner

CMCS 433, Fall 2001 - Alan Sussman 28

Aggregation

Bar

Foo

IUnknown

IFoo

outer

IUnknown

IBar

inner

CMCS 433, Fall 2001 - Alan Sussman 29

Supporting aggregation

  • You must be able to be told that you have an outer component
  • Calls to QueryInterface should be routed to your outer component
  • Reference counts are a little tricky
    • cycle could prevent stuff from being collected