Namespaces Calculator Casestudy Consider a simple desk calculator

  • Slides: 25
Download presentation
Namespaces

Namespaces

Calculator Case-study • Consider a simple desk calculator. It can be viewed as being

Calculator Case-study • Consider a simple desk calculator. It can be viewed as being composed of four parts: – The lexer, composing tokens out of characters. – The parser, doing syntax analysis – The symbol table, holding (string, value) pairs. – The driver, main().

Modularization and Interfaces Driver Parser Lexer Symbol table

Modularization and Interfaces Driver Parser Lexer Symbol table

Namespaces • Ideally, every entity in a program belongs to some recognizable logical unit.

Namespaces • Ideally, every entity in a program belongs to some recognizable logical unit. • A namespace is a mechanism for expressing logical grouping. • A namespace separates the implementation from the interface. • The exception is main, which must be global in order for the run time environment to recognize it as special.

Calculator Parser namespace Parser { double prim(bool); double term(bool); double expr(bool); } double Parser:

Calculator Parser namespace Parser { double prim(bool); double term(bool); double expr(bool); } double Parser: : prim(bool get) { … } double Parser: : term(bool get) { … } double Parser: : expr(bool get) { … }

Qualified Names • A namespace is a scope. • The usual scope rules hold

Qualified Names • A namespace is a scope. • The usual scope rules hold for namespaces. • A name from another namespace can be used when qualified by the name of its namespace.

Qualified Names Example double Parser: : term(bool get) { //qualification for function impl. double

Qualified Names Example double Parser: : term(bool get) { //qualification for function impl. double left = prim(get); //no need for qualification same //namespace for(; ; ) switch(Lexer: : curr_tok) { case Lexer: : MUL: } }

Using Declarations • When a name is frequently used outside its namespace, it can

Using Declarations • When a name is frequently used outside its namespace, it can be bother to repeatedly qualify it with its namespace name.

Using Declarations Problem double Parser: : prim(bool get) { if(get) Lexer: : get_token(); switch(Lexer:

Using Declarations Problem double Parser: : prim(bool get) { if(get) Lexer: : get_token(); switch(Lexer: : curr_tok) { case Lexer: : NUMBER: Lexer: : get_token(); return Lexer: : number_value; case Lexer: : NAME: } The repeated qualification Lexer is tedious and distracting.

Using Declarations Solution-1 double Parser: : prim(bool get) { using Lexer: : get_token; using

Using Declarations Solution-1 double Parser: : prim(bool get) { using Lexer: : get_token; using Lexer: : cur_tok; if(get) get_token(); switch(curr_tok) { case Lexer: : NUMBER: get_token(); return Lexer: : number_value; case Lexer: : NAME: } A using declaration introduces a local synonym.

Using Declarations Solution-2 namespace Parser { double prim(bool); double term(bool); double expr(bool); using Lexer:

Using Declarations Solution-2 namespace Parser { double prim(bool); double term(bool); double expr(bool); using Lexer: : get_token; using Lexer: : cur_tok; }

Using Declarations Solution-3 namespace Parser { double prim(bool); double term(bool); double expr(bool); using namespace

Using Declarations Solution-3 namespace Parser { double prim(bool); double term(bool); double expr(bool); using namespace Lexer; } A using directive makes names from a namespace available almost if they had been detached outside their namespace.

Multiple Interfaces We can view every module used to provide two general things: 1.

Multiple Interfaces We can view every module used to provide two general things: 1. The common environment for the function implementing the module. 2. The external interface offered be the module to its users.

Multiple Interfaces Example Parser Implementation Parser Interface Driver

Multiple Interfaces Example Parser Implementation Parser Interface Driver

Multiple Interfaces Example Cont namespace Parser { double prim(bool); double term(bool); double expr(bool); }

Multiple Interfaces Example Cont namespace Parser { double prim(bool); double term(bool); double expr(bool); } namespace Parser_interface { using Parser: : expr; }

Avoiding Name Clashes Namespaces are intended to express logical structure. The simplest such structure

Avoiding Name Clashes Namespaces are intended to express logical structure. The simplest such structure is the distinction between code written by one person vs. code written by someone else.

Name Clashes Example //my. h char f(char); int f(int); class String { }; //your.

Name Clashes Example //my. h char f(char); int f(int); class String { }; //your. h char f(char); class String { }; double f(double);

Name Clashes Solution namespace My { char f(char); int f(int); class String { };

Name Clashes Solution namespace My { char f(char); int f(int); class String { }; } namespace Your { char f(char); class String { }; double f(double); }

Namespace Aliases • If we give short names to our namespaces, the names of

Namespace Aliases • If we give short names to our namespaces, the names of different namespaces will clash. • Long namespaces names can be impractical in real code. • The dilemma can be resolved by providing a short alias for a longer namespace name.

Namespace Aliases Example namespace Programming_Labratory_A { //too long } namespace PLAB = Programming_Labratory_A;

Namespace Aliases Example namespace Programming_Labratory_A { //too long } namespace PLAB = Programming_Labratory_A;

Namespace Composition and Selection namespace His_String { } namespace Her_vector { template<class T> class

Namespace Composition and Selection namespace His_String { } namespace Her_vector { template<class T> class Vector {}; } namespace My_lib { using namespace His_string; using namespace Her_vector; void my_fct(String&); }

Namespace Composition and Selection namespace His_lib { class String {} } namespace Her_lib {

Namespace Composition and Selection namespace His_lib { class String {} } namespace Her_lib { class String {} } namespace My_lib { using namespace His_lib; using namespace Her_lib; using His_lib: : String; //resolve potential clash template<classs T> class List {}; //additional stuff }

Namespaces and C //stdio. h namespace std { int printf(const char* …); } using

Namespaces and C //stdio. h namespace std { int printf(const char* …); } using namespace std; //cstdio namespace std { int printf(const char* …); }

Namespace Are Open • A namespace is open; that is, you can add names

Namespace Are Open • A namespace is open; that is, you can add names to it form several namespaces declarations. namespace A { int f(); } namespace A { int g(); }

Summery • Use namespaces to express logical structure. • Design a namespace so that

Summery • Use namespaces to express logical structure. • Design a namespace so that you can conveniently use it without accidentally gaining access to unrelated namespaces. • Avoid placing heavy notational burdens on users of your namespaces.