Policy Adaptors and the Boost Iterator Adaptor Library David Abrahams, www.boost.org Jeremy Siek, Indiana University.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

© Copyrights 1998 Algorithmic Research Ltd. All rights Reserved D a t a S e c u r i t y A c r o s s t h e E n t e r p r i s e Algorithmic Research a company.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism.
Design Patterns. What is a Design Pattern? Generic pattern for solving a certain class of problem Learn to recognize the class of problem and apply a.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Order-Independent Texture Synthesis Li-Yi Wei Marc Levoy Gcafe 1/30/2003.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object and Object- Relational Databases.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
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.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Data Structures Using C++ 2E
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Higher order programming Using C++ and boost. C++ concepts // T must model MyFuncConcept template generic_function(T t) { t.MyFunc(); }
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
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.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Boost.Geometry Introduction to Boost.Geometry presented by Mateusz Loskot (Cadcorp Ltd.) at FOSS4G 2010.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
Lexi case study (Part 2) Presentation by Matt Deckard.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
Overview of C++ Templates
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
CSE 332: Design Patterns Introduction to Design Patterns You’ve seen design patterns several times this semester –The Iterator pattern has been discussed.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
C++ Templates.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Introduction to Design Patterns
Behavioral Design Patterns
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
18 – Sequential Containers
Patterns.
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Object Oriented Practices
Lesson 7. Events, Delegates, Generics.
5. 3 Coding with Denotations
Presentation transcript:

Policy Adaptors and the Boost Iterator Adaptor Library David Abrahams, Jeremy Siek, Indiana University

Overview  Motivation  Design of the Library  Implementation of the Library  Metaprogramming Details  Policy Adaptors  Conclusion

Motivation  Container of polymorphic objects  Use a container of pointers? vector  Better: create a wrapper to make it look like a container of objects (not pointers)  To implement, need an iterator adaptor to dereference the pointer

Motivation  Iterator adaptors are everywhere  VTL – M. Weiser & G. Powell  Date Iterator – Jeff Garland  std::reverse_iterator  Checked_iter – Bjarne Stroustrup  Smart Iterators – T. Becker  Compound Iterators – A. Alexandrescu  MTL & BGL iterators

Motivation  Nonetheless, programmers often give up on the idea of creating a new iterator, instead resorting to rewriting algorithms. Why?  Building iterators is  Tedious  Subtle

Motivation  Building iterators is tedious  Redundant operators  Prefix and Postfix:  Dereferencing: * -> []  Comparison: == != >=  Redundant type information  reference == value_type&  pointer == value_type*  Constant/mutable iterator interactions

Motivation  Building iterator adaptors is tedious  Change one aspect of behavior  Leave other aspects the same  Aspects:  Movement  Dereference  Equality Comparison  Dist Measurement  Writing dispatching functions is boring

Motivation  Building iterators is hard to get right  operator-> for input iterators  operator[] for adapted random access iterators

Overview  Motivation  Design of the Library  Implementation of the Library  Policy Adaptors  Conclusion

Design  iterator_adaptor class template  User supplies a policy class and Base type  iterator_adaptor generates a model of Random Access Iterator iterator_adaptor Base Type Policy Class default_iterator_policies

Design  Template parameters:  Base – the underlying adapted type  Policies – defines implementation of core behaviors  Associated Types: Value, Reference, Pointer, and Category  Sometimes deducible from Base (e.g. reverse_iterator)  But not always (e.g., indirect_iterator)

Design  Default policies class forwards all operations to the Base type struct default_iterator_policies { template typename IterAdaptor::reference dereference(const IterAdaptor& x) const { return *x.base(); } //... };

Example Use  indirect_iterator solves container of pointers problem struct indirect_iterator_policies : public default_iterator_policies { template typename IterAdaptor::reference dereference(const IterAdaptor& x) const { return **x.base(); } };

Behavior Delegation indirect_iterator Base iterator indirect_iterator_policies default_iterator_policies value_type dereference

Overview  Motivation  Design of the Library  Implementation of the Library  Policy Adaptors  Conclusion

Implementation  Redundant public interface reduced to core policies reference operator*() const { return policies().dereference(*this); } pointer operator->() const { return &policies().dereference(*this); }

Implementation forwards behavior to the underlying iterator  default_iterator_policies forwards behavior to the underlying iterator template typename IterAdaptor::reference dereference(IterAdaptor& x) { return *x.base(); } // more of the same...

Implementation  Constant/mutable iterator interactions should work: iterator a, b; const_iterator ca, cb a == b; a - b ca == b; ca - cb a == cb; a - cb; ca == cb; ca - cb;

Implementation  All four combinations implemented with one function template template <class B1, class B2, class P, class V1, class V2, class R1, class R2, class P1, class P2, class Cat, class Dist> bool operator==( iterator_adaptor x, iterator_adaptor y) { return x.policies().equal(x.iter(), y.iter()); }

Implementation  Redundant associated types handled via smart defaults  Value type, iterator category, and difference type obtained from std::iterator_traits  Reference and pointer type: if Value explicitly specified then Value&, Value*, otherwise use std::iterator_traits  remove_const  value_type

Implementation  Input iterator dereference may return by- value  operator-> would return a pointer to a temporary! pointer operator->() const { return &policies().dereference(*this); }  Instead return a proxy object that contains the value and has an operator->.  Proxy has lifetime of the full expression

Implementation  Naïve implementation of operator[] reference operator[](difference_type n) { return *(*this+n); }  Consider a disk-based iterator that reads in and caches the object.  (*this + n) creates a temporary iterator  Safe solution: return by value

Implementation  May need additional state  Example: strided_iterator needs to store stride  Solution: store state in policies class; store instance of policies class in iterator_adaptor

Overview  Motivation  Design of the Library  Implementation of the Library  Policy Adaptors  Conclusion

Policy Adaptors  A Design Pattern  Generates new models of a concept (or a family of concepts)  Generates adaptors to selectively modify a behavioral aspect of any model of the concept.

Policy Adaptors 1. Identify core aspects of behavior. For iterators:  Traversal  Dereferencing  Equality comparison  Distance measurement  Associated type exposure 2. Define policy interface that encapsulates the core aspects

Policy Adaptors 3. Write default policies class that dispatches to concept’s public interface 4. Build the adaptor class Generalized model of concept Parameterized on Policies class and Base type Public interface delegates work to Policies class Stores Policies instance as a data member

Behavior Delegation Adaptor Base Type Policies Default Policies Aspect

Policy Adaptors  Policy adaptor design pattern applicable when:  Concept has multiple orthogonal aspects  Concepts with “rich” interface (redundant functions for ease of use)  Concept is popular, i.e., it is a common task to create new models of the concept

Overview  Motivation  Design of the Library  Implementation of the Library  Policy Adaptors  Conclusion

Conclusion  Boost Iterator Adaptor Library automates creation of iterators  Factors out functional redundancy  Change one aspect while reusing others  Makes simple ideas easy to implement  Policy adaptors are a powerful design pattern for creating models of rich concepts