28
Biblioteki Boost Piotr Klecha

Biblioteki Boost

  • Upload
    carney

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

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 x1(new X(10)); - PowerPoint PPT Presentation

Citation preview

Page 1: Biblioteki Boost

Biblioteki BoostPiotr Klecha

Page 2: Biblioteki Boost

C++ Przegląd bibliotek Instalacja Przyszłe wersje C++

Plan prezentacji

Page 3: Biblioteki Boost

trudny język nieczytelny kod zarządzanie pamięcią wielowątkowość STL wydajność

C++

Page 4: Biblioteki Boost

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

Page 5: Biblioteki Boost

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; //2std::vector<XPtr> v;v.push_back(x1);v.push_back(x2);x2.reset();std::cout << x1.use_count() << std::endl; //3for(unsigned i = 0; i < v.size(); i++)

std::cout << v[i]->x << std::endl;

boost::smart_ptr

Page 6: Biblioteki Boost

boost::scoped_ptr boost::shared_ptr boost::intrusive_ptr

boost::weak_ptr

boost::scoped_array boost::shared_array

boost::smart_ptr

Page 7: Biblioteki Boost

pimpl:class X{public:

void func(int x, int y);…

private:class Impl;boost::shared_ptr<Impl> pimpl;

};

boost::smart_ptr

Page 8: Biblioteki Boost

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

Page 9: Biblioteki Boost

boost::optional<bool> boost::logic::tribool

tribool x(indeterminate);tribool y(true);if (indeterminate(x == y))

boost::optional

Page 10: Biblioteki Boost

boost::regex_match

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

boost::regex

Page 11: Biblioteki Boost

boost::regex_search

boost::regex e("(abc)?(def)(ghi)+");std::string s("abcdefghighi defdefghi defghi");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

Page 12: Biblioteki Boost

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::regex

Page 13: Biblioteki Boost

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::lambda

Page 14: Biblioteki Boost

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

Page 15: Biblioteki Boost

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::bind

Page 16: Biblioteki Boost

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

Page 17: Biblioteki Boost

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::function

Page 18: Biblioteki Boost

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 tup2 = boost::make_tuple(a, b, c, d);if (tup == tup2)

boost::tuple

Page 19: Biblioteki Boost

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::tuple

Page 20: Biblioteki Boost

boost::thread_group boost::thread boost::mutex boost::recursive_mutex boost::mutex::scoped_lock

Mutex, Read/Write Mutex

boost::thread

Page 21: Biblioteki Boost

boost::thread_group threads;for (int i = 0; i < 20; ++i)

threads.create_thread(&thread_func);threads.join_all();

boost::thread

Page 22: Biblioteki Boost

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]);

boost::any

Page 23: Biblioteki Boost

boost::asio boost::conversion boost::filesystem boost::gil boost::graph boost::interval boost::math boost::mpl boost::parameter

Pozostałe biblioteki

Page 24: Biblioteki Boost

boost::python boost::random boost::rational boost::spirit boost::timer boost::tokenizer boost::variant

Pozostałe biblioteki

Page 25: Biblioteki Boost

Instalacja

Page 26: Biblioteki Boost

Boost Software License

Licencja

Page 27: Biblioteki Boost

Technical Report 1◦ smart_ptr, function, bind, tuple, regex

C++0x

Przyszłe wersje C++

Page 28: Biblioteki Boost

Koniec :-)

Pytania?