Download presentation
Presentation is loading. Please wait.
Published byMarsha Golden Modified over 9 years ago
1
Boost Candy A quick introduction to some libraries
2
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
3
Smart pointers library scoped_ptr shared_ptr weak_ptr intrusive_ptr
4
scoped_ptr { boost::scoped_ptr ptr(new int);... } class X { private: boost::scoped_ptr iPtr; }; boost::scoped_ptr get_ptr (); // Illegal
5
shared_ptr boost::shared_ptr get_ptr () { boost::shared_ptr local_ptr(new int); return local_ptr; } { boost::shared_ptr ptr; ptr = get_ptr(); }
6
shared_ptr caveats struct C { boost::shared_ptr mNeighbour; }; { boost::shared_ptr a(new C), b(new C); a->mNeighbour = b; b->mNeighbour = a; }
7
shared_ptr caveats Consider: call(boost::shared_ptr (new Foo(...)), throwing_call())
8
shared_ptr caveats Always use named variables: boost::shared_ptr foo(new Foo(...)); call(foo, throwing_call())
9
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; }
10
intrusive_ptr Interface with libraries that uses internal reference counts
11
Smart pointers Don’t use shared_array and scoped_array Use shared/scoped pointer to std::vector, boost::array Full STL compliance
12
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
13
boost::optional Some functions only sometimes return a value double sqrt(double n) char get_async_input() point polygon::get_any_point_effectively_inside( )
14
boost::optional Alternatives to cope with this: Required pre-condition and undefined behaviour Throw exception Special value (i.e. zero, inf) Return std::pair
15
boost::optional approach Single-element container, contain 0 or 1 element
16
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;
17
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;
18
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
19
boost::optional benefits Easy to read Intuitive value semantics – principle of least astonishment ==, !=,, =
20
boost::assign purpose Easy to fill containers Both insert and initialize
21
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; }
22
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”); }
23
boost::assign example #include { std::list list; boost::assign::push_front(list) (1) (2) (3); }
24
boost::assign example #include using namespace boost::assign; { // 1,2,2,2,2,2,3 std::vector = 1, repeat(5, 2), 3; }
25
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) );
26
boost::assign problems Compilers not supporting templates full out Not 100% STL compliant std-lib implementations
27
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 );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.