Template Policies and Traits By Example

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming.
Overview of C++ Templates
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
Overview of STL Function Objects
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Programming Abstractions Cynthia Lee CS106B. Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference.
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
Washington WASHINGTON UNIVERSITY IN ST LOUIS More on the STL: Function Objects and Generic Algorithms in the STL Fred Kuhns Computer Science and Engineering.
Motivation for Generic Programming in C++
Jim Fawcett CSE 691 – Software Modeling and Analysis Fall 2000
Constructors and Destructors
C++ Lesson 1.
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2005.
Copyright © Jim Fawcett Spring 2017
Template Method Pattern Iterator Pattern
Object-Oriented Programming Concepts
CS 106X – Programming Abstractions in C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2010
Jim Fawcett CSE687 – Object Oriented Design Spring 2016
Standard Template Library
How to be generic Lecture 10
Design of a HashTable and its Iterators
C++ Templates.
Design of a HashTable and its Iterators
Templates.
Advanced Object-Oriented Programming Features
Operator overloading Conversions friend inline
Type Traits By Reggie Meisler.
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Operator overloading Conversions friend inline
10.3 Details of Recursion.
Chapter 14 Templates C++ How to Program, 8/e
Satisfying Open/Closed Principle
Operator overloading Conversions friend inline
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Jim Fawcett Copyright ©
The dirty secrets of objects
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Programming Abstractions
Object-Oriented Programming (OOP) Lecture No. 32
Exceptions An exception signals an error, and has the ability to propagate upward through the call stack for easier management To “raise” an exception,
Data structures in C++.
Programming Abstractions
Built-In (a.k.a. Native) Types in C++
Chapter 9 Objects and Classes
Abstraction: Generic Programming, pt. 2
Constructors and Destructors
Strategy and Template Method Patterns, Single User Protection
COP 3330 Object-oriented Programming in C++
Chapter 8: Class Relationships
Overview of C++ Polymorphism
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Lab4 problems More about templates Some STL
Template Policies and Traits By Example
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Jim Fawcett Copyright ©
Recitation Outline Hash tables in C++ STL Examples Recursive example
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Standard Template Library
Introduction to Classes and Objects
Presentation transcript:

Template Policies and Traits By Example Jim Fawcett CSE687 – Object Oriented Design Spring 2003

Template Policies A policy is a class designed to tailor behavior of a template class in some narrow specific way: Locking policy for class that may be used in multi-threaded program: template<typename T, typename LockPolicy> class queue; Allows designer to use Lock or noLock policy. Enqueuing policy for a thread class: template <typename queue> class thread; Allows designer to optionally add queue and queue operations as part of thread class’s functionality. HashTable policy for hashing table addresses: template <typename key, typename value, typename Hash> class HashTable; Allows designer to provide hashing tailored for application long after HashTable class was designed.

Hashing Policy template <typename T> class HashBase { public: HashBase() : _size(0) {}; void Size(long int size) { _size = size; } virtual long int operator() (const T& t) const = 0; protected: long int _size; }; class HashString : public HashBase<std::string> long int operator() (const std::string& s) const;

Hashing Policy Class HashBase: HashString: Provides protocol, e.g.: long int operator()(const T& t) Has _size member and Size(long int) to allow HashTable to inform it about the table size. HashTable does this in its constructor. HashString: Provides the hashing algorithm for its std::string type. These hash classes are functors – function objects. The operator()(T t) provides function call syntax: HashTable<key,value,Hash> ht(size); long int loc = ht.operator()(key)  long int loc = ht(key)

Why Use Non-Polymorphic HashBase? The HashBase class has two significant roles to play: Prevent users from creating hash table for type without a suitable hash function. Results in compile-time failure. Provide table size to user-defined class without the user needing to figure out how to get the table size from the container, or even remembering to do so. HashBase and HashTable conspire to take care of that detail.

Traits are Types and Values that Support Generic Programming Traits types are introduced by typedefs: typedef key key_type; typedef value value_type; typedef HashIterator iterator; Traits allow a template parameterized class to be used in a function that is not aware of the parameter types. The function simply uses the “standard” name for the type provided by the class’s traits

Trait Types: value_type, iterator //----< sum values in container >--------------------------- // // Demonstrates use of container traits // - Sums values across all elements of container // - could just as easily sum keys using key_type // declaration template <typename Cont> Cont::value_type sum(Cont &c) { Cont::value_type sum = Cont::value_type(); Cont::iterator it = c.begin(); while(it != c.end()) sum += (it->Value()); ++it; } return sum;

Traits Used in Cont::value_type sum(Cont &c) In the HashTable module – sum function: Container’s real value type, unknown to sum: std::string value; Trait type, provided by all containers that use sum: Cont::value_type value; Container’s real iterator type: HashIterator<double,string,HashDouble> it Trait type: Cont::iterator it

End Of Traits and Policies Policies allow a designer of a library to make the behavior of a class configurable at application design time. To lock or not to lock To enqueue or not enqueue Which way to hash Traits provide common type aliases used by all containers so functions that operate on the containers can be written to apply to every one of them without modification. std::string  value_type std::string&  reference_type std::string*  pointer_type HashIterator<double,string,HashDouble>  iterator