1 / 27

Boost Candy

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

malise
Télécharger la présentation

Boost Candy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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<int> ptr(new int); ... } class X { private: boost::scoped_ptr<SomeClass> iPtr; }; boost::scoped_ptr<int> get_ptr (); // Illegal

  5. 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(); }

  6. 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; }

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

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

  9. weak_ptr namespace { 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; }

  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

  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<bool, return_value>

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

  16. 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;

  17. boost::optional example enum 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;

  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 <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; }

  22. 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”); }

  23. boost::assign example #include <boost/assign/list_inserter.hpp> { std::list<int> list; boost::assign::push_front(list) (1) (2) (3); }

  24. 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; }

  25. 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) );

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

  27. 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 );

More Related