1 / 28

Biblioteki Boost

Biblioteki Boost. Piotr Klecha. Plan prezentacji. C++ Przegląd bibliotek Instalacja Przyszłe wersje C++. C++. trudny język n ieczytelny kod zarządzanie pamięcią wielowątkowość STL wydajność. boost::smart_ptr. std::auto_ptr { std ::auto_ptr<X> x1(new X(10));

carney
Download Presentation

Biblioteki Boost

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. Biblioteki Boost Piotr Klecha

  2. Plan prezentacji • C++ • Przegląd bibliotek • Instalacja • Przyszłe wersje C++

  3. C++ • trudny język • nieczytelny kod • zarządzanie pamięcią • wielowątkowość • STL • wydajność

  4. boost::smart_ptr • std::auto_ptr { std::auto_ptr<X> x1(new X(10)); std::cout << x1->x << std::endl; std::auto_ptr<X> x2 = x1; //std::cout << x1->x << std::endl; func(x2); //std::cout << x2->x << std::endl; }

  5. boost::smart_ptr typedef boost::shared_ptr<X> XPtr; XPtr x1(new X(5)); //XPtr x1(new X(5), Deleter()); XPtr x2(x1); std::cout << x1.use_count() << std::endl;//2 std::vector<XPtr> v; v.push_back(x1); v.push_back(x2); x2.reset(); std::cout << x1.use_count() << std::endl;//3 for(unsigned i = 0; i < v.size(); i++) std::cout << v[i]->x << std::endl;

  6. boost::smart_ptr • boost::scoped_ptr • boost::shared_ptr • boost::intrusive_ptr • boost::weak_ptr • boost::scoped_array • boost::shared_array

  7. boost::smart_ptr • pimpl: class X { public: void func(int x, int y); … private: class Impl; boost::shared_ptr<Impl> pimpl; };

  8. boost::optional • std::pair<T, bool> boost::optional<int> y; boost::optional<int> z(10); //std::cout << *y << std::endl; y.reset(5); if (z && y) std::cout << "optional: " << *y << "," << *z << std::endl;

  9. boost::optional • boost::optional<bool> • boost::logic::tribool tribool x(indeterminate); tribool y(true); if (indeterminate(x == y)) …

  10. boost::regex • boost::regex_match boost::regex e("(a?(ba)+\\d+)(tm)?"); std::string s("tmababa11tm"); if (boost::regex_match(s, e)) …

  11. boost::regex • boost::regex_search boost::regex e("(abc)?(def)(ghi)+"); std::string s("abcdefghighidefdefghidefghi"); std::string::const_iterator start, end; start = s.begin(); end = s.end(); boost::match_results<std::string::const_iterator> matched; while (boost::regex_search(start, end, matched, e)) { std::cout << " " << matched.str() << std::endl; start = matched[0].second; }

  12. boost::regex • boost::regex_replace boost::regex e("(abc)(def)((ghi)+)"); std::string s("abcdefghighi defghi"); std::string r("\\1\\3"); std::cout << boost::regex_replace(s, e, r) << std::endl;//abcghighi ghi

  13. boost::lambda using namespace boost::lambda; int seq[5] = {6,2,9,1,5}; std::vector<int> v(seq, seq+5); int count = 0; count = (int)std::count_if(v.begin(), v.end(), _1 > 2); std::cout << "count: " << count << std::endl; //C++: _1 + 1 //C#: x => x + 1

  14. boost::bind • std::bind1st, std::bind2nd, std::binder1st, std::binder2nd, std::ptr_fun(), std::mem_fun() template<typename T> class Greater : public std::binary_function<T, T, bool> { public: bool operator()(const T& x, const T& y) const { return x > y; } }; ... int seq[5] = {6,2,9,1,5}; int count = 0; std::vector<int> v(seq, seq+5); count = (int)std::count_if(v.begin(), v.end(), std::bind2nd(Greater<int>(),2)); std::cout << "count: " << count << std::endl;

  15. boost::bind • std::bind1st(std::ptr_fun(funct), 5)(x); • boost::bind(funct, 5, _1)(x); • boost::bind(funct, x, y); • boost::bind(funct, boost::ref(x), y);

  16. boost::function boost::shared_ptr<X> x1(new X(10)); boost::function<double (int, double)> funct; //boost::function2<double, int, double> funct; funct = boost::bind(&X::func, x1, _1, _2); std::cout << fun(5,5.5) << std::endl; //x1->func(5,5.5)

  17. boost::function boost::function<bool (int, int)> fun; fun = Greater<int>(); fun = &funct; std::cout << "fun: " << fun(5,8) << std::endl; //C#: delegate bool fun(int, int)

  18. boost::tuple • std::pair<T, std::pair<U, V> > • boost::tuple<T, U, V> typedef boost::tuple<int, double, int, char> Tuple; int a = 1; double b = 2.4; int c = 8; char d = 'a'; Tupletup(a, b, c, d); Tuple tup2 = boost::make_tuple(a, b, c, d); if (tup == tup2) …

  19. boost::tuple Tuple tup3(3, 4, 5, 'b'); boost::tie(a,b,c,d) = tup3; int i = boost::get<0>(tup); //tup.get<0>(); boost::get<1>(tup) = 5.4; std::cout << tup << std::endl;

  20. boost::thread • boost::thread_group • boost::thread • boost::mutex • boost::recursive_mutex • boost::mutex::scoped_lock • Mutex, Read/Write Mutex

  21. boost::thread boost::thread_group threads; for (int i = 0; i < 20; ++i) threads.create_thread(&thread_func); threads.join_all();

  22. boost::any • void* std::vector<boost::any> v; int m = 12; std::string s("boost"); boost::shared_ptr<int> mp(new int(50)); v.push_back(m); v.push_back(s); v.push_back(mp); //boost::any_cast<std::string>(v[0]); boost::any_cast<std::string>(v[1]); //boost::any_cast<int*>(v[2]); *boost::any_cast<boost::shared_ptr<int> >(v[2]);

  23. Pozostałe biblioteki • boost::asio • boost::conversion • boost::filesystem • boost::gil • boost::graph • boost::interval • boost::math • boost::mpl • boost::parameter

  24. Pozostałe biblioteki • boost::python • boost::random • boost::rational • boost::spirit • boost::timer • boost::tokenizer • boost::variant

  25. Instalacja

  26. Licencja • Boost Software License

  27. Przyszłe wersje C++ • Technical Report 1 • smart_ptr, function, bind, tuple, regex • C++0x

  28. Koniec :-) Pytania?

More Related