C++ Run-Time Representation Point object Point vtable Code for - - PowerPoint PPT Presentation

c run time representation
SMART_READER_LITE
LIVE PREVIEW

C++ Run-Time Representation Point object Point vtable Code for - - PowerPoint PPT Presentation

12/9/15 C++ Run-Time Representation Point object Point vtable Code for move vptr OO: optimizing with static types, x 2 code-sharing mechanisms y 3 1. Prelude: C++-style


slide-1
SLIDE 1

12/9/15 1

OO: ¡optimizing ¡with ¡static ¡types, ¡ code-­‑sharing ¡mechanisms

  • 1. Prelude: ¡ C++-­‑style ¡ representation
  • 2. Multiple ¡inheritance
  • 3. Interfaces
  • 4. T

raits/mixins

Uses ¡slides ¡by ¡Steve ¡Freund

C++ ¡Run-­‑Time ¡Representation

2 Point ¡object x vptr Point ¡vtable Code ¡for ¡move 3 y

C++ ¡Run-­‑Time ¡Representation

2 4 red Point ¡object ColorPoint ¡object x vptr x vptr c Point ¡vtable ColorPoint ¡vtable Code ¡for ¡move Code ¡for ¡move Code ¡for ¡darken Data ¡at ¡same ¡offset Function ¡pointers ¡at ¡same ¡offset 3 5 y y

Contrast ¡with ¡Smalltalk ¡representation

2 3 x y newX:Y: draw moveDx:Dy: Point ¡object Point ¡class Template Method ¡dictionary ... 4 5 x y newX:Y:C: color draw ColorPoint ¡object ColorPoint ¡class Template Method ¡dictionary red color ...

slide-2
SLIDE 2

12/9/15 2

C++ ¡goals

  • Zero-­‑cost ¡abstraction
  • Pay ¡as ¡you ¡go
  • High ¡level ¡of ¡control ¡over ¡

representation/performance.

Single ¡v. ¡Multiple ¡Inheritance

  • Single ¡inheritance: ¡tree
  • one ¡superclass
  • Linear ¡code ¡reuse
  • Multiple ¡inheritance: ¡DAG
  • multiple ¡ superclasses
  • Compositional ¡code ¡reuse

8

A B C D E X Y V W Z

Multiple ¡Inheritance ¡

class Image { int x,y; virtual void show(); }; class Serializable { string file; virtual void write(); virtual void read(); }; class SerialImage: public Image, public Serializable { virtual void write(); virtual void show(); };

vptr y x I::show vptr file S::write S::read

Multiple ¡Inheritance ¡

SerialImage *si = new SerialImage(); si -> show(); si -> write(); si -> read(); Image *i = si; i -> show(); Serializable *s = si; s -> write(); s -> read();

vptr file y x

si

I::show SI::write S::read i? ¡s?

slide-3
SLIDE 3

12/9/15 3

Downside: ¡too ¡many ¡choices

If ¡V and ¡Z both ¡define ¡method ¡ m, ¡what ¡does ¡Y inherit? What ¡does ¡super mean? What ¡if ¡X defines ¡a ¡method ¡ m that ¡Z but ¡ not ¡V overrides? If ¡X defines ¡ fields, ¡should ¡ Y have ¡one ¡or ¡tow ¡fs? Is ¡the ¡answer ¡different ¡ if ¡V ¡and ¡W ¡both ¡ define ¡field ¡g? ... C++ ¡approach: ¡ support ¡ all ¡combinations ¡ of ¡possibilities! ¡

11

X Y V W Z

ArtistCowboys

12

class PocketWearer { var pocket … } class Artist extends PocketWearer { def draw(): Unit = … // get brush from pocket and draw with it } class Cowboy extends PocketWearer { def draw(): Unit = … // draw pistol from pocket and aim } class ArtistCowboy extends Artist, Cowboy { // not Scala! } new ArtistCowboy.draw // ??????????

Java ¡Interfaces

interface KeyListener { void keyPressed(KeyEvent e); void keyReleased(KeyEvent e); void keyTyped(KeyEvent e); } interface MouseListener { void buttonClicked(MouseEvent e); } class TextEditor extends Panel implements KeyListener,MouseListener { void keyPressed(KeyEvent e) { /* code */ } void keyReleased(KeyEvent e) { /* code */ } void keyTyped(KeyEvent e) { /* code */ } void buttonClicked(MouseEvent e) { /* code */ } } MouseListener KeyListener Panel T extEditor

Interfaces ¡vs. ¡Multiple ¡Inheritance

class DefaultKeyListener { // default is to do nothing void keyPressed(KeyEvent e) { } void keyReleased(KeyEvent e) { } void keyTyped(KeyEvent e) { } } class TextField : public Panel, DefaultKeyListener { void keyTyped(KeyEvent e) { /* code here */ } ... } class TerminalWindow : public Panel, DefaultKeyListener { void keyTyped(KeyEvent e) { /* code here */ } ... }

slide-4
SLIDE 4

12/9/15 4

Interfaces ¡vs. ¡Multiple ¡Inheritance

interface KeyListener { void keyPressed(KeyEvent e); void keyReleased(KeyEvent e); void keyTyped(KeyEvent e); } class TextField extends Panel implements KeyListener { void keyPressed(KeyEvent e) { } void keyTyped(KeyEvent e) { /* code here */ } void keyReleased(KeyEvent e) { } } class TerminalWindow extends Panel implements KeyListener { void keyPressed(KeyEvent e) { } void keyTyped(KeyEvent e) { /* code here */ } void keyReleased(KeyEvent e) { } }

Dispatching ¡interface ¡method ¡calls?

2 getX Point ¡object x vptr Point ¡vtable 3 y move

...

2 getX ColorPoint ¡object x vptr ColorPoint ¡vtable 3 y move red c getColor

... ...

Scala ¡Traits

  • Completely ¡ Abstract

trait AbsIterator[T] { def hasNext(): boolean; def next(): T; }

  • Partially ¡ Implemented

trait RichIterator[T] extends AbsIterator[T] { def foreach(f: T => Unit): Unit = { while (hasNext()) f(next()) } }

Scala ¡Traits

trait CountingIterator[T] extends AbsIterator[T] { var count = 0; abstract override def next(): T = { count = count + 1; super.next(); } def count() = count; } class FancyStringIterator(s: String) extends StringIterator(s) with RichIterator[Char] with CountingIterator[Char] { ... }

AbsItr RichItr FancyStringItr StringItr CountItr

slide-5
SLIDE 5

12/9/15 5

Name ¡Resolution ¡via ¡ Linearization

A B C D

trait A { } trait B extends A { } trait C extends A { } class D extends B with C { }

Right-­‑first ¡depth-­‑first ¡search: [D,C,A,B,A] Eliminate ¡all ¡but ¡last ¡occurrence: [D,C,B,A]

Scala ¡Traits

trait CountingIterator[T] extends AbsIterator[T] { var count = 0; abstract override def next(): T = { count = count + 1; super.next(); } def count() = count; } class FancyStringIterator(s: String) extends StringIterator(s) with RichIterator[Char] with CountingIterator[Char] { ... }

AbsItr RichItr FancyStringItr StringItr CountItr http://en.wikipedia.org/wiki/Mixin