Boost Candy A quick introduction to some libraries.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Static modeling (& lifetime) Using types to test, document and reduce system complexity.
Equality Programming in C# Equality CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Model Checking C++ Daniel Kroening Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.
Introduction to the STL
The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
Optional?. Agenda Type traits intro Interface & usage Caveats.
Arrays, Pointers and Structures CS-240 Dick Steflik.
The Standard Template Library. Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai.
CSE333 SECTION 7. Midterm Debrief Hex View 1. Find a hex editor. 2. Learn ‘goto offset’ command. 3. See HW3 pictures. The header: Magic word Checksum.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
 Review structures  Program to demonstrate a structure containing a pointer.
Natalia Yastrebova What is Coverity? Each developer should answer to some very simple, yet difficult to answer questions: How do I find new.
Higher order programming Using C++ and boost. C++ concepts // T must model MyFuncConcept template generic_function(T t) { t.MyFunc(); }
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Chapter 19 Standard Template Library Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 C++ Syntax and Semantics, and the Program Development Process.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
C++ Memory Overview 4 major memory segments Key differences from Java
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
CLIP Command Line InterPreter for a subset of C++.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Writing Correct C++ Programs without “delete” Huang-Ming Huang CSE332 Guest Lecture Washington University in St. Louis.
Templates & STL Stefan Roiser, Lorenzo Moneta CERN PH/SFT.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
TR1: C++ on the Move Pete Isensee Director XNA Developer Connection Microsoft.
CSE 332: Memory management with C++ classes Memory Management with Classes Review: for non-static built-in (native) types –default constructor and destructor.
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
ROOT Workshop M.Frank LHCb/CERN Improvements in the I/O Area (*)  General I/O related improvements  Tree related issues  Plans (*) I present.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Chapter 5 The MFC Collection Classes. How to store many data? 1. Use an Array 2. Use a Linked List value: 10 Node * p a a value: 20 Node * p value: 30.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
Object-Oriented Programming (OOP) Lecture No. 41
CO4301 – Advanced Games Development Week 1 Introduction
C++ Lesson 1.
Memory Management with Classes
UNIT – I Linked Lists.
Pointers, Enum, and Structures
Alexandru Razvan Caciulescu University POLITEHNICA of Bucharest
C++ Memory Management Idioms
Functionality & Performance in Milestone 1
Week 7 Part 2 Kyle Dewey.
Overview of Memory Layout in C++
C++ Smart Pointers CSE 333 Spring 2018
The Standard Template Library
STL Containers Some other containers in the STL.
CSE 333 – Section 10 Final Review.
Functionality & Performance in Milestone 1
C++ Smart Pointers CSE 333 Winter 2019
14 – Sequential Containers
Presentation transcript:

Boost Candy A quick introduction to some libraries

Boost? Collection of C++ libraries and a community Meant to establish best practice Reference implementations for future C++ standards Widely used, well tested, industrial strength code Portable

Smart pointers library scoped_ptr shared_ptr weak_ptr intrusive_ptr

scoped_ptr { boost::scoped_ptr ptr(new int);... } class X { private: boost::scoped_ptr iPtr; }; boost::scoped_ptr get_ptr (); // Illegal

shared_ptr boost::shared_ptr get_ptr () { boost::shared_ptr local_ptr(new int); return local_ptr; } { boost::shared_ptr ptr; ptr = get_ptr(); }

shared_ptr caveats struct C { boost::shared_ptr mNeighbour; }; { boost::shared_ptr a(new C), b(new C); a->mNeighbour = b; b->mNeighbour = a; }

shared_ptr caveats Consider: call(boost::shared_ptr (new Foo(...)), throwing_call())

shared_ptr caveats Always use named variables: boost::shared_ptr foo(new Foo(...)); call(foo, throwing_call())

weak_ptr namespace { weak_ptr cache; } shared_ptr cached_get() { shared_ptr ptr = cache.lock(); if (!ptr) { ptr.reset(new SomeClass(...)); cache = ptr; } return ptr; }

intrusive_ptr Interface with libraries that uses internal reference counts

Smart pointers Don’t use shared_array and scoped_array Use shared/scoped pointer to std::vector, boost::array Full STL compliance

Smart pointers comparison boost::shared_ptr shared ownership boost::weak_ptr break circular ownership, implement cache boost::scoped_ptr RAII boost::intrusive_ptr Interface 3rd party libraries std::auto_ptr Sole, transferable ownership

boost::optional Some functions only sometimes return a value double sqrt(double n) char get_async_input() point polygon::get_any_point_effectively_inside( )

boost::optional Alternatives to cope with this: Required pre-condition and undefined behaviour Throw exception Special value (i.e. zero, inf) Return std::pair

boost::optional approach Single-element container, contain 0 or 1 element

boost::optional example boost::optional sqrt(double n) { boost::optional result; if (n >= 0) result = sqrt_helper(n); return result; } boost::optional root = sqrt(n); if (root) std::cout << *root;

boost::optional example enum ChessPiece { WHITE_KING, BLACK_KING, …}; typedef std::vector > > ChessBoard; ChessBoard board(…); board.at(0).at(3) = WHITE_KING; board.at(7).at(4) = BLACK_KING;

boost::optional approach Deep copy semantics copies of the container implies copies of the value Deep relational semantics compare container size and if match, contained value

boost::optional benefits Easy to read Intuitive value semantics – principle of least astonishment ==, !=,, =

boost::assign purpose Easy to fill containers Both insert and initialize

boost::assign example #include // For += using namespace boost::assign; { std::vector values = (-2) (-1) (0); values += 1,2,3,4,5,6,7,8,9; }

boost::assign example #include namespace boost::assign = ba; { std::map mapping = ba::map_list_of (1, ”one”) (2, ”two”); ba::insert(mapping) (3, ”three”) (4, ”four”); }

boost::assign example #include { std::list list; boost::assign::push_front(list) (1) (2) (3); }

boost::assign example #include using namespace boost::assign; { // 1,2,2,2,2,2,3 std::vector = 1, repeat(5, 2), 3; }

boost::assign example std::map > numbers = map_list_of ( ”Primes", list_of (1)(2)(3)(5) ) ( ”Zero", list_of (0) ) ( ”Odd", list_of (1)(3)(5)(7) );

boost::assign problems Compilers not supporting templates full out Not 100% STL compliant std-lib implementations

boost::assign work-arounds std::vector v = list_of(1)(2)(3).to_container( v ); std::set s = list_of(1)(2)(3)(4).to_container( s ); std::map m = map_list_of(1,2)(2,3).to_container( m ); std::stack st = list_of(1)(2)(3)(4).to_adapter( st ); std::queue q = list_of(1)(2)(3)(4).to_adapter( q ); boost::array a = list_of(1)(2)(3)(4).to_array( a );