280 likes | 412 Views
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));
E N D
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> 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; }
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;
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 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("tmababa11tm"); if (boost::regex_match(s, e)) …
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; }
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
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::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;
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);
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)
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)
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) …
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;
boost::thread • boost::thread_group • boost::thread • boost::mutex • boost::recursive_mutex • boost::mutex::scoped_lock • Mutex, Read/Write Mutex
boost::thread 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
Licencja • Boost Software License
Przyszłe wersje C++ • Technical Report 1 • smart_ptr, function, bind, tuple, regex • C++0x
Koniec :-) Pytania?