Biblioteki Boost Piotr Klecha Plan prezentacji C Przegld
Biblioteki Boost Piotr Klecha
Plan prezentacji � C++ � Przegląd bibliotek � Instalacja � Przyszłe wersje C++
C++ � trudny język � nieczytelny kod � zarządzanie pamięcią � wielowątkowość � STL � wydajność
boost: : smart_ptr � std: : auto_ptr { std: : auto_ptr<X> x 1(new X(10)); std: : cout << x 1 ->x << std: : endl; std: : auto_ptr<X> x 2 = x 1; //std: : cout << x 1 ->x << std: : endl; func(x 2); } //std: : cout << x 2 ->x << std: : endl;
boost: : smart_ptr typedef boost: : shared_ptr<X> XPtr; XPtr x 1(new X(5)); //XPtr x 1(new X(5), Deleter()); XPtr x 2(x 1); std: : cout << x 1. use_count() << std: : endl; //2 std: : vector<XPtr> v; v. push_back(x 1); v. push_back(x 2); x 2. reset(); std: : cout << x 1. use_count() << std: : endl; //3 for(unsigned i = 0; i < v. size(); i++) std: : cout << v[i]->x << std: : endl;
boost: : smart_ptr � boost: : scoped_ptr � boost: : shared_ptr � boost: : intrusive_ptr � boost: : weak_ptr � boost: : scoped_array � boost: : shared_array
boost: : smart_ptr � pimpl: class X { public: void func(int x, int y); … private: class Impl; boost: : shared_ptr<Impl> pimpl; };
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;
boost: : optional � boost: : optional<bool> � boost: : logic: : tribool x(indeterminate); tribool y(true); if (indeterminate(x == y)) …
boost: : regex � boost: : regex_match boost: : regex e("(a? (ba)+\d+)(tm)? "); std: : string s("tmababa 11 tm"); if (boost: : regex_match(s, e)) …
boost: : regex � boost: : regex_search boost: : regex e("(abc)? (def)(ghi)+"); std: : string s("abcdefghighi defdefghi"); 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; }
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
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
boost: : bind � std: : bind 1 st, std: : bind 2 nd, std: : binder 1 st, std: : binder 2 nd, 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: : bind 2 nd(Greater<int>(), 2)); std: : cout << "count: " << count << std: : endl;
boost: : bind � std: : bind 1 st(std: : ptr_fun(funct), � boost: : bind(funct, 5, _1)(x); 5)(x); x, y); � boost: : bind(funct, boost: : ref(x), y);
boost: : function boost: : shared_ptr<X> x 1(new X(10)); boost: : function<double (int, double)> funct; //boost: : function 2<double, int, double> funct; funct = boost: : bind(&X: : func, x 1, _2); std: : cout << fun(5, 5. 5) << std: : endl; //x 1 - >func(5, 5. 5)
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)
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'; Tuple tup(a, b, c, d); Tuple tup 2 = boost: : make_tuple(a, b, c, d); if (tup == tup 2) …
boost: : tuple Tuple tup 3(3, 4, 5, 'b'); boost: : tie(a, b, c, d) = tup 3; int i = boost: : get<0>(tup); //tup. get<0>(); boost: : get<1>(tup) = 5. 4; std: : cout << tup << std: : endl;
boost: : thread � boost: : thread_group � boost: : thread � boost: : mutex � boost: : recursive_mutex � boost: : mutex: : scoped_lock � Mutex, Read/Write Mutex
boost: : thread_group threads; for (int i = 0; i < 20; ++i) threads. create_thread(&thread_func); threads. join_all();
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]);
Pozostałe biblioteki � boost: : asio � boost: : conversion � boost: : filesystem � boost: : gil � boost: : graph � boost: : interval � boost: : math � boost: : mpl � boost: : parameter
Pozostałe biblioteki � boost: : python � boost: : random � boost: : rational � boost: : spirit � boost: : timer � boost: : tokenizer � boost: : variant
Instalacja
Licencja � Boost Software License
Przyszłe wersje C++ � Technical Report 1 ◦ smart_ptr, function, bind, tuple, regex � C++0 x
Koniec : -) Pytania?
- Slides: 28