Docsity

Implementation Hiding - Introduction to Low Level Programs | CMSC 212, Study notes of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 โ€“ F05 (lect 14)
Announcements
โ—Program #4
โ€“ Posted
โ—Reading
โ€“ Chapter 17
โ€ข skip 17.5.4
โ€“ Chapter 13
2
CMSC 212 โ€“ F05 (lect 14)
Implementation Hiding
โ—Goals
โ€“ Export a public API
โ€“ Hide details of implementation
โ€ข allows implementation to evolve
โ€ข better able to reason about a large system
โ€“ don't need to know how it works, just that it works
โ€ข protects IP in implementation
โ€“ Get the right abstractions
โ€ข This is the hardest part!
โ—Techniques
โ€“ No implementation code in header files
โ€“ Careful use of pointer definitions
โ€ข use opaque pointers
pf3
pf4
pf5
pf8

Partial preview of the text

Download Implementation Hiding - Introduction to Low Level Programs | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 โ€“ F05 (lect 14)^1

Announcements

l Program

  • Posted

l Reading

  • Chapter 17
    • skip 17.5.
  • Chapter 13

CMSC 212 โ€“ F05 (lect 14)^2

Implementation Hiding

l Goals

  • Export a public API
  • Hide details of implementation
    • allows implementation to evolve
    • better able to reason about a large system
      • don't need to know how it works, just that it works
    • protects IP in implementation
  • Get the right abstractions
    • This is the hardest part!

l Techniques

  • No implementation code in header files
  • Careful use of pointer definitions
    • use opaque pointers

CMSC 212 โ€“ F05 (lect 14)^3

Opaque Pointer Types

l Generic Pointers

โ€“ make API return void* pointers

  • lacks type checking
  • useful for building containers
    • such as maps, sets, etc.
  • Example:
    • void *createTable();
    • addItem(void *table, void *data);

l Pointers to structs (struct defintions are hidden)

  • Public header files define API and pointer to abstract types
    • API users only see public header files
  • Private header files define actual structures
    • Include public header files
    • Visible to modules implementing the API

CMSC 212 โ€“ F05 (lect 14)^4

Information Hiding Example

l Project #4 revisited

  • (but donโ€™t change project 3 โ€“ this is just another model)

l Change ast.h (to be the public header)

struct _node; typedef struct _node ASTnode;

l Create astP.h (private header)

struct _node { nodeType type; struct _node *left; struct _node *right; int value; operatorType operator; char *name; };

CMSC 212 โ€“ F05 (lect 14)^7

Table Container

l Can have many underlying implementations

  • Arrays
  • Linked list
  • Hash table (open or chained)
  • Trees (many different trees possible)

l Each implementation has performance attributes

  • How much space is required?
  • How efficient is insert vs. lookup vs. delete?
  • Implementation time vs. amount of time/space saved?

l No single right implementation for all purposes

  • Project #5 will explore two alternative implementations โ€“ different implementations same interface

CMSC 212 โ€“ F05 (lect 14)^8

Designing a Good API

l Why it matters:

  • Can always change a bad implementation
  • Changing an API impacts all users

l Elements of a good design

  • Can be easily used for many different purposes
  • Focus on key ideas, not how they work
    • Lookup/Store data NOT insert into tree
  • Logical units of work with each function call
  • Simple (and common) things should be easy
    • add convenience functions if needed
      • functions that can be expressed using others in API

l Elements of a bad design

  • Exposes aspects of the implementation
  • Abstractions don't match the common usage pattern

CMSC 212 โ€“ F05 (lect 14)^9

A Bad API Example

typedef struct { float real; float imaginary; } complexNumber;

complexNumber *createComplex(); float getRealPart(complexNumber *num); float getImaginaryPart(complexNumber *num); void setNumber(complexNumber *num, float real, float imaginary);

l Hides nothing

l Provides little (to no) value added beyond struct

CMSC 212 โ€“ F05 (lect 14)^10

More Elements of good API Design

l Consistent Return Values across functions

  • 0 is always success
  • negative is always failure

l Consistent parameters

  • use the same names for the same things in different functions
  • how copy semantics are handled
    • are strings copied when passed to API

CMSC 212 โ€“ F05 (lect 14)^13

Command Line Arguments -- Table Example

int debugFlag, limit;

char *inputFileName;

option optionTable[] = {

{ "-debug", Flag, &debugFlag },

{ "-file", StrParam, &inputFileName },

{ "-limit", IntParam, &limit },

/* possible other options go here */

int optionCount = sizeof(optionTable)/sizeof(option);

CMSC 212 โ€“ F05 (lect 14)^14

Project 3 Using Table Driven Parsing

typedef struct { char *name; int opCode; int numRegisters; int usesMem; } opInfo;

static opInfo opTable[] = { { "Load", 1, 1, 1 }, { "Move", 2, 2, 0 }, { "Store", 3, 1, 1 }, { "Add", 4, 3, 0 }, { "Halt", 5, 0, 0 }, { "Negate", 6, 1, 0 }, { "Branch", 7, 2, 1 }, { "Bnn", 8, 1, 1 }, { "Input", 10, 1, 0 }, { "Output", 11, 1, 0 }, };

CMSC 212 โ€“ F05 (lect 14)^15

Environment Variables

l Examples:

  • PATH - used by shell to find programs
  • PRINTER - default printer to use

l Passed as the third parameter to main

  • int main(int argc, char *argv[], char *envp[])
  • consists of a null terminated array of characters
    • KEYWORD=VALUE stored in each one
  • where KEYWORK is the variable name

l C helper functions:

  • char *getenv(const char *keyword);
  • int putenv(const char *string);
    • where keyword is the variable name or string is KEYWORD=VALUE

l Shell Commands to Setup Environement:

  • C-shell and T-shell: setenv FOO_CONFIG ~/foo
  • Bash and Bourne shell: FOO_CONFIG=~/foo