27
Boost Candy A quick introduction to some libraries

Boost Candy

  • Upload
    malise

  • View
    42

  • Download
    1

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Boost Candy

Boost CandyA quick introduction to some libraries

johant
En introduktion till några väldigt andvändbara bibliotek.Även C++, generic programming och STL.Bitvis mycket tekniska detaljer - avbryt och fråga!Tänkt som diskussionKom gärna förbi mig och fråga saker
Page 2: Boost Candy

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

johant
I'd say much better quality than standard library
johant
Ten boost libraries accepted in C++0x
johant
Windows, linux, mac, solaris and moreAll big compilers
johant
Founded by C++ Standard Commitee
Page 3: Boost Candy

Smart pointers library• scoped_ptr• shared_ptr • weak_ptr• intrusive_ptr

johant
Does everybody know what a smart pointer is
Page 4: Boost Candy

scoped_ptr{boost::scoped_ptr<int> ptr(new int);...

}

class X {private:boost::scoped_ptr<SomeClass> iPtr;

};

boost::scoped_ptr<int> get_ptr (); // Illegal

Page 5: Boost Candy

shared_ptr

boost::shared_ptr<int> get_ptr ()

{

boost::shared_ptr<int> local_ptr(new int);

return local_ptr;

}

{

boost::shared_ptr<int> ptr;

ptr = get_ptr();

}

Page 6: Boost Candy

shared_ptr caveats

struct C

{

boost::shared_ptr<C> mNeighbour;

};

{

boost::shared_ptr<C> a(new C), b(new C);

a->mNeighbour = b;

b->mNeighbour = a;

}

Page 7: Boost Candy

shared_ptr caveats

Consider:

call(boost::shared_ptr<Foo>(new Foo(...)),

throwing_call())

Page 8: Boost Candy

shared_ptr caveats

Always use named variables:

boost::shared_ptr<Foo> foo(new Foo(...));

call(foo, throwing_call())

Page 9: Boost Candy

weak_ptrnamespace {weak_ptr<SomeClass> cache;

}

shared_ptr<SomeClass> cached_get(){shared_ptr<SomeClass> ptr = cache.lock();

if (!ptr){

ptr.reset(new SomeClass(...));cache = ptr;

}return ptr;

}

Page 10: Boost Candy

intrusive_ptr

Interface with libraries that uses internal reference counts

Page 11: Boost Candy

Smart pointersDon’t use shared_array and scoped_array

Use shared/scoped pointer to std::vector, boost::array

Full STL compliance

Page 12: Boost Candy

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

Page 13: Boost Candy

boost::optional

Some functions only sometimes return a value

double sqrt(double n)

char get_async_input()

point polygon::get_any_point_effectively_inside()

Page 14: Boost Candy

boost::optional

Alternatives to cope with this:

• Required pre-condition and undefined behaviour• Throw exception• Special value (i.e. zero, inf)• Return std::pair<bool, return_value>

Page 15: Boost Candy

boost::optional approach

Single-element container, contain 0 or 1 element

Page 16: Boost Candy

boost::optional example

boost::optional<double> sqrt(double n)

{

boost::optional<double> result;

if (n >= 0) result = sqrt_helper(n);

return result;

}

boost::optional<double> root = sqrt(n);

if (root) std::cout << *root;

Page 17: Boost Candy

boost::optional exampleenum ChessPiece { WHITE_KING, BLACK_KING, …};

typedef std::vector< std::vector< boost::optional<ChessPiece> > >

ChessBoard;

ChessBoard board(…);

board.at(0).at(3) = WHITE_KING;

board.at(7).at(4) = BLACK_KING;

Page 18: Boost Candy

boost::optional approach

• Deep copy semanticscopies of the container implies copies of the value • Deep relational semanticscompare container size and if match, contained value

Page 19: Boost Candy

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

Page 20: Boost Candy

boost::assign purpose

Easy to fill containers

Both insert and initialize

Page 21: Boost Candy

boost::assign example

#include <boost/assign/std/vector.hpp> // For +=

using namespace boost::assign;

{

std::vector<int> values = (-2) (-1) (0);

values += 1,2,3,4,5,6,7,8,9;

}

Page 22: Boost Candy

boost::assign example

#include <boost/assign/map_list_of.hpp>

#include <boost/assign/list_inserter.hpp>

namespace boost::assign = ba;

{

std::map<int, std::string> mapping

= ba::map_list_of (1, ”one”) (2, ”two”);

ba::insert(mapping) (3, ”three”) (4, ”four”);

}

Page 23: Boost Candy

boost::assign example

#include <boost/assign/list_inserter.hpp>

{

std::list<int> list;

boost::assign::push_front(list) (1) (2) (3);

}

Page 24: Boost Candy

boost::assign example

#include <boost/assign.hpp>

using namespace boost::assign;

{

// 1,2,2,2,2,2,3

std::vector<int> = 1, repeat(5, 2), 3;

}

Page 25: Boost Candy

boost::assign example

std::map<std::string, std::list<int> > numbers

= map_list_of ( ”Primes", list_of (1)(2)(3)(5) )

( ”Zero", list_of (0) )

( ”Odd", list_of (1)(3)(5)(7) );

Page 26: Boost Candy

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

Page 27: Boost Candy

boost::assign work-arounds

std::vector<int> v = list_of(1)(2)(3).to_container( v );

std::set<int> s = list_of(1)(2)(3)(4).to_container( s );

std::map<int,int> m = map_list_of(1,2)(2,3).to_container( m );

std::stack<int> st = list_of(1)(2)(3)(4).to_adapter( st );

std::queue<int> q = list_of(1)(2)(3)(4).to_adapter( q );

boost::array<int,4> a = list_of(1)(2)(3)(4).to_array( a );