├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── LICENSE ├── README.md ├── any.cpp ├── function.cpp ├── make_shared.cpp ├── property_tree.cpp ├── smart_pointer.cpp └── thread.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | release 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # https://travis-ci.org 2 | 3 | language: cpp 4 | 5 | install: 6 | - sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa 7 | - sudo add-apt-repository --yes ppa:lttng/daily 8 | - sudo apt-get update -qq 9 | - sudo apt-get install -qq libboost-all-dev 10 | script: 11 | - mkdir release && cd release 12 | - cmake .. 13 | - make 14 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(boost_tutorial) 2 | 3 | cmake_minimum_required(VERSION 2.8) 4 | 5 | if(POLICY CMP0042) 6 | cmake_policy(SET CMP0042 NEW) 7 | endif() 8 | 9 | include_directories(/usr/local/include) 10 | 11 | link_directories(/usr/local/lib) 12 | set(REQUIRED_LIB boost_thread boost_system) 13 | 14 | macro (make_exec name) 15 | add_executable(${name} ${name}.cpp) 16 | target_link_libraries(${name} ${REQUIRED_LIB}) 17 | endmacro (make_exec) 18 | 19 | make_exec(any) 20 | make_exec(function) 21 | make_exec(make_shared) 22 | make_exec(property_tree) 23 | make_exec(smart_pointer) 24 | # make_exec(thread) 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 zddhub (zdd) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | boost_tutorial [![Build Status](https://travis-ci.org/zddhub/boost_tutorial.svg?branch=master)](https://travis-ci.org/zddhub/boost_tutorial) 2 | ============== 3 | 4 | A simple tutorial of boost 5 | 6 | includes: 7 | ========= 8 | 9 | * smart pointer : boost::scoped_ptr boost::shared_ptr boost::weak_ptr 10 | * boost::property_tree : parser JSON file 11 | * boost::any 12 | * boost::make_shared 13 | * boost::function (callback) 14 | 15 | How to run 16 | ========== 17 | 18 | ```sh 19 | mkdir release && cd release 20 | cmake .. 21 | make 22 | ``` 23 | -------------------------------------------------------------------------------- /any.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * boost::property_tree tutorial 3 | * 4 | * zdd / zddhub@gmail.com 5 | * https://github.com/zddhub/boost_tutorial 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | #include 13 | 14 | class Customer { 15 | public: 16 | Customer(const std::string& name) : _name(name) {} 17 | std::string toString() { 18 | return _name; 19 | } 20 | private: 21 | std::string _name; 22 | }; 23 | 24 | int main() 25 | { 26 | cout << "boost::any: " << endl; 27 | 28 | //boost::any可以存放任意类型的对象 29 | typedef std::vector vector_any; 30 | vector_any va; 31 | 32 | va.push_back(100); //存放常数 33 | va.push_back(std::string("zddhub.com")); //存放string,注意vector_any.push_back("zddhub.com")会被当成字符数组 34 | va.push_back(new Customer("zdd"));//自定义类型 35 | va.push_back(Customer("zddhub")); 36 | 37 | //根据类型进行显示 38 | for(vector_any::iterator it = va.begin(); it != va.end(); ++it) { 39 | if(it->type() == typeid(int)) { 40 | cout << "int: " << boost::any_cast(*it)<type() == typeid(std::string)) { 42 | cout << "string: " << boost::any_cast(*it).c_str() <type() == typeid(Customer*)) { 44 | Customer *c = boost::any_cast(*it); 45 | cout << "Custormer* : " << c->toString().c_str()<type() == typeid(Customer)) { 48 | cout << "Custormer : " << boost::any_cast(*it).toString().c_str()< 2 | #include 3 | using namespace std; 4 | 5 | #include 6 | #include 7 | 8 | typedef boost::function Callback; 9 | 10 | void test(int n, Callback callback = Callback()) { 11 | for(int i = 0; i < n; i++) { 12 | //cout << "i*i = " << i*i < 8 | #include 9 | using namespace std; 10 | 11 | #include 12 | #include 13 | 14 | class Base { 15 | public: 16 | Base(const std::string& name ): _name(name) { 17 | cout << _name.c_str() << " : Base()" <(),用来显式的消除new调用 53 | //make_shared 和 shared_ptr配合使用,你的代码中将不再需要new和delete 54 | boost::shared_ptr sp = boost::make_shared("Base 1"); 55 | sp->toString(); 56 | 57 | boost::shared_ptr sp1 = boost::make_shared("SubClass 1"); 58 | sp1->toString(); 59 | 60 | //make_shared 比直接创建shared_ptr快,它仅仅分配一次内存,消除了shared_ptr构造时的开销 61 | //time: sp2 > sp3 62 | boost::shared_ptr sp2 = boost::shared_ptr(new SubClass("SubClass 2")); 63 | boost::shared_ptr sp3 = boost::make_shared("SubClass 3"); 64 | 65 | //make_shared limited your to a maximux of 9 arguments 66 | boost::shared_ptr sp4 = boost::make_shared("SubClass 4", 4); 67 | 68 | sp2->toString(); 69 | sp3->toString(); 70 | 71 | 72 | return 0; 73 | } 74 | 75 | -------------------------------------------------------------------------------- /property_tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * boost::property_tree tutorial 3 | * 4 | * zdd / zddhub@gmail.com 5 | * https://github.com/zddhub/boost_tutorial 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | #include 13 | #include 14 | 15 | typedef boost::property_tree::ptree ptree; 16 | 17 | //create JSON file 18 | void write_json(const string& filename) { 19 | 20 | ptree boost_tutorial; 21 | boost_tutorial.put("name", "boost_tutorial"); 22 | 23 | ptree author; 24 | author.put("name", "zdd"); 25 | author.put("email", "zddhub@gmail.com"); 26 | author.put("website", "zddhub.com"); 27 | boost_tutorial.put_child("author", author); 28 | 29 | ptree content; 30 | content.push_back(std::make_pair("chapter1", "boost::thread")); 31 | content.push_back(std::make_pair("chapter2", "smart pointer")); 32 | content.push_back(std::make_pair("chapter3", "boost::property_tree")); 33 | 34 | //if key == null, is array, else is object 35 | ptree array; 36 | array.push_back(std::make_pair("", "boost::scoped_ptr")); 37 | array.push_back(std::make_pair("", "boost::shared_ptr")); 38 | array.push_back(std::make_pair("", "boost::weak_ptr")); 39 | content.put_child("chapter2", array); 40 | 41 | boost_tutorial.put_child("content", content); 42 | 43 | ostringstream os; 44 | boost::property_tree::write_json(os, boost_tutorial); 45 | cout << "JSON file: " <("name")<("name")<("email")<("website")<("chapter1")<("chapter2")<first.data(), value: it->second.data(). 72 | //cout << " " << it->first.data()<second.get("")<second.data()<("chapter3")< 9 | #include 10 | using namespace std; 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | class Object { 17 | public: 18 | Object() { 19 | cout << "Object constructor" < s_ptr(new Object()); 46 | s_ptr->do_something(); 47 | } 48 | 49 | //#: shared_ptr: (强引用)可以共享所有权 50 | /* 51 | * boost::shared_ptr的使用规则: 52 | * 1、避免对shared_ptr所管理的对象直接了进行内存管理操作 53 | * 2、不能自动管理循环引用 54 | * 3、不要构造一个临时的shared_ptr作为函数的参数 55 | * 如:错误的用法: fun(boost:;shared_ptr(new Object()), g()); 56 | * 正确的用法如下: 57 | * boost:;shared_ptr sp(new Object()); 58 | * fun(sp, g()); 59 | */ 60 | boost::shared_ptr sp1(new Object()); 61 | cout << "Ref count: " << sp1.use_count() < sp2 = sp1; 64 | cout << "Ref count: " << sp2.use_count() < sp(new Object()); 76 | boost::weak_ptr wp(sp); 77 | cout << "Ref count: " << sp.use_count() < 9 | #include 10 | 11 | using namespace std; 12 | 13 | #include 14 | 15 | //#1 16 | void run() 17 | { 18 | cout << "Method #1" < f = boost::bind(&InnerThread2::run, this); 56 | boost::thread thread(f); 57 | thread.join(); 58 | } 59 | }; 60 | 61 | //#4 62 | class SingletonThread { 63 | public: 64 | void run() { 65 | cout << "Singleton thread" < f = boost::bind(&SingletonThread::run, SingletonThread::getInstance()); 70 | boost::thread thread(f); 71 | thread.join(); 72 | } 73 | 74 | static SingletonThread* getInstance() { 75 | if(!_instance) { 76 | _instance = new SingletonThread; 77 | } 78 | return _instance; 79 | } 80 | private: 81 | SingletonThread() {} 82 | static SingletonThread* _instance; 83 | }; 84 | SingletonThread* SingletonThread::_instance = 0; 85 | 86 | //#5 87 | class Class { 88 | public: 89 | void run(const std::string& str) { 90 | cout <start(); 130 | 131 | //#5: Outer class thread 132 | Class cls; 133 | boost::thread outer_thread(boost::bind(&Class::run, &cls, "Outer thread")); 134 | outer_thread.join(); 135 | 136 | //#6: Complex method: with return value 137 | boost::function2 f = &add; 138 | boost::packaged_task pt(boost::bind(f, 1, 2)); 139 | boost::unique_future fi = pt.get_future(); 140 | boost::thread th2(boost::move(pt)); 141 | fi.wait(); 142 | th2.join(); 143 | cout <<"x+y="< pt_cls(boost::bind(&Class::add, &cls, 1, 2)); 146 | boost::unique_future fi_cls = pt_cls.get_future(); 147 | boost::thread th2_cls(boost::move(pt_cls)); 148 | fi_cls.wait(); 149 | th2_cls.join(); 150 | cout <<"x+y="<