Created
July 10, 2014 10:23
-
-
Save chenzx/308ef32b452ddeea3901 to your computer and use it in GitHub Desktop.
深入实践Boost:Boost程序库开发的94个秘笈 笔记
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
深入实践Boost:Boost程序库开发的94个秘笈 | |
跳转至: 导航、 搜索 | |
目录 | |
1 开始编写应用程序 | |
2 数据转换 | |
3 资源管理 | |
4 编译期技巧 | |
5 多线程 | |
6 处理任务 | |
7 处理字符串 | |
8 元编程 | |
9 容器 | |
10 收集平台和编译器信息 | |
11 与系统打交道 | |
12 揭开冰山一角 | |
开始编写应用程序 | |
boost::any依赖于RTTI? | |
typedef boost::variant<boost::blank, int, const char*, std::string> may_var_union_t; | |
Boost.Variant变量不在堆中分配内存,不使用RTTI | |
boost::static_visitor<T>? | |
boost::optional<T>与boost::none | |
看这个变态的类型声明:char ( &f( char (&val)[4] ) )[4]; //f接受并返回一个包含4个元素的数组 | |
boost::tuple ~ std::pair | |
重排参数顺序:boost::bind(std::plus<T>(), _1, _1) | |
固定一个参数的取值: | |
std::bind1st(std::less<int>(), 5) --> boost::bind(std::less<int>(), 5, _1) | |
优化(绕过)复制:ref cref | |
模拟右值引用? | |
... : private boost::nonccopyable { | |
不可复制但可移动? | |
数据转换 | |
int i = boost::lexical_cast<int>("100"); | |
反之亦可 | |
UDT与std::string的转换:定制operator<<及>> | |
dynamic_cast<const T&>(*p).f(); ==> boost::polymorphic_cast<const T*>(p)->f(); | |
boost::spirit::qi::parse(... ?(看来有点复杂,重载了>>、-、|等用作EBNF符号) | |
boost::spirit::qi::rule<const char*, void()> rule_; | |
资源管理 | |
scoped_ptr | |
scoped_array | |
shared_ptr | |
shared_array 或shared_ptr<char[]> cpy = make_shared<char[]>(size); | |
存储任意函数对象:typedef boost::function<void(int)> f_t; | |
boost::function<>也可以接受函数指针或C++ 11 lambda对象 | |
指针的容器:boost::ptr_set<int> s; | |
用派生类的成员初始化基类?? | |
编译期技巧 | |
BOOST_STATIC_ASSERT:如果表达式有,,需要再加一层() | |
typename boost::enable_if_c<boost::is_float<T>::value>::type ??? | |
实现:利用了SFINAE机制? | |
C++ 11 <type_traits>中的std::enable_if | |
C++ 11中没有disable_if,可用std::enable_if<!bool_value>::type代替 | |
从数值创建一个类型:boost::mpl::int_<4> | |
conditional<bool, class T, class F> 这也太神奇了吧(无非就是用typedef抽取名字而已) | |
typedef BOOST_TYPEOF(0.5+0.5f) type; | |
C++ 03中是怎么做到这一只有C++ 11才有的decltype特性的?? | |
多线程 | |
boost::thread t(boost::bind(&f, ....)).detach(); | |
t.join(); //等待线程t完成 | |
boost::scoped_thread<boost::join_if_joinable> t( (boost::thread(&some_func)) ); | |
每个共享变量配一个互斥锁: | |
int shared_i = 0; | |
boost::mutex mutex_i; | |
boost::lock_guard<boost::mutex> lock(mutex_i); | |
多次锁定:使用boost::recursive_mutex! | |
使用原子变量:boost::atomic<int> shared_2(0); | |
work_queue | |
boost::deque< boost::function<void()> > tasks_; | |
应该在一个循环中检测条件,而不是只在一个if中检查 | |
std::condition_variable? | |
允许共享锁(读锁):boost::shared_mutex? | |
线程局部变量 | |
boost::thread_specific_ptr<connection> ptr; | |
if(!ptr.get()) ptr.reset(new connection); | |
//对比:C++ 11有特殊的关键字thread_local! | |
中断线程 | |
待中断线程设置中断点:boost::this_thread::interruption_point(); | |
外部线程中断之:that_thread.interrupt(); | |
如果成功中断,将触发that_thread抛出boost::thread_interrupted异常,从而介绍执行 | |
创建线程组:boost::thread_group threads; | |
=> threads.join/interrupt_all(); //这只是一个简单的API封装吧? | |
处理任务 | |
boost::asio::io_service? | |
boost::asio::deadline_timer | |
非阻塞障碍? | |
p133 根本不需要阻塞!需要做的事情只是发送4个fill_data任务,并让最后完成的任务调用compute_send_data | |
?照这里说来,barrier原语都不需要用了,只要有atomic/lock-free的计数器就行了 | |
处理字符串 | |
使用正则表达式查找替换 | |
boost::smatch results; | |
bool matched = regex_search(str, results, re); | |
if( matched ) | |
std::copy(results.begin()+1 /*第一个是整体匹配*/, results.end(), \ | |
std::ostream_iterator<std::string>(std::cout), ", ") ); | |
如果要替换,使用regex_replace(str, re, replace); 其中replace使用$1、$2、...引用子表达式 | |
返回一对迭代器表示子串: | |
split_iter_t sentences = boost::make_split_iterator(str, | |
boost::algorithm::token_finder(boost::is_any_of("?!.")) ); | |
boost::iterator_range<const char*> range = *sentences; | |
boost::string_ref:引用一个string对象的部分,不创建临时对象 | |
元编程 | |
类型向量?boost::mpl::vector | |
p163 最难的部分来了 | |
typedef boost::mpl::transform<types, boost::remove_cv<boost::mpl::_1> >::type concv_types; | |
?懒惰地对元函数求值 | |
boost::fusion::for_each:把元组包装成迭代器 | |
容器 | |
Boost.Bimap:key<-->value双向映射 | |
typedef boost::bimap<std::string, boost::bimaps::multiset_of<std::size_t> > name_id_t; | |
多索引容器(比多个hashmap使用更少的内存) | |
typedef boost::multi_index::multi_index_container<person, | |
boost::multi_index::ordered_unique<boost::multi_index::identity<person> >, | |
boost::multi_index::hashed_non_unique<boost::multi_index::member<person, int, &person::age_> >, | |
... | |
boost::container::slist:删除插入不使得迭代器失效? | |
boost::container::flat_set:这比vector到底有何改善?? | |
收集平台和编译器信息 | |
#if (BOOST_VERSION >= 105200) | |
与系统打交道 | |
IPC传递一块内存: | |
boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "shm-1", 1024); | |
atomic_t& a = *segment.find_or_construct<atomic_t>("shm-counter")(0); | |
在共享内存中使用指针: | |
boost::interprocess::offset_ptr<int> pointer_; //pointer_ = &value;(重载了赋值操作?) | |
p231 内存映射文件比传统的读取速度更快。。。 | |
协程? | |
typedef boost::coroutines::coroutine<std::string&(std::size_t)> corout_t; | |
...(难道是用longjmp实现的) | |
揭开冰山一角 | |
使用Graphviz工具进行Boost.Graph的可视化? | |
p244 boost::math::changesign:可以处理NaN?? | |
Boost.Gil库:处理图像? | |
boost::gil::any_image<img_types> source; //支持哪些输入格式 | |
boost::gil::png_read_image(filepath, source); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment