├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── common ├── jamfile ├── std.hpp └── test.cpp ├── functional ├── factory.cpp ├── hash.cpp ├── jamfile └── mem_fn.cpp ├── generic ├── call_traits.cpp ├── concept_check.cpp ├── enable_if.cpp ├── function_types.cpp └── jamfile ├── go ├── intrusive ├── jamfile ├── list.cpp ├── multi_hooks.cpp ├── sample1.cpp ├── sample2.cpp ├── sample3.cpp ├── sample4.cpp ├── set.cpp └── unordered_set.cpp ├── iostream ├── array.cpp ├── bracket_line_filter.cpp ├── combine.cpp ├── compose.cpp ├── container.cpp ├── counter.cpp ├── file.cpp ├── filtering_stream.cpp ├── hex_filter.cpp ├── invert.cpp ├── jamfile ├── newline.cpp ├── regex.cpp ├── restrict.cpp ├── sample1.cpp ├── sample2.cpp ├── sha1_filter.cpp ├── source_sink.cpp ├── tee.cpp ├── traits.cpp └── zlib.cpp ├── iterator ├── counting_iterator.cpp ├── filter_iterator.cpp ├── function_input_iterator.cpp ├── function_output_iterator.cpp ├── generator_iterator.cpp ├── indirect_iterator.cpp ├── iterator_adaptor.cpp ├── iterator_facade.cpp ├── iterator_traits.cpp ├── jamfile ├── next_prior.cpp ├── permutation_iterator.cpp ├── reverse_iterator.cpp ├── shared_container_iterator.cpp ├── step_iterator.hpp ├── transform_iterator.cpp └── zip_iterator.cpp ├── jamroot ├── lib └── jamfile ├── meta ├── jamfile ├── meta.cpp └── mp_utils.hpp ├── mordencpp ├── auto.cpp ├── class.cpp ├── for.cpp ├── generic.cpp ├── init.cpp ├── jamfile ├── lambda.cpp ├── nullptr.cpp ├── others.cpp ├── rvalue.cpp ├── safety.cpp └── thread.cpp ├── mpl ├── algorithm.cpp ├── assert.cpp ├── container.cpp ├── dl1.cpp ├── dl2.cpp ├── if.cpp ├── integral.cpp ├── jamfile ├── lambda.cpp └── libtest.cpp ├── multi_index ├── composite_key.cpp ├── hashed_index.cpp ├── jamfile ├── key_extractor.cpp ├── modify.cpp ├── multi_index_container.cpp ├── ordered_index.cpp ├── person.hpp ├── random_access_index.cpp ├── sample1.cpp ├── sample2.cpp ├── sample3.cpp └── sequenced_index.cpp ├── practice ├── client.cpp ├── intdef.hpp ├── io_service_pool.hpp ├── jamfile ├── srv.cpp ├── tcp_buffer.hpp ├── tcp_server.hpp └── tcp_session.hpp ├── practice2 ├── client.cpp ├── intdef.hpp ├── io_service_pool.hpp ├── jamfile ├── srv.cpp ├── tcp_buffer.hpp ├── tcp_server.hpp └── tcp_session.hpp ├── preprocessor ├── arithmetic.cpp ├── basic.cpp ├── cat.cpp ├── functions.cpp └── seq.cpp ├── ptr_container ├── algorithm.cpp ├── assign.cpp ├── indirect_fun.cpp ├── jamfile ├── nullable.cpp ├── ptr_array.cpp ├── ptr_circular_buffer.cpp ├── ptr_deque.cpp ├── ptr_list.cpp ├── ptr_map.cpp ├── ptr_sequence_adapter.cpp ├── ptr_set.cpp ├── ptr_unordered_map.cpp ├── ptr_unordered_set.cpp ├── ptr_vector.cpp ├── reversible_ptr_container.cpp ├── sample.cpp └── view_clone_allocator.cpp ├── range ├── adaptors.cpp ├── algorithm.cpp ├── iterator_range.cpp ├── jamfile ├── join.cpp └── traits.cpp ├── type_traits ├── categories.cpp ├── examples.cpp ├── function_traits.cpp ├── implementation.cpp ├── jamfile ├── modifications.cpp ├── properties.cpp └── relationships.cpp └── utility ├── addressof.cpp ├── base_from_member.cpp ├── checked_delete.cpp ├── compressed_pair.cpp ├── conversion.cpp ├── jamfile ├── numeric.cpp ├── pointee.cpp ├── predef.cpp └── scope_exit.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | *.swp 31 | bin/ 32 | 33 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | dist: trusty #1404 3 | #dist: xenial #1604 4 | 5 | os: linux 6 | 7 | notifications: 8 | email: false 9 | 10 | language: cpp 11 | 12 | compiler: 13 | - gcc 14 | - clang 15 | 16 | cache: 17 | ccache: true 18 | 19 | env: 20 | global: 21 | - BOOST_VERSION=1_64_0 22 | 23 | #before_install: 24 | 25 | install: 26 | - wget -O boost_${BOOST_VERSION}.tar.gz https://dl.bintray.com/boostorg/release/1.64.0/source/boost_${BOOST_VERSION}.tar.gz && tar -xzf boost_${BOOST_VERSION}.tar.gz 27 | 28 | script: 29 | - cd boost_${BOOST_VERSION} 30 | - ./bootstrap.sh --without-libraries=graph,locale,regex,serialization,wave,mpi > build.log 2>&1 || (cat build.log && exit 1) 31 | - sudo ./b2 link=static -j2 install > build.log 2>&1 || (cat build.log && exit 1) 32 | - cd ./tools/build 33 | - ./bootstrap.sh 34 | - sudo ./b2 install > build.log 2>&1 || (cat build.log && exit 1) 35 | - cd ../../.. 36 | - ./go 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Professional Boost 2 | Professional C++11/14 & Boost Development Guide 3 | 4 | [![Build Status](https://travis-ci.org/chronolaw/professional_boost.svg?branch=master)](https://travis-ci.org/chronolaw/professional_boost) 5 | 6 | ## Requirements 7 | * Linux or other UNIX like 8 | * C++11(gcc 4.6 or later) 9 | * Boost(1.60 or later) 10 | 11 | ## See Also 12 | * [ngx_cpp_dev](https://github.com/chronolaw/ngx_cpp_dev) - Nginx cpp development kit, with the power of C++11 and Boost Library. 13 | * [favorite-nginx](https://github.com/chronolaw/favorite-nginx) - Selected favorite nginx modules and resources. 14 | * [ngx_google_perftools_profiler](https://github.com/chronolaw/ngx_google_perftools_profiler_module) - A better tools for nginx with google_perftools 15 | * [boost guide](https://github.com/chronolaw/boost_guide.git) - Sample code for Boost library Guide. 16 | * [annotated_nginx](https://github.com/chronolaw/annotated_nginx) - 注释nginx,学习研究源码 17 | 18 | -------------------------------------------------------------------------------- /common/jamfile: -------------------------------------------------------------------------------- 1 | unit-test t : test.cpp ; 2 | -------------------------------------------------------------------------------- /common/std.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PROFESSIONAL_BOOST_STD_HPP 4 | #define _PROFESSIONAL_BOOST_STD_HPP 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | //using namespace std; 20 | 21 | #endif //_PROFESSIONAL_BOOST_STD_HPP 22 | 23 | -------------------------------------------------------------------------------- /common/test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | cout << __cplusplus << endl; 12 | cout << BOOST_VERSION << endl; 13 | cout << BOOST_LIB_VERSION<< endl; 14 | 15 | cout << BOOST_PLATFORM << endl; 16 | cout << BOOST_COMPILER << endl; 17 | cout << BOOST_STDLIB << endl; 18 | } 19 | -------------------------------------------------------------------------------- /functional/factory.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | using namespace boost; 11 | 12 | /////////////////////////////////////// 13 | 14 | void case1() 15 | { 16 | auto pi = factory()(); 17 | auto ps = factory()(); 18 | auto pp = factory*>()(); 19 | 20 | checked_delete(pi); 21 | checked_delete(ps); 22 | checked_delete(pp); 23 | 24 | auto up = factory >()(); 25 | auto sp = factory >()(); 26 | 27 | } 28 | 29 | /////////////////////////////////////// 30 | 31 | void case2() 32 | { 33 | int a = 10, b = 20; 34 | auto pi = factory()(a); 35 | auto ps = factory()("char* lvalue"); 36 | auto pp = factory*>()(a, b); 37 | 38 | checked_delete(pi); 39 | checked_delete(ps); 40 | checked_delete(pp); 41 | 42 | //auto pi2 = factory()(10); 43 | //auto pp2 = factory*>()(1, 2); 44 | 45 | auto p = std::bind(factory(),10)(); 46 | checked_delete(p); 47 | } 48 | 49 | /////////////////////////////////////// 50 | 51 | #include 52 | #include 53 | 54 | void case3() 55 | { 56 | auto i = value_factory()(); 57 | auto str = value_factory()("hello"); 58 | auto p = value_factory>()(i, str); 59 | 60 | { 61 | auto i = bind(value_factory(), 10)(); 62 | 63 | boost::ignore_unused(i); 64 | } 65 | } 66 | 67 | /////////////////////////////////////// 68 | 69 | int main() 70 | { 71 | std::cout << "hello factory" << std::endl; 72 | 73 | case1(); 74 | case2(); 75 | case3(); 76 | } 77 | -------------------------------------------------------------------------------- /functional/hash.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | #include 5 | //using namespace std; 6 | 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | std::cout << std::hex; 15 | 16 | std::cout << hash()(0x2000) << std::endl; 17 | std::cout << hash()(1.732) << std::endl; 18 | std::cout << hash()("string") << std::endl; 19 | 20 | std::complex c(1.0, 2.0); 21 | std::cout << hash()(c) << std::endl; 22 | 23 | std::cout << hash()("string") << std::endl; 24 | 25 | std::vector v(12); 26 | std::cout << hash()(v) << std::endl; 27 | 28 | std::map m; 29 | std::cout << hash()(m); 30 | 31 | std::array ar; 32 | std::cout << hash()(ar) << std::endl; 33 | 34 | //std::stack s; 35 | //std::cout << hash()(s) << std::endl; 36 | } 37 | 38 | /////////////////////////////////////// 39 | 40 | #include 41 | #include 42 | 43 | void case2() 44 | { 45 | boost::unordered_set > us; 46 | boost::unordered_map > um; 47 | } 48 | 49 | /////////////////////////////////////// 50 | 51 | class person final 52 | { 53 | private: 54 | int id; 55 | std::string name; 56 | unsigned int age; 57 | public: 58 | person(int a, const char* b, unsigned int c): 59 | id(a),name(b), age(c) 60 | {} 61 | 62 | size_t hash_value() const 63 | { 64 | //return hash()(id); 65 | 66 | size_t seed = 1984; 67 | hash_combine(seed, id); 68 | hash_combine(seed, name); 69 | hash_combine(seed, age); 70 | 71 | return seed; 72 | } 73 | 74 | friend bool operator==(person const & l, person const & r) 75 | { return l.id == r.id;} 76 | 77 | }; 78 | 79 | size_t hash_value(person const & p) 80 | { return p.hash_value();} 81 | 82 | void case3() 83 | { 84 | person p(1, "adam", 20); 85 | std::cout << hash()(p) << std::endl; 86 | 87 | unordered_set us; //无序集合容器 88 | 89 | us.insert(person(1, "adam", 20)); 90 | us.insert(person(2, "eva", 20)); 91 | assert(us.size() == 2); 92 | 93 | } 94 | 95 | /////////////////////////////////////// 96 | class demo_class 97 | { 98 | private: 99 | std::vector v; 100 | int x; 101 | public: 102 | size_t hash_value() 103 | { 104 | size_t seed = 0; 105 | hash_combine(seed, x); 106 | hash_range(seed, v.rbegin(), v.rend()); 107 | 108 | return seed; 109 | } 110 | }; 111 | 112 | void case4() 113 | { 114 | std::vector v {1,2,5,8,15}; 115 | auto hv = hash_range(v.begin(), v.end()); 116 | 117 | unordered_set us {1,2,5,8,15}; 118 | hv = hash_range(us.begin(), us.end()); 119 | 120 | (void)hv; 121 | } 122 | 123 | /////////////////////////////////////// 124 | 125 | int main() 126 | { 127 | std::cout << "hello hash" << std::endl; 128 | 129 | case1(); 130 | case2(); 131 | case3(); 132 | case4(); 133 | } 134 | -------------------------------------------------------------------------------- /functional/jamfile: -------------------------------------------------------------------------------- 1 | unit-test hash : hash.cpp ; 2 | unit-test mem_fn : mem_fn.cpp ; 3 | unit-test factory : factory.cpp ; 4 | -------------------------------------------------------------------------------- /functional/mem_fn.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | #include 5 | //using namespace std; 6 | 7 | #include 8 | using namespace boost; 9 | 10 | class demo_class 11 | { 12 | public: 13 | int x; 14 | demo_class(int a = 0):x(a){} 15 | 16 | void print() 17 | { 18 | std::cout << x << std::endl; 19 | } 20 | 21 | void hello(const char* str) 22 | { 23 | std::cout << str << std::endl; 24 | } 25 | }; 26 | 27 | /////////////////////////////////////// 28 | 29 | void case1() 30 | { 31 | demo_class d; 32 | mem_fn(&demo_class::print)(d); 33 | 34 | demo_class *p = &d; 35 | mem_fn(&demo_class::hello)(p, "hello"); 36 | 37 | std::unique_ptr up(new demo_class(100)); 38 | mem_fn(&demo_class::print)(up); 39 | assert(up.get() != 0); 40 | 41 | std::shared_ptr sp(new demo_class); 42 | mem_fn(&demo_class::hello)(sp, "world"); 43 | 44 | std::vector v(10); 45 | std::for_each(v.begin(), v.end(), 46 | mem_fn(&demo_class::print)); 47 | 48 | std::cout << "mem var : " << mem_fn(&demo_class::x)(d) << std::endl; 49 | } 50 | 51 | /////////////////////////////////////// 52 | #include 53 | 54 | void case2() 55 | { 56 | demo_class d; 57 | 58 | bind(&demo_class::print, _1)(d); 59 | bind(&demo_class::hello, _1, "hello")(&d); 60 | 61 | std::vector v(10); 62 | std::for_each(v.begin(), v.end(), 63 | bind(&demo_class::hello, _1, "world")); //调用有参成员函数 64 | 65 | } 66 | 67 | /////////////////////////////////////// 68 | 69 | int main() 70 | { 71 | std::cout << "hello mem_fn" << std::endl; 72 | 73 | case1(); 74 | case2(); 75 | } 76 | -------------------------------------------------------------------------------- /generic/call_traits.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | typedef call_traits str_traits; 12 | 13 | str_traits::value_type 14 | scat(str_traits::param_type s1, 15 | str_traits::param_type s2) 16 | { 17 | assert(is_reference::value); 18 | 19 | assert((is_same::value)); 21 | 22 | return s1 + s2; 23 | } 24 | 25 | /////////////////////////////////////// 26 | 27 | template 28 | class demo_class 29 | { 30 | public: 31 | typedef typename call_traits::value_type v_type; 32 | typedef typename call_traits::param_type p_type; 33 | typedef typename call_traits::reference r_type; 34 | typedef typename call_traits::const_reference cr_type; 35 | private: 36 | v_type v; 37 | public: 38 | demo_class(p_type p): v(p){} 39 | 40 | v_type value() 41 | { return v; } 42 | 43 | r_type get() 44 | { return v; } 45 | }; 46 | 47 | void case1() 48 | { 49 | int a[3] = {1,2,3}; 50 | demo_class di(a); 51 | assert(di.value()[0] == 1); 52 | 53 | char c = 'A'; 54 | demo_class dc(c); 55 | assert(dc.get() == c); 56 | 57 | } 58 | 59 | /////////////////////////////////////// 60 | 61 | int main() 62 | { 63 | std::cout << "hello call_traits" << std::endl; 64 | 65 | case1(); 66 | } 67 | -------------------------------------------------------------------------------- /generic/enable_if.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | template 12 | //T print(T x , typename boost::enable_if >::type* =0) 13 | //typename boost::enable_if, T >::type print(T x) 14 | typename disable_if, T >::type print(T x) 15 | { 16 | cout << "int:" << x << endl; 17 | return x; 18 | } 19 | 20 | void case1() 21 | { 22 | print(10); 23 | //print(10.0); 24 | } 25 | 26 | /////////////////////////////////////// 27 | 28 | template 29 | class demo_class 30 | { }; 31 | 32 | template 33 | class demo_class >::type> 34 | { }; 35 | 36 | 37 | /////////////////////////////////////// 38 | 39 | int main() 40 | { 41 | std::cout << "hello enable_if" << std::endl; 42 | 43 | case1(); 44 | } 45 | -------------------------------------------------------------------------------- /generic/jamfile: -------------------------------------------------------------------------------- 1 | unit-test enable_if : enable_if.cpp ; 2 | unit-test call_traits : call_traits.cpp ; 3 | unit-test concept_check : concept_check.cpp ; 4 | 5 | # disable 6 | #unit-test function_types : function_types.cpp ; 7 | -------------------------------------------------------------------------------- /go: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | dirs="common functional generic 4 | intrusive iostream iterator 5 | meta mordencpp mpl 6 | multi_index practice practice2 7 | ptr_container 8 | range type_traits utility 9 | " 10 | 11 | for d in $dirs 12 | do 13 | echo "-------$d-------" 14 | cd ${d} 15 | #b2 clean 16 | b2 > build.log 2>&1 || (cat build.log && exit 1) 17 | b2 clean > build.log 2>&1 || (cat build.log && exit 1) 18 | rm build.log 19 | cd .. 20 | done 21 | -------------------------------------------------------------------------------- /intrusive/jamfile: -------------------------------------------------------------------------------- 1 | unit-test sample1 : sample1.cpp ; 2 | unit-test sample2 : sample2.cpp ; 3 | unit-test sample3 : sample3.cpp ; 4 | unit-test sample4 : sample4.cpp ; 5 | 6 | unit-test list : list.cpp ; 7 | unit-test set : set.cpp ; 8 | unit-test unordered_set : unordered_set.cpp ; 9 | 10 | unit-test multi_hooks : multi_hooks.cpp ; 11 | -------------------------------------------------------------------------------- /intrusive/multi_hooks.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | using namespace boost::intrusive; 15 | using namespace boost; 16 | 17 | /////////////////////////////////////// 18 | 19 | class point: 20 | public list_base_hook<>, 21 | public unordered_set_base_hook> 22 | { 23 | public: 24 | int x,y; 25 | point(int a = 0, int b = 0): 26 | x(a), y(b){} 27 | 28 | set_member_hook<> m_shook; 29 | set_member_hook<> m_mshook; 30 | 31 | friend bool operator==(const point& l, const point& r) 32 | { return l.x == r.x && l.y == r.y;} 33 | 34 | friend bool operator<(const point& l, const point& r) 35 | { return l.x < r.x; } 36 | 37 | friend std::size_t hash_value(const point& p) 38 | { 39 | size_t seed = 2016; 40 | hash_combine(seed, p.x); 41 | hash_combine(seed, p.y); 42 | return seed; 43 | } 44 | }; 45 | 46 | /////////////////////////////////////// 47 | 48 | void case1() 49 | { 50 | using namespace boost::assign; 51 | ptr_vector vec = 52 | ptr_list_of(0)(1)(2)(3)(4)(3)(2); 53 | 54 | typedef make_list::type list_t; 55 | 56 | typedef make_unordered_set>> >::type uset_t; 58 | 59 | typedef make_set, &point::m_shook>>::type set_t; 61 | 62 | typedef make_multiset, &point::m_mshook>>::type mset_t; 64 | 65 | list_t lt(vec.rbegin(), vec.rend()); 66 | for(auto& p : lt) 67 | { 68 | cout << p.x << ","; 69 | } 70 | cout << endl; 71 | 72 | set_t s(lt.begin(), lt.end()); 73 | mset_t ms(lt.begin(), lt.end()); 74 | assert(s.size() == 5 && ms.size() == 7); 75 | 76 | uset_t::bucket_type buckets[20]; 77 | uset_t us(uset_t::bucket_traits(buckets, 20)); 78 | 79 | us.insert(vec.begin(), vec.end()); 80 | assert(us.size() == 5); 81 | 82 | lt.pop_front(); 83 | assert(lt.size() == 6 && 84 | s.size() == 5 && ms.size() == 7); 85 | 86 | set_t::iterator iter = 87 | set_t::s_iterator_to(*us.begin()); 88 | cout << iter->x << endl; 89 | 90 | } 91 | 92 | /////////////////////////////////////// 93 | 94 | int main() 95 | { 96 | std::cout << "hello intrusive multi hooks" << std::endl; 97 | 98 | case1(); 99 | } 100 | -------------------------------------------------------------------------------- /intrusive/sample1.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | class point: boost::noncopyable 11 | { 12 | public: 13 | int x,y; 14 | 15 | typedef point* node_ptr; 16 | node_ptr next; 17 | 18 | point(int a = 0, int b = 0): 19 | x(a), y(b), next(nullptr){} 20 | 21 | node_ptr get_next() 22 | { return next;} 23 | 24 | void set_next(node_ptr p) 25 | { next = p;} 26 | }; 27 | 28 | /////////////////////////////////////// 29 | 30 | void case1() 31 | { 32 | point p1, p2(2,2), p3(3,3); 33 | 34 | p1.set_next(&p2); 35 | p2.set_next(&p3); 36 | 37 | for (point::node_ptr p = &p1; 38 | p != nullptr; 39 | p = p->get_next()) 40 | { 41 | cout << p->x << "-" << p->y << " "; 42 | } 43 | 44 | p1.set_next(&p3); 45 | 46 | } 47 | 48 | /////////////////////////////////////// 49 | 50 | int main() 51 | { 52 | std::cout << "hello intrusive sample1" << std::endl; 53 | 54 | case1(); 55 | } 56 | -------------------------------------------------------------------------------- /intrusive/sample2.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost::intrusive; 9 | 10 | 11 | /////////////////////////////////////// 12 | 13 | class point: public slist_base_hook<> 14 | { 15 | public: 16 | int x,y; 17 | point(int a = 0, int b = 0): 18 | x(a), y(b){} 19 | }; 20 | 21 | /////////////////////////////////////// 22 | 23 | void case1() 24 | { 25 | point p1, p2(2,2), p3(3,3); 26 | 27 | //slist sl; 28 | slist > sl; 29 | 30 | sl.push_front(p1); 31 | sl.push_front(p2); 32 | sl.push_front(p3); 33 | 34 | assert(sl.size() == 3); 35 | sl.reverse(); 36 | 37 | for(auto& p : sl) 38 | { 39 | cout << p.x << "-" << p.y << " "; 40 | } 41 | 42 | sl.erase(boost::next(sl.begin())); 43 | 44 | } 45 | 46 | /////////////////////////////////////// 47 | 48 | int main() 49 | { 50 | std::cout << "hello intrusive sample2" << std::endl; 51 | 52 | case1(); 53 | } 54 | -------------------------------------------------------------------------------- /intrusive/sample3.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace boost::intrusive; 14 | using namespace boost; 15 | 16 | /////////////////////////////////////// 17 | 18 | class point final 19 | { 20 | public: 21 | int x,y; 22 | point(int a = 0, int b = 0): 23 | x(a), y(b){} 24 | 25 | slist_member_hook<> m_hook; 26 | }; 27 | 28 | /////////////////////////////////////// 29 | 30 | void case1() 31 | { 32 | using namespace boost::assign; 33 | 34 | ptr_vector vec = 35 | ptr_list_of()(2,2)(3,3); 36 | 37 | typedef member_hook, 38 | &point::m_hook> member_option; 39 | slist sl; 40 | 41 | BOOST_REVERSE_FOREACH(auto& p, vec) 42 | { 43 | sl.push_front(p); 44 | } 45 | assert(sl.size() == 3); 46 | 47 | for(point& p : sl) 48 | { 49 | cout << p.x << "-" << p.y << " "; 50 | } 51 | 52 | sl.clear(); 53 | } 54 | 55 | /////////////////////////////////////// 56 | 57 | int main() 58 | { 59 | std::cout << "hello intrusive sample3" << std::endl; 60 | 61 | case1(); 62 | } 63 | -------------------------------------------------------------------------------- /intrusive/sample4.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost::intrusive; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | typedef slist_node node_t; 15 | typedef slist_node_traits node_traits_t; 16 | typedef circular_slist_algorithms algo; 17 | 18 | node_t n1, n2; 19 | 20 | algo::init_header(&n1); 21 | assert(algo::count(&n1) == 1); 22 | 23 | algo::link_after(&n1, &n2); 24 | assert(algo::count(&n1) == 2); 25 | 26 | algo::unlink(&n1); 27 | assert(algo::count(&n2) == 1); 28 | 29 | } 30 | 31 | /////////////////////////////////////// 32 | 33 | int main() 34 | { 35 | std::cout << "hello intrusive sample4" << std::endl; 36 | 37 | case1(); 38 | } 39 | -------------------------------------------------------------------------------- /iostream/array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace io = boost::iostreams; 11 | using namespace io; 12 | 13 | 14 | /////////////////////////////////////// 15 | 16 | void case1() 17 | { 18 | char buf[] = "123"; 19 | array_source as1(buf, buf + 3); 20 | array_source as2(buf); 21 | 22 | assert(as1.input_sequence().first == buf); 23 | assert(as1.input_sequence().second == buf + 3); 24 | assert(as2.input_sequence().second == buf + 4); 25 | 26 | int buf1[10] = {1, 2, 3}, buf2[10]; 27 | basic_array_source ai(buf1); 28 | basic_array ar(buf2); 29 | 30 | stream > in(ai); 31 | stream > out(ar); 32 | 33 | io::copy(in, out); 34 | 35 | } 36 | 37 | /////////////////////////////////////// 38 | 39 | int main() 40 | { 41 | std::cout << "hello iostreams array" << std::endl; 42 | 43 | case1(); 44 | } 45 | -------------------------------------------------------------------------------- /iostream/bracket_line_filter.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace io = boost::iostreams; 15 | using namespace io; 16 | using namespace boost; 17 | 18 | /////////////////////////////////////// 19 | 20 | class bracket_line_filter final: 21 | public io::basic_line_filter 22 | { 23 | private: 24 | typedef io::basic_line_filter super_type; 25 | typedef typename super_type::string_type string_type; 26 | 27 | virtual string_type do_filter(const string_type& line) 28 | { 29 | return "<" + line + ">"; 30 | } 31 | }; 32 | 33 | BOOST_IOSTREAMS_PIPABLE(bracket_line_filter, 0); 34 | 35 | /////////////////////////////////////// 36 | 37 | int main() 38 | { 39 | string str("The\nPhantom\nPain"); 40 | 41 | filtering_istream in( 42 | bracket_line_filter() | 43 | boost::make_iterator_range(str)); 44 | io::copy(in, cout); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /iostream/combine.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace io = boost::iostreams; 15 | using namespace io; 16 | //using namespace boost; 17 | 18 | /////////////////////////////////////// 19 | 20 | void case1() 21 | { 22 | char src[] = "12345678"; 23 | string result; 24 | 25 | typedef io::combination > bi_dev; 27 | 28 | assert(is_device::value); 29 | assert((std::is_same< 30 | mode_of::type, bidirectional 31 | >::value)); 32 | 33 | bi_dev dev = io::combine( 34 | array_source(src), 35 | io::back_inserter(result)); 36 | 37 | io::write(dev, src, 2); 38 | assert(result.size() == 2); 39 | 40 | } 41 | 42 | /////////////////////////////////////// 43 | 44 | int main() 45 | { 46 | std::cout << "hello iostreams combine" << std::endl; 47 | 48 | case1(); 49 | } 50 | -------------------------------------------------------------------------------- /iostream/compose.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace io = boost::iostreams; 13 | using namespace io; 14 | using namespace boost; 15 | 16 | /////////////////////////////////////// 17 | 18 | int main() 19 | { 20 | std::cout << "hello iostreams compose" << std::endl; 21 | 22 | string str("abcdef aochijk"); 23 | 24 | io::copy( 25 | boost::make_iterator_range(str), 26 | compose(regex_filter(regex("a.c"), "test"), 27 | cout) 28 | ); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /iostream/container.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | namespace io = boost::iostreams; 12 | using namespace io; 13 | using namespace boost; 14 | 15 | /////////////////////////////////////// 16 | 17 | void case1() 18 | { 19 | string str("123"); 20 | filtering_istream in(make_iterator_range(str)); 21 | 22 | vector v(str.begin(), str.end()); 23 | filtering_istream in2(make_iterator_range(v)); 24 | 25 | } 26 | 27 | /////////////////////////////////////// 28 | 29 | #include 30 | 31 | void case2() 32 | { 33 | string str("123"); 34 | vector v; 35 | 36 | io::copy( make_iterator_range(str), 37 | io::back_inserter(v)); 38 | 39 | } 40 | 41 | /////////////////////////////////////// 42 | 43 | int main() 44 | { 45 | std::cout << "hello iostreams container" << std::endl; 46 | 47 | case1(); 48 | case2(); 49 | } 50 | -------------------------------------------------------------------------------- /iostream/counter.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | namespace io = boost::iostreams; 15 | using namespace io; 16 | using namespace boost; 17 | 18 | /////////////////////////////////////// 19 | 20 | void case1() 21 | { 22 | string str("counter\nfilter\n"); 23 | 24 | filtering_ostream out(counter() | null_sink() ); 25 | 26 | io::copy( 27 | boost::make_iterator_range(str), out ); 28 | 29 | auto pc = out.component(0); 30 | assert(pc->characters() == 15); 31 | assert(pc->lines() == 2); 32 | 33 | } 34 | 35 | /////////////////////////////////////// 36 | 37 | int main() 38 | { 39 | std::cout << "hello iostreams counter" << std::endl; 40 | 41 | case1(); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /iostream/file.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace io = boost::iostreams; 11 | using namespace io; 12 | using namespace boost; 13 | 14 | /////////////////////////////////////// 15 | 16 | void case1() 17 | { 18 | string str("file device"); 19 | file_sink fsink("test.txt"); 20 | 21 | io::copy( 22 | make_iterator_range(str), 23 | fsink); 24 | 25 | file_source fsrc("test.txt"); 26 | io::copy(fsrc, cout); 27 | 28 | } 29 | 30 | /////////////////////////////////////// 31 | 32 | int main() 33 | { 34 | std::cout << "hello iostreams file" << std::endl; 35 | 36 | case1(); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /iostream/filtering_stream.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace io = boost::iostreams; 14 | using namespace io; 15 | //using namespace boost; 16 | 17 | /////////////////////////////////////// 18 | 19 | void case1() 20 | { 21 | char ar[10] = "abcd"; 22 | stream sa(ar); 23 | 24 | counter ct; 25 | filtering_istream in; 26 | assert(in.empty()); 27 | 28 | in.push(boost::ref(ct)); 29 | assert(!in.is_complete()); 30 | in.push(sa); 31 | assert(in.is_complete()); 32 | 33 | filtering_ostream out(cout); 34 | io::copy(in, out); 35 | 36 | assert(ct.characters() == 10); 37 | 38 | } 39 | 40 | /////////////////////////////////////// 41 | 42 | int main() 43 | { 44 | std::cout << "hello iostreams filtering_stream" << std::endl; 45 | 46 | case1(); 47 | } 48 | -------------------------------------------------------------------------------- /iostream/hex_filter.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace io = boost::iostreams; 18 | using namespace io; 19 | using namespace boost; 20 | 21 | /////////////////////////////////////// 22 | 23 | class hex_filter final: 24 | public io::aggregate_filter 25 | { 26 | private: 27 | typedef io::aggregate_filter super_type; 28 | typedef super_type::vector_type vector_type; 29 | 30 | virtual void do_filter(const vector_type& src, vector_type& dest) 31 | { 32 | boost::algorithm::hex(src, std::back_inserter(dest)); 33 | } 34 | }; 35 | 36 | BOOST_IOSTREAMS_PIPABLE(hex_filter, 0) 37 | 38 | /////////////////////////////////////// 39 | 40 | int main() 41 | { 42 | char str[] = "313"; 43 | string result; 44 | 45 | filtering_ostream out( 46 | hex_filter() | 47 | counter() | 48 | io::back_inserter(result)); 49 | 50 | io::copy(array_source(str, str + 3), out); 51 | 52 | cout << result << endl; 53 | assert(result.size() == 6); 54 | assert(out.component(1)->characters() == 6); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /iostream/invert.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #if BOOST_VERSION <= 106400 8 | #include 9 | #else 10 | #include 11 | #endif 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace io = boost::iostreams; 22 | using namespace io; 23 | using namespace boost; 24 | 25 | /////////////////////////////////////// 26 | 27 | class null_filter final 28 | { 29 | public: 30 | typedef char char_type; 31 | struct category: 32 | output, 33 | filter_tag, 34 | multichar_tag, 35 | closable_tag 36 | { }; 37 | public: 38 | template 39 | std::streamsize write(Sink& snk, const char_type* s, std::streamsize n) 40 | { 41 | io::write(snk, s, n); 42 | return n; 43 | } 44 | 45 | template 46 | void close(Sink& snk) 47 | {} 48 | }; 49 | BOOST_IOSTREAMS_PIPABLE(null_filter, 0); 50 | BOOST_IOSTREAMS_PIPABLE(inverse, 1); 51 | 52 | int main() 53 | { 54 | char str[] = "Ground Zero"; 55 | string result; 56 | 57 | filtering_istream in( 58 | invert(null_filter()) | 59 | array_source(str)); 60 | 61 | io::copy(in, io::back_inserter(result)); 62 | 63 | //cout << result << endl; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /iostream/jamfile: -------------------------------------------------------------------------------- 1 | unit-test sample1 : sample1.cpp ; 2 | unit-test sample2 : sample2.cpp ; 3 | 4 | unit-test traits : traits.cpp ; 5 | 6 | unit-test array : array.cpp ; 7 | unit-test container : container.cpp ; 8 | unit-test file : file.cpp ; 9 | 10 | unit-test counter : counter.cpp ; 11 | unit-test newline : newline.cpp ; 12 | unit-test regex : regex.cpp /lib//boost_regex ; 13 | unit-test zlib : zlib.cpp 14 | /lib//z /lib//boost_iostreams 15 | ; 16 | 17 | unit-test filtering_stream : filtering_stream.cpp ; 18 | unit-test source_sink : source_sink.cpp ; 19 | 20 | unit-test hex_filter : hex_filter.cpp ; 21 | unit-test bracket_line_filter : bracket_line_filter.cpp ; 22 | unit-test sha1_filter : sha1_filter.cpp ; 23 | 24 | unit-test invert : invert.cpp ; 25 | unit-test restrict : restrict.cpp ; 26 | unit-test tee : tee.cpp /lib//boost_regex ; 27 | 28 | unit-test combine : combine.cpp ; 29 | unit-test compose : compose.cpp /lib//boost_regex ; 30 | -------------------------------------------------------------------------------- /iostream/newline.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace io = boost::iostreams; 14 | using namespace io; 15 | using namespace boost; 16 | 17 | /////////////////////////////////////// 18 | 19 | void case1() 20 | { 21 | string str( "abcdef\n" 22 | "12345\n" 23 | "aochijk\n"); 24 | string result; 25 | 26 | filtering_ostream out( 27 | newline_filter(newline::mac) | 28 | io::back_inserter(result)); 29 | 30 | io::copy(boost::make_iterator_range(str), 31 | out); 32 | cout << result << endl; 33 | 34 | } 35 | 36 | /////////////////////////////////////// 37 | 38 | void case2() 39 | { 40 | string str( "abcdef\n" 41 | "12345\n" 42 | "aochijk\n"); 43 | 44 | filtering_ostream out( 45 | newline_checker() | io::null_sink()); 46 | 47 | io::copy(boost::make_iterator_range(str), out); 48 | assert(out.component(0)->is_posix()); 49 | 50 | } 51 | 52 | /////////////////////////////////////// 53 | 54 | int main() 55 | { 56 | std::cout << "hello iostreams newline" << std::endl; 57 | 58 | case1(); 59 | case2(); 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /iostream/regex.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace io = boost::iostreams; 15 | using namespace io; 16 | using namespace boost; 17 | 18 | /////////////////////////////////////// 19 | 20 | void case1() 21 | { 22 | regex reg("a.c"); 23 | string str("abcdef aochijk"); 24 | string result; 25 | 26 | filtering_ostream out( 27 | regex_filter(reg, "test") | 28 | io::back_inserter(result)); 29 | 30 | io::copy( 31 | boost::make_iterator_range(str), 32 | out); 33 | 34 | cout << result << endl; 35 | 36 | } 37 | 38 | /////////////////////////////////////// 39 | 40 | string reg_format(const match_results& match) 41 | { 42 | return string("test-") + match[1] + "-"; 43 | } 44 | 45 | 46 | void case2() 47 | { 48 | regex reg("a(.)c"); 49 | string str("abcdef aochijk"); 50 | string result; 51 | 52 | auto formatter = [](const regex_filter::match_type& match) 53 | { 54 | return string("test-") + match[1] + "-"; 55 | }; 56 | filtering_ostream out( 57 | //regex_filter(reg, reg_format)| 58 | regex_filter(reg, formatter) | 59 | io::back_inserter(result)); 60 | 61 | io::copy( 62 | boost::make_iterator_range(str), 63 | out); 64 | cout << result << endl; 65 | 66 | } 67 | 68 | /////////////////////////////////////// 69 | 70 | void case3() 71 | { 72 | regex reg("a.c"); 73 | string str( "abcdef\n" 74 | "12345\n" 75 | "aochijk\n"); 76 | string result; 77 | 78 | //filtering_ostream out( 79 | // grep_filter(reg) | 80 | // io::back_inserter(result)); 81 | 82 | filtering_ostream out( 83 | grep_filter(reg, regex_constants::match_default, grep::invert) | 84 | io::back_inserter(result)); 85 | 86 | 87 | io::copy( 88 | boost::make_iterator_range(str), 89 | out); 90 | cout << result << endl; 91 | 92 | } 93 | 94 | /////////////////////////////////////// 95 | 96 | int main() 97 | { 98 | std::cout << "hello iostreams regex" << std::endl; 99 | 100 | case1(); 101 | case2(); 102 | case3(); 103 | } 104 | -------------------------------------------------------------------------------- /iostream/restrict.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #if BOOST_VERSION <= 106400 8 | #include 9 | #else 10 | #include 11 | #endif 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace io = boost::iostreams; 23 | using namespace io; 24 | using namespace boost; 25 | 26 | /////////////////////////////////////// 27 | 28 | BOOST_IOSTREAMS_PIPABLE(restriction, 1); 29 | 30 | int main() 31 | { 32 | char str[] = "1234"; 33 | string result; 34 | 35 | filtering_istream in( 36 | restrict(counter(),1,2) | 37 | array_source(str)); 38 | 39 | io::copy(in, io::back_inserter(result)); 40 | assert(result == "23"); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /iostream/sample1.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | namespace io = boost::iostreams; 10 | using namespace io; 11 | 12 | int main() 13 | { 14 | char str[] = "123"; 15 | array_source asrc(str, str + 3); 16 | stream in(asrc); 17 | 18 | char c1, c2, c3; 19 | in >> c1 >> c2; 20 | assert(c1 == '1' && c2 == '2'); 21 | 22 | in.get(c3); 23 | assert(c3 == '3' && in.good()); 24 | assert(in.get(c3).eof()); 25 | 26 | char str2[10]; 27 | array_sink asnk(str2); 28 | stream out(asnk); 29 | 30 | out << 'a' << 'b' << 'c'; 31 | assert(str2[0] == 'a' && str2[2] == 'c'); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /iostream/sample2.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace io = boost::iostreams; 14 | using namespace io; 15 | 16 | int main() 17 | { 18 | char arr[] = "12345678"; 19 | stream in(arr, arr + 8); 20 | 21 | string str; 22 | filtering_ostream out( 23 | counter() | 24 | io::back_inserter(str)); 25 | 26 | io::copy(in, out); 27 | assert(str.size() == 8); 28 | assert(str == arr); 29 | assert(out.component(0)->characters() == 8); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /iostream/sha1_filter.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #if BOOST_VERSION <= 106400 8 | #include 9 | #else 10 | #include 11 | #endif 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace io = boost::iostreams; 22 | using namespace io; 23 | using namespace boost; 24 | 25 | /////////////////////////////////////// 26 | 27 | class sha1_filter final : public io::multichar_output_filter 28 | { 29 | public: 30 | typedef char char_type; 31 | //struct category: 32 | // output, 33 | // filter_tag, 34 | // multichar_tag, 35 | // closable_tag 36 | // { }; 37 | private: 38 | boost::uuids::detail::sha1 sha; 39 | public: 40 | sha1_filter() = default; 41 | ~sha1_filter() = default; 42 | public: 43 | 44 | template 45 | std::streamsize write(Sink&, const char_type* s, std::streamsize n) 46 | { 47 | sha.process_bytes(s, n); 48 | return n; 49 | } 50 | 51 | template 52 | void close(Sink& snk) 53 | { 54 | unsigned int digest[5]; 55 | sha.get_digest(digest); 56 | 57 | char_type* p = reinterpret_cast(digest); 58 | io::write(snk, p, sizeof(digest)/sizeof(char_type)); 59 | } 60 | }; 61 | 62 | BOOST_IOSTREAMS_PIPABLE(sha1_filter, 0); 63 | 64 | /////////////////////////////////////// 65 | 66 | void case1() 67 | { 68 | char str[] = "The Evil Within"; 69 | string result; 70 | 71 | filtering_ostream out( 72 | sha1_filter() | 73 | io::back_inserter(result)); 74 | 75 | io::write(out, str, 3); 76 | assert(result.empty()); 77 | 78 | io::write(out, str + 3, 3); 79 | assert(result.empty()); 80 | 81 | io::close(out); 82 | assert(result.size() == 20); 83 | } 84 | 85 | /////////////////////////////////////// 86 | 87 | template 88 | std::streamsize 89 | copy_no_close_impl(Source src, Sink snk, 90 | std::streamsize buffer_size ) 91 | { 92 | return io::detail::copy_operation( 93 | src , snk, buffer_size ) 94 | (); 95 | } 96 | 97 | template 98 | std::streamsize 99 | copy_no_close( const Source& src, Sink& snk, 100 | std::streamsize buffer_size = 101 | io::default_device_buffer_size 102 | BOOST_IOSTREAMS_ENABLE_IF_STREAM(Source) 103 | BOOST_IOSTREAMS_ENABLE_IF_STREAM(Sink) ) 104 | { 105 | return copy_no_close_impl( 106 | io::detail::wrap(src), 107 | io::detail::wrap(snk), 108 | buffer_size ); 109 | } 110 | 111 | void case2() 112 | { 113 | char str[] = "The Evil Within"; 114 | string result; 115 | 116 | stream in(str); 117 | 118 | filtering_ostream out( 119 | sha1_filter() | 120 | io::back_inserter(result)); 121 | 122 | copy_no_close(in, out); 123 | assert(in.is_open()); 124 | 125 | io::close(in); 126 | io::close(out); 127 | 128 | assert(result.size() == 20); 129 | } 130 | 131 | /////////////////////////////////////// 132 | 133 | int main() 134 | { 135 | std::cout << "hello iostreams sha1_filter" << std::endl; 136 | 137 | case1(); 138 | case2(); 139 | } 140 | 141 | -------------------------------------------------------------------------------- /iostream/source_sink.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace io = boost::iostreams; 14 | using namespace io; 15 | //using namespace boost; 16 | 17 | /////////////////////////////////////// 18 | 19 | template 20 | class basic_rand_source: public T 21 | { 22 | private: 23 | std::streamsize count; 24 | public: 25 | typedef T super_type; 26 | typedef typename super_type::char_type char_type; 27 | public: 28 | basic_rand_source(int c): 29 | count(c){ } 30 | 31 | std::streamsize read(char_type* s, std::streamsize n) 32 | { 33 | using namespace std; 34 | 35 | auto read_count = (min)(n, count); 36 | if (!read_count) 37 | { 38 | return EOF; 39 | } 40 | 41 | for (streamsize i = 0;i < read_count; ++i) 42 | { 43 | *s++ = rand()(); 44 | } 45 | 46 | count -= read_count; 47 | return read_count; 48 | } 49 | private: 50 | typedef boost::variate_generator> rand_t; 51 | 52 | static rand_t& rand() 53 | { 54 | static rand_t r(boost::rand48(time(0)), boost::uniform_smallint<>('0', 'z')); 55 | 56 | return r; 57 | } 58 | }; 59 | 60 | /////////////////////////////////////// 61 | 62 | void case1() 63 | { 64 | //typedef std::basic_string block; 65 | 66 | typedef basic_rand_source<> rand_source; 67 | 68 | string out; 69 | io::copy(rand_source (20), 70 | io::back_inserter(out)); 71 | assert(out.size() == 20); 72 | 73 | cout << out << endl; 74 | } 75 | 76 | /////////////////////////////////////// 77 | #include 78 | 79 | void case2() 80 | { 81 | typedef std::basic_string block; 82 | typedef basic_rand_source> rand_source; 83 | 84 | block out; 85 | io::copy(rand_source (10), 86 | io::back_inserter(out)); 87 | assert(out.size() == 10); 88 | 89 | boost::copy(out, ostream_iterator(cout, " ")); 90 | cout << endl; 91 | } 92 | 93 | /////////////////////////////////////// 94 | 95 | class cout_sink: public io::sink 96 | { 97 | public: 98 | streamsize write(const char_type* s, streamsize n) 99 | { 100 | cout << string(s,n) << endl; 101 | return n; 102 | } 103 | }; 104 | 105 | void case3() 106 | { 107 | string str("abcd"); 108 | io::copy( 109 | boost::make_iterator_range(str), 110 | cout_sink()); //直接向接收设备输出 111 | 112 | } 113 | 114 | 115 | /////////////////////////////////////// 116 | 117 | int main() 118 | { 119 | std::cout << "hello iostreams rand_source" << std::endl; 120 | 121 | case1(); 122 | case2(); 123 | case3(); 124 | } 125 | -------------------------------------------------------------------------------- /iostream/tee.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace io = boost::iostreams; 20 | using namespace io; 21 | using namespace boost; 22 | 23 | /////////////////////////////////////// 24 | 25 | class hex_filter final: 26 | public io::aggregate_filter 27 | { 28 | private: 29 | typedef io::aggregate_filter super_type; 30 | typedef super_type::vector_type vector_type; 31 | 32 | virtual void do_filter(const vector_type& src, vector_type& dest) 33 | { 34 | boost::algorithm::hex(src, std::back_inserter(dest)); 35 | } 36 | }; 37 | 38 | BOOST_IOSTREAMS_PIPABLE(hex_filter, 0) 39 | 40 | /////////////////////////////////////// 41 | 42 | void case1() 43 | { 44 | string str("abcdef aochijk"); 45 | string result; 46 | 47 | filtering_ostream out( 48 | tee( 49 | compose(regex_filter(regex("a.c"), "test"), cout), 50 | compose(hex_filter(), io::back_inserter(result)) 51 | ) 52 | ); 53 | 54 | io::copy( 55 | boost::make_iterator_range(str), 56 | out); 57 | 58 | cout << endl << result << endl; 59 | } 60 | 61 | 62 | /////////////////////////////////////// 63 | 64 | int main() 65 | { 66 | std::cout << "hello iostreams tee" << std::endl; 67 | 68 | case1(); 69 | } 70 | 71 | -------------------------------------------------------------------------------- /iostream/traits.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | //#include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace io = boost::iostreams; 14 | using namespace io; 15 | 16 | /////////////////////////////////////// 17 | 18 | void case1() 19 | { 20 | assert((is_same::type, char>::value)); 21 | assert((is_same::type , char>::value)); 22 | 23 | assert((is_same::type, wchar_t>::value)); 24 | assert((!is_same::type, 25 | char_type_of::type>::value)); 26 | } 27 | 28 | /////////////////////////////////////// 29 | 30 | void case2() 31 | { 32 | assert((is_same::type, input_seekable>::value)); 33 | assert((is_same::type, output_seekable>::value)); 34 | 35 | assert((is_same::type, seekable>::value)); 36 | } 37 | 38 | /////////////////////////////////////// 39 | 40 | void case3() 41 | { 42 | assert((is_convertible::type, 43 | input_seekable>::value)); 44 | assert((is_convertible::type, 45 | device_tag>::value)); 46 | 47 | assert((is_convertible::type, filter_tag>::value)); 48 | assert((is_convertible::type, dual_use>::value)); 49 | 50 | } 51 | 52 | /////////////////////////////////////// 53 | 54 | int main() 55 | { 56 | std::cout << "hello iostreams traits" << std::endl; 57 | 58 | case1(); 59 | case2(); 60 | case3(); 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /iostream/zlib.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace io = boost::iostreams; 14 | using namespace io; 15 | using namespace boost; 16 | 17 | /////////////////////////////////////// 18 | 19 | void case1() 20 | { 21 | string str("12345678 12345678"); 22 | string zip_str, unzip_str; 23 | 24 | filtering_ostream zout( 25 | zlib_compressor() | io::back_inserter(zip_str) ); 26 | 27 | io::copy( 28 | boost::make_iterator_range(str), 29 | zout); 30 | 31 | filtering_ostream uzout( 32 | zlib_decompressor() | io::back_inserter(unzip_str) ); 33 | 34 | io::copy( 35 | boost::make_iterator_range(zip_str), 36 | uzout); 37 | 38 | assert(unzip_str == str); 39 | 40 | } 41 | 42 | /////////////////////////////////////// 43 | 44 | int main() 45 | { 46 | std::cout << "hello iostreams zlib" << std::endl; 47 | 48 | case1(); 49 | } 50 | -------------------------------------------------------------------------------- /iterator/counting_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | counting_iterator i(100); 15 | 16 | assert(*i++ == 100); 17 | assert(*i == 101); 18 | assert(*++i == 102); 19 | 20 | vector v; 21 | std::copy( 22 | make_counting_iterator(0), 23 | make_counting_iterator(10), 24 | back_inserter(v) 25 | ); 26 | } 27 | 28 | /////////////////////////////////////// 29 | 30 | template 31 | class rand_int 32 | { 33 | private: 34 | R &r; 35 | int count; 36 | public: 37 | rand_int(R& _r, int c = 0): 38 | r(_r), count(c) {} 39 | rand_int(rand_int const &other) = default; 40 | rand_int& operator=(rand_int const &other) = default; 41 | 42 | void operator++() 43 | { ++count;} 44 | 45 | friend bool operator==(rand_int const &l, rand_int const &r) 46 | { return l.count == r.count;} 47 | 48 | operator typename R::result_type () const 49 | { return r(); } 50 | }; 51 | 52 | void case2() 53 | { 54 | typedef counting_iterator, 55 | boost::single_pass_traversal_tag, int> RandIter; 56 | 57 | boost::rand48 r; 58 | 59 | rand_int r1(r, 0), r2(r, 10); 60 | RandIter first(r1), last(r2); 61 | 62 | vector v; 63 | std::copy(first, last, back_inserter(v)); 64 | assert(v.size() == 10); 65 | 66 | } 67 | 68 | /////////////////////////////////////// 69 | 70 | int main() 71 | { 72 | std::cout << "hello counting_iterator" << std::endl; 73 | 74 | case1(); 75 | case2(); 76 | } 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /iterator/filter_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | #include 9 | using namespace boost; 10 | 11 | /////////////////////////////////////// 12 | 13 | bool is_prime(int x) 14 | { 15 | for (int i = 2; i < x / 2; ++i) 16 | { 17 | if (x % i == 0) 18 | { return false;} 19 | } 20 | return true; 21 | } 22 | 23 | #include "step_iterator.hpp" 24 | 25 | void case1() 26 | { 27 | typedef counting_iterator ci_t; 28 | ci_t c1(11), c2(101); 29 | 30 | typedef step_iterator si_t; 31 | si_t si1(c1), si2(c2); 32 | 33 | //typedef filter_iterator fi_t; 34 | typedef filter_iterator fi_t; 35 | 36 | fi_t first(&is_prime, si1, si2); 37 | fi_t last(&is_prime, si2, si2); 38 | 39 | //std::copy(first, last, 40 | // ostream_iterator(cout, " ")); 41 | 42 | std::copy( 43 | make_filter_iterator(&is_prime, si1, si2), 44 | make_filter_iterator(&is_prime, si2, si2), 45 | ostream_iterator(cout, " ")); 46 | 47 | } 48 | 49 | /////////////////////////////////////// 50 | 51 | int main() 52 | { 53 | std::cout << "hello filter_iterator" << std::endl; 54 | 55 | case1(); 56 | } 57 | 58 | -------------------------------------------------------------------------------- /iterator/function_input_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | rand48 rng; 15 | 16 | std::copy( 17 | make_function_input_iterator(rng, 0), 18 | make_function_input_iterator(rng, 5), 19 | ostream_iterator(cout, "\n") 20 | ); 21 | 22 | vector v(10); 23 | 24 | std::copy( 25 | make_function_input_iterator(rng, v.begin()), 26 | make_function_input_iterator(rng, v.end()), 27 | v.begin() ); 28 | 29 | } 30 | 31 | /////////////////////////////////////// 32 | 33 | int main() 34 | { 35 | std::cout << "hello function_input_iterator" << std::endl; 36 | 37 | case1(); 38 | } 39 | -------------------------------------------------------------------------------- /iterator/function_output_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | class to_hex 13 | { 14 | private: 15 | vector &v; 16 | int count; 17 | 18 | char trans(const char c) const 19 | { 20 | if (c >= 'a') 21 | { return c - 'a' + 10;} 22 | else if (c >= 'A') 23 | { return c - 'A' + 10;} 24 | else 25 | { return c - '0';} 26 | } 27 | public: 28 | to_hex(vector &_v): 29 | v(_v), count(0){} 30 | 31 | void operator()(const char c) 32 | { 33 | static char tmp; 34 | if ((count++) % 2 == 0) 35 | { 36 | tmp = trans(c)* 0x10; 37 | } 38 | else 39 | { 40 | tmp += trans(c); 41 | v.push_back(tmp); 42 | } 43 | } 44 | }; 45 | 46 | void case1() 47 | { 48 | char s[] = "1234abcd"; 49 | 50 | vector v; 51 | to_hex h(v); 52 | function_output_iterator foi(h); 53 | 54 | //std::copy(s, s + 8, foi); 55 | std::copy(s, s + 8, 56 | make_function_output_iterator(to_hex(v))); 57 | 58 | assert(v.size() == 4); 59 | 60 | } 61 | 62 | /////////////////////////////////////// 63 | 64 | int main() 65 | { 66 | std::cout << "hello function_output_iterator" << std::endl; 67 | 68 | case1(); 69 | } 70 | 71 | -------------------------------------------------------------------------------- /iterator/generator_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | boost::rand48 rng; 15 | 16 | auto iter = make_generator_iterator(rng); 17 | 18 | for (int i = 0;i < 5; ++i) 19 | { 20 | cout << *++iter << ","; 21 | } 22 | 23 | } 24 | 25 | /////////////////////////////////////// 26 | 27 | int main() 28 | { 29 | std::cout << "hello generator_iterator" << std::endl; 30 | 31 | case1(); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /iterator/indirect_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | vector v = {new int(1), new int(2)}; 15 | 16 | for (auto pos = v.begin(); pos != v.end(); ++pos) 17 | { 18 | cout << **pos << ","; 19 | } 20 | cout << endl; 21 | 22 | for (auto& x : v) 23 | { 24 | cout << *x << ","; 25 | } 26 | cout << endl; 27 | 28 | auto start = make_indirect_iterator(v.begin()); 29 | auto finish = make_indirect_iterator(v.end()); 30 | for(;start != finish;) 31 | { 32 | cout << *start++ << ","; 33 | } 34 | 35 | for_each(v.begin(), v.end(), checked_deleter()); 36 | 37 | } 38 | 39 | /////////////////////////////////////// 40 | 41 | int main() 42 | { 43 | std::cout << "hello indirect_iterator" << std::endl; 44 | 45 | case1(); 46 | } 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /iterator/iterator_adaptor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | template 12 | class array_iter: 13 | public boost::iterator_adaptor, P > 14 | { 15 | BOOST_STATIC_ASSERT(std::is_pointer

::value); 16 | public: 17 | typedef typename array_iter::iterator_adaptor_ super_type; 18 | array_iter(P x): super_type(x) 19 | {} 20 | }; 21 | 22 | void case1() 23 | { 24 | int a[10] = {1,2,3}; 25 | array_iter start(a), finish(a + 10); 26 | start += 1; 27 | 28 | std::copy(start, finish, 29 | ostream_iterator(cout, ",")); 30 | 31 | cout << endl; 32 | } 33 | 34 | /////////////////////////////////////// 35 | 36 | template 37 | class delta_iterator : public boost::iterator_adaptor< 38 | delta_iterator, I , 39 | typename std::iterator_traits::value_type, 40 | boost::single_pass_traversal_tag, 41 | typename std::iterator_traits::value_type const > 42 | { 43 | private: 44 | friend class boost::iterator_core_access; 45 | typedef delta_iterator this_type; 46 | typedef typename this_type::iterator_adaptor_ super_type; 47 | 48 | typename super_type::value_type m_value; 49 | public: 50 | explicit delta_iterator(const I& iter): 51 | super_type(iter), m_value(0) 52 | {} 53 | private: 54 | using super_type::base; 55 | using super_type::base_reference; 56 | 57 | typename super_type::reference dereference() const 58 | { 59 | return m_value + *base(); 60 | } 61 | 62 | void increment() 63 | { 64 | m_value += *base(); 65 | ++base_reference(); 66 | } 67 | }; 68 | 69 | void case2() 70 | { 71 | vector a = {1,2,3}; 72 | 73 | //typedef delta_iterator::const_iterator> delta_iter; 74 | typedef delta_iterator delta_iter; 75 | 76 | delta_iter start(a.begin()), finish(a.end()); 77 | 78 | std::copy(start, finish, 79 | ostream_iterator(cout, ",")); 80 | 81 | } 82 | 83 | /////////////////////////////////////// 84 | 85 | int main() 86 | { 87 | std::cout << "hello iterator_adaptor" << std::endl; 88 | 89 | case1(); 90 | case2(); 91 | } 92 | -------------------------------------------------------------------------------- /iterator/iterator_facade.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | template 12 | class vs_iterator : 13 | public boost::iterator_facade< 14 | vs_iterator, T, 15 | boost::single_pass_traversal_tag> 16 | { 17 | public: 18 | typedef boost::iterator_facade< 19 | vs_iterator, T,boost::single_pass_traversal_tag> super_type; 20 | typedef vs_iterator this_type; 21 | 22 | typedef typename super_type::reference reference; 23 | // using typename super_type::reference ; 24 | 25 | private: 26 | std::vector &v; 27 | size_t current_pos; 28 | public: 29 | vs_iterator(vector &_v, size_t pos = 0): 30 | v(_v), current_pos(pos) 31 | {} 32 | vs_iterator(this_type const& other): 33 | v(other.v), current_pos(other.current_pos) 34 | {} 35 | void operator=(this_type const& other) 36 | { 37 | this->v = other.v; 38 | this->current_pos = other.current_pos; 39 | } 40 | private: 41 | friend class boost::iterator_core_access; 42 | 43 | reference dereference() const 44 | { return v[current_pos]; } 45 | 46 | void increment() 47 | { ++current_pos; } 48 | 49 | bool equal(this_type const& other) const 50 | { return this->current_pos == other.current_pos;} 51 | }; 52 | 53 | void case1() 54 | { 55 | vector v{1,2,3,4,5}; 56 | 57 | vs_iterator vsi(v), vsi_end(v, v.size()); 58 | *vsi = 9; 59 | 60 | std::copy(vsi, vsi_end, 61 | ostream_iterator(cout, ",")); 62 | 63 | cout << endl; 64 | } 65 | 66 | /////////////////////////////////////// 67 | 68 | #include "step_iterator.hpp" 69 | 70 | void case2() 71 | { 72 | char s[] = "12345678"; 73 | std::copy(s, s + 8, 74 | std::ostream_iterator(cout)); 75 | cout << endl; 76 | 77 | step_iterator first(s), last(s + 8); 78 | std::copy(first, last, 79 | std::ostream_iterator(cout)); 80 | 81 | } 82 | 83 | /////////////////////////////////////// 84 | 85 | int main() 86 | { 87 | std::cout << "hello iterator_facade" << std::endl; 88 | 89 | case1(); 90 | case2(); 91 | } 92 | -------------------------------------------------------------------------------- /iterator/iterator_traits.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef int* I; 14 | 15 | assert((is_same::type, int>::value)); 16 | assert((is_same::type, int&>::value)); 17 | assert((is_same::type, 18 | std::random_access_iterator_tag>::value)); 19 | 20 | typedef list::const_iterator II; 21 | 22 | assert((is_same::type, int>::value)); 23 | assert((is_same::type, 24 | const int&>::value)); 25 | assert((is_same::type, 26 | std::bidirectional_iterator_tag>::value)); 27 | } 28 | 29 | /////////////////////////////////////// 30 | 31 | int main() 32 | { 33 | std::cout << "hello iterator_traits" << std::endl; 34 | 35 | case1(); 36 | } 37 | -------------------------------------------------------------------------------- /iterator/jamfile: -------------------------------------------------------------------------------- 1 | unit-test next_prior : next_prior.cpp ; 2 | unit-test iterator_traits : iterator_traits.cpp ; 3 | unit-test iterator_facade : iterator_facade.cpp ; 4 | unit-test iterator_adaptor : iterator_adaptor.cpp ; 5 | unit-test shared_container_iterator : shared_container_iterator.cpp ; 6 | unit-test generator_iterator : generator_iterator.cpp ; 7 | unit-test reverse_iterator : reverse_iterator.cpp ; 8 | unit-test indirect_iterator : indirect_iterator.cpp ; 9 | unit-test counting_iterator : counting_iterator.cpp ; 10 | unit-test function_input_iterator : function_input_iterator.cpp ; 11 | unit-test function_output_iterator : function_output_iterator.cpp ; 12 | unit-test filter_iterator : filter_iterator.cpp ; 13 | unit-test transform_iterator : transform_iterator.cpp ; 14 | unit-test permutation_iterator : permutation_iterator.cpp ; 15 | unit-test zip_iterator : zip_iterator.cpp ; 16 | -------------------------------------------------------------------------------- /iterator/next_prior.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | //using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | template 12 | typename iterator_traits::value_type 13 | get_n(I& iter, int n) 14 | { 15 | return *(boost::next(iter, n)); 16 | } 17 | 18 | void case1() 19 | { 20 | list l{1,2,3,4}; 21 | 22 | auto p1 = l.begin(); 23 | auto p2 = l.end(); 24 | 25 | assert(get_n(p1, 1) == 2); 26 | assert(get_n(p2, -1) == 4); 27 | assert(get_n(p2, -2) == 3); 28 | 29 | } 30 | 31 | /////////////////////////////////////// 32 | 33 | int main() 34 | { 35 | std::cout << "hello next_prior" << std::endl; 36 | 37 | case1(); 38 | } 39 | -------------------------------------------------------------------------------- /iterator/permutation_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | char s[] = "abcdefg"; 14 | int idx[] = {6, 0, 2, 2, 4}; 15 | 16 | std::copy( 17 | make_permutation_iterator(s, idx), 18 | make_permutation_iterator(s, idx + 5), 19 | ostream_iterator(cout, " ") 20 | ); 21 | 22 | } 23 | 24 | /////////////////////////////////////// 25 | 26 | int main() 27 | { 28 | std::cout << "hello permutation_iterator" << std::endl; 29 | 30 | case1(); 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /iterator/reverse_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | //using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | char s[] = "hello iterator."; 14 | 15 | // after c++14 there is std::make_reverse_iterator 16 | 17 | std::copy( 18 | boost::make_reverse_iterator(s + 19 | char_traits::length(s)), 20 | boost::make_reverse_iterator(s), 21 | ostream_iterator(cout)); 22 | 23 | } 24 | 25 | /////////////////////////////////////// 26 | 27 | int main() 28 | { 29 | std::cout << "hello reverse_iterator" << std::endl; 30 | 31 | case1(); 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /iterator/shared_container_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | auto sv = boost::make_shared>(10); 15 | 16 | typedef shared_container_iterator > sci_t; 17 | 18 | sci_t first(sv->begin(), sv); 19 | sci_t last (sv->end() , sv); 20 | 21 | std::fill(first, last, 9); 22 | 23 | std::fill( 24 | make_shared_container_iterator(sv->begin(), sv), 25 | make_shared_container_iterator(sv->end() ,sv), 26 | 9); 27 | 28 | } 29 | 30 | /////////////////////////////////////// 31 | 32 | int main() 33 | { 34 | std::cout << "hello shared_container_iterator" << std::endl; 35 | 36 | case1(); 37 | } 38 | -------------------------------------------------------------------------------- /iterator/step_iterator.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef STEP_ITERATOR_HPP_ 4 | #define STEP_ITERATOR_HPP_ 5 | 6 | #include 7 | 8 | template 9 | class step_iterator: 10 | public boost::iterator_facade< 11 | step_iterator, 12 | typename boost::iterator_value::type const, 13 | boost::single_pass_traversal_tag> 14 | { 15 | private: 16 | I m_iter; 17 | public: 18 | 19 | typedef boost::iterator_facade< 20 | step_iterator,typename boost::iterator_value::type const, 21 | boost::single_pass_traversal_tag> super_type; 22 | typedef step_iterator this_type; 23 | 24 | using typename super_type::reference; 25 | 26 | public: 27 | step_iterator(I x): 28 | m_iter(x) {} 29 | 30 | step_iterator(this_type const& other) = default; 31 | this_type& operator=(this_type const& other) = default; 32 | 33 | //step_iterator(this_type const& other): 34 | // m_iter(other.m_iter){} 35 | 36 | //void operator = (this_type const& other) 37 | //{ m_iter = other.m_iter; } 38 | private: 39 | friend class boost::iterator_core_access; 40 | 41 | reference dereference() const 42 | { return *m_iter; } 43 | 44 | void increment() 45 | { std::advance(m_iter, N); } 46 | 47 | bool equal(this_type const& other) const 48 | { return m_iter == other.m_iter;} 49 | }; 50 | 51 | #endif //STEP_ITERATOR_HPP_ 52 | -------------------------------------------------------------------------------- /iterator/transform_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | #include 9 | using namespace boost; 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | typedef counting_iterator ci_t; 16 | 17 | vector v; 18 | std::copy(ci_t(0), ci_t(10), back_inserter(v)); 19 | 20 | using std::placeholders::_1; 21 | 22 | auto f = bind(plus(), _1, 5); 23 | 24 | std::copy( 25 | make_transform_iterator(v.begin(), f), 26 | make_transform_iterator(v.end(), f), 27 | ostream_iterator(cout, " ")); 28 | } 29 | 30 | /////////////////////////////////////// 31 | 32 | int main() 33 | { 34 | std::cout << "hello transform_iterator" << std::endl; 35 | 36 | case1(); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /iterator/zip_iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | using namespace boost; 11 | 12 | #include "step_iterator.hpp" 13 | 14 | /////////////////////////////////////// 15 | 16 | class to_hex2 17 | { 18 | private: 19 | vector &v; 20 | char trans(const char c) const 21 | { 22 | if (c >= 'a') 23 | { return c - 'a' + 10;} 24 | else if (c >= 'A') 25 | { return c - 'A' + 10;} 26 | else 27 | { return c - '0';} 28 | } 29 | public: 30 | to_hex2(vector &_v): 31 | v(_v){} 32 | 33 | typedef boost::tuple Tuple; 34 | void operator()(Tuple const& t) const 35 | { 36 | static char tmp; 37 | tmp = trans(get<0>(t))* 0x10; 38 | tmp += trans(get<1>(t)); 39 | v.push_back(tmp); 40 | } 41 | }; 42 | 43 | void case1() 44 | { 45 | char s[] = "1234aBcD"; 46 | vector v; 47 | 48 | typedef step_iterator si_t; 49 | 50 | for_each( 51 | make_zip_iterator( 52 | boost::make_tuple(si_t(s), si_t(s + 1))), 53 | make_zip_iterator( 54 | boost::make_tuple(si_t(s + 8), si_t(s + 9))), 55 | to_hex2(v) 56 | ); 57 | 58 | assert(v.size() == 4); 59 | 60 | std::copy( 61 | make_zip_iterator( 62 | boost::make_tuple(si_t(s), si_t(s + 1))), 63 | make_zip_iterator( 64 | boost::make_tuple(si_t(s + 8), si_t(s + 9))), 65 | make_function_output_iterator(to_hex2(v)) 66 | ); 67 | 68 | } 69 | 70 | /////////////////////////////////////// 71 | 72 | int main() 73 | { 74 | std::cout << "hello zip_iterator" << std::endl; 75 | 76 | case1(); 77 | } 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /jamroot: -------------------------------------------------------------------------------- 1 | 2 | import testing ; 3 | 4 | using gcc : : g++ 5 | : -std=c++11 6 | -fPIC 7 | -Wno-multichar # for boost::mpl 8 | -Wno-attributes # for [[deprecated]] 9 | ; 10 | 11 | path-constant DEV_ROOT : . ; 12 | 13 | project : requirements 14 | multi 15 | static 16 | 17 | ./common/ 18 | ; 19 | 20 | use-project /lib : lib ; 21 | 22 | -------------------------------------------------------------------------------- /lib/jamfile: -------------------------------------------------------------------------------- 1 | 2 | 3 | lib z ; 4 | lib dl ; 5 | lib rt ; 6 | 7 | lib boost_date_time ; 8 | lib boost_system ; 9 | lib boost_program_options ; 10 | lib boost_context ; 11 | lib boost_atomic ; 12 | lib boost_regex ; 13 | 14 | # need bzlib.h 15 | lib boost_iostreams ; 16 | 17 | lib boost_unit_test_framework ; 18 | lib boost_test_exec_monitor ; 19 | 20 | lib boost_chrono : boost_system : boost_chrono ; 21 | lib boost_timer : boost_chrono : boost_timer ; 22 | lib boost_filesystem : boost_system : boost_filesystem ; 23 | lib boost_thread : boost_chrono : boost_thread ; 24 | 25 | lib boost_coroutine : boost_context boost_system : boost_coroutine ; 26 | 27 | -------------------------------------------------------------------------------- /meta/jamfile: -------------------------------------------------------------------------------- 1 | unit-test t : meta.cpp ; 2 | -------------------------------------------------------------------------------- /meta/meta.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | // for c++98 7 | #include 8 | 9 | /////////////////////////////////////// 10 | 11 | template 12 | struct meta_func 13 | { 14 | static const int value = N + M; 15 | 16 | //static constexpr int value = N + M; 17 | 18 | // for c++98 19 | //BOOST_STATIC_CONSTANT(int, value = N + M); 20 | }; 21 | 22 | template 23 | struct select1st 24 | { 25 | typedef T1 type; 26 | }; 27 | 28 | template 29 | struct forward : select1st 30 | {}; 31 | 32 | /////////////////////////////////////// 33 | 34 | #include "mp_utils.hpp" 35 | 36 | mp_arglist 37 | mp_function static_min 38 | { 39 | static const int value = (L < R) ? L : R; 40 | }; 41 | 42 | /////////////////////////////////////// 43 | #include 44 | 45 | mp_arglist 46 | mp_function demo_func 47 | { 48 | mp_return(const T*); 49 | }; 50 | 51 | mp_arglist 52 | mp_function demo_func 53 | { 54 | mp_return(const T); 55 | }; 56 | 57 | 58 | int main() 59 | { 60 | cout << "hello meta-programing" << endl; 61 | 62 | cout << meta_func<10, 10>::value << endl; 63 | 64 | //int i = 10, j = 10; 65 | //meta_func::value; 66 | 67 | assert((static_min<10, 20>::value == 10)); 68 | 69 | assert((is_same) , const int*>::value)); 70 | assert((is_same), const int >::value)); 71 | 72 | } 73 | -------------------------------------------------------------------------------- /meta/mp_utils.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _MP_UTILS_HPP 4 | #define _MP_UTILS_HPP 5 | 6 | #define mp_arglist template 7 | #define mp_arg typename 8 | #define mp_function struct 9 | #define mp_data typedef 10 | 11 | #define mp_return(T) mp_data T type 12 | //#define mp_return(T) using type=T 13 | #define mp_exec(Func) Func::type 14 | #define mp_eval(Func) Func::value 15 | 16 | #endif // _MP_UTILS_HPP 17 | -------------------------------------------------------------------------------- /mordencpp/for.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | void case1() 11 | { 12 | int a[] = {2,3,5,7}; 13 | 14 | for(int i = 0;i < 4; ++i) 15 | { 16 | cout << a[i] << ","; 17 | } 18 | cout << endl; 19 | 20 | vector v = {253, 874}; 21 | for(auto iter = v.begin(); iter != v.end(); ++iter) 22 | { 23 | cout << *iter << ","; 24 | } 25 | cout << endl; 26 | } 27 | 28 | /////////////////////////////////////// 29 | 30 | void case2() 31 | { 32 | int a[] = {2,3,5,7}; 33 | 34 | for(auto&& x : a) 35 | { 36 | cout << x << ","; 37 | } 38 | cout << endl; 39 | 40 | vector v = {253, 874}; 41 | for(const auto& x : v) 42 | { 43 | cout << x << ","; 44 | } 45 | cout << endl; 46 | 47 | for(auto& x : v) 48 | { 49 | cout << ++x << ","; 50 | } 51 | cout << endl; 52 | 53 | auto&& _range = v; 54 | for(auto _begin = std::begin(_range), 55 | _end = std::end(_range); 56 | _begin!= _end; ++_begin) 57 | { 58 | auto x = *_begin; 59 | boost::ignore_unused(x); 60 | } 61 | } 62 | 63 | /////////////////////////////////////// 64 | 65 | namespace std { 66 | 67 | template 68 | auto begin(const std::pair& p) ->decltype(p.first) 69 | { 70 | return p.first; 71 | } 72 | 73 | template 74 | auto end(const std::pair& p) -> decltype(p.second) 75 | { 76 | return p.second; 77 | } 78 | 79 | } //namespace std 80 | 81 | void case3() 82 | { 83 | vector v = {1,2,3,4,5}; 84 | 85 | auto r = std::make_pair(v.begin(), v.begin() + 3); 86 | 87 | for(auto x : r) 88 | { 89 | cout << x << ","; 90 | } 91 | } 92 | 93 | /////////////////////////////////////// 94 | 95 | int main() 96 | { 97 | std::cout << "hello C++11/14 for" << std::endl; 98 | 99 | case1(); 100 | case2(); 101 | case3(); 102 | } 103 | -------------------------------------------------------------------------------- /mordencpp/generic.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | void case1() 11 | { 12 | using int64 = long; 13 | using llong = long long; 14 | 15 | //typedef long int64; 16 | 17 | boost::ignore_unused(); 18 | } 19 | 20 | /////////////////////////////////////// 21 | 22 | template 23 | class demo final {}; 24 | 25 | template 26 | using demo_long = demo; 27 | 28 | template 29 | using int_map = std::map; 30 | 31 | void case2() 32 | { 33 | int_map m; 34 | demo d1; 35 | demo_long d2; 36 | 37 | boost::ignore_unused(m, d1, d2); 38 | } 39 | 40 | /////////////////////////////////////// 41 | 42 | void case3() 43 | { 44 | static_assert(sizeof(int)==4, "int must be 32bit"); 45 | static_assert(sizeof(long)>=8, "need 64bit!"); 46 | } 47 | 48 | /////////////////////////////////////// 49 | 50 | constexpr long giga() 51 | { 52 | return 1000*1000*1000; 53 | } 54 | 55 | void case4() 56 | { 57 | const int k = 1024; 58 | constexpr int kk = 1024; 59 | 60 | static_assert(kk == 1024, ""); 61 | 62 | boost::ignore_unused(k); 63 | } 64 | 65 | /////////////////////////////////////// 66 | 67 | template 68 | using x = std::map; 69 | 70 | // can not work on latest gcc 71 | // error: pack expansion argument for non-pack parameter 72 | // 73 | // template 74 | // class variadic_class 75 | // { 76 | // using type = x; 77 | // }; 78 | 79 | template 80 | void variadic_func(Args... args) 81 | { 82 | cout << sizeof...(Args) << endl; 83 | } 84 | 85 | template 86 | int print( const char *format, const Args& ... args) 87 | //int print( const char *format, Args&& ... args) 88 | { 89 | return printf(format, args...); 90 | //return printf(format, std::forward(args)...); 91 | } 92 | 93 | void case5() 94 | { 95 | variadic_func(1,2,3); 96 | print("hello %s %s\n", "variadic", "template"); 97 | } 98 | 99 | /////////////////////////////////////// 100 | 101 | int main() 102 | { 103 | std::cout << "hello C++11/14 generic" << std::endl; 104 | 105 | case1(); 106 | case2(); 107 | case3(); 108 | case4(); 109 | case5(); 110 | } 111 | 112 | -------------------------------------------------------------------------------- /mordencpp/init.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | set get_set() 11 | { 12 | return {2,4,6}; 13 | } 14 | 15 | void case1() 16 | { 17 | int x{}; 18 | double y{2.718}; 19 | complex c{1,1}; 20 | string s{"venom snake"}; 21 | 22 | assert(x == 0); 23 | boost::ignore_unused(y, s); 24 | 25 | int a[] = {1,2,3}; 26 | vector v = {4,5,6}; 27 | 28 | auto ss = get_set(); 29 | assert(ss.size() == 3); 30 | 31 | boost::ignore_unused(a, v); 32 | } 33 | 34 | /////////////////////////////////////// 35 | 36 | int main() 37 | { 38 | std::cout << "hello C++11/14 init" << std::endl; 39 | 40 | case1(); 41 | } 42 | -------------------------------------------------------------------------------- /mordencpp/jamfile: -------------------------------------------------------------------------------- 1 | 2 | unit-test rvalue : rvalue.cpp ; 3 | unit-test auto : auto.cpp ; 4 | unit-test nullptr : nullptr.cpp ; 5 | unit-test f : for.cpp ; 6 | unit-test init : init.cpp ; 7 | unit-test c : class.cpp ; 8 | unit-test generic : generic.cpp ; 9 | unit-test thread : thread.cpp /lib//boost_thread ; 10 | unit-test lambda : lambda.cpp ; 11 | unit-test safety : safety.cpp ; 12 | unit-test others : others.cpp ; 13 | -------------------------------------------------------------------------------- /mordencpp/lambda.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | void case1() 11 | { 12 | //auto f1 = [](){}; 13 | 14 | auto f1 = [](int x){ 15 | return x*x; 16 | }; 17 | 18 | auto f2 = [](string s) { 19 | cout << "lambda : " << s << endl; 20 | }; 21 | 22 | auto f3 = [](int x, int y) { 23 | return x < y; 24 | }; 25 | 26 | cout << f1(3) << endl; 27 | f2("heavy rain"); 28 | cout << f3(1,5) << endl; 29 | 30 | vector v = {1,3,5,7}; 31 | std::for_each(v.begin(), v.end(), [](int x ){ 32 | cout << x << ","; 33 | }); 34 | cout << endl; 35 | 36 | std::for_each(v.begin(), v.end(), [](int& x ){ 37 | if(x > 3) { x *=2;} 38 | }); 39 | 40 | for(auto&& x : v) 41 | { 42 | cout << x << ","; 43 | } 44 | cout << endl; 45 | 46 | auto f4 = [](int x) -> long { 47 | return x*x; 48 | }; 49 | 50 | boost::ignore_unused(f1, f2, f3, f4); 51 | } 52 | 53 | /////////////////////////////////////// 54 | 55 | void case2() 56 | { 57 | int x = 0, y = 0; 58 | 59 | //auto f1 = [](){return x;}; // error 60 | auto f1 = [=](){return x;}; 61 | auto f2 = [&](){return ++x;}; 62 | auto f3 = [x](){return x;}; 63 | auto f4 = [x,&y](){y += x;}; 64 | auto f5 = [&,y](){x += y;}; 65 | auto f6 = [&](){y += ++x;}; 66 | 67 | assert(f1() == 0 && x == 0); 68 | assert(f2() == 1 && x == 1); 69 | assert(f3() == 0); 70 | f4();assert(x == 1 && y == 0); 71 | f5();assert(x == 1 && y == 0); 72 | f6();assert(x == 2 && y == 2); 73 | } 74 | 75 | /////////////////////////////////////// 76 | 77 | void case3() 78 | { 79 | int x = 0; 80 | 81 | auto f = [=]() mutable {return ++x;}; 82 | assert(f() == 1 && x == 0); 83 | } 84 | 85 | /////////////////////////////////////// 86 | 87 | void case4() 88 | { 89 | vector v = {1,2,3,4,5}; 90 | int sum = 0; 91 | 92 | std::for_each(v.begin(), v.end(), 93 | [&](int x){ 94 | sum += x; 95 | }); 96 | 97 | assert(sum == 15); 98 | 99 | int k = 3; 100 | auto c = std::count_if( 101 | v.begin(), v.end(), 102 | [=](int x){ 103 | return x > k; 104 | }); 105 | 106 | assert(c == 2); 107 | } 108 | 109 | /////////////////////////////////////// 110 | 111 | void case5() 112 | { 113 | int x = 0; 114 | 115 | auto f = [](){}; 116 | auto g = [&](){++x;}; 117 | 118 | typedef void (*func)(); 119 | 120 | func p1 = f; 121 | func p2 = [](){cout< 4 | using namespace std; 5 | 6 | // typedef decltype(nullptr) nullptr_t; 7 | 8 | /////////////////////////////////////// 9 | 10 | void case1() 11 | { 12 | int* p1 = nullptr; 13 | vector* p2 = nullptr; 14 | 15 | assert(!p1 && !p2); 16 | assert(p1 == nullptr); 17 | 18 | //assert(10 >= nullptr); 19 | 20 | nullptr_t nil; 21 | 22 | double* p3 = nil; 23 | assert(nil == nullptr); 24 | assert(!p3); 25 | } 26 | 27 | /////////////////////////////////////// 28 | 29 | int main() 30 | { 31 | std::cout << "hello C++11/14 nullptr" << std::endl; 32 | 33 | case1(); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /mordencpp/others.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | #if __cplusplus < 201103L 11 | #error "we need the latest C++ standard!" 12 | #endif 13 | 14 | /////////////////////////////////////// 15 | 16 | void case1() 17 | { 18 | cout << "c++ : " << __cplusplus << endl; 19 | } 20 | 21 | /////////////////////////////////////// 22 | 23 | void case2() 24 | { 25 | static_assert(sizeof(long long) >= 8, "long long is 64bit"); 26 | 27 | long long m = -1000*1000; 28 | unsigned long long n = 1000*1000; 29 | 30 | auto a = 524287LL; 31 | auto b = 2147483647uLL; 32 | 33 | boost::ignore_unused(m, n, a, b); 34 | } 35 | 36 | /////////////////////////////////////// 37 | 38 | void case3() 39 | { 40 | string s = R"(this is a "\string\" )"; 41 | cout << s << endl; 42 | 43 | string reg = R"(^\d+\s\w+)"; 44 | string reg2 = "^\\d+\\s\\w+"; 45 | assert(reg == reg2); 46 | //cout << reg << endl; 47 | //cout << reg2 << endl; 48 | 49 | auto b = R"***(BioShock Infinity)***"; 50 | auto d = R"====(Dark Souls)===="; 51 | 52 | cout << b << "," << d << endl; 53 | } 54 | 55 | /////////////////////////////////////// 56 | #include 57 | 58 | void case4() 59 | { 60 | auto f = 3.14f; 61 | auto s = L"wide char"; 62 | auto x = 0x100L; 63 | 64 | using namespace boost; 65 | cout << typeindex::type_id().pretty_name() << endl; 66 | cout << typeindex::type_id().pretty_name() << endl; 67 | cout << typeindex::type_id().pretty_name() << endl; 68 | } 69 | 70 | /////////////////////////////////////// 71 | 72 | #include 73 | #include 74 | 75 | long operator"" _kb(unsigned long long v) 76 | { 77 | return v * 1024; 78 | } 79 | 80 | complex operator"" _c(const char* s, size_t n) 81 | { 82 | using namespace boost::xpressive; 83 | 84 | auto reg = cregex::compile(R"--(([0-9\.]+)\+([0-9\.]+)i)--"); 85 | cmatch what; 86 | 87 | auto ok = regex_match(s, what, reg); 88 | assert(ok); 89 | 90 | return complex(stod(what[1]), stod(what[2])); 91 | } 92 | 93 | 94 | void case5() 95 | { 96 | auto x = 2_kb; 97 | assert(x == 2*1024); 98 | 99 | auto c = "1.414+1.414i"_c; 100 | cout << c << endl; 101 | } 102 | 103 | /////////////////////////////////////// 104 | 105 | int main() 106 | { 107 | std::cout << "hello C++11/14 features" << std::endl; 108 | 109 | case1(); 110 | case2(); 111 | case3(); 112 | case4(); 113 | case5(); 114 | } 115 | -------------------------------------------------------------------------------- /mordencpp/rvalue.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | void case1() 11 | { 12 | int x = 0; 13 | int* p = &++x; 14 | assert(p); 15 | ++x = 10; 16 | 17 | //p = &x++; 18 | } 19 | 20 | /////////////////////////////////////// 21 | 22 | void case2() 23 | { 24 | int x = 0; 25 | 26 | int& r1 = ++x; 27 | int&& r2 = x++; 28 | const int& r3 = x++; 29 | const int&& r4 = x++; 30 | 31 | cout << r1 << "," << r2 << "," << r3 << endl; 32 | 33 | boost::ignore_unused(r1, r2, r3, r4); 34 | } 35 | 36 | /////////////////////////////////////// 37 | 38 | class moveable 39 | { 40 | private: 41 | int x; 42 | public: 43 | moveable() {} 44 | moveable(moveable&& other) 45 | { 46 | std::swap(x, other.x); 47 | } 48 | 49 | moveable& operator=(moveable&& other) 50 | { 51 | std::swap(x, other.x); 52 | return *this; 53 | } 54 | public: 55 | static moveable create() 56 | { 57 | moveable obj; 58 | return obj; 59 | } 60 | }; 61 | 62 | void case3() 63 | { 64 | moveable m1; 65 | 66 | moveable m2(std::move(m1)); 67 | 68 | moveable m3 = moveable::create(); 69 | 70 | 71 | boost::ignore_unused(m1, m2, m3); 72 | } 73 | 74 | /////////////////////////////////////// 75 | 76 | void case4() 77 | { 78 | vector> v; 79 | 80 | v.emplace_back(3,4); 81 | assert(v.size() == 1); 82 | 83 | map m; 84 | 85 | m.emplace("metroid", "prime"); 86 | assert(m.size() == 1); 87 | } 88 | 89 | /////////////////////////////////////// 90 | 91 | void check(int&) 92 | { 93 | cout << "lvalue" << endl; 94 | } 95 | 96 | void check(int&&) 97 | { 98 | cout << "rvalue" << endl; 99 | } 100 | 101 | template 102 | void print(T&& v) 103 | { 104 | check(std::forward(v)); 105 | } 106 | 107 | void case5() 108 | { 109 | int x = 10; 110 | 111 | print(x); 112 | print(std::move(x)); 113 | } 114 | 115 | /////////////////////////////////////// 116 | 117 | int main() 118 | { 119 | std::cout << "hello C++11/14 rvalue" << std::endl; 120 | 121 | case1(); 122 | case2(); 123 | case3(); 124 | case4(); 125 | case5(); 126 | } 127 | -------------------------------------------------------------------------------- /mordencpp/safety.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | void func() noexcept 11 | {} 12 | 13 | /////////////////////////////////////// 14 | 15 | namespace { 16 | int x = 0; 17 | } 18 | 19 | inline namespace temp{ 20 | int xx = 0; 21 | } 22 | 23 | namespace release { 24 | namespace v001 { 25 | void func() {} 26 | } 27 | inline namespace v002 { 28 | void func() {} 29 | } 30 | } 31 | 32 | void case1() 33 | { 34 | assert(x == 0); 35 | assert(xx == 0); 36 | 37 | release::func(); 38 | } 39 | 40 | /////////////////////////////////////// 41 | 42 | enum class color : char { 43 | red = 1, 44 | green = 2, 45 | blue = 3 46 | }; 47 | 48 | void case2() 49 | { 50 | //int red = 1; 51 | //assert(red == green -1); 52 | auto x = color::red; 53 | //auto y = red; 54 | //auto z = color::red + 1; 55 | 56 | boost::ignore_unused(x); 57 | } 58 | 59 | /////////////////////////////////////// 60 | 61 | 62 | void case3() 63 | { 64 | //[[deprecated]] int x = 0; 65 | 66 | class [[deprecated]] demo {}; 67 | 68 | //boost::ignore_unused(x); 69 | } 70 | 71 | /////////////////////////////////////// 72 | 73 | int main() 74 | { 75 | std::cout << "hello C++11/14 safety features" << std::endl; 76 | 77 | case1(); 78 | case2(); 79 | case3(); 80 | } 81 | 82 | -------------------------------------------------------------------------------- /mordencpp/thread.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #define BOOST_THEAD_VERSION 4 7 | #include 8 | 9 | #include 10 | 11 | /////////////////////////////////////// 12 | 13 | extern int x; 14 | static int y = 0; 15 | thread_local int z = 0; 16 | 17 | //void test() 18 | //{ 19 | // ++y; 20 | // ++z; 21 | // 22 | // cout << y << "," << z << endl; 23 | //} 24 | 25 | void case1() 26 | { 27 | using namespace boost; 28 | 29 | auto f = [&](){ 30 | ++y; ++z; 31 | cout << y << "," << z << endl; 32 | }; 33 | 34 | thread t1(f); 35 | thread t2(f); 36 | 37 | t1.join(); 38 | t2.join(); 39 | 40 | cout << y << "," << z << endl; 41 | } 42 | 43 | /////////////////////////////////////// 44 | 45 | class demo final 46 | { 47 | public: 48 | demo() 49 | { 50 | cout << "demo" << endl; 51 | } 52 | ~demo() 53 | { 54 | cout << "~demo" << endl; 55 | } 56 | }; 57 | 58 | void test_obj() 59 | { 60 | cout << "begin func" << endl; 61 | thread_local demo obj; 62 | boost::ignore_unused(obj); 63 | cout << "end func" << endl; 64 | } 65 | 66 | void case2() 67 | { 68 | using namespace boost; 69 | 70 | auto f = [&](){ 71 | cout << "begin thread" << endl; 72 | test_obj(); 73 | cout << "end thread" << endl; 74 | }; 75 | 76 | thread t1(f); 77 | thread t2(f); 78 | 79 | t1.join(); 80 | t2.join(); 81 | 82 | cout << "end case" << endl; 83 | } 84 | 85 | /////////////////////////////////////// 86 | 87 | class demo2 final 88 | { 89 | public: 90 | demo2() 91 | { 92 | cout << "demo2" << endl; 93 | } 94 | ~demo2() 95 | { 96 | cout << "~demo2" << endl; 97 | } 98 | }; 99 | 100 | 101 | demo& test_static() 102 | { 103 | thread_local demo obj; 104 | demo2 obj2; 105 | 106 | return obj; 107 | } 108 | 109 | void case3() 110 | { 111 | using namespace boost; 112 | 113 | auto f = [&](){ 114 | auto& obj = test_static(); 115 | cout << "obj = " << &obj << endl; 116 | }; 117 | 118 | thread t1(f); 119 | 120 | t1.join(); 121 | } 122 | 123 | /////////////////////////////////////// 124 | 125 | int main() 126 | { 127 | std::cout << "hello C++11/14 thread" << std::endl; 128 | 129 | case1(); 130 | case2(); 131 | case3(); 132 | } 133 | 134 | -------------------------------------------------------------------------------- /mpl/assert.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace boost; 15 | using namespace boost::mpl; 16 | 17 | /////////////////////////////////////// 18 | 19 | #include 20 | 21 | void case1() 22 | { 23 | typedef mpl::vector vec; 24 | 25 | BOOST_MPL_ASSERT((is_same::type> )); 27 | 28 | //BOOST_MPL_ASSERT((equal_to< 29 | // size::type, int_<3>>)); 30 | 31 | } 32 | 33 | /////////////////////////////////////// 34 | template 35 | struct my_operation 36 | { 37 | BOOST_MPL_ASSERT_NOT((is_integral)); 38 | BOOST_MPL_ASSERT(( 39 | is_integral)); 40 | 41 | typedef typename next::type type; 42 | }; 43 | 44 | void case2() 45 | { 46 | typedef my_operation>::type t; 47 | //typedef my_operation::type error; 48 | 49 | boost::ignore_unused(); 50 | 51 | BOOST_MPL_ASSERT_RELATION(int_<5>::value, >, 0); 52 | //BOOST_MPL_ASSERT_RELATION(sizeof(int), >, sizeof(long)); 53 | } 54 | 55 | 56 | /////////////////////////////////////// 57 | 58 | template 59 | struct my_operation2 60 | { 61 | BOOST_MPL_ASSERT_MSG(!is_pod::value, 62 | IS_POD_ERROR, (T)); 63 | }; 64 | 65 | void case3() 66 | { 67 | //BOOST_MPL_ASSERT_MSG(1>2, DEMO_MESSAGE, ()); 68 | //my_operation2::type; 69 | } 70 | 71 | /////////////////////////////////////// 72 | #include 73 | #include 74 | 75 | void case4() 76 | { 77 | typedef mpl::print::type x; 78 | typedef mpl::vector, float> vec; 79 | typedef transform>::type none; 80 | 81 | boost::ignore_unused(); 82 | } 83 | 84 | /////////////////////////////////////// 85 | 86 | int main() 87 | { 88 | std::cout << "hello debug" << std::endl; 89 | 90 | case1(); 91 | case2(); 92 | case3(); 93 | //case4(); 94 | } 95 | -------------------------------------------------------------------------------- /mpl/dl1.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | #include 6 | using namespace std; 7 | 8 | extern "C" { 9 | #include 10 | } 11 | 12 | class DlManager 13 | { 14 | public: 15 | DlManager(const char* name) 16 | { 17 | m_h = dlopen(name, RTLD_NOW); 18 | assert(m_h); 19 | } 20 | ~DlManager() 21 | { 22 | dlclose(m_h); 23 | } 24 | 25 | template 26 | FuncType load(const char* func_name) 27 | { 28 | FuncType pf = reinterpret_cast 29 | (dlsym(m_h, func_name)); 30 | if (!pf) 31 | { 32 | throw std::runtime_error(dlerror()); 33 | } 34 | return pf; 35 | } 36 | private: 37 | typedef void* handle_t; 38 | handle_t m_h; 39 | }; 40 | 41 | typedef int (*Func1)(int); 42 | typedef int (*Func2)(int, int); 43 | 44 | int main() 45 | { 46 | cout << "dll version 1" << endl; 47 | 48 | DlManager dm("libtest.so"); 49 | 50 | Func1 f1 = dm.load("so_func1"); 51 | Func2 f2 = dm.load("so_func2"); 52 | 53 | cout << f1(10) << endl; 54 | cout << f2(10, 20) << endl; 55 | 56 | cout << dm.load("so_func1")(10) << endl; 57 | cout << dm.load("so_func2")(10, 20) << endl; 58 | 59 | } 60 | 61 | -------------------------------------------------------------------------------- /mpl/dl2.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | #include 6 | using namespace std; 7 | 8 | extern "C" { 9 | #include 10 | } 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | namespace mpl = boost::mpl; 20 | 21 | struct dl_front 22 | { 23 | typedef mpl::string<'lib','test','.so'> so_name; 24 | 25 | typedef int (*Func1)(int); 26 | typedef int (*Func2)(int, int); 27 | 28 | typedef mpl::string<'so_','func','1'> fun1_name; 29 | typedef mpl::string<'so_','func','2'> fun2_name; 30 | 31 | typedef mpl::map< 32 | mpl::pair, 33 | mpl::pair 34 | > map_fun; 35 | }; 36 | 37 | template 38 | class dl_back 39 | { 40 | private: 41 | void* m_h = nullptr; 42 | public: 43 | dl_back() 44 | { 45 | m_h = dlopen( 46 | mpl::c_str::value, RTLD_NOW); 47 | } 48 | ~dl_back() 49 | { 50 | dlclose(m_h); 51 | } 52 | public: 53 | //template 54 | //typename mpl::at::type 55 | //func() 56 | //{ 57 | // typedef typename 58 | // mpl::at::type result_type; 59 | // result_type pf = reinterpret_cast 60 | // (dlsym(m_h, mpl::c_str::value)); 61 | 62 | // return pf; 63 | //} 64 | 65 | template 66 | typename mpl::at::type 67 | func_ptr() 68 | { 69 | typedef typename 70 | mpl::at::type result_type; 71 | result_type pf = reinterpret_cast 72 | (dlsym(m_h, mpl::c_str::value)); 73 | 74 | return pf; 75 | } 76 | public: 77 | template 78 | 79 | // result_of 80 | //typename boost::result_of< 81 | // typename mpl::at::type(Args...) 82 | // >::type 83 | 84 | // function_traits + remove_pointer 85 | typename boost::function_traits< 86 | typename boost::remove_pointer< 87 | typename mpl::at::type>::type 88 | >::result_type 89 | func(Args const& ... args) 90 | { 91 | //typedef typename mpl::at::type 92 | // func_type; 93 | 94 | //func_type pf = reinterpret_cast 95 | // (dlsym(m_h, mpl::c_str::value)); 96 | 97 | //return pf(args...); 98 | 99 | return func_ptr()(args...); 100 | } 101 | 102 | }; 103 | 104 | int main() 105 | { 106 | cout << "dll version 2" << endl; 107 | 108 | dl_back dl; 109 | 110 | cout << dl.func_ptr()(10) << endl; 111 | cout << dl.func_ptr()(10,20) << endl; 112 | 113 | cout << dl.func(10) << endl; 114 | cout << dl.func(10,20) << endl; 115 | } 116 | -------------------------------------------------------------------------------- /mpl/if.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | using namespace boost::mpl; 9 | 10 | /////////////////////////////////////// 11 | #include 12 | 13 | void case1() 14 | { 15 | typedef if_c::type mdata1; 16 | assert((std::is_same::value)); 17 | 18 | typedef if_::type mdata2; 19 | assert((std::is_same::value)); 20 | 21 | typedef if_, 22 | integral_promotion::type, 23 | floating_point_promotion::type 24 | >::type mdata3; 25 | assert((std::is_same::value)); 26 | 27 | } 28 | 29 | /////////////////////////////////////// 30 | 31 | #include 32 | #include 33 | 34 | void case2() 35 | { 36 | typedef eval_if_c, identity>::type mdata1; 38 | assert((std::is_same::value)); 39 | 40 | typedef eval_if, identity>::type mdata2; 42 | assert((std::is_same::value)); 43 | 44 | typedef eval_if, 45 | integral_promotion, 46 | floating_point_promotion 47 | >::type mdata3; 48 | assert((std::is_same::value)); 49 | 50 | } 51 | 52 | /////////////////////////////////////// 53 | 54 | int main() 55 | { 56 | std::cout << "hello if" << std::endl; 57 | 58 | case1(); 59 | case2(); 60 | } 61 | -------------------------------------------------------------------------------- /mpl/integral.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | //#include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace boost; 13 | using namespace boost::mpl; 14 | 15 | /////////////////////////////////////// 16 | 17 | 18 | void case1() 19 | { 20 | typedef int_<2> i2; 21 | typedef integral_c s2; 22 | 23 | assert(i2::value == 2); 24 | assert(i2::value == s2::value); 25 | 26 | assert((is_same::value)); 27 | assert((is_same::value)); 28 | 29 | assert(i2::next::value == 3); 30 | assert(prior::type::value == 1); 31 | 32 | i2 two1; 33 | s2 two2; 34 | 35 | int i = two1 + two2; 36 | assert(i == int_<4>()); 37 | 38 | } 39 | 40 | /////////////////////////////////////// 41 | 42 | #include 43 | 44 | void case2() 45 | { 46 | assert(true_::value == true); 47 | assert(false_::value == false); 48 | 49 | assert((is_same >::value)); 50 | assert((is_same::value)); 51 | 52 | //next::type; 53 | 54 | } 55 | 56 | /////////////////////////////////////// 57 | 58 | #include 59 | #include 60 | #include 61 | 62 | void case3() 63 | { 64 | typedef int_<2> i2; 65 | typedef int_<5> i5; 66 | typedef int_<7> i7; 67 | 68 | assert((boost::mpl::plus::type::value == 14)); 69 | assert((boost::mpl::equal_to::type, i2>::type::value)); 70 | 71 | assert((boost::mpl::less::type::value)); 72 | assert((is_same::type, true_>::value)); 73 | 74 | assert((not_::type>::type::value)); 75 | assert((or_::type())); 76 | 77 | } 78 | 79 | /////////////////////////////////////// 80 | 81 | #include 82 | #include 83 | 84 | void case4() 85 | { 86 | typedef boost::mpl::plus, char_<2>, long_<3>>::type 87 | result; 88 | 89 | assert(!(is_same>::type::value)); 90 | 91 | assert((is_same>::type::value)); 92 | 93 | } 94 | 95 | /////////////////////////////////////// 96 | 97 | int main() 98 | { 99 | std::cout << "hello integral" << std::endl; 100 | 101 | case1(); 102 | case2(); 103 | case3(); 104 | } 105 | -------------------------------------------------------------------------------- /mpl/jamfile: -------------------------------------------------------------------------------- 1 | unit-test integral : integral.cpp ; 2 | unit-test ifc : if.cpp ; 3 | unit-test container : container.cpp ; 4 | unit-test algorithm : algorithm.cpp ; 5 | unit-test lambda : lambda.cpp ; 6 | unit-test assert : assert.cpp ; 7 | 8 | lib libtest : libtest.cpp : shared ; 9 | 10 | # cp *.so 11 | path-constant PWD : . ; 12 | install $PWD : libtest ; 13 | 14 | #g++ -g -o dl1 dl1.cpp -ldl 15 | 16 | lib dl : : shared ; 17 | 18 | unit-test dl1 : dl1.cpp dl ; 19 | unit-test dl2 : dl2.cpp dl ; 20 | -------------------------------------------------------------------------------- /mpl/libtest.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | 4 | extern "C" 5 | { 6 | 7 | int so_func1(int x) 8 | { return x * x;} 9 | 10 | int so_func2(int x, int y) 11 | { return x + y;} 12 | 13 | } 14 | 15 | -------------------------------------------------------------------------------- /multi_index/composite_key.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace boost; 16 | 17 | #define BOOST_MULTI_INDEX_DISABLE_SERIALIZATION 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace boost; 25 | using namespace boost::multi_index; 26 | 27 | namespace mi = boost::multi_index; 28 | 29 | /////////////////////////////////////// 30 | 31 | #include "person.hpp" 32 | 33 | /////////////////////////////////////// 34 | 35 | void case1() 36 | { 37 | typedef composite_key comp_key; 41 | 42 | typedef multi_index_container< 43 | person*, 44 | indexed_by< 45 | ordered_unique 46 | > 47 | > mic_t; 48 | 49 | ptr_vector vec; 50 | using namespace boost::assign; 51 | 52 | ptr_push_back(vec) 53 | (2, "agent", "smith") 54 | (1, "anderson", "neo") 55 | (1, "the one", "neo"); 56 | 57 | mic_t mic; 58 | for(auto& p : vec) 59 | { 60 | mic.insert(&p); 61 | } 62 | 63 | for(auto p : mic) 64 | { 65 | cout << p->m_id << ":" << p->m_fname << ","; 66 | } 67 | 68 | cout << endl; 69 | 70 | assert(mic.count(boost::make_tuple(1, "anderson")) == 1); 71 | assert(mic.find(boost::make_tuple(2, "agent")) != mic.end()); 72 | 73 | assert(mic.count(make_tuple(1)) == 2); 74 | assert(mic.count(1) == 2); 75 | } 76 | 77 | /////////////////////////////////////// 78 | 79 | void case2() 80 | { 81 | typedef composite_key comp_key; 85 | 86 | typedef composite_key_compare< 87 | std::less, 88 | std::greater 89 | > comp_key_compare; 90 | 91 | typedef multi_index_container< 92 | person*, 93 | indexed_by< 94 | ordered_unique< 95 | comp_key, 96 | comp_key_compare 97 | > 98 | > 99 | > mic_t; 100 | 101 | typedef multi_index_container< 102 | person*, 103 | indexed_by< 104 | ordered_unique< 105 | comp_key, 106 | composite_key_result_greater 107 | > 108 | > 109 | > mic_t2; 110 | 111 | boost::ignore_unused(); 112 | } 113 | 114 | /////////////////////////////////////// 115 | 116 | int main() 117 | { 118 | std::cout << "hello composite_key" << std::endl; 119 | 120 | case1(); 121 | } 122 | -------------------------------------------------------------------------------- /multi_index/hashed_index.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace boost; 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace boost; 17 | using namespace boost::multi_index; 18 | 19 | namespace mi = boost::multi_index; 20 | 21 | #include "person.hpp" 22 | 23 | /////////////////////////////////////// 24 | 25 | typedef boost::tuple hash_key_t; 26 | 27 | struct hash_func 28 | { 29 | typedef size_t result_type; 30 | size_t operator()(const hash_key_t& k) const //必须是const 31 | { 32 | size_t seed = 2016; 33 | hash_combine(seed, k.get<1>()); 34 | hash_combine(seed, k.get<2>()); 35 | return seed; 36 | } 37 | }; 38 | 39 | //定义相等比较函数对象 40 | struct equal_func 41 | { 42 | typedef bool result_type; 43 | bool operator()(const hash_key_t& k, const person& p) const 44 | { return k.get<0>() == p.m_id;} 45 | bool operator()(const person& p, const hash_key_t& k) const 46 | { return k.get<0>() == p.m_id;} 47 | }; 48 | 49 | /////////////////////////////////////// 50 | 51 | int main() 52 | { 53 | std::cout << "hello hashed_index" << std::endl; 54 | 55 | using namespace boost::assign; 56 | 57 | typedef multi_index_container> 60 | > 61 | > mic_t; 62 | 63 | mic_t mic; 64 | insert(mic) 65 | (person(2, "agent", "smith")) 66 | (person(1, "anderson", "neo")) 67 | (person(10, "lee", "bruce")); 68 | 69 | assert(mic.size() == 3); 70 | assert(mic.find(person(1, "anderson", "neo"))!= mic.end()); 71 | 72 | assert(mic.count(boost::make_tuple(1, "anderson", "neo"), 73 | hash_func(), equal_func()) == 1); 74 | assert(mic.find(boost::make_tuple(10, "lee", "bruce"), 75 | hash_func(), equal_func())!= mic.end()); 76 | 77 | }; 78 | 79 | -------------------------------------------------------------------------------- /multi_index/jamfile: -------------------------------------------------------------------------------- 1 | unit-test sample1 : sample1.cpp ; 2 | unit-test sample2 : sample2.cpp ; 3 | unit-test sample3 : sample3.cpp ; 4 | 5 | unit-test key_extractor : key_extractor.cpp ; 6 | unit-test sequenced_index : sequenced_index.cpp ; 7 | unit-test random_access_index : random_access_index.cpp ; 8 | unit-test ordered_index : ordered_index.cpp ; 9 | unit-test hashed_index : hashed_index.cpp ; 10 | unit-test modify : modify.cpp ; 11 | 12 | unit-test multi_index_container : multi_index_container.cpp ; 13 | 14 | unit-test composite_key : composite_key.cpp ; 15 | -------------------------------------------------------------------------------- /multi_index/key_extractor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | using namespace boost::multi_index; 9 | namespace mi = boost::multi_index; 10 | 11 | #include "person.hpp" 12 | 13 | /////////////////////////////////////// 14 | 15 | void case1() 16 | { 17 | assert((is_same::result_type>::value)); 18 | assert(identity()(10) == 10); 19 | assert(identity()("abc") == "abc"); 20 | 21 | int* p = new int(100); 22 | int** pp = &p; 23 | assert(identity()(pp) == 100); 24 | 25 | } 26 | 27 | /////////////////////////////////////// 28 | 29 | void case2() 30 | { 31 | typedef pair pair_t; 32 | pair_t p(1, "one"); 33 | 34 | assert((member()(p) == 1)); 35 | assert((member()(p) == "one")); 36 | 37 | person per(1, "anderson", "neo"); 38 | assert((member()(per) == 1)); 39 | 40 | assert((BOOST_MULTI_INDEX_MEMBER(pair_t, int, first)()(p) == 1)); 41 | assert((BOOST_MULTI_INDEX_MEMBER(pair_t, string, second)()(p) == "one")); 42 | assert((BOOST_MULTI_INDEX_MEMBER(person, int, m_id)()(per) == 1)); 43 | 44 | } 45 | 46 | /////////////////////////////////////// 47 | 48 | void case3() 49 | { 50 | typedef BOOST_MULTI_INDEX_CONST_MEM_FUN(person, \ 51 | const string&, first_name) cmf_t2; 52 | typedef BOOST_MULTI_INDEX_CONST_MEM_FUN(string, size_t, size) cmf_t; 53 | 54 | string str("abc"); 55 | //typedef const_mem_fun cmf_t; 56 | assert(cmf_t()(str) == 3); 57 | 58 | person per(1, "anderson", "neo"); 59 | //typedef const_mem_fun cmf_t2; 60 | assert((cmf_t2()(per) == "anderson")); 61 | 62 | } 63 | 64 | /////////////////////////////////////// 65 | 66 | void case4() 67 | { 68 | person per(1, "anderson", "neo"); 69 | typedef global_fun gf_t; 70 | assert(gf_t()(per) == "anderson neo"); 71 | 72 | } 73 | 74 | /////////////////////////////////////// 75 | 76 | int main() 77 | { 78 | std::cout << "hello key_extractor" << std::endl; 79 | 80 | case1(); 81 | case2(); 82 | case3(); 83 | case4(); 84 | } 85 | -------------------------------------------------------------------------------- /multi_index/multi_index_container.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace boost; 11 | 12 | #define BOOST_MULTI_INDEX_DISABLE_SERIALIZATION 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | using namespace boost; 22 | using namespace boost::multi_index; 23 | 24 | namespace mi = boost::multi_index; 25 | 26 | /////////////////////////////////////// 27 | 28 | #include "person.hpp" 29 | 30 | struct compare_by_id 31 | { 32 | typedef bool result_type; 33 | bool operator()(const person& p, int id) const 34 | { return p.m_id < id;} 35 | bool operator()(int id, const person& p) const 36 | { return id < p.m_id ;} 37 | }; 38 | 39 | /////////////////////////////////////// 40 | 41 | int main() 42 | { 43 | std::cout << "hello multi_index_container" << std::endl; 44 | 45 | typedef sequenced > idx_sf0; 46 | typedef random_access > idx_sf1; 47 | 48 | typedef ordered_unique> idx_sf2; 49 | typedef ordered_non_unique< 50 | BOOST_MULTI_INDEX_MEMBER(person, string, m_fname) > idx_sf3; 51 | typedef ordered_unique< 52 | BOOST_MULTI_INDEX_CONST_MEM_FUN( 53 | person, const string&, first_name), 54 | std::greater > idx_sf4; 55 | 56 | typedef hashed_unique< 57 | BOOST_MULTI_INDEX_MEMBER(person, string, m_lname) > idx_sf5; 58 | typedef hashed_non_unique< 59 | global_fun > idx_sf6; 60 | typedef hashed_unique< person_name> idx_sf7; 61 | 62 | typedef multi_index_container 68 | > mic_t; 69 | 70 | mic_t mic ; 71 | 72 | using namespace boost::assign; 73 | push_back(mic) 74 | (person(2, "agent", "smith")) 75 | (person(20, "lee", "someone")) 76 | (person(1, "anderson", "neo")) 77 | (person(10, "lee", "bruce")); 78 | 79 | assert(mic.size() == 3); 80 | 81 | auto& idx1 = mic.get(); 82 | auto& idx2 = mic.get<2>(); 83 | 84 | auto pos = idx2.find(1, compare_by_id()); 85 | auto pos2 = mic.project(pos); 86 | 87 | assert(pos2 == idx1.iterator_to(idx1[2])); 88 | 89 | typedef boost::shared_ptr person_ptr; 90 | 91 | typedef hashed_unique< 92 | BOOST_MULTI_INDEX_MEM_FUN( 93 | person, string&, last_name) 94 | > idx_sf8; 95 | 96 | typedef multi_index_container > mic_t2; 98 | 99 | mic_t2 mic2 ; 100 | mic2.insert(boost::make_shared(2, "agent", "smith")); 101 | 102 | } 103 | 104 | -------------------------------------------------------------------------------- /multi_index/ordered_index.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | #include 11 | #include 12 | #include 13 | using namespace boost::multi_index; 14 | 15 | /////////////////////////////////////// 16 | 17 | void case1() 18 | { 19 | using namespace boost::assign; 20 | 21 | typedef multi_index_container> 24 | > 25 | > mic_t1; 26 | 27 | mic_t1 mic1; 28 | insert(mic1)(2), 3, 5, 7, 11; 29 | 30 | assert(mic1.size() == 5); 31 | assert(!mic1.insert(3).second); 32 | assert(mic1.find(7) != mic1.end()); 33 | } 34 | 35 | /////////////////////////////////////// 36 | 37 | void case2() 38 | { 39 | using namespace boost::assign; 40 | 41 | typedef multi_index_container 45 | > 46 | > mic_t2; 47 | 48 | mic_t2 mic2; 49 | insert(mic2)("111")("22")("333")("4444"); 50 | assert(mic2.count(3) == 2); 51 | 52 | BOOST_FOREACH(auto& str, mic2.equal_range(3)) 53 | { cout << str << ",";} 54 | 55 | cout << endl; 56 | } 57 | 58 | /////////////////////////////////////// 59 | 60 | #include "person.hpp" 61 | 62 | struct compare_by_id 63 | { 64 | typedef bool result_type; 65 | bool operator()(const person& p, int id) const 66 | { return p.m_id < id;} 67 | bool operator()(int id, const person& p) const 68 | { return id < p.m_id ;} 69 | }; 70 | 71 | struct lower_bounder 72 | { 73 | typedef bool result_type; 74 | bool operator()(const person& p) 75 | { return p.m_id >= 2;} 76 | }; 77 | struct upper_bounder 78 | { 79 | typedef bool result_type; 80 | bool operator()(const person& p) 81 | { return p.m_id < 20;} 82 | }; 83 | 84 | void case3() 85 | { 86 | using namespace boost::assign; 87 | 88 | typedef multi_index_container> 91 | > 92 | > mic_t; 93 | 94 | mic_t mic; 95 | insert(mic) 96 | (person(2, "agent", "smith")) 97 | (person(20, "lee", "someone")) 98 | (person(1, "anderson", "neo")) 99 | (person(10, "lee", "bruce")); 100 | 101 | assert(mic.count(person(1, "abc","xby")) == 1); 102 | 103 | assert(mic.count(1, compare_by_id()) == 1); 104 | assert(mic.find(10, compare_by_id()) != mic.end()); 105 | 106 | mic_t::iterator l = mic.lower_bound(2 , compare_by_id()); 107 | 108 | mic_t::iterator u = mic.lower_bound(20, compare_by_id()); 109 | 110 | //BOOST_FOREACH(const person& p, std::make_pair(l, u)) 111 | 112 | //BOOST_FOREACH(const person& p, 113 | // mic.range(lower_bounder(), upper_bounder())) 114 | 115 | BOOST_FOREACH(const person& p, 116 | mic.range( 117 | [](const person& p){ return p.m_id >= 2;}, 118 | [](const person& p){ return p.m_id < 20;} 119 | )) 120 | { 121 | cout << p.m_id << ":" 122 | << nameof(p) << endl; 123 | } 124 | 125 | } 126 | 127 | /////////////////////////////////////// 128 | 129 | int main() 130 | { 131 | std::cout << "hello ordered_index" << std::endl; 132 | 133 | case1(); 134 | case2(); 135 | case3(); 136 | } 137 | -------------------------------------------------------------------------------- /multi_index/person.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_PERSON_HPP 4 | #define _PRO_BOOST_PERSON_HPP 5 | 6 | #include 7 | #include 8 | 9 | class person: 10 | boost::less_than_comparable 11 | { 12 | public: 13 | int m_id; 14 | string m_fname, m_lname; 15 | 16 | person(int id, const string& f, const string& l): 17 | m_id(id), m_fname(f), m_lname(l){} 18 | 19 | const string& first_name() const 20 | { return m_fname;} 21 | 22 | string& last_name() 23 | { return m_lname;} 24 | 25 | friend bool operator<(const person&l, const person& r) 26 | { return l.m_id < r.m_id;} 27 | 28 | friend bool operator==(const person& l, const person& r) 29 | { return l.m_id == r.m_id ;} 30 | 31 | friend std::size_t hash_value(const person& p) 32 | { 33 | size_t seed = 2016; 34 | boost::hash_combine(seed, p.m_fname); 35 | boost::hash_combine(seed, p.m_lname); 36 | return seed; 37 | } 38 | }; 39 | 40 | struct person_name 41 | { 42 | typedef string result_type; 43 | result_type operator()(const person& p) const 44 | { 45 | return p.m_fname + " " +p.m_lname; 46 | } 47 | result_type operator()(person *const p) const 48 | { 49 | return p->m_fname + " " +p->m_lname; 50 | } 51 | }; 52 | 53 | string nameof(const person& p) 54 | { 55 | return p.m_fname + " " +p.m_lname; 56 | } 57 | 58 | #endif // _PRO_BOOST_PERSON_HPP 59 | -------------------------------------------------------------------------------- /multi_index/random_access_index.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include 10 | #include 11 | using namespace boost::multi_index; 12 | 13 | int main() 14 | { 15 | typedef multi_index_container > 17 | > mic_t; 18 | 19 | using namespace boost::assign; 20 | mic_t mic1 = (list_of(2),3,5,7,11); 21 | 22 | assert(mic1[0] == 2); 23 | assert(mic1.at(2) == 5); 24 | 25 | mic1.erase(boost::next(mic1.begin(), 2)); 26 | assert(mic1[2] == 7); 27 | 28 | mic_t mic2; 29 | mic2.splice(mic2.end(), mic1); 30 | assert(mic1.empty() && mic2.size() == 4); 31 | 32 | push_front(mic1)(8), 10, 20, 16; 33 | 34 | mic1.sort(); 35 | mic2.sort(); 36 | 37 | mic1.merge(mic2); 38 | 39 | for (auto iter = mic1.rbegin(); 40 | iter != mic1.rend(); ++iter) 41 | { 42 | cout << *iter << ","; 43 | } 44 | 45 | //std::sort(mic1.begin(), mic1.end()); 46 | //std::random_shuffle(mic1.begin(), mic1.end()); 47 | //std::replace(mic1.begin(), mic1.end(), 2, 222); 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /multi_index/sample1.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace boost::multi_index; 11 | namespace mi = boost::multi_index; 12 | 13 | int main() 14 | { 15 | multi_index_container mic; 16 | assert(mic.empty()); 17 | 18 | mic.insert(1); 19 | assert(mic.size() == 1); 20 | 21 | using namespace boost::assign; 22 | insert(mic)(2), 7, 6, 8; 23 | 24 | assert(mic.size() == 5); 25 | assert(mic.count(2) == 1); 26 | assert(mic.find(10) == mic.end()); 27 | 28 | for(int i : mic) 29 | { 30 | cout << i << ','; 31 | } 32 | cout << endl; 33 | 34 | decltype(mic) c = {1,2,3}; 35 | for(int i : c) 36 | { 37 | cout << i << ','; 38 | } 39 | cout << endl; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /multi_index/sample2.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | using namespace boost::multi_index; 14 | namespace mi = boost::multi_index; 15 | 16 | int main() 17 | { 18 | typedef multi_index_container>, 21 | hashed_unique > 22 | > 23 | > mic_t; 24 | mic_t mic = {2, 1, 7, 6, 8}; 25 | 26 | //using namespace boost::assign; 27 | //insert(mic)(2), 1, 7, 6, 8; 28 | 29 | assert(mic.size() == 5); 30 | assert(mic.count(2) == 1); 31 | assert(mic.find(10) == mic.end()); 32 | 33 | auto& hash_index = mic.get<1>(); 34 | 35 | assert(hash_index.size() == 5); 36 | assert(hash_index.count(2) == 1); 37 | assert(hash_index.find(10) == hash_index.end()); 38 | 39 | BOOST_FOREACH(int i, hash_index) 40 | { 41 | cout << i << ","; 42 | } 43 | cout << endl; 44 | 45 | for(int i : mic) 46 | { 47 | cout << i << ','; 48 | } 49 | cout << endl; 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /multi_index/sample3.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace boost::multi_index; 16 | namespace mi = boost::multi_index; 17 | 18 | #include "person.hpp" 19 | 20 | /////////////////////////////////////// 21 | 22 | int main() 23 | { 24 | typedef multi_index_container< 25 | person, 26 | indexed_by< 27 | sequenced<>, 28 | ordered_unique>, 29 | ordered_non_unique< 30 | member>, 31 | hashed_unique> 32 | > 33 | > mic_t; 34 | 35 | mic_t mic; 36 | //= { 37 | //person(2, "agent", "smith"), 38 | //person(20, "lee", "someone"), 39 | //person(1, "anderson", "neo"), 40 | //person(10, "lee", "bruce") 41 | //}; 42 | 43 | using namespace boost::assign; 44 | push_front(mic) 45 | (person(2, "agent", "smith")) 46 | (person(20, "lee", "someone")) 47 | (person(1, "anderson", "neo")) 48 | (person(10, "lee", "bruce")); 49 | 50 | auto& index0 = mic.get<0>(); 51 | auto& index1 = mic.get<1>(); 52 | auto& index2 = mic.get<2>(); 53 | auto& index3 = mic.get<3>(); 54 | 55 | for(const person& p : index0) 56 | { 57 | cout << p.m_id << p.m_fname << ","; 58 | } 59 | 60 | assert(index1.begin()->m_id == 1); 61 | 62 | assert(index2.count("lee") == 2); 63 | 64 | insert(index2)(person(30, "lee", "test")); 65 | assert(index3.size() == 5); 66 | 67 | assert(!index2.insert(person(2, "lee", "test2")).second); 68 | } 69 | 70 | -------------------------------------------------------------------------------- /multi_index/sequenced_index.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost::multi_index; 9 | 10 | int main() 11 | { 12 | std::cout << "hello sequenced_index" << std::endl; 13 | 14 | typedef multi_index_container > > 17 | > mic_t; 18 | 19 | //using namespace boost::assign; 20 | //mic_t mic = (list_of(2),3,5,7,11); 21 | mic_t mic = {2,3,5,7,11}; 22 | 23 | assert(!mic.empty() && mic.size() == 5); 24 | 25 | assert(mic.front() == 2); 26 | assert(mic.back() == 11); 27 | 28 | assert(mic.push_front(2).second); 29 | assert(mic.push_back(19).second); 30 | 31 | auto& seq_index = mic.get(); 32 | 33 | seq_index.insert(seq_index.begin(), 5, 100); 34 | assert(std::count(seq_index.begin(), mic.end(), 100) == 5); 35 | 36 | seq_index.unique(); 37 | assert(std::count(seq_index.begin(), mic.end(), 100) == 1); 38 | 39 | //mic_t mic1 = (list_of(2),3,5,7,11); 40 | mic_t mic1 = {2,3,5,7,11}; 41 | 42 | mic_t mic2 = mic1; 43 | assert(mic1 == mic2); 44 | 45 | mic2.push_back(3); 46 | assert(mic1 < mic2); 47 | 48 | //*seq_index.begin() = 9; 49 | 50 | auto& x = mic.front(); 51 | assert(mic.begin() == mic.iterator_to(x)); 52 | assert(mic.begin() != mic.iterator_to(2)); 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /practice/client.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | using std::string; 9 | 10 | #include 11 | using namespace boost; 12 | using namespace boost::asio; 13 | 14 | int main() 15 | { 16 | cout << "client start" << endl; 17 | 18 | io_service ios; 19 | ip::tcp::socket sock(ios); 20 | 21 | ip::tcp::endpoint ep( 22 | ip::address::from_string("127.0.0.1"), 6677); 23 | sock.connect(ep); 24 | 25 | string str("hello world"); 26 | sock.write_some(buffer(str)); 27 | 28 | vector v(100, 0); 29 | size_t n = sock.read_some(buffer(v)); 30 | cout << "recv "<< n << " :" << v.data() << endl; 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /practice/intdef.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_INTDEF_HPP 4 | #define _PRO_BOOST_INTDEF_HPP 5 | 6 | #include 7 | 8 | using boost::int8_t; 9 | using boost::uint8_t; 10 | using boost::int16_t; 11 | using boost::uint16_t; 12 | using boost::int32_t; 13 | using boost::uint32_t; 14 | using boost::int64_t; 15 | using boost::uint64_t; 16 | 17 | typedef boost::uint8_t byte_t; 18 | typedef boost::uint8_t uchar_t; 19 | typedef unsigned short ushort_t; 20 | typedef unsigned int uint_t; 21 | typedef unsigned long ulong_t; 22 | 23 | typedef boost::uint16_t word_t; 24 | typedef boost::uint32_t dword_t; 25 | 26 | #endif //_PRO_BOOST_INTDEF_HPP 27 | 28 | -------------------------------------------------------------------------------- /practice/io_service_pool.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_IO_SERVICE_HPP 4 | #define _PRO_BOOST_IO_SERVICE_HPP 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | class io_service_pool final : boost::noncopyable 20 | { 21 | public: 22 | typedef boost::asio::io_service ios_type; 23 | typedef boost::asio::io_service::work work_type; 24 | typedef boost::ptr_vector io_services_type; 25 | typedef boost::ptr_vector works_type; 26 | 27 | private: 28 | io_services_type m_io_services; 29 | works_type m_works; 30 | 31 | boost::thread_group m_threads; 32 | std::size_t m_next_io_service; 33 | public: 34 | explicit io_service_pool(int n = 1): 35 | m_next_io_service(0) 36 | { 37 | BOOST_ASSERT(n > 0); 38 | init(n); 39 | } 40 | private: 41 | void init(int n) 42 | { 43 | for (int i = 0;i < n; ++i) 44 | { 45 | m_io_services.push_back( 46 | boost::factory()()); 47 | 48 | m_works.push_back( 49 | boost::factory() 50 | (m_io_services.back())); 51 | } 52 | } 53 | public: 54 | ios_type& get() 55 | { 56 | if (m_next_io_service >= m_io_services.size()) 57 | { m_next_io_service = 0; } 58 | 59 | return m_io_services[m_next_io_service++]; 60 | } 61 | public: 62 | void start() 63 | { 64 | if (m_threads.size() > 0) 65 | { return; } 66 | 67 | for(ios_type& ios : m_io_services) 68 | { 69 | m_threads.create_thread( 70 | boost::bind(&ios_type::run, 71 | boost::ref(ios))); 72 | } 73 | } 74 | 75 | void run() 76 | { 77 | start(); 78 | m_threads.join_all(); 79 | } 80 | public: 81 | void stop() 82 | { 83 | m_works.clear(); 84 | 85 | std::for_each(m_io_services.begin(), m_io_services.end(), 86 | boost::bind(&ios_type::stop, _1)); 87 | } 88 | }; 89 | 90 | #endif // _PRO_BOOST_IO_SERVICE_HPP 91 | -------------------------------------------------------------------------------- /practice/jamfile: -------------------------------------------------------------------------------- 1 | 2 | exe srv : srv.cpp /lib//boost_thread ; 3 | exe client : client.cpp /lib//boost_thread ; 4 | -------------------------------------------------------------------------------- /practice/srv.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | using std::cout; 6 | using std::endl; 7 | 8 | #include "tcp_server.hpp" 9 | 10 | int main() 11 | { 12 | cout << "server start" << endl; 13 | 14 | tcp_server svr(6677); 15 | svr.run(); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /practice/tcp_buffer.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_TCP_BUFFER_HPP 4 | #define _PRO_BOOST_TCP_BUFFER_HPP 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #if BOOST_VERSION <= 106400 11 | #include 12 | #else 13 | #include 14 | #endif 15 | 16 | class tcp_buffer final 17 | { 18 | public: 19 | typedef std::size_t size_type; 20 | typedef boost::asio::streambuf streambuf_type; 21 | typedef streambuf_type::const_buffers_type const_buffers_type; 22 | typedef streambuf_type::mutable_buffers_type mutable_buffers_type; 23 | 24 | private: 25 | BOOST_STATIC_CONSTANT(size_type, BUF_SIZE = 512); 26 | streambuf_type m_buf; 27 | public: 28 | mutable_buffers_type prepare(size_type n = BUF_SIZE) 29 | { return m_buf.prepare(n);} 30 | 31 | void retrieve(size_type n) 32 | { m_buf.commit(n);} 33 | 34 | size_type size() const 35 | { return m_buf.size();} 36 | 37 | const char* peek() const 38 | { return boost::asio::buffer_cast(m_buf.data());} 39 | public: 40 | void append(const void* data, size_type len) 41 | { 42 | m_buf.sputn(static_cast(data), 43 | boost::numeric_cast(len)); 44 | } 45 | const_buffers_type data() const 46 | { return m_buf.data();} 47 | public: 48 | void consume(size_type n) 49 | { m_buf.consume(n);} 50 | }; 51 | 52 | 53 | #endif //_PRO_BOOST_TCP_BUFFER_HPP 54 | -------------------------------------------------------------------------------- /practice/tcp_server.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_TCP_SERVER_HPP 4 | #define _PRO_BOOST_TCP_SERVER_HPP 5 | 6 | #include 7 | #include 8 | 9 | #include "intdef.hpp" 10 | #include "tcp_session.hpp" 11 | #include "io_service_pool.hpp" 12 | 13 | class tcp_server final 14 | { 15 | public: 16 | typedef tcp_server this_type; 17 | 18 | typedef io_service_pool::ios_type ios_type; 19 | 20 | typedef boost::asio::ip::tcp::acceptor acceptor_type; 21 | typedef boost::asio::ip::tcp::endpoint endpoint_type; 22 | public: 23 | tcp_server( ushort_t port, int n = 1 ): 24 | m_ios_pool(n), 25 | m_acceptor(m_ios_pool.get(), 26 | endpoint_type( 27 | boost::asio::ip::tcp::v4(), port)) 28 | { 29 | start_accept(); 30 | } 31 | 32 | public: 33 | void start() 34 | { m_ios_pool.start();} 35 | 36 | void run() 37 | { m_ios_pool.run();} 38 | 39 | private: 40 | io_service_pool m_ios_pool; 41 | acceptor_type m_acceptor; 42 | 43 | private: 44 | void start_accept() 45 | { 46 | auto session = 47 | boost::factory()(m_ios_pool.get()); 48 | 49 | m_acceptor.async_accept( session->socket(), 50 | bind(&this_type::handle_accept, this, 51 | boost::asio::placeholders::error, session)); 52 | } 53 | 54 | void handle_accept(const boost::system::error_code& error, tcp_session_ptr session) 55 | { 56 | start_accept(); 57 | 58 | if (error) 59 | { 60 | session->close(); 61 | return; 62 | } 63 | 64 | session->start(); 65 | } 66 | 67 | }; 68 | 69 | 70 | #endif // _PRO_BOOST_TCP_SERVER_HPP 71 | -------------------------------------------------------------------------------- /practice/tcp_session.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_TCP_SESSION_HPP 4 | #define _PRO_BOOST_TCP_SESSION_HPP 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include "tcp_buffer.hpp" 11 | 12 | class tcp_session final : 13 | public boost::enable_shared_from_this 14 | { 15 | public: 16 | typedef tcp_session this_type; 17 | 18 | typedef boost::asio::ip::tcp::socket socket_type; 19 | typedef boost::asio::io_service ios_type; 20 | typedef tcp_buffer buffer_type; 21 | 22 | private: 23 | socket_type m_socket; 24 | 25 | buffer_type m_read_buf; 26 | buffer_type m_write_buf; 27 | public: 28 | tcp_session( ios_type& ios ): 29 | m_socket(ios) {} 30 | 31 | public: 32 | socket_type& socket() 33 | { return m_socket;} 34 | 35 | ios_type& io_service() 36 | { return m_socket.get_io_service();} 37 | 38 | buffer_type& read_buf() 39 | { return m_read_buf;} 40 | 41 | buffer_type& write_buf() 42 | { return m_write_buf;} 43 | 44 | public: 45 | void start() 46 | { 47 | //cout << "session start" << endl; 48 | 49 | read(); 50 | } 51 | 52 | void close() 53 | { 54 | boost::system::error_code ignored_ec; 55 | 56 | m_socket.shutdown( 57 | boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); 58 | 59 | m_socket.close(ignored_ec); 60 | } 61 | 62 | private: 63 | void read() 64 | { 65 | m_socket.async_read_some( 66 | m_read_buf.prepare(), 67 | bind(&this_type::handle_read, shared_from_this(), 68 | boost::asio::placeholders::error, 69 | boost::asio::placeholders::bytes_transferred) 70 | ); 71 | } 72 | 73 | void handle_read(const boost::system::error_code& error, size_t bytes_transferred) 74 | { 75 | if (error) 76 | { 77 | close(); 78 | return; 79 | } 80 | 81 | //cout << "read size" << bytes_transferred << endl; 82 | 83 | m_read_buf.retrieve(bytes_transferred); 84 | //cout << string(m_read_buf.peek(), bytes_transferred) << endl; 85 | 86 | write(m_read_buf.peek(), bytes_transferred); 87 | 88 | m_read_buf.consume(bytes_transferred); 89 | 90 | read(); 91 | } 92 | 93 | public: 94 | void write( const void* data, size_t len ) 95 | { 96 | //cout << "write:" << len << endl; 97 | //cout << static_cast(data) << endl; 98 | 99 | m_write_buf.append(data, len); 100 | 101 | write(); 102 | } 103 | 104 | void write() 105 | { 106 | m_socket.async_write_some( 107 | m_write_buf.data(), 108 | bind(&this_type::handle_write, shared_from_this(), 109 | boost::asio::placeholders::error, 110 | boost::asio::placeholders::bytes_transferred) 111 | ); 112 | } 113 | 114 | private: 115 | void handle_write( const boost::system::error_code& error, size_t bytes_transferred ) 116 | { 117 | if (error) 118 | { 119 | close(); 120 | return; 121 | } 122 | 123 | m_write_buf.consume(bytes_transferred); 124 | //cout << "write complete" << endl; 125 | } 126 | }; 127 | 128 | typedef boost::shared_ptr tcp_session_ptr; 129 | 130 | 131 | #endif // _PRO_BOOST_TCP_SESSION_HPP 132 | -------------------------------------------------------------------------------- /practice2/client.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | using std::string; 9 | 10 | #include 11 | using namespace boost; 12 | using namespace boost::asio; 13 | 14 | int main() 15 | { 16 | cout << "client start" << endl; 17 | 18 | io_service ios; 19 | ip::tcp::socket sock(ios); 20 | 21 | ip::tcp::endpoint ep( 22 | ip::address::from_string("127.0.0.1"), 6677); 23 | sock.connect(ep); 24 | 25 | string str("hello world"); 26 | sock.write_some(buffer(str)); 27 | 28 | vector v(100, 0); 29 | size_t n = sock.read_some(buffer(v)); 30 | cout << "recv "<< n << " :" << v.data() << endl; 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /practice2/intdef.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_INTDEF_HPP 4 | #define _PRO_BOOST_INTDEF_HPP 5 | 6 | #include 7 | 8 | using boost::int8_t; 9 | using boost::uint8_t; 10 | using boost::int16_t; 11 | using boost::uint16_t; 12 | using boost::int32_t; 13 | using boost::uint32_t; 14 | using boost::int64_t; 15 | using boost::uint64_t; 16 | 17 | typedef boost::uint8_t byte_t; 18 | typedef boost::uint8_t uchar_t; 19 | typedef unsigned short ushort_t; 20 | typedef unsigned int uint_t; 21 | typedef unsigned long ulong_t; 22 | 23 | typedef boost::uint16_t word_t; 24 | typedef boost::uint32_t dword_t; 25 | 26 | #endif //_PRO_BOOST_INTDEF_HPP 27 | 28 | -------------------------------------------------------------------------------- /practice2/io_service_pool.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_IO_SERVICE_HPP 4 | #define _PRO_BOOST_IO_SERVICE_HPP 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | class io_service_pool final : boost::noncopyable 20 | { 21 | public: 22 | typedef boost::asio::io_service ios_type; 23 | typedef boost::asio::io_service::work work_type; 24 | typedef boost::ptr_vector io_services_type; 25 | typedef boost::ptr_vector works_type; 26 | 27 | private: 28 | io_services_type m_io_services; 29 | works_type m_works; 30 | 31 | boost::thread_group m_threads; 32 | std::size_t m_next_io_service; 33 | public: 34 | explicit io_service_pool(int n = 1): 35 | m_next_io_service(0) 36 | { 37 | BOOST_ASSERT(n > 0); 38 | init(n); 39 | } 40 | private: 41 | void init(int n) 42 | { 43 | for (int i = 0;i < n; ++i) 44 | { 45 | m_io_services.push_back( 46 | boost::factory()()); 47 | 48 | m_works.push_back( 49 | boost::factory() 50 | (m_io_services.back())); 51 | } 52 | } 53 | public: 54 | ios_type& get() 55 | { 56 | if (m_next_io_service >= m_io_services.size()) 57 | { m_next_io_service = 0; } 58 | 59 | return m_io_services[m_next_io_service++]; 60 | } 61 | public: 62 | void start() 63 | { 64 | if (m_threads.size() > 0) 65 | { return; } 66 | 67 | for(ios_type& ios : m_io_services) 68 | { 69 | m_threads.create_thread( 70 | boost::bind(&ios_type::run, 71 | boost::ref(ios))); 72 | } 73 | } 74 | 75 | void run() 76 | { 77 | start(); 78 | m_threads.join_all(); 79 | } 80 | public: 81 | void stop() 82 | { 83 | m_works.clear(); 84 | 85 | std::for_each(m_io_services.begin(), m_io_services.end(), 86 | boost::bind(&ios_type::stop, _1)); 87 | } 88 | }; 89 | 90 | #endif // _PRO_BOOST_IO_SERVICE_HPP 91 | -------------------------------------------------------------------------------- /practice2/jamfile: -------------------------------------------------------------------------------- 1 | 2 | exe srv : srv.cpp 3 | /lib//boost_thread 4 | /lib//boost_date_time 5 | ; 6 | exe client : client.cpp /lib//boost_thread ; 7 | #exe echo : echo.cpp /lib//boost_thread ; 8 | -------------------------------------------------------------------------------- /practice2/tcp_buffer.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_TCP_BUFFER_HPP 4 | #define _PRO_BOOST_TCP_BUFFER_HPP 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #if BOOST_VERSION <= 106400 11 | #include 12 | #else 13 | #include 14 | #endif 15 | 16 | class tcp_buffer final 17 | { 18 | public: 19 | typedef std::size_t size_type; 20 | typedef boost::asio::streambuf streambuf_type; 21 | typedef streambuf_type::const_buffers_type const_buffers_type; 22 | typedef streambuf_type::mutable_buffers_type mutable_buffers_type; 23 | 24 | private: 25 | BOOST_STATIC_CONSTANT(size_type, BUF_SIZE = 512); 26 | streambuf_type m_buf; 27 | public: 28 | mutable_buffers_type prepare(size_type n = BUF_SIZE) 29 | { return m_buf.prepare(n);} 30 | 31 | void retrieve(size_type n) 32 | { m_buf.commit(n);} 33 | 34 | size_type size() const 35 | { return m_buf.size();} 36 | 37 | const char* peek() const 38 | { return boost::asio::buffer_cast(m_buf.data());} 39 | public: 40 | void append(const void* data, size_type len) 41 | { 42 | m_buf.sputn(static_cast(data), 43 | boost::numeric_cast(len)); 44 | } 45 | const_buffers_type data() const 46 | { return m_buf.data();} 47 | public: 48 | void consume(size_type n) 49 | { m_buf.consume(n);} 50 | }; 51 | 52 | 53 | #endif //_PRO_BOOST_TCP_BUFFER_HPP 54 | -------------------------------------------------------------------------------- /practice2/tcp_server.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #ifndef _PRO_BOOST_TCP_SERVER_HPP 4 | #define _PRO_BOOST_TCP_SERVER_HPP 5 | 6 | #include 7 | #include 8 | 9 | #include "intdef.hpp" 10 | #include "tcp_session.hpp" 11 | #include "io_service_pool.hpp" 12 | 13 | template 14 | class tcp_server final 15 | { 16 | public: 17 | typedef tcp_server this_type; 18 | typedef Handler handler_type; 19 | 20 | typedef tcp_session session_type; 21 | typedef boost::shared_ptr session_ptr; 22 | 23 | typedef io_service_pool::ios_type ios_type; 24 | 25 | typedef boost::asio::ip::tcp::acceptor acceptor_type; 26 | typedef boost::asio::ip::tcp::endpoint endpoint_type; 27 | public: 28 | tcp_server( ushort_t port, int n = 1 ): 29 | m_ios_pool(n), 30 | m_acceptor(m_ios_pool.get(), 31 | endpoint_type( 32 | boost::asio::ip::tcp::v4(), port)) 33 | { 34 | start_accept(); 35 | } 36 | 37 | public: 38 | void start() 39 | { m_ios_pool.start();} 40 | 41 | void run() 42 | { m_ios_pool.run();} 43 | 44 | private: 45 | io_service_pool m_ios_pool; 46 | acceptor_type m_acceptor; 47 | 48 | private: 49 | void start_accept() 50 | { 51 | auto session = 52 | boost::factory()(m_ios_pool.get()); 53 | 54 | m_acceptor.async_accept( session->socket(), 55 | bind(&this_type::handle_accept, this, 56 | boost::asio::placeholders::error, session)); 57 | } 58 | 59 | void handle_accept(const boost::system::error_code& error, session_ptr session) 60 | { 61 | start_accept(); 62 | 63 | if (error) 64 | { 65 | session->close(); 66 | return; 67 | } 68 | 69 | session->start(); 70 | } 71 | 72 | }; 73 | 74 | 75 | #endif // _PRO_BOOST_TCP_SERVER_HPP 76 | -------------------------------------------------------------------------------- /preprocessor/arithmetic.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | // gcc -P -E -o a.out arithmetic.cpp;cat a.out;rm a.out 6 | # 7 | # 8 | # 9 | 10 | #if 1+1 > 3 11 | #error test arithmetic in if/endif 12 | #endif 13 | 14 | #define calc(x, y) int x = y 15 | //calc(x, 1+2) 16 | calc(x, BOOST_PP_ADD(1,2)) 17 | 18 | #define x 1 19 | #define y 2 20 | 21 | #define v BOOST_PP_ADD(x, y) 22 | 23 | // check v 24 | v 25 | 26 | #if v != 3 27 | BOOST_PP_ASSERT(0) 28 | #endif 29 | 30 | #define u BOOST_PP_SUB(v, x) 31 | u 32 | 33 | #if u != y 34 | BOOST_PP_ASSERT(0) 35 | #endif 36 | 37 | #define w BOOST_PP_INC(BOOST_PP_INC(u)) 38 | w 39 | 40 | #if w != 4 41 | BOOST_PP_ASSERT_MSG(0) 42 | #endif 43 | 44 | #if BOOST_PP_BOOL(w) 45 | #define a BOOST_PP_MOD(10, 4) 46 | a 47 | //#define b BOOST_PP_MUL(a, 300) 48 | //b 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /preprocessor/basic.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | // gcc -P -E -o a.out basic.cpp;cat a.out;rm a.out 6 | 7 | ///////////////// 8 | 9 | #if 0x10 > -1L 10 | # // do nothing 11 | #endif 12 | 13 | // no float in if 14 | //#if 3.14 > 0 15 | //# 16 | //#endif 17 | 18 | ///////////////// 19 | 20 | #define t1 3.14 21 | #define t2 ~!@#$%^&*()-+ 22 | #define t3 'snake' 23 | #define t4 "quiet" 24 | 25 | // check meta data 26 | t1 27 | t2 28 | t3 29 | t4 30 | 31 | ///////////////// 32 | 33 | #define data1 vector 34 | #define data2 boost::factory 35 | 36 | // check meta data 37 | data1 38 | data2 39 | 40 | ///////////////// 41 | 42 | #define func(x,y) int x = y 43 | #define vfunc(x,...) x{__VA_ARGS__} 44 | 45 | // call meta function 46 | func(data1, data2) 47 | vfunc(vector v, 1,2,3) 48 | 49 | ///////////////// 50 | 51 | #define op1(x) BOOST_PP_STRINGIZE(x) 52 | #define op2(x,y,z) BOOST_PP_CAT(x, \ 53 | BOOST_PP_CAT(y,z)) 54 | //#define op1(x) #x 55 | //#define op2(x,y,z) x##y##z 56 | 57 | op1(data1) 58 | op2(data1, data2, z) 59 | 60 | ///////////////// 61 | #undef data1 62 | #undef data2 63 | -------------------------------------------------------------------------------- /preprocessor/cat.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | // gcc -P -E -o a.out cat.cpp;cat a.out;rm a.out 6 | 7 | #define token BOOST_PP_CAT(x, y) 8 | 9 | // check token == xy 10 | token 11 | 12 | -------------------------------------------------------------------------------- /preprocessor/functions.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | // gcc -std=c++11 -P -E -o a.out functions.cpp;cat a.out;rm a.out 6 | 7 | ///////////////// 8 | 9 | #if __cplusplus < 201103L 10 | BOOST_PP_ASSERT(0) 11 | #endif 12 | 13 | #define x BOOST_PP_MUL(6, 6) 14 | 15 | BOOST_PP_ASSERT(BOOST_PP_EQUAL(x, 36)) 16 | 17 | #undef x 18 | 19 | ///////////////// 20 | 21 | #define helper(z, n, d) BOOST_PP_CAT(d,n) 22 | #define DECL_VARS(n, var) BOOST_PP_ENUM(n, helper, var) 23 | 24 | int DECL_VARS(10, x); 25 | 26 | #undef helper 27 | #undef DECL_VARS 28 | 29 | #define NGX_NULL_HELPER(z, n, d) d 30 | #define NGX_MODULE_NULL(n) \ 31 | BOOST_PP_ENUM(n, NGX_NULL_HELPER, nullptr) 32 | 33 | {NGX_MODULE_NULL(3)} 34 | 35 | ///////////////// 36 | 37 | #define helper(z, n, d) d##n{n}; 38 | #define DECL_VARS(n, decl) BOOST_PP_REPEAT(n, helper, decl) 39 | 40 | DECL_VARS(3, int x) 41 | 42 | #undef helper 43 | #undef DECL_VARS 44 | 45 | -------------------------------------------------------------------------------- /preprocessor/seq.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | // gcc -P -E -o a.out seq.cpp;cat a.out;rm a.out 6 | 7 | ///////////////// 8 | 9 | #define seq1 (a)(b)(c)(d)(e) 10 | #define seq2 (~123)([])(--{}--) 11 | 12 | BOOST_PP_SEQ_SIZE(seq1) 13 | BOOST_PP_SEQ_CAT(seq1) 14 | 15 | int BOOST_PP_SEQ_HEAD(seq1); 16 | 17 | #define helper(r, d, e) BOOST_PP_STRINGIZE(e) 18 | BOOST_PP_SEQ_FOR_EACH(helper, ~, seq2) 19 | 20 | #undef seq1 21 | #undef seq2 22 | 23 | ///////////////// 24 | 25 | #define bns_helper(r, d, e) namespace e { 26 | 27 | #define begin_namespace(s) \ 28 | BOOST_PP_SEQ_FOR_EACH(bns_helper, ~, s) 29 | 30 | #define ens_helper(r, d, e) } 31 | 32 | #define end_namespace(s) \ 33 | BOOST_PP_SEQ_FOR_EACH(ens_helper, ~, s) 34 | 35 | #define seq (boost) (detail) (test) 36 | 37 | // namespace boost { namespace detail { namespace test { 38 | begin_namespace(seq) 39 | 40 | // } } } 41 | end_namespace(seq) 42 | -------------------------------------------------------------------------------- /ptr_container/assign.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | using namespace boost; 15 | 16 | /////////////////////////////////////// 17 | 18 | void case1() 19 | { 20 | using namespace boost::assign; 21 | 22 | ptr_vector v; 23 | ptr_push_back(v)()(1)(2)(100); 24 | assert(v.size() == 4); 25 | 26 | ptr_list > lt; 27 | ptr_push_front(lt)(1, 2)(0.618, 1.732); 28 | ptr_push_back>(lt)(2.718, 3.14); 29 | assert(lt.size() == 3); 30 | 31 | ptr_multimap m; 32 | ptr_map_insert(m)(1, "one")(1, "neo"); 33 | assert(m.size() == 2); 34 | 35 | } 36 | 37 | /////////////////////////////////////// 38 | #include 39 | 40 | void case2() 41 | { 42 | using namespace boost::assign; 43 | 44 | ptr_vector v = ptr_list_of()(1)(2); 45 | 46 | ptr_deque > dq = 47 | ptr_list_of>(1, 2)(0.618, 1.732); 48 | 49 | ptr_set s ; 50 | s = ptr_list_of()("abc") ("xyz").to_container(s); 51 | } 52 | 53 | 54 | /////////////////////////////////////// 55 | 56 | int main() 57 | { 58 | std::cout << "hello ptr_unordered_set" << std::endl; 59 | 60 | case1(); 61 | case2(); 62 | } 63 | 64 | -------------------------------------------------------------------------------- /ptr_container/indirect_fun.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | int main() 11 | { 12 | std::cout << "hello indirect_fun" << std::endl; 13 | 14 | typedef boost::shared_ptr ptr_string; 15 | 16 | ptr_string s1(new string("chrono")); 17 | ptr_string s2(new string("trigger")); 18 | 19 | assert(indirect_fun >()(s1, s2)); 20 | cout << indirect_fun >()(s1, s2) << endl; 21 | 22 | void* p1 = s1.get(); 23 | void* p2 = s2.get(); 24 | 25 | assert((void_ptr_indirect_fun, string >()(p1, p2))); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /ptr_container/jamfile: -------------------------------------------------------------------------------- 1 | unit-test sample : sample.cpp ; 2 | unit-test reversible_ptr_container : reversible_ptr_container.cpp ; 3 | unit-test ptr_sequence_adapter : ptr_sequence_adapter.cpp ; 4 | 5 | unit-test ptr_vector : ptr_vector.cpp ; 6 | unit-test ptr_deque : ptr_deque.cpp ; 7 | unit-test ptr_list : ptr_list.cpp ; 8 | unit-test ptr_circular_buffer : ptr_circular_buffer.cpp ; 9 | unit-test ptr_array : ptr_array.cpp ; 10 | 11 | unit-test nullable : nullable.cpp ; 12 | 13 | unit-test ptr_set : ptr_set.cpp ; 14 | unit-test ptr_unordered_set : ptr_unordered_set.cpp ; 15 | unit-test ptr_map : ptr_map.cpp ; 16 | unit-test ptr_unordered_map : ptr_unordered_map.cpp ; 17 | 18 | unit-test assign : assign.cpp ; 19 | unit-test algorithm : algorithm.cpp ; 20 | unit-test indirect_fun : indirect_fun.cpp ; 21 | unit-test view_clone_allocator : view_clone_allocator.cpp ; 22 | -------------------------------------------------------------------------------- /ptr_container/nullable.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | #include 9 | using namespace boost; 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | typedef ptr_vector > ptr_null_vec; 16 | ptr_null_vec vec; 17 | 18 | vec.push_back(nullptr); 19 | vec.push_back(new int(100)); 20 | 21 | assert(vec.is_null(0)); 22 | assert(!vec.is_null(1)); 23 | 24 | for (auto i = vec.begin(); 25 | i != vec.end(); ++i) 26 | { 27 | if (!boost::is_null(i)) 28 | { cout << *i << " "; } 29 | } 30 | 31 | cout << endl; 32 | } 33 | 34 | /////////////////////////////////////// 35 | 36 | class item : boost::noncopyable 37 | { 38 | public: 39 | virtual ~item(){} 40 | virtual void print() = 0; 41 | }; 42 | class abstract_item : public item 43 | { 44 | string name; 45 | public: 46 | abstract_item(const string& str):name(str){} 47 | virtual ~abstract_item(){} 48 | 49 | virtual void print() final override 50 | { cout << name << endl; } 51 | }; 52 | 53 | class television : public abstract_item 54 | { 55 | public: 56 | television():abstract_item("television") 57 | {} 58 | }; 59 | class computer : public abstract_item 60 | { 61 | public: 62 | computer():abstract_item("computer") 63 | {} 64 | }; 65 | class null_item final : public item 66 | { 67 | virtual void print() {} 68 | }; 69 | 70 | void case2() 71 | { 72 | typedef ptr_vector ptr_vec; 73 | ptr_vec vec; 74 | 75 | vec.push_back(new television); 76 | vec.push_back(new computer); 77 | vec.push_back(new null_item); 78 | 79 | for(auto& i : vec) 80 | { 81 | i.print(); 82 | } 83 | 84 | vec.replace(2, new computer); 85 | vec[2].print(); 86 | } 87 | 88 | /////////////////////////////////////// 89 | #include 90 | 91 | void case3() 92 | { 93 | typedef ptr_vector ptr_vec; 94 | ptr_vec vec; 95 | 96 | using namespace boost::assign; 97 | 98 | ptr_push_back(vec)(); 99 | ptr_push_back(vec)(); 100 | 101 | ptr_vec v2; 102 | v2.transfer(v2.begin(), vec.begin(), vec); 103 | 104 | { 105 | //ptr_vec v2(vec); 106 | //v2 = vec.clone(); 107 | } 108 | 109 | } 110 | 111 | 112 | /////////////////////////////////////// 113 | 114 | int main() 115 | { 116 | std::cout << "hello nullable" << std::endl; 117 | 118 | case1(); 119 | case2(); 120 | case3(); 121 | } 122 | -------------------------------------------------------------------------------- /ptr_container/ptr_array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_array ptr_arr; 14 | ptr_arr arr; 15 | 16 | assert(!arr.empty()); 17 | assert(arr.size() == 5 && arr.max_size() == 5); 18 | assert(arr.base().front() == nullptr); 19 | 20 | arr.replace(0, new string("metroid")); 21 | arr.replace<2>(new string("prime")); 22 | arr.replace<4>(new string("other m")); 23 | assert(arr.front() == "metroid"); 24 | assert(arr.back() == "other m"); 25 | 26 | arr[0] = "samus"; 27 | arr.at<2>() = "alan"; 28 | 29 | assert(*boost::next(arr.begin(), 2) == "alan"); 30 | 31 | } 32 | 33 | /////////////////////////////////////// 34 | 35 | int main() 36 | { 37 | std::cout << "hello ptr_array" << std::endl; 38 | 39 | case1(); 40 | } 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /ptr_container/ptr_circular_buffer.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_circular_buffer ptr_buffer; 14 | ptr_buffer cb(5); 15 | 16 | assert(cb.empty()); 17 | 18 | cb.push_front(new string("link")); 19 | cb.push_back(new string("zelda")); 20 | cb.push_back(new string("epona")); 21 | 22 | assert(cb.size() == 3); 23 | assert(!cb.full()); 24 | assert(cb[1] == "zelda"); 25 | 26 | string** p = cb.linearize(); 27 | assert(*p[0] == "link"); 28 | 29 | cb.rotate( boost::prior(cb.end()) ); 30 | assert(*cb.c_array()[0] == "epona"); 31 | 32 | } 33 | 34 | /////////////////////////////////////// 35 | 36 | int main() 37 | { 38 | std::cout << "hello ptr_circular_buffer" << std::endl; 39 | 40 | case1(); 41 | } 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /ptr_container/ptr_deque.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_deque ptr_dq; 14 | ptr_dq dq; 15 | 16 | assert(dq.empty()); 17 | 18 | dq.push_front(new string("mario")); 19 | dq.push_back(new string("peach")); 20 | assert(dq[0] == "mario"); 21 | 22 | dq.replace(0, new string("luigi")); 23 | assert(dq.front() == "luigi"); 24 | 25 | string** arr = new string*[2]; 26 | arr[0] = new string("123"); 27 | arr[1] = new string("abc"); 28 | 29 | dq.transfer(dq.begin(), arr, 2); 30 | assert(dq.size() == 4); 31 | 32 | string** p = dq.c_array(); 33 | assert(*p[0] == "123" && *p[2] == "luigi"); 34 | 35 | } 36 | 37 | /////////////////////////////////////// 38 | 39 | int main() 40 | { 41 | std::cout << "hello ptr_deque" << std::endl; 42 | 43 | case1(); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /ptr_container/ptr_list.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | void case1() 13 | { 14 | typedef ptr_list ptr_lt; 15 | ptr_lt lt; 16 | 17 | lt.push_front(new string("mario")); 18 | lt.push_back(new string("peach")); 19 | lt.insert(lt.end(), new string("yoshi")); 20 | assert(lt.size() == 3); 21 | 22 | ptr_deque dq(lt.begin(), std::next(lt.begin(), 2)); 23 | assert(dq.size() == 2); 24 | assert(lt.size() == 3); 25 | 26 | } 27 | 28 | /////////////////////////////////////// 29 | 30 | int main() 31 | { 32 | std::cout << "hello ptr_list" << std::endl; 33 | 34 | case1(); 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /ptr_container/ptr_map.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_map ptr_map_t; 14 | ptr_map_t m; 15 | 16 | int a = 1; 17 | m.insert(a, new string("one")); 18 | a = 10; 19 | m.insert(a, new string("ten")); 20 | //m.insert(10, auto_ptr(new string("ten"))); 21 | 22 | assert(m.size() == 2); 23 | assert(m[10] == "ten"); 24 | 25 | m.replace(m.begin(), new string("neo")); 26 | m[3] = "three"; 27 | assert(m.at(1) == "neo" && m.at(3) == "three"); 28 | 29 | for(const auto& i : m) 30 | //for(const ptr_map_t::value_type& i : m) 31 | { 32 | cout << *i->second << ","; 33 | } 34 | 35 | typedef ptr_multimap ptr_multimap_t; 36 | ptr_multimap_t mm; 37 | 38 | mm.transfer(m.begin(), m); 39 | assert(m.size() == 2); 40 | assert(mm.count(1) == 1); 41 | 42 | } 43 | 44 | /////////////////////////////////////// 45 | 46 | void case2() 47 | { 48 | typedef ptr_map > ptr_map_t; 49 | ptr_map_t m; 50 | 51 | int a = 10; 52 | m.insert(a, (new string("ten"))); 53 | //m.insert(10, auto_ptr(new string("ten"))); 54 | m.at(10) = "tenten"; 55 | 56 | //m[3] = "three"; 57 | 58 | } 59 | 60 | /////////////////////////////////////// 61 | 62 | int main() 63 | { 64 | std::cout << "hello ptr_map" << std::endl; 65 | 66 | case1(); 67 | case2(); 68 | } 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /ptr_container/ptr_sequence_adapter.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_vector ptr_vec; 14 | ptr_vec vec; 15 | 16 | vec.push_back(new string("123")); 17 | vec.push_back(new string("abc")); 18 | 19 | assert(vec.front() == "123"); 20 | assert(vec.back() == "abc"); 21 | 22 | ptr_vec vec2; 23 | vec2.assign(vec.begin(), vec.end()); 24 | assert(vec2.size() == 2); 25 | 26 | assert(*vec2.pop_back() == "abc"); 27 | assert(vec2.size() == 1); 28 | 29 | vec.resize(5); 30 | assert(vec.back().empty()); 31 | 32 | vec.resize(10, new string("xyz")); 33 | assert(vec.back() == "xyz"); 34 | 35 | vec.transfer(vec.end(), vec2.begin(), vec2); 36 | assert(vec.size() == 11); 37 | assert(vec2.size() == 0); 38 | 39 | vec2.transfer(vec2.end(), vec.begin(),vec.begin() + 5, vec); 40 | assert(vec.size() == 6); 41 | assert(vec2.size() == 5); 42 | 43 | vec.transfer(vec.begin(), vec2); 44 | assert(vec.size() == 11); 45 | assert(vec2.size() == 0); 46 | 47 | } 48 | 49 | /////////////////////////////////////// 50 | 51 | int main() 52 | { 53 | std::cout << "hello ptr_sequence_adapter" << std::endl; 54 | 55 | case1(); 56 | } 57 | -------------------------------------------------------------------------------- /ptr_container/ptr_set.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_set ptr_set_t; 14 | ptr_set_t s; 15 | assert(s.empty()); 16 | 17 | assert(s.insert(new string("fire")).second); 18 | assert(s.insert(new string("emblem")).second); 19 | assert(s.size() == 2); 20 | 21 | assert(!s.insert(new string("fire")).second); 22 | assert(s.size() == 2); 23 | 24 | auto ap = s.release(); 25 | assert(s.empty()); 26 | 27 | typedef ptr_multiset ptr_mset_t; 28 | ptr_mset_t ms(ap->begin(), ap->end()); 29 | assert(ms.size() == 2); 30 | 31 | ms.insert(new string("fire")); 32 | ms.insert(new string("emblem")); 33 | assert(ms.size() == 4); 34 | assert(ms.count("fire") == 2); 35 | 36 | } 37 | 38 | /////////////////////////////////////// 39 | 40 | int main() 41 | { 42 | std::cout << "hello ptr_set" << std::endl; 43 | 44 | case1(); 45 | } 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /ptr_container/ptr_unordered_map.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_unordered_map ptr_map_t; 14 | ptr_map_t m; 15 | 16 | int a = 1; 17 | m.insert(m.begin(),a, new string("one")); 18 | a = 10; 19 | m.insert(m.end(), a, 20 | (new string("ten"))); 21 | //auto_ptr(new string("ten"))); 22 | m[3] = "three"; 23 | 24 | assert(m.at(3) == "three"); 25 | assert(m[10] == "ten"); 26 | 27 | for(const auto& i : m) 28 | { 29 | cout << i->second << ","; 30 | } 31 | 32 | typedef ptr_unordered_multimap ptr_multimap_t; 33 | ptr_multimap_t mm; 34 | 35 | mm.transfer(m.begin(), m.end(), m); 36 | assert(mm.size() == 3); 37 | assert(m.empty() ); 38 | 39 | } 40 | 41 | /////////////////////////////////////// 42 | 43 | int main() 44 | { 45 | std::cout << "hello ptr_unordered_map" << std::endl; 46 | 47 | case1(); 48 | } 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /ptr_container/ptr_unordered_set.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_unordered_set ptr_set_t; 14 | ptr_set_t s; 15 | assert(s.empty()); 16 | 17 | assert(s.insert(new string("king")).second); 18 | assert(s.insert(new string("kong")).second); 19 | assert(s.size() == 2); 20 | 21 | assert(!s.insert(new string("king")).second); 22 | assert(s.size() == 2); 23 | 24 | ptr_set_t s2; 25 | s2.transfer(s); 26 | assert(s.empty() && s2.size() == 2); 27 | 28 | typedef ptr_unordered_multiset ptr_mset_t; 29 | ptr_mset_t ms; 30 | 31 | ms.transfer(s2); 32 | assert(ms.size() == 2); 33 | 34 | ms.insert(new string("king")); 35 | ms.insert(new string("kong")); 36 | assert(ms.size() == 4); 37 | assert(ms.count("king") == 2); 38 | 39 | } 40 | 41 | /////////////////////////////////////// 42 | 43 | int main() 44 | { 45 | std::cout << "hello ptr_unordered_set" << std::endl; 46 | 47 | case1(); 48 | } 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /ptr_container/ptr_vector.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | 12 | int main() 13 | { 14 | std::cout << "hello ptr_vector" << std::endl; 15 | 16 | typedef ptr_vector ptr_vec; 17 | ptr_vec vec(10); 18 | 19 | assert(vec.empty()); 20 | assert(vec.capacity() == 10); 21 | 22 | vec.push_back(new string("star")); 23 | assert(vec[0] == "star"); 24 | 25 | vec.replace(0, new string("fox")); 26 | assert(vec[0] == "fox"); 27 | 28 | auto arr = new string*[2]; 29 | arr[0] = new string("123"); 30 | arr[1] = new string("abc"); 31 | 32 | vec.transfer(vec.begin(), arr, 2); 33 | assert(vec.size() == 3); 34 | 35 | auto p = vec.c_array(); 36 | assert(*p[0] == "123" && *p[2] == "fox"); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ptr_container/reversible_ptr_container.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_vector ptr_vec; 14 | 15 | assert((std::is_same::value)); 16 | assert((std::is_same::value)); 17 | assert((std::is_same::value)); 18 | 19 | ptr_vec vec; 20 | assert(vec.empty()); 21 | 22 | vec.push_back(new string("123")); 23 | vec.push_back(new string("abc")); 24 | assert(vec.size() == 2); 25 | 26 | ptr_vec vec2(vec); 27 | assert(vec2.size() == 2); 28 | assert(vec.size() == 2); 29 | 30 | auto apv = vec.release(); 31 | assert(vec.empty()); 32 | 33 | ptr_vec vec3(apv); 34 | assert(vec3.size() == 2); 35 | 36 | ptr_vec vec4, vec5; 37 | 38 | vec4 = vec2; 39 | assert(vec2.size() == 2); 40 | assert(vec4.size() == 2); 41 | 42 | vec5 = vec3.release(); 43 | assert(vec3.empty()); 44 | assert(vec5.size() == 2); 45 | 46 | } 47 | 48 | /////////////////////////////////////// 49 | 50 | void case2() 51 | { 52 | typedef ptr_vector ptr_vec; 53 | ptr_vec vec; 54 | 55 | vec.push_back(new string("123")); 56 | vec.push_back(new string("abc")); 57 | assert(vec.size() == 2); 58 | 59 | auto pos = 60 | vec.insert(vec.begin(), new string("000")); 61 | assert(vec.size() == 3); 62 | assert(pos == vec.begin()); 63 | 64 | ++pos; 65 | pos = vec.erase(pos); 66 | assert(vec.size() == 2); 67 | assert(*pos == "abc"); 68 | 69 | auto p = 70 | vec.replace(pos, new string("xyz")); 71 | assert(*p == "abc"); 72 | assert(vec.size() == 2); 73 | 74 | ptr_vec vec2; 75 | vec2 = vec.clone(); 76 | assert(vec.size() == 2); 77 | assert(vec2.size() == 2); 78 | } 79 | 80 | /////////////////////////////////////// 81 | 82 | int main() 83 | { 84 | std::cout << "hello reversible_ptr_container" << std::endl; 85 | 86 | case1(); 87 | case2(); 88 | } 89 | 90 | -------------------------------------------------------------------------------- /ptr_container/sample.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | typedef ptr_vector ptr_vec; 14 | ptr_vec vec; 15 | 16 | vec.push_back(new string("123")); 17 | vec.push_back(new string("abc")); 18 | 19 | assert(!vec.empty()); 20 | assert(vec.size() == 2); 21 | 22 | assert(vec[0] == "123"); 23 | vec.back() = "def"; 24 | assert(vec[1] == "def"); 25 | 26 | auto iter = vec.begin(); 27 | assert(iter->length() == 3); 28 | 29 | vec.clear(); 30 | assert(vec.empty()); 31 | 32 | } 33 | 34 | /////////////////////////////////////// 35 | 36 | void case2() 37 | { 38 | vector vec; 39 | vec.push_back(new string("123")); 40 | vec.push_back(new string("123")); 41 | 42 | assert(*vec[0] == "123"); 43 | *vec.back() = "def"; 44 | assert(*vec[1] == "def"); 45 | 46 | auto iter = vec.begin(); 47 | assert((*iter)->length() == 3); 48 | 49 | } 50 | 51 | /////////////////////////////////////// 52 | 53 | void case3() 54 | { 55 | typedef ptr_vector ptr_vec; 56 | ptr_vec vec; 57 | 58 | auto ap = (new string("123")); 59 | //auto_ptr ap(new string("123")); 60 | vec.push_back(ap); 61 | //assert(ap.get() == 0); 62 | 63 | vec.push_back((new string("abc"))); 64 | //vec.push_back(auto_ptr(new string("abc"))); 65 | vec.push_back(new string("xyz")); 66 | assert(vec.size() == 3); 67 | 68 | ptr_vec::auto_type p = 69 | vec.release(vec.begin()); 70 | assert(vec.size() == 2); 71 | assert(p && *p == "123"); 72 | 73 | string* sp = p.release(); 74 | assert(!p); 75 | delete sp; 76 | 77 | ptr_vec vec2; 78 | 79 | vec2.transfer(vec2.end(),vec); 80 | assert(vec.empty()); 81 | assert(vec2.size() == 2); 82 | 83 | } 84 | 85 | /////////////////////////////////////// 86 | 87 | int main() 88 | { 89 | std::cout << "hello ptr sample" << std::endl; 90 | 91 | case1(); 92 | case2(); 93 | case3(); 94 | } 95 | -------------------------------------------------------------------------------- /ptr_container/view_clone_allocator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace boost; 10 | 11 | int main() 12 | { 13 | using namespace boost::assign; 14 | 15 | ptr_vector v = ptr_list_of(100)(1)(10) (2); 16 | 17 | typedef ptr_set, boost::view_clone_allocator> set_view_t; 18 | set_view_t view(v.begin(), v.end()); 19 | 20 | for(auto& i : view) 21 | { 22 | cout << i << ","; 23 | } 24 | cout << endl; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /range/adaptors.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace boost; 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | vector v{7,8,4,6,53,2,6}; 16 | 17 | boost::copy( 18 | boost::sort(v) | 19 | adaptors::uniqued, 20 | ostream_iterator(cout, ",")); 21 | 22 | cout << endl; 23 | 24 | assert(boost::count(v, 6) == 2); 25 | 26 | auto even = [](int x){ return x%2==0;}; 27 | 28 | assert( boost::distance( 29 | v | adaptors::filtered(even)) == 5); 30 | 31 | boost::copy(v | adaptors::copied(0,5), 32 | ostream_iterator(cout, ",")); 33 | cout << endl; 34 | 35 | cout << "---" << endl; 36 | 37 | boost::copy(v | adaptors::filtered(even), 38 | ostream_iterator(cout, ",")); 39 | cout << endl; 40 | 41 | boost::copy(v | adaptors::reversed, 42 | ostream_iterator(cout, ",")); 43 | cout << endl; 44 | 45 | boost::copy(v | 46 | adaptors::filtered(even) | adaptors::reversed, 47 | ostream_iterator(cout, ",")); 48 | cout << endl; 49 | } 50 | 51 | /////////////////////////////////////// 52 | 53 | int main() 54 | { 55 | std::cout << "hello iterator adaptors" << std::endl; 56 | 57 | case1(); 58 | } 59 | -------------------------------------------------------------------------------- /range/algorithm.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | #include 6 | using namespace std; 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | using namespace boost; 14 | 15 | /////////////////////////////////////// 16 | 17 | void case1() 18 | { 19 | vector v{8,1,16,3,7,3,42}; 20 | 21 | assert(boost::count(v, 3) == 2); 22 | assert(boost::find(v, 7) != v.end()); 23 | 24 | boost::sort(v); 25 | assert(boost::adjacent_find(v) != v.end()); 26 | 27 | } 28 | 29 | /////////////////////////////////////// 30 | 31 | void case2() 32 | { 33 | vector v(10); 34 | rand48 rnd; 35 | 36 | boost::sort( 37 | boost::random_shuffle( 38 | boost::generate(v, rnd))); 39 | 40 | boost::copy(v, ostream_iterator(cout, " ")); 41 | 42 | cout << endl; 43 | } 44 | 45 | /////////////////////////////////////// 46 | 47 | void case3() 48 | { 49 | vector v{8,1,16,3,7,3,42}; 50 | 51 | boost::erase(v, 52 | boost::remove(v, 3)); 53 | 54 | assert(boost::find(v, 3) == v.end()); 55 | boost::copy(v, ostream_iterator(cout, ",")); 56 | 57 | } 58 | 59 | /////////////////////////////////////// 60 | 61 | int main() 62 | { 63 | std::cout << "hello range algorithm" << std::endl; 64 | 65 | case1(); 66 | case2(); 67 | case3(); 68 | } 69 | 70 | 71 | -------------------------------------------------------------------------------- /range/iterator_range.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace boost; 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | vector v(10); 16 | 17 | //typedef iterator_range::iterator> vec_range; 18 | //vec_range r1(v); 19 | 20 | auto r1 = make_iterator_range(v); 21 | 22 | assert(!r1.empty()); 23 | assert(r1.size() == 10); 24 | 25 | int a[10]; 26 | 27 | //typedef iterator_range int_range; 28 | //int_range r2(a, a + 5); 29 | 30 | auto r2 = make_iterator_range(a, a + 5); 31 | 32 | assert(r2.size() == 5); 33 | 34 | auto r3 = make_iterator_range(a, 1, -1); 35 | assert(r3.size() == 8); 36 | 37 | } 38 | 39 | /////////////////////////////////////// 40 | 41 | #include 42 | 43 | void case2() 44 | { 45 | char a[] = "iterator range"; 46 | 47 | auto r = boost::find_first(a, " "); 48 | assert(r.front() == ' '); 49 | 50 | auto r2 = make_iterator_range(a, r.begin()); 51 | assert(copy_range(r2) == "iterator"); 52 | 53 | } 54 | 55 | /////////////////////////////////////// 56 | 57 | #include 58 | #include 59 | #include 60 | 61 | void case3() 62 | { 63 | boost::copy( 64 | counting_range(0,10), ostream_iterator(cout, ",")); 65 | 66 | cout << endl; 67 | 68 | //boost::copy(istream_range(cin), ostream_iterator(cout, ",")); 69 | 70 | boost::copy(irange(0,10), ostream_iterator(cout, ",")); 71 | 72 | cout << endl; 73 | 74 | boost::copy(irange(0,20,2), ostream_iterator(cout, ",")); 75 | 76 | cout << endl; 77 | } 78 | 79 | /////////////////////////////////////// 80 | 81 | #include 82 | 83 | void case4() 84 | { 85 | string str = "abcde"; 86 | vector v = {5,4,3,2,1}; 87 | 88 | auto r = combine(str, v); 89 | 90 | for(const auto& x : r) 91 | { 92 | auto& c = get<0>(x); 93 | auto& i = get<1>(x); 94 | cout << "(" << c << "," << i << ") "; 95 | } 96 | 97 | cout << endl; 98 | 99 | //string s2 = "xyz"; 100 | 101 | //auto r2 = combine(s2, v); 102 | 103 | //for(const auto& x : r2) 104 | //{ 105 | // auto& c = get<0>(x); 106 | // auto& i = get<1>(x); 107 | // cout << "(" << c << "," << i << ") "; 108 | //} 109 | 110 | } 111 | 112 | /////////////////////////////////////// 113 | 114 | #include 115 | 116 | void case5() 117 | { 118 | list l = {1,3,5,7,9}; 119 | 120 | //typedef any_range_type_generator::type range_type; 121 | typedef any_range range_type; 122 | 123 | range_type r(l); 124 | 125 | for(const auto& x : r) 126 | { 127 | cout << x << ","; 128 | } 129 | 130 | cout << endl; 131 | 132 | vector v = {2,4,6,8,0}; 133 | 134 | r = v; 135 | 136 | for(const auto& x : r) 137 | { 138 | cout << x << ","; 139 | } 140 | 141 | cout << endl; 142 | 143 | set s = {-1,-2,-3}; 144 | 145 | r = s; 146 | 147 | for(const auto& x : r) 148 | { 149 | cout << x << ","; 150 | } 151 | } 152 | 153 | /////////////////////////////////////// 154 | 155 | int main() 156 | { 157 | std::cout << "hello iterator_range" << std::endl; 158 | 159 | case1(); 160 | case2(); 161 | case3(); 162 | case4(); 163 | case5(); 164 | } 165 | 166 | -------------------------------------------------------------------------------- /range/jamfile: -------------------------------------------------------------------------------- 1 | unit-test traits : traits.cpp ; 2 | unit-test algorithm : algorithm.cpp ; 3 | unit-test iterator_range : iterator_range.cpp ; 4 | unit-test adaptors : adaptors.cpp ; 5 | unit-test join : join.cpp ; 6 | -------------------------------------------------------------------------------- /range/join.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace boost; 10 | 11 | /////////////////////////////////////// 12 | 13 | #include 14 | #include 15 | 16 | void case1() 17 | { 18 | vector v; 19 | boost::copy(boost::irange(0,10), std::back_inserter(v)); 20 | 21 | auto r1 = make_iterator_range(v.begin(), v.begin() + 3); 22 | auto r2 = make_iterator_range(v.begin() + 5, v.end() ); 23 | 24 | auto r3 = boost::join(r1, r2); 25 | 26 | boost::copy(r3, ostream_iterator(cout, ",")); 27 | 28 | } 29 | 30 | /////////////////////////////////////// 31 | 32 | int main() 33 | { 34 | std::cout << "hello range join" << std::endl; 35 | 36 | case1(); 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /range/traits.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | 5 | #include 6 | using namespace std; 7 | 8 | #include 9 | #include 10 | using namespace boost; 11 | 12 | /////////////////////////////////////// 13 | 14 | void case1() 15 | { 16 | assert(has_range_iterator>::value); 17 | assert(has_range_iterator::value); 18 | assert(!has_range_iterator>::value); 19 | 20 | typedef boost::array array_t; 21 | assert(has_range_iterator::value); 22 | 23 | typedef pair pair_t; 24 | assert(has_range_iterator::value); 25 | 26 | char a[] = "range"; 27 | assert(has_range_iterator::value); 28 | 29 | assert(!has_range_iterator::value); 30 | 31 | } 32 | 33 | /////////////////////////////////////// 34 | 35 | int main() 36 | { 37 | std::cout << "hello range traits" << std::endl; 38 | 39 | case1(); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /type_traits/categories.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include "../meta/mp_utils.hpp" 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | assert(mp_eval(is_integral)); 16 | assert(mp_eval(is_integral)); 17 | assert(mp_eval(is_integral)); 18 | assert(!mp_eval(is_integral)); 19 | 20 | assert(mp_eval(is_floating_point)); 21 | assert(mp_eval(is_floating_point)); 22 | assert(!mp_eval(is_floating_point)); 23 | 24 | assert(mp_eval(is_void)); 25 | assert(!mp_eval(is_void)); 26 | 27 | // c++14 28 | //assert(mp_eval(is_null_pointer)); 29 | } 30 | 31 | /////////////////////////////////////// 32 | 33 | void case2() 34 | { 35 | assert(mp_eval(is_array)); 36 | assert(mp_eval(is_array)); 37 | 38 | assert(mp_eval(is_class)); 39 | assert(mp_eval(is_class >)); 40 | 41 | assert(mp_eval(is_pointer)); 42 | assert(mp_eval(is_pointer)); 43 | 44 | assert(mp_eval(is_function)); 45 | 46 | typedef float& float_ref; 47 | assert(mp_eval(is_lvalue_reference)); 48 | assert(mp_eval(is_lvalue_reference)); 49 | assert(mp_eval(is_lvalue_reference)); 50 | assert(mp_eval(is_rvalue_reference)); 51 | 52 | } 53 | 54 | /////////////////////////////////////// 55 | 56 | struct dummy 57 | { 58 | int x; 59 | double y; 60 | void func(){} 61 | }; 62 | 63 | void case3() 64 | { 65 | assert(mp_eval(is_member_object_pointer)); 66 | assert(mp_eval(is_member_object_pointer)); 67 | assert(mp_eval(is_member_function_pointer)); 68 | } 69 | 70 | /////////////////////////////////////// 71 | 72 | void case4() 73 | { 74 | assert(mp_eval(is_reference)); 75 | assert(mp_eval(is_reference)); 76 | assert(mp_eval(is_reference const&>)); 77 | 78 | assert(mp_eval(is_arithmetic) ); 79 | assert(mp_eval(is_arithmetic)); 80 | 81 | assert(!mp_eval(is_arithmetic)); 82 | assert(mp_eval(is_fundamental)); 83 | 84 | assert(mp_eval(is_member_pointer)); 85 | 86 | assert(mp_eval(is_compound)); 87 | assert(mp_eval(is_object)); 88 | 89 | assert(mp_eval(is_scalar)); 90 | assert(!mp_eval(is_scalar >)); 91 | 92 | } 93 | 94 | /////////////////////////////////////// 95 | 96 | int main() 97 | { 98 | std::cout << "hello type categories" << std::endl; 99 | 100 | case1(); 101 | case2(); 102 | case3(); 103 | case4(); 104 | } 105 | -------------------------------------------------------------------------------- /type_traits/examples.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include "../meta/mp_utils.hpp" 10 | 11 | /////////////////////////////////////// 12 | 13 | mp_arglist 14 | mp_function demo_func: 15 | conditional< 16 | is_pointer::value, 17 | typename add_const< 18 | typename remove_pointer::type 19 | >::type, 20 | typename add_pointer< 21 | typename add_const::type 22 | >::type 23 | >{}; 24 | 25 | /////////////////////////////////////// 26 | 27 | #include 28 | 29 | mp_arglist 30 | mp_function demo_func2: 31 | mpl::eval_if< 32 | is_pointer, 33 | add_const< 34 | typename remove_pointer::type 35 | >, 36 | add_pointer< 37 | typename add_const::type 38 | > 39 | >{}; 40 | 41 | 42 | /////////////////////////////////////// 43 | 44 | int main() 45 | { 46 | std::cout << "hello type examples" << std::endl; 47 | } 48 | -------------------------------------------------------------------------------- /type_traits/function_traits.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include "../meta/mp_utils.hpp" 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | mp_data void(mdata1)(int, std::string); 16 | assert((is_function::value)); 17 | 18 | const size_t n = function_traits::arity; 19 | assert((n == 2)); 20 | 21 | mp_data function_traits::result_type rtype; 22 | assert((is_void::value)); 23 | 24 | mp_data function_traits::arg2_type a2type; 25 | assert((is_same::value)); 26 | 27 | } 28 | 29 | /////////////////////////////////////// 30 | 31 | int main() 32 | { 33 | std::cout << "hello function_traits" << std::endl; 34 | 35 | case1(); 36 | } 37 | -------------------------------------------------------------------------------- /type_traits/implementation.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include "../meta/mp_utils.hpp" 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | assert((integral_constant::value == 10)); 16 | 17 | static_assert(integral_constant::value == '0', ""); 18 | 19 | static_assert(integral_constant::value == 100, ""); 20 | 21 | static_assert(true_type::value == true, ""); 22 | 23 | static_assert(mp_eval(false_type) == false, ""); 24 | 25 | } 26 | 27 | /////////////////////////////////////// 28 | 29 | int main() 30 | { 31 | std::cout << "hello type implementation" << std::endl; 32 | 33 | case1(); 34 | //case2(); 35 | //case3(); 36 | //case4(); 37 | } 38 | -------------------------------------------------------------------------------- /type_traits/jamfile: -------------------------------------------------------------------------------- 1 | unit-test categories : categories.cpp ; 2 | unit-test properties : properties.cpp ; 3 | unit-test relationships : relationships.cpp ; 4 | unit-test modifications : modifications.cpp ; 5 | unit-test function_traits : function_traits.cpp ; 6 | unit-test implementation : implementation.cpp ; 7 | unit-test examples : examples.cpp ; 8 | -------------------------------------------------------------------------------- /type_traits/modifications.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include "../meta/mp_utils.hpp" 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | mp_data mp_exec(add_const) mdata1; 16 | assert((is_const::value)); 17 | assert((is_same::value)); 18 | 19 | mp_data mp_exec(add_pointer) mdata2; 20 | assert((is_pointer::value)); 21 | assert((is_same::value)); 22 | 23 | mp_data mp_exec(add_lvalue_reference) mdata3; 24 | assert((is_lvalue_reference::value)); 25 | assert((is_same::value)); 26 | 27 | mp_data mp_exec(add_lvalue_reference) mdata4; 28 | assert((is_void::value)); 29 | 30 | } 31 | 32 | /////////////////////////////////////// 33 | 34 | void case2() 35 | { 36 | mp_data int const **& mdata1; 37 | 38 | mp_data mp_exec(remove_pointer) mdata2; 39 | assert((is_same::value)); 40 | 41 | mp_data mp_exec(remove_reference) mdata3; 42 | assert((is_pointer::value)); 43 | assert((is_same::value)); 44 | 45 | mp_data mp_exec(remove_pointer< 46 | mp_exec(remove_pointer)>) mdata4; 47 | 48 | assert((is_const::value)); 49 | assert((is_integral::value)); 50 | assert((is_same::value)); 51 | 52 | mp_data mp_exec(remove_const) mdata5; 53 | assert((is_same::value)); 54 | 55 | } 56 | 57 | /////////////////////////////////////// 58 | 59 | void case3() 60 | { 61 | mp_data short const mdata1; 62 | 63 | mp_data mp_exec(make_signed) mdata2; 64 | assert((is_const::value)); 65 | assert((is_signed::value)); 66 | 67 | assert((is_same::value)); 68 | 69 | mp_data mp_exec(make_unsigned) mdata3; 70 | assert((is_same::value)); 71 | } 72 | 73 | /////////////////////////////////////// 74 | 75 | void case4() 76 | { 77 | //mp_data int(mdata1)[5][7][9] ; 78 | using mdata1 = int[5][7][9]; 79 | 80 | mp_data mp_exec(remove_extent) mdata2; 81 | assert((is_same::value)); 82 | 83 | mp_data mp_exec(remove_all_extents) mdata4; 84 | assert((is_same::value)); 85 | 86 | } 87 | 88 | /////////////////////////////////////// 89 | void case5() 90 | { 91 | typedef common_type::type mdata1; 92 | assert((is_same::value)); 93 | 94 | typedef common_type::type mdata2; 95 | assert((is_same::value)); 96 | 97 | //typedef common_type::type mdata3; 98 | 99 | } 100 | 101 | /////////////////////////////////////// 102 | 103 | int main() 104 | { 105 | std::cout << "hello type modifications" << std::endl; 106 | 107 | case1(); 108 | case2(); 109 | case3(); 110 | case4(); 111 | case5(); 112 | } 113 | -------------------------------------------------------------------------------- /type_traits/properties.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include "../meta/mp_utils.hpp" 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | typedef const volatile int cv_int; 16 | assert(mp_eval(is_const)); 17 | assert(mp_eval(is_volatile)); 18 | assert(mp_eval(is_signed)); 19 | 20 | assert(mp_eval(rank) == 2); 21 | 22 | assert((extent::value == 3)); 23 | } 24 | 25 | /////////////////////////////////////// 26 | 27 | void case2() 28 | { 29 | assert(!mp_eval(is_pod)); 30 | assert(mp_eval(is_empty >)); 31 | assert(mp_eval(is_polymorphic)); 32 | 33 | struct x final {}; 34 | assert(mp_eval(!is_abstract)); 35 | assert(mp_eval(is_final)); 36 | } 37 | 38 | /////////////////////////////////////// 39 | 40 | void case3() 41 | { 42 | assert(mp_eval(has_greater)); 43 | assert(mp_eval(has_less)); 44 | assert(mp_eval(has_equal_to)); 45 | 46 | assert(mp_eval(has_greater)); 47 | assert(mp_eval(has_equal_to)); 48 | assert(mp_eval(has_plus)); 49 | assert(!mp_eval(has_minus)); 50 | 51 | assert(mp_eval(has_pre_increment)); 52 | } 53 | 54 | /////////////////////////////////////// 55 | 56 | int main() 57 | { 58 | std::cout << "hello type properties" << std::endl; 59 | 60 | case1(); 61 | case2(); 62 | case3(); 63 | } 64 | -------------------------------------------------------------------------------- /type_traits/relationships.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | #include "../meta/mp_utils.hpp" 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | mp_data int meta_data1; 16 | mp_data short meta_data2; 17 | 18 | assert((is_same::value)); 19 | assert((is_convertible::value)); 20 | 21 | assert((is_base_of::value)); 22 | assert((is_base_of::value)); 23 | 24 | assert((!is_virtual_base_of::value)); 25 | assert((is_virtual_base_of, std::ostream>::value)); 26 | } 27 | 28 | /////////////////////////////////////// 29 | 30 | int main() 31 | { 32 | std::cout << "hello type relationships" << std::endl; 33 | 34 | case1(); 35 | } 36 | -------------------------------------------------------------------------------- /utility/addressof.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | void case1() 12 | { 13 | int i; 14 | string s; 15 | 16 | assert(&i == boost::addressof(i)); 17 | assert(&s == boost::addressof(s)); 18 | 19 | int a[10]; 20 | 21 | assert(&a == boost::addressof(a)); 22 | assert(a + 1 == boost::addressof(a[1])); 23 | assert(&printf == boost::addressof(printf)); 24 | } 25 | 26 | /////////////////////////////////////// 27 | 28 | class dont_do_this 29 | { 30 | public: 31 | int x,y; 32 | size_t operator&() const 33 | { return (size_t)&y; } 34 | }; 35 | 36 | void case2() 37 | { 38 | dont_do_this d; 39 | assert(&d != (size_t)boost::addressof(d)); 40 | assert(&d == (size_t)&d.y); 41 | dont_do_this *p = boost::addressof(d); 42 | p->x = 1; 43 | 44 | } 45 | 46 | /////////////////////////////////////// 47 | class danger_class 48 | { 49 | void operator&() const; 50 | }; 51 | 52 | void case3() 53 | { 54 | danger_class d; 55 | // cout << &d; 56 | cout << boost::addressof(d); 57 | } 58 | 59 | 60 | /////////////////////////////////////// 61 | 62 | int main() 63 | { 64 | std::cout << "hello addressof" << std::endl; 65 | 66 | case1(); 67 | case2(); 68 | case3(); 69 | } 70 | -------------------------------------------------------------------------------- /utility/base_from_member.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #define BOOST_BASE_FROM_MEMBER_MAX_ARITY 2 7 | #include 8 | using namespace boost; 9 | 10 | /////////////////////////////////////// 11 | 12 | class base 13 | { 14 | public: 15 | base(complex c) 16 | { 17 | cout << "base ctor" << endl; 18 | cout << c << endl; 19 | } 20 | }; 21 | 22 | class base2 23 | { 24 | public: 25 | base2(string &x, string &y) 26 | { 27 | cout << "base2 ctor" << endl; 28 | cout << x << y << endl; 29 | } 30 | }; 31 | 32 | /////////////////////////////////////// 33 | 34 | //class derived:public base 35 | //{ 36 | // complex c; 37 | //public: 38 | // derived(int a, int b):c(a, b),base(c) 39 | // { 40 | // cout << "derived ctor" << endl; 41 | // cout << c << endl; 42 | // } 43 | //}; 44 | 45 | namespace ns1 { 46 | 47 | class derived: 48 | private base_from_member >, 49 | public base 50 | { 51 | //complex c; 52 | typedef base_from_member > pbase_type; 53 | public: 54 | derived(int a, int b):pbase_type(a, b),base(member) 55 | { 56 | cout << "derived ctor" << endl; 57 | cout << member << endl; 58 | } 59 | }; 60 | 61 | } 62 | 63 | void case1() 64 | { 65 | ns1::derived d(10, 20); 66 | } 67 | 68 | /////////////////////////////////////// 69 | 70 | namespace ns2 { 71 | 72 | class derived: 73 | private base_from_member >, 74 | private base_from_member, 75 | private base_from_member, 76 | public base, public base2 77 | { 78 | typedef base_from_member > pbase_type; 79 | typedef base_from_member pbase_type1; 80 | typedef base_from_member pbase_type2; 81 | public: 82 | derived(int a, int b): 83 | pbase_type(a, b), 84 | pbase_type1("str1"),pbase_type2("str2"), 85 | base(pbase_type::member), 86 | base2(pbase_type1::member, pbase_type2::member) 87 | { 88 | cout << "derived ctor2" << endl; 89 | cout << pbase_type::member << endl; 90 | } 91 | }; 92 | 93 | } 94 | 95 | void case2() 96 | { 97 | ns2::derived d(10, 20); 98 | } 99 | 100 | /////////////////////////////////////// 101 | 102 | namespace ns3 { 103 | 104 | class pbase_type 105 | { 106 | protected: 107 | complex cp; 108 | string x,y; 109 | pbase_type(int a, int b, string c, string d): 110 | cp(a,b), x(c), y(d) 111 | {} 112 | }; 113 | 114 | class derived: private pbase_type, public base, public base2 115 | { 116 | public: 117 | derived(int a, int b): 118 | pbase_type(a, b, "str1", "str2"), 119 | base(cp), base2(x, y) 120 | { 121 | cout << "derived ctor3" << endl; 122 | cout << cp << endl; 123 | } 124 | 125 | }; 126 | 127 | } 128 | 129 | void case3() 130 | { 131 | ns3::derived d(10, 20); 132 | } 133 | 134 | /////////////////////////////////////// 135 | 136 | int main() 137 | { 138 | std::cout << "hello base_from_member" << std::endl; 139 | 140 | case1(); 141 | case2(); 142 | case3(); 143 | } 144 | -------------------------------------------------------------------------------- /utility/checked_delete.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | using namespace boost; 8 | 9 | /////////////////////////////////////// 10 | 11 | class demo_class; 12 | 13 | void do_delete(demo_class* p) 14 | //{ delete p;} 15 | { checked_delete(p);} 16 | 17 | /////////////////////////////////////// 18 | 19 | void case1() 20 | { 21 | auto p1 = new int(10); 22 | checked_delete(p1); 23 | 24 | auto p2 = new int[10]; 25 | checked_array_delete(p2); 26 | 27 | } 28 | 29 | /////////////////////////////////////// 30 | 31 | class demo_class 32 | { 33 | public: 34 | ~demo_class() 35 | { cout << "dtor exec." << endl; } 36 | }; 37 | 38 | void case2() 39 | { 40 | auto p1 = new demo_class; 41 | 42 | //checked_delete(p1); 43 | checked_deleter()(p1); 44 | 45 | auto p2 = new demo_class[10]; 46 | 47 | //checked_array_delete(p2); 48 | checked_array_deleter()(p2); 49 | 50 | cout << "try for_each" << endl; 51 | 52 | vector v; 53 | v.push_back(new demo_class); 54 | v.push_back(new demo_class); 55 | 56 | for_each(v.begin(),v.end(), checked_deleter()); 57 | 58 | } 59 | 60 | /////////////////////////////////////// 61 | struct my_checked_deleter 62 | { 63 | typedef void result_type; 64 | 65 | template 66 | void operator()(T* x) const 67 | { 68 | boost::checked_delete(x); 69 | } 70 | }; 71 | 72 | void case3() 73 | { 74 | cout << "my_checked_deleter" << endl; 75 | 76 | auto p = new int(10); 77 | my_checked_deleter()(p); 78 | 79 | vector v; 80 | 81 | v.push_back(new int(10)); 82 | for_each(v.begin(), v.end(), 83 | my_checked_deleter()); 84 | 85 | } 86 | 87 | /////////////////////////////////////// 88 | 89 | void case4() 90 | { 91 | cout << "incomplete type" << endl; 92 | 93 | auto p = new demo_class(); 94 | //auto p = (demo_class*)(1996); 95 | 96 | do_delete(p); 97 | } 98 | 99 | /////////////////////////////////////// 100 | 101 | #include 102 | 103 | template inline 104 | void my_checked_delete(Pointer p) 105 | { 106 | BOOST_STATIC_ASSERT(is_pointer::value); 107 | 108 | typedef typename remove_pointer::type T; 109 | BOOST_STATIC_ASSERT(is_object::value); 110 | BOOST_STATIC_ASSERT(!is_array::value); 111 | BOOST_STATIC_ASSERT(sizeof(T) >0 ); 112 | 113 | delete p; 114 | } 115 | 116 | void case5() 117 | { 118 | //auto p = new int[2][2]; 119 | auto p = new int; 120 | my_checked_delete(p); 121 | } 122 | 123 | /////////////////////////////////////// 124 | 125 | int main() 126 | { 127 | std::cout << "hello checked_delete" << std::endl; 128 | 129 | case1(); 130 | case2(); 131 | case3(); 132 | case4(); 133 | case5(); 134 | } 135 | -------------------------------------------------------------------------------- /utility/jamfile: -------------------------------------------------------------------------------- 1 | unit-test scope_exit : scope_exit.cpp ; 2 | unit-test compressed_pair : compressed_pair.cpp ; 3 | unit-test checked_delete : checked_delete.cpp ; 4 | unit-test addressof : addressof.cpp ; 5 | #unit-test value_init : value_init.cpp ; 6 | unit-test pointee : pointee.cpp ; 7 | unit-test base_from_member : base_from_member.cpp ; 8 | unit-test conversion : conversion.cpp ; 9 | unit-test numeric : numeric.cpp ; 10 | unit-test predef : predef.cpp ; 11 | -------------------------------------------------------------------------------- /utility/numeric.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace boost; 10 | 11 | /////////////////////////////////////// 12 | 13 | void case1() 14 | { 15 | cout << numeric_limits::min(); //-32768 16 | cout << numeric_limits::max(); //32767 17 | 18 | cout << numeric_limits::min(); //0 19 | cout << numeric_limits::max(); //65535 20 | 21 | cout << numeric_limits::min(); //1.17549e-38 22 | cout << numeric_limits::max(); //3.40282e+38 23 | 24 | cout << endl; 25 | 26 | using namespace boost::numeric; 27 | 28 | cout << bounds::lowest(); //-32768 29 | cout << bounds::highest(); //32767; 30 | cout << bounds::smallest(); //1 31 | 32 | cout << bounds::lowest(); //-3.40282e+38 33 | cout << bounds::highest(); //3.40282e+38 34 | cout << bounds::smallest(); //1.17549e-38 35 | 36 | assert(bounds::lowest()==numeric_limits::min()); 37 | assert(bounds::lowest()==-numeric_limits::max()); 38 | 39 | assert(bounds::lowest()==numeric_limits::lowest()); 40 | 41 | cout << endl; 42 | } 43 | 44 | /////////////////////////////////////// 45 | 46 | void case2() 47 | { 48 | using namespace boost::numeric; 49 | 50 | short s = bounds::highest(); 51 | int i = numeric_cast(s); 52 | assert(i == s); 53 | 54 | try 55 | { 56 | char c = numeric_cast(s); 57 | 58 | ignore_unused(c); 59 | } 60 | catch (std::bad_cast& e) 61 | { 62 | cout << e.what() << endl; 63 | } 64 | 65 | } 66 | 67 | /////////////////////////////////////// 68 | 69 | int main() 70 | { 71 | std::cout << "hello numeric" << std::endl; 72 | 73 | case1(); 74 | case2(); 75 | //case3(); 76 | } 77 | -------------------------------------------------------------------------------- /utility/predef.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | void case1() 11 | { 12 | cout << __cplusplus << endl; 13 | cout << BOOST_LANG_STDCPP << endl; 14 | } 15 | 16 | /////////////////////////////////////// 17 | 18 | void case2() 19 | { 20 | cout << BOOST_OS_LINUX << endl; 21 | cout << BOOST_OS_MACOS << endl; 22 | cout << BOOST_OS_BSD << endl; 23 | cout << BOOST_OS_IOS << endl; 24 | } 25 | 26 | /////////////////////////////////////// 27 | 28 | int main() 29 | { 30 | std::cout << "hello predef" << std::endl; 31 | 32 | case1(); 33 | case2(); 34 | } 35 | -------------------------------------------------------------------------------- /utility/scope_exit.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 2 | // Author: Chrono Law 3 | #include 4 | //using namespace std; 5 | 6 | #include 7 | 8 | /////////////////////////////////////// 9 | 10 | void case1() 11 | { 12 | int *p = new int[100]; 13 | 14 | BOOST_SCOPE_EXIT((p)) 15 | { 16 | std::cout << "scope exit called." << std::endl; 17 | delete[] p; 18 | std::cout << "scope exit end." << std::endl; 19 | }BOOST_SCOPE_EXIT_END 20 | 21 | std::cout << "ok. scope exit." << std::endl; 22 | } 23 | 24 | /////////////////////////////////////// 25 | 26 | #include 27 | using namespace boost; 28 | 29 | void case2() 30 | { 31 | int *p = new int[100]; 32 | 33 | shared_ptr x(nullptr, [&](void*) 34 | { 35 | std::cout << "lambda exit called." << std::endl; 36 | delete[] p; 37 | std::cout << "lambda exit end." << std::endl; 38 | } 39 | ); 40 | 41 | std::cout << "ok. lambda exit." << std::endl; 42 | } 43 | 44 | /////////////////////////////////////// 45 | 46 | int main() 47 | { 48 | std::cout << "hello scope_exit" << std::endl; 49 | 50 | case1(); 51 | case2(); 52 | } 53 | --------------------------------------------------------------------------------