├── Effective_Modern_Cpp ├── Item01-template_deduction │ ├── Item01.md │ ├── Item01_en.md │ ├── Makefile │ └── code.cc ├── Item02-auto_deduction │ ├── Item02.md │ ├── Makefile │ └── code.cc ├── Item03-decltype │ ├── Item03.md │ ├── Makefile │ └── code.cc ├── Item04-check_deduced_type │ ├── Item04.md │ ├── Makefile │ └── code.cc ├── Item05-use_auto │ ├── Item05.md │ ├── Makefile │ └── code.cc ├── Item06-not_use_auto │ ├── Item06.md │ ├── Makefile │ └── code.cc ├── Item07-initializer │ ├── Item07.md │ ├── Makefile │ └── code.cc ├── Item08-nullptr │ ├── Item08.md │ ├── Makefile │ └── code.cc ├── Item09-using_vs_typedef │ ├── Item09.md │ ├── Makefile │ └── code.cc ├── Item10-scoped_enum │ ├── Item10.md │ ├── Makefile │ └── code.cc ├── Item11-deleted │ ├── Item11.md │ ├── Makefile │ └── code.cc ├── Item12-override │ ├── Item12.md │ ├── Makefile │ └── code.cc ├── Item13-const_iterator │ ├── Item13.md │ ├── Makefile │ └── find_and_insert.cc ├── Item14-noexcept │ ├── Item14.md │ ├── Makefile │ └── code.cc ├── Item15-constexpr │ ├── Item15.md │ ├── Makefile │ └── code.cc ├── Item16-make_const_thread_safe │ ├── Item16.md │ ├── Makefile │ └── code.cc ├── Item17-autogen_member_functions │ ├── Item17.md │ ├── Makefile │ ├── code.cc │ └── default.cc ├── Item18-unique_ptr │ ├── Item18.md │ ├── Makefile │ ├── code.cc │ └── printargs.cc ├── Item19-shared_ptr │ ├── Item19.md │ ├── Makefile │ ├── code.cc │ ├── code.cc.origin │ └── shared_ptr.png ├── Item20-weak_ptr │ ├── Item20.md │ ├── Makefile │ └── code.cc ├── Item21-make_shared_make_uniqure │ ├── Item21.md │ ├── Makefile │ └── code.cc ├── Item22-Pimp_Idiom │ ├── Item22.md │ ├── Makefile │ ├── c++98 │ │ ├── Makefile │ │ ├── code.cc │ │ ├── gadget.h │ │ ├── widget.cc │ │ └── widget.h │ ├── code.cc │ ├── gadget.h │ ├── widget.cc │ └── widget.h ├── Item23-move_forward │ ├── Item23.md │ ├── Makefile │ └── code.cc ├── Item24-universal_reference │ ├── Item24.md │ ├── Makefile │ └── code.cc ├── Item25-move@rvalue_forward@universal │ ├── Item25.md │ ├── Makefile │ └── code.cc ├── Item26-avoid_overload@universal │ ├── Item26.md │ ├── Makefile │ └── code.cc ├── Item27-alternatives_to_overload@universal │ ├── Item27.md │ ├── Makefile │ └── code.cc ├── Item28-reference_collapsing │ ├── Item28.md │ ├── Makefile │ └── code.cc ├── Item29-move-failure │ ├── Item29.md │ ├── Makefile │ ├── arr_move.png │ ├── code.cc │ └── vec_move.png ├── Item30-imperfect_forward │ ├── Item30.md │ ├── Makefile │ └── code.cc ├── Item31-avoid_default_capture │ ├── Item31.md │ ├── Makefile │ └── code.cc ├── Item32-use_init_capture │ ├── Item32.md │ ├── Makefile │ └── code.cc ├── Item33-universal_auto_in_lambda │ ├── Item33.md │ ├── Makefile │ └── code.cc ├── Item34-prefer_lambda_to_bind │ ├── Item34.md │ ├── Makefile │ └── code.cc ├── Item35-prefer_task_to_thread │ ├── Item35.md │ ├── Makefile │ └── code.cc ├── Item36-async_if_asynchronicity@launch@std │ ├── Item36.md │ ├── Makefile │ └── code.cc ├── Item37-unjoinable │ ├── Item37.md │ ├── Makefile │ └── code.cc ├── Item38-varying_thread_destructor │ ├── Item38.md │ ├── Makefile │ └── code.cc ├── Item39-void_futures │ ├── ConditionVariable.cc │ ├── Item39.md │ ├── Makefile │ └── code.cc ├── Item40-atomic_volatile@std │ ├── Item40.md │ ├── Makefile │ ├── code │ └── code.cc ├── Item41-always_copy │ ├── Item41.md │ ├── Makefile │ └── code.cc ├── Item42-emplace_back │ ├── Item42.md │ ├── Makefile │ └── code.cc └── README.md ├── Fluent_Python ├── ch01-data-model │ ├── cards.py │ └── vec.py ├── ch02-array-seq │ ├── 2.3tuple.py │ ├── cartesian.py │ ├── genexps.py │ ├── listcomps.py │ └── my_bisect.py ├── ch03-dict-set │ ├── StrKeyDict.py │ ├── dialcodes.py │ └── index.py ├── ch04-text-byte │ ├── README.rst │ ├── default_encodings.py │ ├── normeq.py │ ├── numerics_demo.py │ ├── ramanujan.py │ └── sanitize.py ├── ch05-1class-func │ ├── bingocall.py │ ├── clip.py │ ├── factorial.py │ ├── functor.py │ └── tagger.py ├── ch06-dp-1class-func │ ├── README.rst │ ├── classic_strategy.py │ ├── strategy_best │ │ ├── promotions.py │ │ └── strategy_best.py │ └── strategy_func │ │ ├── c++_version │ │ ├── Makefile │ │ └── code.cc │ │ └── strategy_func.py ├── ch07-deco-closure │ ├── README.rst │ ├── average │ │ ├── average.py │ │ └── average_oo.py │ ├── clockdeco │ │ ├── clock_param │ │ │ ├── __pycache__ │ │ │ │ └── clockdeco_param.cpython-36.pyc │ │ │ ├── clockdeco_param.py │ │ │ ├── clockdeco_param_demo1.py │ │ │ └── clockdeco_param_demo2.py │ │ ├── clockdeco │ │ │ ├── clockdeco.py │ │ │ ├── clockdeco_demo.py │ │ │ ├── fibo_demo.py │ │ │ └── fibo_demo_lru.py │ │ └── clockdeco_cls.py │ ├── fibo_demo.py │ ├── fibo_demo_lru.py │ ├── generic.py │ ├── global_x_local.rst │ ├── registration.py │ ├── registration_abridged.py │ ├── registration_param.py │ └── strategy_best.py ├── ch09-pythonic-obj │ ├── mem_test.py │ └── vector2d.py ├── ch10-seq-hacking │ ├── README.rst │ └── vector.py ├── ch11-iface-abc │ ├── frenchdeck_class │ │ └── frenchdeck2.py │ ├── tombola_class │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-36.pyc │ │ │ ├── bingo.cpython-36.pyc │ │ │ ├── drum.cpython-36.pyc │ │ │ ├── lotto.cpython-36.pyc │ │ │ ├── tombola.cpython-36.pyc │ │ │ └── tombolist.cpython-36.pyc │ │ ├── bingo.py │ │ ├── drum.py │ │ ├── lotto.py │ │ ├── tombola.py │ │ ├── tombola_subhook.py │ │ └── tombolist.py │ └── tombola_runner.py ├── ch12-inheritance │ └── diamond.py ├── ch13-op-overloading │ ├── bingo_addable │ │ ├── bingo.py │ │ ├── bingoaddable.py │ │ └── tombola.py │ ├── unary_plus_decimal.py │ └── vector.py ├── ch14-it-generator │ ├── aritprog │ │ ├── aritprog_float_error.py │ │ ├── aritprog_runner.py │ │ └── class │ │ │ ├── __pycache__ │ │ │ ├── aritprog_v0.cpython-36.pyc │ │ │ ├── aritprog_v1.cpython-36.pyc │ │ │ ├── aritprog_v2.cpython-36.pyc │ │ │ └── aritprog_v3.cpython-36.pyc │ │ │ ├── aritprog_v0.py │ │ │ ├── aritprog_v1.py │ │ │ ├── aritprog_v2.py │ │ │ └── aritprog_v3.py │ ├── isis2json │ │ ├── README.rst │ │ ├── cds.iso │ │ ├── cds.mst │ │ ├── cds1.json │ │ ├── cds2.json │ │ ├── isis2json.py │ │ ├── iso2709.py │ │ └── subfield.py │ ├── sentence │ │ ├── class │ │ │ ├── __pycache__ │ │ │ │ ├── sentence_gen.cpython-36.pyc │ │ │ │ ├── sentence_gen2.cpython-36.pyc │ │ │ │ ├── sentence_genexp.cpython-36.pyc │ │ │ │ ├── sentence_getitem.cpython-36.pyc │ │ │ │ ├── sentence_iter.cpython-36.pyc │ │ │ │ └── sentence_iter2.cpython-36.pyc │ │ │ ├── sentence_gen.py │ │ │ ├── sentence_gen2.py │ │ │ ├── sentence_genexp.py │ │ │ ├── sentence_getitem.py │ │ │ ├── sentence_iter.py │ │ │ └── sentence_iter2.py │ │ └── sentence_runner.py │ └── soapbox │ │ ├── fibo_by_hand.py │ │ └── yield_delegate │ │ ├── yield_delegate_fail.py │ │ └── yield_delegate_fix.py ├── ch15-context-mngr │ ├── contextlib │ │ └── mirror_gen_exc.py │ └── mirror.py ├── ch16-coroutine │ ├── c_impl │ │ ├── Makefile │ │ ├── coro.cc │ │ ├── coro.h │ │ └── main.cc │ ├── coro_except │ │ ├── coro_exc_demo.py │ │ └── coro_finally_demo.py │ ├── coroaverager │ │ ├── coroaverager1-prime │ │ │ ├── coroaverager1.py │ │ │ └── coroutil.py │ │ └── coroaverager3.py │ ├── simple.py │ ├── taxi │ │ ├── taxi_sim.py │ │ ├── taxi_sim0.py │ │ └── taxi_sim_delay.py │ └── yield_from_expansion │ │ ├── yield_from_expansion.py │ │ └── yield_from_expansion_simplified.py ├── ch17-futures │ ├── demo_executor_map.py │ ├── flags │ │ ├── __pycache__ │ │ │ └── flags.cpython-36.pyc │ │ ├── downloads │ │ │ ├── bd.gif │ │ │ ├── br.gif │ │ │ ├── cd.gif │ │ │ ├── cn.gif │ │ │ ├── de.gif │ │ │ ├── eg.gif │ │ │ ├── et.gif │ │ │ ├── fr.gif │ │ │ ├── id.gif │ │ │ ├── in.gif │ │ │ ├── ir.gif │ │ │ ├── jp.gif │ │ │ ├── mx.gif │ │ │ ├── ng.gif │ │ │ ├── ph.gif │ │ │ ├── pk.gif │ │ │ ├── ru.gif │ │ │ ├── tr.gif │ │ │ ├── us.gif │ │ │ └── vn.gif │ │ ├── flags.py │ │ ├── flags_threadpool.py │ │ └── flags_threadpool_ac.py │ ├── flags2 │ │ ├── __pycache__ │ │ │ ├── flags2_common.cpython-36.pyc │ │ │ └── flags2_sequential.cpython-36.pyc │ │ ├── downloads │ │ │ ├── bd.gif │ │ │ ├── br.gif │ │ │ ├── cd.gif │ │ │ ├── cn.gif │ │ │ ├── de.gif │ │ │ ├── eg.gif │ │ │ ├── et.gif │ │ │ ├── fr.gif │ │ │ ├── id.gif │ │ │ ├── in.gif │ │ │ ├── ir.gif │ │ │ ├── jp.gif │ │ │ ├── mx.gif │ │ │ ├── ng.gif │ │ │ ├── ph.gif │ │ │ ├── pk.gif │ │ │ ├── ru.gif │ │ │ ├── tr.gif │ │ │ ├── us.gif │ │ │ └── vn.gif │ │ ├── flags2_asyncio_executor.py │ │ ├── flags2_common.py │ │ ├── flags2_sequential.py │ │ └── flags2_threadpool.py │ └── flags3 │ │ └── flags3_threadpool.py ├── ch18-asyncio │ ├── charfinder │ │ ├── charfinder.py │ │ ├── http_charfinder.html │ │ ├── http_charfinder.py │ │ ├── requirements.txt │ │ ├── tcp_charfinder.py │ │ └── test_charfinder.py │ ├── flags │ │ ├── downloads │ │ │ ├── bd.gif │ │ │ ├── br.gif │ │ │ ├── cd.gif │ │ │ ├── cn.gif │ │ │ ├── de.gif │ │ │ ├── eg.gif │ │ │ ├── et.gif │ │ │ ├── fr.gif │ │ │ ├── id.gif │ │ │ ├── in.gif │ │ │ ├── ir.gif │ │ │ ├── jp.gif │ │ │ ├── mx.gif │ │ │ ├── ng.gif │ │ │ ├── ph.gif │ │ │ ├── pk.gif │ │ │ ├── ru.gif │ │ │ ├── tr.gif │ │ │ ├── us.gif │ │ │ └── vn.gif │ │ ├── flags2_asyncio.py │ │ ├── flags2_asyncio_executor.py │ │ ├── flags_asyncio.py │ │ └── imported │ │ │ ├── flags.py │ │ │ └── flags2_common.py │ └── spinner │ │ ├── spinner_asyncio.py │ │ ├── spinner_curio.py │ │ └── spinner_thread.py ├── ch18b-async-await │ ├── README.rst │ ├── charfinder │ │ ├── __pycache__ │ │ │ └── charfinder.cpython-36.pyc │ │ ├── charfinder.py │ │ ├── http_charfinder.html │ │ ├── http_charfinder.py │ │ ├── tcp_charfinder.py │ │ └── test_charfinder.py │ ├── flags │ │ ├── flags2_await.py │ │ ├── flags_await.py │ │ └── imported │ │ │ ├── __pycache__ │ │ │ ├── flags.cpython-36.pyc │ │ │ └── flags2_common.cpython-36.pyc │ │ │ ├── flags.py │ │ │ └── flags2_common.py │ └── spinner │ │ ├── spinner_asyncio.py │ │ ├── spinner_await.py │ │ ├── spinner_curio.py │ │ └── spinner_thread.py ├── ch19-dyn-attr-prop │ ├── blackknight.py │ ├── bulkfood │ │ ├── bulkfood_v1.py │ │ ├── bulkfood_v2.py │ │ ├── bulkfood_v2b.py │ │ └── bulkfood_v2prop.py │ ├── doc_property.py │ ├── oscon │ │ ├── __pycache__ │ │ │ └── osconfeed.cpython-36.pyc │ │ ├── data │ │ │ └── osconfeed.json │ │ ├── demo_schedule2.py │ │ ├── explore2.py │ │ ├── osconfeed.py │ │ ├── schedule2.py │ │ ├── test_schedule1.py │ │ └── test_schedule2.py │ └── pseudo_construction.py ├── ch20-descriptor │ ├── bulkfood │ │ ├── bulkfood_v3.py │ │ ├── bulkfood_v4.py │ │ ├── bulkfood_v4b.py │ │ ├── bulkfood_v4c.py │ │ ├── bulkfood_v4prop.py │ │ ├── bulkfood_v5.py │ │ ├── bulkfood_v5_check.py │ │ ├── model_v4c.py │ │ ├── model_v5.py │ │ └── model_v5_check.py │ ├── descriptorkinds.py │ ├── descriptorkinds_dump.py │ └── method_is_descriptor.py └── ch21-class-metaprog │ ├── README.rst │ ├── bulkfood │ ├── bulkfood_v6.py │ ├── bulkfood_v7.py │ ├── bulkfood_v8.py │ ├── model_v6.py │ ├── model_v7.py │ └── model_v8.py │ ├── evalsupport.py │ ├── evaltime.py │ ├── evaltime_meta.py │ └── factories.py ├── In-depth_C++11 ├── ch01-moderner_and_simpler │ ├── .cc │ ├── 1-01_auto.cc │ ├── 1-02_auto.cc │ ├── 1-03_decltype.cc │ ├── 1-04_decltype.cc │ ├── 1-05_template.cc │ ├── 1-06_》.cc │ ├── 1-07_init.cc │ ├── 1-08_init.cc │ ├── 1-09_init.cc │ ├── 1-10_init_narrow.cc │ ├── 1-11_1-15_for_range │ │ ├── Range.hpp │ │ └── Test.cc │ ├── 1-16_functor.cc │ ├── 1-17_std_function.cc │ ├── 1-19.cc │ ├── 1-20.cc │ ├── 1-21.cc │ ├── 1-22.cc │ ├── 1-23.cc │ ├── 1-24.cc │ └── Makefile ├── ch02-rvalue │ ├── 2-1.cc │ ├── 2-2.cc │ ├── 2-3.cc │ ├── 2-4.cc │ ├── 2-5.cc │ ├── 2-6.cc │ └── Makefile ├── ch03-type_traits_and_vargs │ ├── 3-1.cc │ ├── 3-10 │ │ ├── 3-10.sln │ │ ├── 3-10.v12.suo │ │ └── 3-10 │ │ │ ├── 3-10.vcxproj │ │ │ ├── 3-10.vcxproj.filters │ │ │ ├── DllParser.hpp │ │ │ ├── MyDLL.dll │ │ │ └── Test.cpp │ ├── 3-12 │ │ ├── 3-12.sln │ │ ├── 3-12.v12.suo │ │ └── 3-12 │ │ │ ├── 3-12.vcxproj │ │ │ ├── 3-12.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-13 │ │ ├── 3-13.sln │ │ ├── 3-13.v12.suo │ │ └── 3-13 │ │ │ ├── 3-13.vcxproj │ │ │ ├── 3-13.vcxproj.filters │ │ │ ├── Any.hpp │ │ │ └── 源.cpp │ ├── 3-14 │ │ ├── 3-14.sln │ │ ├── 3-14.v12.suo │ │ └── 3-14 │ │ │ ├── 3-14.vcxproj │ │ │ ├── 3-14.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-15 │ │ ├── 3-15.sln │ │ ├── 3-15.v12.suo │ │ └── 3-15 │ │ │ ├── 3-15.vcxproj │ │ │ ├── 3-15.vcxproj.filters │ │ │ ├── Test.cpp │ │ │ └── Variant.hpp │ ├── 3-17 │ │ ├── 3-17.sln │ │ ├── 3-17.v12.suo │ │ └── 3-17 │ │ │ ├── 3-17.vcxproj │ │ │ ├── 3-17.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-18 │ │ ├── 3-18.sln │ │ ├── 3-18.v12.suo │ │ └── 3-18 │ │ │ ├── 3-18.vcxproj │ │ │ ├── 3-18.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-19 │ │ ├── 3-19.sln │ │ ├── 3-19.v11.suo │ │ ├── 3-19.v12.suo │ │ └── 3-19 │ │ │ ├── 3-19.vcxproj │ │ │ ├── 3-19.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-2.cc │ ├── 3-20 │ │ ├── 3-20.sln │ │ ├── 3-20.v12.suo │ │ └── 3-20 │ │ │ ├── 3-20.vcxproj │ │ │ ├── 3-20.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-21 │ │ ├── 3-21.sln │ │ ├── 3-21.v12.suo │ │ └── 3-21 │ │ │ ├── 3-21.vcxproj │ │ │ ├── 3-21.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-22 │ │ ├── 3-22.sln │ │ ├── 3-22.v12.suo │ │ └── 3-22 │ │ │ ├── 3-22.vcxproj │ │ │ ├── 3-22.vcxproj.filters │ │ │ ├── Test.cpp │ │ │ ├── TpForeach.hpp │ │ │ └── TpIndexs.hpp │ ├── 3-23 │ │ ├── 3-23.sln │ │ ├── 3-23.v12.suo │ │ └── 3-23 │ │ │ ├── 3-23.vcxproj │ │ │ ├── 3-23.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-24 │ │ ├── 3-24.sln │ │ ├── 3-24.v12.suo │ │ └── 3-24 │ │ │ ├── 3-24.vcxproj │ │ │ ├── 3-24.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-25 │ │ ├── 3-25.sln │ │ ├── 3-25.v12.suo │ │ └── 3-25 │ │ │ ├── 3-25.vcxproj │ │ │ ├── 3-25.vcxproj.filters │ │ │ ├── Test.cpp │ │ │ └── TpZip.hpp │ ├── 3-3 │ │ ├── 3-3.sln │ │ ├── 3-3.v12.suo │ │ └── 3-3 │ │ │ ├── 3-3.vcxproj │ │ │ ├── 3-3.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-4 │ │ ├── 3-4.sln │ │ ├── 3-4.v12.suo │ │ └── 3-4 │ │ │ ├── 3-4.vcxproj │ │ │ ├── 3-4.vcxproj.filters │ │ │ └── Test.cpp │ ├── 3-5 │ │ ├── 3-5.sln │ │ ├── 3-5.v12.suo │ │ └── 3-5 │ │ │ ├── 3-5.vcxproj │ │ │ ├── 3-5.vcxproj.filters │ │ │ ├── Test.cpp │ │ │ └── optional.hpp │ ├── 3-7~3-8 │ │ ├── 3-7~3-8.sln │ │ ├── 3-7~3-8.v12.suo │ │ └── 3-7~3-8 │ │ │ ├── 3-7~3-8.vcxproj │ │ │ ├── 3-7~3-8.vcxproj.filters │ │ │ ├── Lazy.hpp │ │ │ └── Test.cpp │ ├── 3-9 │ │ ├── 3-9.sln │ │ ├── 3-9.v12.suo │ │ └── 3-9 │ │ │ ├── 3-9.vcxproj │ │ │ ├── 3-9.vcxproj.filters │ │ │ ├── MyDLL.dll │ │ │ ├── MyDLL.h │ │ │ ├── MyDLL.lib │ │ │ └── Test.cpp │ ├── 3.16 │ │ ├── ScopeGuard.h │ │ └── Test.cc │ ├── Makefile │ └── enable_if.cc ├── ch04-smart-pointer │ ├── 4-1 │ ├── 4-1.cc │ └── Makefile ├── ch05-thread │ ├── 5-1 │ │ ├── 5-1.sln │ │ ├── 5-1.v12.suo │ │ └── 5-1 │ │ │ ├── 5-1.vcxproj │ │ │ ├── 5-1.vcxproj.filters │ │ │ └── Test.cpp │ ├── 5-10 │ │ ├── 5-10.sln │ │ ├── 5-10.v12.suo │ │ └── 5-10 │ │ │ ├── 5-10.vcxproj │ │ │ ├── 5-10.vcxproj.filters │ │ │ └── Test.cpp │ ├── 5-2 │ │ ├── 5-2.sln │ │ ├── 5-2.v12.suo │ │ └── 5-2 │ │ │ ├── 5-2.vcxproj │ │ │ ├── 5-2.vcxproj.filters │ │ │ └── Test.cpp │ ├── 5-3 │ │ ├── 5-3.sln │ │ ├── 5-3.v12.suo │ │ └── 5-3 │ │ │ ├── 5-3.vcxproj │ │ │ ├── 5-3.vcxproj.filters │ │ │ └── Test.cpp │ ├── 5-4 │ │ ├── 5-4.sln │ │ ├── 5-4.v12.suo │ │ └── 5-4 │ │ │ ├── 5-4.vcxproj │ │ │ ├── 5-4.vcxproj.filters │ │ │ └── Test.cpp │ ├── 5-5 │ │ ├── 5-5.sln │ │ ├── 5-5.v12.suo │ │ └── 5-5 │ │ │ ├── 5-5.vcxproj │ │ │ ├── 5-5.vcxproj.filters │ │ │ ├── SyncQueue.hpp │ │ │ └── Test.cpp │ ├── 5-6 │ │ ├── 5-6.sln │ │ ├── 5-6.v12.suo │ │ └── 5-6 │ │ │ ├── 5-6.vcxproj │ │ │ ├── 5-6.vcxproj.filters │ │ │ ├── SimpleSyncQueue.hpp │ │ │ └── Test.cpp │ ├── 5-7 │ │ ├── 5-7.sln │ │ ├── 5-7.v12.suo │ │ └── 5-7 │ │ │ ├── 5-7.vcxproj │ │ │ ├── 5-7.vcxproj.filters │ │ │ └── Test.cpp │ ├── 5-8 │ │ ├── 5-8.sln │ │ ├── 5-8.v12.suo │ │ └── 5-8 │ │ │ ├── 5-8.vcxproj │ │ │ ├── 5-8.vcxproj.filters │ │ │ └── Test.cpp │ └── 5-9 │ │ ├── 5-9.sln │ │ ├── 5-9.v12.suo │ │ └── 5-9 │ │ ├── 5-9.vcxproj │ │ ├── 5-9.vcxproj.filters │ │ └── Test.cpp ├── ch06-chrono_and_convert │ ├── 6-1 │ ├── 6-2 │ ├── 6-2.cc │ ├── 6-3.cc │ ├── 6.1 │ │ ├── Test.cc │ │ ├── Timer.h │ │ └── Timer.hpp │ └── Makefile └── ch08-Pattern │ ├── 8-3 │ ├── 8-3.cc │ ├── 8-5 │ ├── 8-5.cc │ ├── Command.hpp │ ├── Makefile │ ├── NonCopyable.hpp │ ├── ObjectPool.hpp │ ├── Observer.hpp │ ├── Singleton.h │ └── Visitor.hpp ├── Muduo_ChenShuo ├── Makefile ├── ch01.cc ├── ch02.cc ├── ch05.cc ├── ch06_sudoku │ ├── CMakeLists.txt │ ├── README │ ├── batch.cc │ ├── build.sh │ ├── diff_basic_multiloop.diff │ ├── diff_basic_threadpool.diff │ ├── diff_hybrid_prod.diff │ ├── loadtest.cc │ ├── percentile.h │ ├── pipeline.cc │ ├── req.txt │ ├── server_basic.cc │ ├── server_hybrid.cc │ ├── server_multiloop.cc │ ├── server_prod.cc │ ├── server_threadpool.cc │ ├── stat.h │ ├── stat_unittest.cc │ ├── sudoku.cc │ └── sudoku.h └── ch08_reactor │ ├── Makefile │ ├── README │ ├── mkdiff.sh │ ├── s00_EventLoop │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── Makefile │ ├── test1.cc │ └── test2.cc │ ├── s01_timerfd │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ └── test3.cc │ ├── s02_TimerQueue │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ └── test4.cc │ ├── s03_runInLoop │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ ├── test5.cc │ └── test6.cc │ ├── s04_Acceptor │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ └── test7.cc │ ├── s05_TcpConnection │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ └── test8.cc │ ├── s06_TcpConnection │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ └── test8.cc │ ├── s07_Buffer │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Buffer.cc │ ├── Buffer.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ ├── test3.cc │ └── test8.cc │ ├── s08_send │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Buffer.cc │ ├── Buffer.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ ├── test10.cc │ └── test9.cc │ ├── s09_SIGPIPE │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Buffer.cc │ ├── Buffer.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ ├── test10.cc │ ├── test11.cc │ └── test9.cc │ ├── s10_multithread │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Buffer.cc │ ├── Buffer.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── EventLoopThreadPool.cc │ ├── EventLoopThreadPool.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ ├── test10.cc │ ├── test11.cc │ ├── test8.cc │ └── test9.cc │ ├── s11_Connector │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Buffer.cc │ ├── Buffer.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── Connector.cc │ ├── Connector.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── EventLoopThreadPool.cc │ ├── EventLoopThreadPool.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ ├── test12.cc │ └── test4.cc │ ├── s12_TcpClient │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Buffer.cc │ ├── Buffer.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── Connector.cc │ ├── Connector.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── EventLoopThreadPool.cc │ ├── EventLoopThreadPool.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpClient.cc │ ├── TcpClient.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ └── test13.cc │ └── s13_epoll │ ├── Acceptor.cc │ ├── Acceptor.h │ ├── Buffer.cc │ ├── Buffer.h │ ├── Callbacks.h │ ├── Channel.cc │ ├── Channel.h │ ├── Connector.cc │ ├── Connector.h │ ├── EPoller.cc │ ├── EPoller.h │ ├── EventLoop.cc │ ├── EventLoop.h │ ├── EventLoopThread.cc │ ├── EventLoopThread.h │ ├── EventLoopThreadPool.cc │ ├── EventLoopThreadPool.h │ ├── InetAddress.cc │ ├── InetAddress.h │ ├── Makefile │ ├── Poller.cc │ ├── Poller.h │ ├── Socket.cc │ ├── Socket.h │ ├── SocketsOps.cc │ ├── SocketsOps.h │ ├── TcpClient.cc │ ├── TcpClient.h │ ├── TcpConnection.cc │ ├── TcpConnection.h │ ├── TcpServer.cc │ ├── TcpServer.h │ ├── Timer.cc │ ├── Timer.h │ ├── TimerId.h │ ├── TimerQueue.cc │ ├── TimerQueue.h │ ├── test1.cc │ ├── test10.cc │ ├── test11.cc │ ├── test12.cc │ ├── test13.cc │ ├── test2.cc │ ├── test3.cc │ ├── test4.cc │ ├── test5.cc │ ├── test6.cc │ ├── test7.cc │ ├── test8.cc │ └── test9.cc ├── README.md └── Using_STL ├── .vscode ├── launch.json ├── settings.json └── tasks.json ├── Checkout ├── Checkout_object │ ├── Checkout.h │ ├── Customer.h │ └── Ex3_02.cpp ├── Checkout_up │ ├── Checkout.h │ ├── Customer.h │ ├── Makefile │ └── code.cc ├── checkout.h ├── customer.h └── main.cc ├── Makefile ├── ch01.cc ├── ch02.cc ├── ch06.cc ├── ch07.cc ├── ch09.cc ├── ch09_cin.cc ├── ch10.cc ├── ch10_gaussion.cc ├── cin_recovery.cc └── data ├── Renewables_vs_kwh_cost.txt ├── dictionary.txt ├── dictionary_copy.txt ├── fibonacci.txt └── temperatures.txt /Effective_Modern_Cpp/Item01-template_deduction/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | code: CXXFLAGS += -Wall 17 | code: code.cc 18 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item02-auto_deduction/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item03-decltype/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item04-check_deduced_type/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item04-check_deduced_type/code.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | template 6 | void f(const T& param) 7 | { 8 | using boost::typeindex::type_id_with_cvr; 9 | 10 | // show T 11 | std::cout << "T = " 12 | << type_id_with_cvr().pretty_name() 13 | << '\n'; 14 | // show param's type 15 | std::cout << "param = " 16 | << type_id_with_cvr().pretty_name() 17 | << '\n'; 18 | //… 19 | } 20 | 21 | struct Widget{}; 22 | std::vector vec; 23 | std::vector createVec() 24 | { 25 | const Widget w; 26 | vec.push_back(w); 27 | 28 | return vec; 29 | } 30 | 31 | int main(void) 32 | { 33 | const auto vw = createVec(); 34 | 35 | if(!vw.empty()){ 36 | f(&vw[0]); 37 | //... 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item05-use_auto/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item06-not_use_auto/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item07-initializer/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: code.cc 18 | 19 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item08-nullptr/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | #code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item09-using_vs_typedef/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | #code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item10-scoped_enum/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | #code: CXXFLAGS += -Wall 17 | code: code.cc 18 | 19 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item11-deleted/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | #code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item12-override/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | code: CXXFLAGS += -Wall 17 | code: code.cc 18 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item13-const_iterator/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = find_and_insert 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | #find_and_insert: CXXFLAGS += -Wall 17 | find_and_insert: find_and_insert.cc 18 | 19 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item14-noexcept/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item15-constexpr/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | #code: CXXFLAGS += -Wall 17 | code: code.cc 18 | 19 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item16-make_const_thread_safe/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item17-autogen_member_functions/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code default 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | default: default.cc 21 | 22 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item18-unique_ptr/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code printargs 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | printargs: printargs.cc 21 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item19-shared_ptr/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item19-shared_ptr/shared_ptr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Effective_Modern_Cpp/Item19-shared_ptr/shared_ptr.png -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item20-weak_ptr/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item21-make_shared_make_uniqure/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc widget.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/c++98/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc widget.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/c++98/code.cc: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | 3 | int main(void) 4 | { 5 | Widget w; 6 | 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/c++98/gadget.h: -------------------------------------------------------------------------------- 1 | //自定义类型的定义 2 | typedef int Gadget; 3 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/c++98/widget.cc: -------------------------------------------------------------------------------- 1 | #include "widget.h" //在实现文件"widget.cpp"中 2 | #include "gadget.h" 3 | #include 4 | #include 5 | 6 | struct Widget::Impl{ // 带有之前在Widget中的数据成员的 7 | std::string name; // Widget::Impl的定义 8 | std::vector data; 9 | Gadget g1, g2, g3; 10 | }; 11 | 12 | Widget::Widget() // 分配Widget对象的数据成员 13 | : pImpl(new Impl) 14 | {} 15 | 16 | Widget::~Widget() // 归还这个对象的数据成员 17 | { delete pImpl; } 18 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/c++98/widget.h: -------------------------------------------------------------------------------- 1 | class Widget{ // 还是在头文件"widget.h"中 2 | public: 3 | Widget(); 4 | ~Widget(); // 看下面的内容可以得知析构函数是需要的 5 | //... 6 | 7 | private: 8 | struct Impl; // 声明一个要实现的结构 9 | Impl *pImpl; // 并用指针指向它 10 | }; 11 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/code.cc: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | 3 | int main(void) 4 | { 5 | 6 | Widget w; //如果析构函数~Widget()不能实现申明和定义分离,则报错 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/gadget.h: -------------------------------------------------------------------------------- 1 | //自定义类型的定义 2 | typedef int Gadget; 3 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/widget.cc: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | #include "gadget.h" 3 | #include 4 | #include 5 | 6 | struct Widget::Impl { // 和以前一样 7 | std::string name; 8 | std::vector data; 9 | Gadget g1, g2, g3; 10 | }; 11 | 12 | //Widget::~Widget(){} //显式地定义析构函数, 申明和定义分离 13 | Widget::~Widget() = default; //效果同上 14 | 15 | Widget::Widget() //通过std::make_unique来创建一个std::unique_ptr 16 | : pImpl(std::make_unique()) 17 | {} 18 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item22-Pimp_Idiom/widget.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //unique_ptr.h:76:22: error: invalid application of ‘sizeof’ to incomplete type ‘Widget::Impl’ 4 | //error产生的根源: “编译器产生的”析构函数,在析构之前,需要销毁被pImpl指向的对象,但是在Widget的头文件中,pImpl指向一个不完整类型。 5 | //error产生的根源: “编译器产生的” operator move 在重新赋值前,需要销毁被pImpl指向的对象,但是在Widget的头文件中,pImpl指向一个不完整类型。 6 | class Widget{ 7 | public: 8 | Widget(); 9 | //... 10 | ~Widget(); // 为了消除siezof error,避免内联, 声明和实现析构函数必须分离, 则可以防止static_assert出错 11 | //~Widget(){} //声明+定义~Widget()依然报sizeof error错. 12 | 13 | private: 14 | struct Impl; //前置申明 15 | std::unique_ptr pImpl;// 使用智能指针来替换原始指针 16 | }; 17 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item23-move_forward/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item24-universal_reference/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item25-move@rvalue_forward@universal/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item26-avoid_overload@universal/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item27-alternatives_to_overload@universal/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item28-reference_collapsing/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item29-move-failure/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item29-move-failure/arr_move.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Effective_Modern_Cpp/Item29-move-failure/arr_move.png -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item29-move-failure/code.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | 6 | std::vector vw1; 7 | // put data into vw1 8 | 9 | //... 10 | 11 | // move vw1 into vw2. Runs in 12 | // constant time. Only ptrs 13 | // in vw1 and vw2 are modified 14 | auto vw2 = std::move(vw1); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item29-move-failure/vec_move.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Effective_Modern_Cpp/Item29-move-failure/vec_move.png -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item30-imperfect_forward/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item31-avoid_default_capture/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item32-use_init_capture/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item33-universal_auto_in_lambda/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item33-universal_auto_in_lambda/code.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func(int x) 5 | { 6 | std::cout<(param))); 17 | }; 18 | f(2); 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item34-prefer_lambda_to_bind/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item35-prefer_task_to_thread/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item35-prefer_task_to_thread/code.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int doAsyncWork() 6 | { 7 | for(int i=0; i<30000; i++) 8 | for(int i=0; i<90000; i++) 9 | ; 10 | 11 | std::cout << "do AsyncWork..." << std::endl; 12 | return 9; 13 | } 14 | 15 | int main(void) 16 | { 17 | std::thread t(doAsyncWork); 18 | t.join(); 19 | 20 | //std::future holds a shared state 21 | //std::async allow us to run the code asynchronously. 22 | auto fut = std::async(doAsyncWork); // fut for future 23 | // onus of thread mgmt is 24 | // on implements of 25 | // the STL 26 | int ret=fut.get(); 27 | std::cout << "ret = " << ret << std::endl; 28 | 29 | 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item36-async_if_asynchronicity@launch@std/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item37-unjoinable/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item38-varying_thread_destructor/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item39-void_futures/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code ConditionVariable 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | ConditionVariable: ConditionVariable.cc 21 | 22 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item40-atomic_volatile@std/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | 21 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item40-atomic_volatile@std/code: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Effective_Modern_Cpp/Item40-atomic_volatile@std/code -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item41-always_copy/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/Item42-emplace_back/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | 20 | -------------------------------------------------------------------------------- /Effective_Modern_Cpp/README.md: -------------------------------------------------------------------------------- 1 | # Effective_Modern_Cpp 2 | <> Chinese version and source code 3 | 4 | in every dir (corresponding to every chaptor) 5 | Use method: 6 | ``` 7 | $ make 8 | $ cgdb code 9 | ``` 10 | 11 | -------------------------------------------------------------------------------- /Fluent_Python/ch01-data-model/vec.py: -------------------------------------------------------------------------------- 1 | from math import hypot 2 | 3 | class Vector: 4 | def __init__(self, x=0, y=0): 5 | self.x = x; 6 | self.y = y; 7 | def __repr__(self): 8 | #return 'Vector(%r, %r)' %(self.x, self.y) 9 | return f'Vector({self.x}, {self.y})' 10 | def __abs__(self): 11 | return hypot(self.x, self.y) 12 | def __add__(self, other): 13 | x = self.x + other.x 14 | y = self.y + other.y 15 | return Vector(x, y) 16 | def __mul__(self, scalar): 17 | return Vector(self.x*scalar, self.y*scalar) 18 | def __rmul__(self, scalar): 19 | return Vector(self.x*scalar, self.y*scalar) 20 | 21 | v1=Vector(1,2) 22 | v2 = Vector(3,4) 23 | print(v1+v2) 24 | print(v1*3) 25 | print(4*v1) -------------------------------------------------------------------------------- /Fluent_Python/ch02-array-seq/cartesian.py: -------------------------------------------------------------------------------- 1 | colors = ['black', 'white'] 2 | sizes = ['S', 'M', 'L'] 3 | tshirts = [(color, size) for color in colors for size in sizes] 4 | print(tshirts) 5 | 6 | print('-------') 7 | for color in colors: 8 | for size in sizes: 9 | print((color, size)) 10 | 11 | print('-------') 12 | tshirts = [(color,size) for size in sizes for color in colors] 13 | print(tshirts) 14 | -------------------------------------------------------------------------------- /Fluent_Python/ch02-array-seq/genexps.py: -------------------------------------------------------------------------------- 1 | symbols='A中国人' 2 | t=tuple(ord(symbol) for symbol in symbols) 3 | print(t) 4 | 5 | print('---------') 6 | import array 7 | a = array.array('I', (ord(symbol) for symbol in symbols)) 8 | print(a) 9 | 10 | -------------------------------------------------------------------------------- /Fluent_Python/ch03-dict-set/dialcodes.py: -------------------------------------------------------------------------------- 1 | # BEGIN DIALCODES 2 | # dial codes of the top 10 most populous countries 3 | DIAL_CODES = [ 4 | (86, 'China'), 5 | (91, 'India'), 6 | (1, 'United STates'), 7 | (62, 'Indonesia'), 8 | (55, 'Brazil'), 9 | (92, 'Pakistan'), 10 | (880, 'Bangladesh'), 11 | (234, 'Nigeria'), 12 | (7, 'Russia'), 13 | (81, 'Japan'), 14 | ] 15 | d1 = dict(DIAL_CODES) # <1> 16 | print('d1:', d1.keys()) # 打印原始的key列表 17 | 18 | d2 = dict(sorted(DIAL_CODES)) # <2> 19 | print('d2:', d2.keys()) # 打印按照key排序的key列表 20 | 21 | # lambda 参数列表 : 表达式 22 | d3 = dict(sorted(DIAL_CODES, key=lambda x:x[1])) # <3> 23 | print('d3:', d3.keys()) # 打印按照value排序的key列表 24 | 25 | # 用冒号:分割dict的key和value 26 | country_code = { country:code for code,country in DIAL_CODES } 27 | print(country_code) 28 | 29 | upper = { code:country.upper() for country,code in country_code.items() if code < 66 } 30 | print(upper) 31 | -------------------------------------------------------------------------------- /Fluent_Python/ch04-text-byte/README.rst: -------------------------------------------------------------------------------- 1 | Sample code for Chapter 4 - "Text and bytes" 2 | 3 | From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) 4 | http://shop.oreilly.com/product/0636920032519.do 5 | -------------------------------------------------------------------------------- /Fluent_Python/ch04-text-byte/default_encodings.py: -------------------------------------------------------------------------------- 1 | import sys, locale 2 | 3 | expressions = """ 4 | locale.getpreferredencoding() 5 | type(my_file) 6 | my_file.encoding 7 | sys.stdout.isatty() 8 | sys.stdout.encoding 9 | sys.stdin.isatty() 10 | sys.stdin.encoding 11 | sys.stderr.isatty() 12 | sys.stderr.encoding 13 | sys.getdefaultencoding() 14 | sys.getfilesystemencoding() 15 | """ 16 | 17 | my_file = open('dummy', 'w') 18 | 19 | for expression in expressions.split(): 20 | value = eval(expression) 21 | print(expression.rjust(30), '->', repr(value)) 22 | -------------------------------------------------------------------------------- /Fluent_Python/ch04-text-byte/numerics_demo.py: -------------------------------------------------------------------------------- 1 | # BEGIN NUMERICS_DEMO 2 | import unicodedata 3 | import re 4 | 5 | re_digit = re.compile(r'\d') 6 | 7 | sample = '1\xbc\xb2\u0969\u136b\u216b\u2466\u2480\u3285' 8 | sample2= '1\ubcb2\u0969\u136b\u216b\u2466\u2480\u3285' 9 | 10 | def print_str(sample): 11 | for char in sample: 12 | print('U+%04x' % ord(char), # <1> 13 | char.center(6), # <2> 14 | 're_dig' if re_digit.match(char) else '-', # <3> 是正则中的数字吗? 15 | 'isdig' if char.isdigit() else '-', # <4> 是数字吗? 16 | 'isnum' if char.isnumeric() else '-', # <5> 是数值吗? 17 | format(unicodedata.numeric(char), '5.2f'), # <6> 更多字符类型信息 18 | unicodedata.name(char), # <7> 更多字符类型信息 19 | sep='\t') 20 | 21 | print_str(sample) 22 | print_str(sample2) 23 | 24 | # END NUMERICS_DEMO 25 | -------------------------------------------------------------------------------- /Fluent_Python/ch04-text-byte/ramanujan.py: -------------------------------------------------------------------------------- 1 | # BEGIN RE_DEMO 2 | import re 3 | 4 | re_numbers_str = re.compile(r'\d+') # <1> 5 | re_words_str = re.compile(r'\w+') 6 | re_numbers_bytes = re.compile(rb'\d+') # <2> 7 | re_words_bytes = re.compile(rb'\w+') 8 | 9 | text_str = ("Ramanujan saw \u0be7\u0bed\u0be8\u0bef" # <3> 10 | " as 1729 = 1³ + 12³ = 9³ + 10³.") # <4> 11 | 12 | text_bytes = text_str.encode('utf_8') # <5> 13 | 14 | print('Text', repr(text_str), sep='\n ') 15 | print('Numbers') 16 | print(' str :', re_numbers_str.findall(text_str)) # <6> 17 | print(' bytes:', re_numbers_bytes.findall(text_bytes)) # <7> 18 | print('Words') 19 | print(' str :', re_words_str.findall(text_str)) # <8> 20 | print(' bytes:', re_words_bytes.findall(text_bytes)) # <9> 21 | # END RE_DEMO 22 | -------------------------------------------------------------------------------- /Fluent_Python/ch05-1class-func/bingocall.py: -------------------------------------------------------------------------------- 1 | 2 | # BEGIN BINGO 3 | 4 | import random 5 | 6 | class BingoCage: 7 | 8 | def __init__(self, items): 9 | self._items = list(items) # <1> 10 | random.shuffle(self._items) # <2> 11 | 12 | def pick(self): # <3> 13 | try: 14 | return self._items.pop() 15 | except IndexError: 16 | raise LookupError('pick from empty BingoCage') # <4> 17 | 18 | def __call__(self): # <5> 19 | return self.pick() 20 | 21 | bingo = BingoCage(range(3)) 22 | 23 | print(bingo.pick()) # 0 24 | print(bingo()) # 2 25 | print(callable(bingo)) # True 26 | print(callable(bingo())) # False 27 | print(callable(BingoCage)) # True 28 | print(callable(BingoCage( (1, 2, 4) ))) # True 29 | 30 | 31 | 32 | # END BINGO 33 | -------------------------------------------------------------------------------- /Fluent_Python/ch05-1class-func/factorial.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | ''' returns n! ''' 3 | return 1 if n<2 else n*factorial(n-1) 4 | 5 | print(factorial(42)) 6 | print(factorial.__doc__) 7 | print(type(factorial)) 8 | 9 | 10 | fact = factorial 11 | print(fact) 12 | print(fact(5)) 13 | print(map(factorial, range(11))) 14 | 15 | 16 | -------------------------------------------------------------------------------- /Fluent_Python/ch05-1class-func/functor.py: -------------------------------------------------------------------------------- 1 | fruits = [ 'strawberry', 'fig', 'apple', 'cherry', 'raspberry', 'banana'] 2 | print(sorted(fruits, key=len)) 3 | 4 | def reverse(word): 5 | return word[::-1] 6 | 7 | print(reverse('testing')) 8 | 9 | print(sorted(fruits, key=reverse)) 10 | 11 | 12 | class C: pass 13 | obj = C() 14 | def func(): pass 15 | fun_prop = sorted(set(dir(func)) - set(dir(obj))) 16 | print(fun_prop) 17 | -------------------------------------------------------------------------------- /Fluent_Python/ch06-dp-1class-func/README.rst: -------------------------------------------------------------------------------- 1 | Sample code for Chapter 6 - "Design patterns with first class functions" 2 | 3 | From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) 4 | http://shop.oreilly.com/product/0636920032519.do 5 | 6 | 7 | classic_strategy.py 传统的策略默认, 继承抽象类(虚函数),重写虚函数的做法 8 | strategy_func.py 函数式编程 9 | strategy_best.py 在函数式编程的基础上,进一步优选“促销策略” 10 | -------------------------------------------------------------------------------- /Fluent_Python/ch06-dp-1class-func/strategy_best/promotions.py: -------------------------------------------------------------------------------- 1 | 2 | def fidelity_promo(order): 3 | """5% discount for customers with 1000 or more fidelity points""" 4 | return order.total() * .05 if order.customer.fidelity >= 1000 else 0 5 | 6 | 7 | def bulk_item_promo(order): 8 | """10% discount for each LineItem with 20 or more units""" 9 | discount = 0 10 | for item in order.cart: 11 | if item.quantity >= 20: 12 | discount += item.total() * .1 13 | return discount 14 | 15 | def large_order_promo(order): 16 | """7% discount for orders with 10 or more distinct items""" 17 | distinct_items = {item.product for item in order.cart} 18 | if len(distinct_items) >= 10: 19 | return order.total() * .07 20 | return 0 21 | -------------------------------------------------------------------------------- /Fluent_Python/ch06-dp-1class-func/strategy_func/c++_version/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | code: CXXFLAGS += -Wall 18 | code: code.cc 19 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/README.rst: -------------------------------------------------------------------------------- 1 | Sample code for Chapter 7 - "Closures and decorators" 2 | 3 | From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) 4 | http://shop.oreilly.com/product/0636920032519.do 5 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/average/average.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> avg = make_averager() 3 | >>> avg(10) 4 | 10.0 5 | >>> avg(11) 6 | 10.5 7 | >>> avg(12) 8 | 11.0 9 | >>> avg.__code__.co_varnames 10 | ('new_value', 'total') 11 | >>> avg.__code__.co_freevars 12 | ('series',) 13 | >>> avg.__closure__ # doctest: +ELLIPSIS 14 | (,) 15 | >>> avg.__closure__[0].cell_contents 16 | [10, 11, 12] 17 | """ 18 | 19 | DEMO = """ 20 | >>> avg.__closure__ 21 | (,) 22 | """ 23 | 24 | 25 | def make_averager(): 26 | series = [] 27 | 28 | def averager(new_value): 29 | series.append(new_value) 30 | total = sum(series) 31 | return total/len(series) 32 | 33 | return averager 34 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/average/average_oo.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> avg = Averager() 3 | >>> avg(10) 4 | 10.0 5 | >>> avg(11) 6 | 10.5 7 | >>> avg(12) 8 | 11.0 9 | 10 | """ 11 | 12 | 13 | class Averager(): 14 | 15 | def __init__(self): 16 | self.series = [] 17 | 18 | def __call__(self, new_value): 19 | self.series.append(new_value) 20 | total = sum(self.series) 21 | return total/len(self.series) 22 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/clockdeco/clock_param/__pycache__/clockdeco_param.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch07-deco-closure/clockdeco/clock_param/__pycache__/clockdeco_param.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/clockdeco/clock_param/clockdeco_param_demo1.py: -------------------------------------------------------------------------------- 1 | import time 2 | from clockdeco_param import clock 3 | 4 | @clock('{name}: {elapsed}s') 5 | def snooze(seconds): 6 | time.sleep(seconds) 7 | 8 | for i in range(3): 9 | snooze(.123) 10 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/clockdeco/clock_param/clockdeco_param_demo2.py: -------------------------------------------------------------------------------- 1 | import time 2 | from clockdeco_param import clock 3 | 4 | @clock('{name}({args}) dt={elapsed:0.3f}s') 5 | def snooze(seconds): 6 | time.sleep(seconds) 7 | 8 | for i in range(3): 9 | snooze(.123) 10 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/clockdeco/clockdeco/clockdeco.py: -------------------------------------------------------------------------------- 1 | # clockdeco.py 2 | 3 | import time 4 | 5 | 6 | def clock(func): 7 | def clocked(*args): 8 | t0 = time.time() 9 | result = func(*args) 10 | elapsed = time.time() - t0 11 | name = func.__name__ 12 | arg_str = ', '.join(repr(arg) for arg in args) 13 | print('[%0.8fs] %s(%s) -> %r' % (elapsed, name, arg_str, result)) 14 | return result 15 | return clocked 16 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/clockdeco/clockdeco/clockdeco_demo.py: -------------------------------------------------------------------------------- 1 | # clockdeco_demo.py 2 | 3 | import time 4 | from clockdeco import clock 5 | 6 | @clock 7 | def snooze(seconds): 8 | time.sleep(seconds) 9 | 10 | @clock 11 | def factorial(n): 12 | if n<2: 13 | return 1 14 | else: 15 | return n*factorial(n-1) 16 | 17 | if __name__=='__main__': 18 | print('*' * 40, 'Calling snooze(.123)') 19 | snooze(.123) 20 | print('*' * 40, 'Calling factorial(6)') 21 | print('6! =', factorial(6)) 22 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/clockdeco/clockdeco/fibo_demo.py: -------------------------------------------------------------------------------- 1 | # import functools 2 | from clockdeco import clock 3 | 4 | # @functools.lru_cache() 5 | @clock 6 | def fibonacci(n): 7 | if n<2: 8 | return n 9 | else: 10 | return fibonacci(n-2) + fibonacci(n-1) 11 | 12 | if __name__ == '__main__': 13 | print(fibonacci(30)) 14 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/clockdeco/clockdeco/fibo_demo_lru.py: -------------------------------------------------------------------------------- 1 | import functools 2 | from clockdeco import clock 3 | 4 | @functools.lru_cache() 5 | @clock 6 | def fibonacci(n): 7 | if n<2: 8 | return n 9 | else: 10 | return fibonacci(n-2) + fibonacci(n-1) 11 | 12 | if __name__ == '__main__': 13 | print(fibonacci(30)) 14 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/fibo_demo.py: -------------------------------------------------------------------------------- 1 | from clockdeco import clock 2 | 3 | @clock 4 | def fibonacci(n): 5 | if n < 2: 6 | return n 7 | return fibonacci(n-2) + fibonacci(n-1) 8 | 9 | if __name__=='__main__': 10 | print(fibonacci(6)) 11 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/fibo_demo_lru.py: -------------------------------------------------------------------------------- 1 | import functools 2 | 3 | from clockdeco import clock 4 | 5 | @functools.lru_cache() # <1> 6 | @clock # <2> 7 | def fibonacci(n): 8 | if n < 2: 9 | return n 10 | return fibonacci(n-2) + fibonacci(n-1) 11 | 12 | if __name__=='__main__': 13 | print(fibonacci(6)) 14 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/registration.py: -------------------------------------------------------------------------------- 1 | # BEGIN REGISTRATION 2 | 3 | registry = [] # <1> 4 | 5 | def register(func): # <2> 6 | print('running register(%s)' % func) # <3> 7 | registry.append(func) # <4> 8 | return func # <5> 9 | 10 | @register # <6> 11 | def f1(): 12 | print('running f1()') 13 | 14 | @register 15 | def f2(): 16 | print('running f2()') 17 | 18 | def f3(): # <7> 19 | print('running f3()') 20 | 21 | def main(): # <8> 22 | print('running main()') 23 | print('registry ->', registry) 24 | f1() 25 | f2() 26 | f3() 27 | 28 | if __name__=='__main__': 29 | main() # <9> 30 | 31 | # END REGISTRATION -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/registration_abridged.py: -------------------------------------------------------------------------------- 1 | # BEGIN REGISTRATION_ABRIDGED 2 | registry = [] 3 | 4 | def register(func): 5 | print('running register(%s)' % func) 6 | registry.append(func) 7 | return func 8 | 9 | @register 10 | def f1(): 11 | print('running f1()') 12 | 13 | print('running main()') 14 | print('registry ->', registry) 15 | f1() 16 | # END REGISTRATION_ABRIDGED 17 | -------------------------------------------------------------------------------- /Fluent_Python/ch07-deco-closure/registration_param.py: -------------------------------------------------------------------------------- 1 | # BEGIN REGISTRATION_PARAM 2 | 3 | registry = set() # <1> 4 | 5 | def register(active=True): # <2> 6 | def decorate(func): # <3> 7 | print('running register(active=%s)->decorate(%s)' 8 | % (active, func)) 9 | if active: # <4> 10 | registry.add(func) 11 | else: 12 | registry.discard(func) # <5> 13 | 14 | return func # <6> 15 | return decorate # <7> 16 | 17 | @register(active=False) # <8> 18 | def f1(): 19 | print('running f1()') 20 | 21 | @register() # <9> 22 | def f2(): 23 | print('running f2()') 24 | 25 | def f3(): 26 | print('running f3()') 27 | 28 | # END REGISTRATION_PARAM 29 | -------------------------------------------------------------------------------- /Fluent_Python/ch09-pythonic-obj/mem_test.py: -------------------------------------------------------------------------------- 1 | import importlib 2 | import sys 3 | import resource 4 | 5 | NUM_VECTORS = 10**7 6 | 7 | if len(sys.argv) == 2: 8 | module_name = sys.argv[1].replace('.py', '') 9 | module = importlib.import_module(module_name) 10 | else: 11 | print('Usage: {} '.format()) 12 | sys.exit(1) 13 | 14 | fmt = 'Selected Vector2d type: {.__name__}.{.__name__}' 15 | print(fmt.format(module, module.Vector2d)) 16 | 17 | mem_init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 18 | print('Creating {:,} Vector2d instances'.format(NUM_VECTORS)) 19 | 20 | vectors = [module.Vector2d(3.0, 4.0) for i in range(NUM_VECTORS)] 21 | 22 | mem_final = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 23 | print('Initial RAM usage: {:14,}'.format(mem_init)) 24 | print(' Final RAM usage: {:14,}'.format(mem_final)) 25 | -------------------------------------------------------------------------------- /Fluent_Python/ch10-seq-hacking/README.rst: -------------------------------------------------------------------------------- 1 | Sample code for Chapter 10 - "Sequence hacking, hashing and slicing" 2 | 3 | From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) 4 | http://shop.oreilly.com/product/0636920032519.do 5 | -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/frenchdeck_class/frenchdeck2.py: -------------------------------------------------------------------------------- 1 | import collections 2 | 3 | Card = collections.namedtuple('Card', ['rank', 'suit']) 4 | 5 | class FrenchDeck2(collections.MutableSequence): 6 | ranks = [str(n) for n in range(2, 11)] + list('JQKA') 7 | suits = 'spades diamonds clubs hearts'.split() 8 | 9 | def __init__(self): 10 | self._cards = [Card(rank, suit) for suit in self.suits 11 | for rank in self.ranks] 12 | 13 | def __len__(self): 14 | return len(self._cards) 15 | 16 | def __getitem__(self, position): 17 | return self._cards[position] 18 | 19 | def __setitem__(self, position, value): # <1> 20 | self._cards[position] = value 21 | 22 | def __delitem__(self, position): # <2> 23 | del self._cards[position] 24 | 25 | def insert(self, position, value): # <3> 26 | self._cards.insert(position, value) 27 | -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch11-iface-abc/tombola_class/__init__.py -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/bingo.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/bingo.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/drum.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/drum.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/lotto.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/lotto.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/tombola.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/tombola.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/tombolist.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch11-iface-abc/tombola_class/__pycache__/tombolist.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/bingo.py: -------------------------------------------------------------------------------- 1 | # BEGIN TOMBOLA_BINGO 2 | 3 | import random 4 | from tombola_class.tombola import Tombola 5 | 6 | 7 | class BingoCage(Tombola): # <1> 8 | 9 | def __init__(self, items): 10 | self._randomizer = random.SystemRandom() # <2> 11 | self._items = [] 12 | self.load(items) # <3> 13 | 14 | def load(self, items): 15 | self._items.extend(items) 16 | self._randomizer.shuffle(self._items) # <4> 17 | 18 | def pick(self): # <5> 19 | try: 20 | return self._items.pop() 21 | except IndexError: 22 | raise LookupError('pick from empty BingoCage') 23 | 24 | def __call__(self): # <7> 25 | self.pick() 26 | 27 | # END TOMBOLA_BINGO 28 | -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/drum.py: -------------------------------------------------------------------------------- 1 | from random import shuffle 2 | 3 | from tombola_class.tombola import Tombola 4 | 5 | 6 | class TumblingDrum(Tombola): 7 | 8 | def __init__(self, iterable): 9 | self._balls = [] 10 | self.load(iterable) 11 | 12 | def load(self, iterable): 13 | self._balls.extend(iterable) 14 | shuffle(self._balls) 15 | 16 | def pick(self): 17 | return self._balls.pop() 18 | -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/lotto.py: -------------------------------------------------------------------------------- 1 | # BEGIN LOTTERY_BLOWER 2 | 3 | import random 4 | 5 | from tombola_class.tombola import Tombola 6 | 7 | 8 | class LotteryBlower(Tombola): 9 | 10 | def __init__(self, iterable): 11 | self._balls = list(iterable) # <1> 12 | 13 | def load(self, iterable): 14 | self._balls.extend(iterable) 15 | 16 | def pick(self): 17 | try: 18 | position = random.randrange(len(self._balls)) # <2> 19 | except ValueError: 20 | raise LookupError('pick from empty BingoCage') 21 | return self._balls.pop(position) # <3> 22 | 23 | def loaded(self): # <4> 24 | return bool(self._balls) 25 | 26 | def inspect(self): # <5> 27 | return tuple(sorted(self._balls)) 28 | 29 | 30 | # END LOTTERY_BLOWER 31 | -------------------------------------------------------------------------------- /Fluent_Python/ch11-iface-abc/tombola_class/tombolist.py: -------------------------------------------------------------------------------- 1 | from random import randrange 2 | 3 | from tombola_class.tombola import Tombola 4 | 5 | @Tombola.register # <1> 6 | class TomboList(list): # <2> 7 | 8 | def pick(self): 9 | if self: # <3> 10 | position = randrange(len(self)) 11 | return self.pop(position) # <4> 12 | else: 13 | raise LookupError('pop from empty TomboList') 14 | 15 | load = list.extend # <5> 16 | 17 | def loaded(self): 18 | return bool(self) # <6> 19 | 20 | def inspect(self): 21 | return tuple(sorted(self)) 22 | 23 | # Tombola.register(TomboList) # <7> 24 | -------------------------------------------------------------------------------- /Fluent_Python/ch12-inheritance/diamond.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def ping(self): 3 | print('ping:', self) 4 | 5 | 6 | class B(A): 7 | def pong(self): 8 | print('pong:', self) 9 | 10 | 11 | class C(A): 12 | def pong(self): 13 | print('PONG:', self) 14 | 15 | 16 | class D(B, C): 17 | 18 | def ping(self): 19 | super().ping() # A.ping(self) 绕过mro,直接到达任意一个父类 20 | print('post-ping:', self) 21 | 22 | def pingpong(self): 23 | self.ping() 24 | super().ping() 25 | self.pong() 26 | super().pong() 27 | C.pong(self) 28 | 29 | if __name__ == '__main__': 30 | d = D() 31 | d.pong() 32 | C.pong(d) 33 | 34 | print(D.__mro__) # 经常查看每一个类的__mro__,分析调用优先级 35 | d.ping() 36 | 37 | d.pingpong() 38 | 39 | -------------------------------------------------------------------------------- /Fluent_Python/ch13-op-overloading/bingo_addable/bingo.py: -------------------------------------------------------------------------------- 1 | # BEGIN TOMBOLA_BINGO 2 | 3 | import random 4 | 5 | from tombola import Tombola 6 | 7 | 8 | class BingoCage(Tombola): # <1> 9 | 10 | def __init__(self, items): 11 | self._randomizer = random.SystemRandom() # <2> 12 | self._items = [] 13 | self.load(items) # <3> 14 | 15 | def load(self, items): 16 | self._items.extend(items) 17 | self._randomizer.shuffle(self._items) # <4> 18 | 19 | def pick(self): # <5> 20 | try: 21 | return self._items.pop() 22 | except IndexError: 23 | raise LookupError('pick from empty BingoCage') 24 | 25 | def __call__(self): # <7> 26 | self.pick() 27 | 28 | # END TOMBOLA_BINGO 29 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/aritprog/aritprog_float_error.py: -------------------------------------------------------------------------------- 1 | """ 2 | Demonstrate difference between Arithmetic Progression calculated 3 | as a series of increments accumulating errors versus one addition 4 | and one multiplication. 5 | """ 6 | 7 | from fractions import Fraction 8 | from aritprog_v0 import ArithmeticProgression as APv0 9 | from aritprog_v1 import ArithmeticProgression as APv1 10 | 11 | if __name__ == '__main__': 12 | 13 | ap0 = iter(APv0(1, .1)) 14 | ap1 = iter(APv1(1, .1)) 15 | ap_frac = iter(APv1(Fraction(1, 1), Fraction(1, 10))) 16 | epsilon = 10**-10 17 | iteration = 0 18 | delta = next(ap0) - next(ap1) 19 | frac = next(ap_frac) 20 | while abs(delta) <= epsilon: 21 | delta = next(ap0) - next(ap1) 22 | frac = next(ap_frac) 23 | iteration +=1 24 | 25 | print('iteration: {}\tfraction: {}\tepsilon: {}\tdelta: {}'. 26 | format(iteration, frac, epsilon, delta)) 27 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v0.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v0.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v1.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v1.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v2.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v2.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v3.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/aritprog/class/__pycache__/aritprog_v3.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/aritprog/class/aritprog_v0.py: -------------------------------------------------------------------------------- 1 | """ 2 | Arithmetic progression class 3 | 4 | >>> ap = ArithmeticProgression(1, .5, 3) 5 | >>> list(ap) 6 | [1.0, 1.5, 2.0, 2.5] 7 | 8 | 9 | """ 10 | 11 | 12 | class ArithmeticProgression: 13 | 14 | def __init__(self, begin, step, end=None): 15 | self.begin = begin 16 | self.step = step 17 | self.end = end # None -> "infinite" series 18 | 19 | def __iter__(self): 20 | result = type(self.begin + self.step)(self.begin) 21 | forever = self.end is None 22 | while forever or result < self.end: 23 | yield result 24 | result += self.step 25 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/aritprog/class/aritprog_v3.py: -------------------------------------------------------------------------------- 1 | # BEGIN ARITPROG_ITERTOOLS 2 | import itertools 3 | 4 | 5 | def aritprog_gen(begin, step, end=None): 6 | first = type(begin + step)(begin) 7 | ap_gen = itertools.count(first, step) 8 | if end is not None: 9 | ap_gen = itertools.takewhile(lambda n: n < end, ap_gen) 10 | return ap_gen 11 | # END ARITPROG_ITERTOOLS 12 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/isis2json/cds.iso: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/isis2json/cds.iso -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/isis2json/cds.mst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/isis2json/cds.mst -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/isis2json/cds2.json: -------------------------------------------------------------------------------- 1 | { "docs" : -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_gen.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_gen.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_gen2.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_gen2.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_genexp.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_genexp.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_getitem.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_getitem.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_iter.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_iter.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_iter2.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch14-it-generator/sentence/class/__pycache__/sentence_iter2.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/sentence_gen.py: -------------------------------------------------------------------------------- 1 | """ 2 | Sentence: iterate over words using a generator function 3 | """ 4 | 5 | import re 6 | import reprlib 7 | 8 | RE_WORD = re.compile('\w+') 9 | 10 | 11 | class Sentence: 12 | 13 | def __init__(self, text): 14 | self.text = text 15 | self.words = RE_WORD.findall(text) 16 | 17 | def __repr__(self): 18 | return 'Sentence(%s)' % reprlib.repr(self.text) 19 | 20 | def __iter__(self): 21 | for word in self.words: # <1> 22 | yield word # <2> 23 | return # <3> 24 | 25 | # done! <4> 26 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/sentence_gen2.py: -------------------------------------------------------------------------------- 1 | """ 2 | Sentence: iterate over words using a generator function 3 | """ 4 | 5 | import re 6 | import reprlib 7 | 8 | RE_WORD = re.compile('\w+') 9 | 10 | 11 | class Sentence: 12 | 13 | def __init__(self, text): 14 | self.text = text # <1> 15 | 16 | def __repr__(self): 17 | return 'Sentence(%s)' % reprlib.repr(self.text) 18 | 19 | def __iter__(self): 20 | for match in RE_WORD.finditer(self.text): # <2> 21 | yield match.group() # <3> 22 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/sentence_getitem.py: -------------------------------------------------------------------------------- 1 | """ 2 | Sentence: access words by index 3 | """ 4 | 5 | import re 6 | import reprlib 7 | 8 | RE_WORD = re.compile('\w+') 9 | 10 | 11 | class Sentence: 12 | 13 | def __init__(self, text): 14 | self.text = text 15 | self.words = RE_WORD.findall(text) # <1> 16 | 17 | def __getitem__(self, index): 18 | return self.words[index] # <2> 19 | 20 | def __len__(self, index): # <3> 21 | return len(self.words) 22 | 23 | def __repr__(self): 24 | return 'Sentence(%s)' % reprlib.repr(self.text) # <4> 25 | 26 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/sentence/class/sentence_iter2.py: -------------------------------------------------------------------------------- 1 | """ 2 | Sentence: iterate over words using the Iterator Pattern, take #2 3 | 4 | WARNING: the Iterator Pattern is much simpler in idiomatic Python; 5 | see: sentence_gen*.py. 6 | """ 7 | 8 | import re 9 | import reprlib 10 | 11 | RE_WORD = re.compile('\w+') 12 | 13 | 14 | class Sentence: 15 | 16 | def __init__(self, text): 17 | self.text = text 18 | 19 | def __repr__(self): 20 | return 'Sentence(%s)' % reprlib.repr(self.text) 21 | 22 | def __iter__(self): 23 | word_iter = RE_WORD.finditer(self.text) # <1> 24 | return SentenceIter(word_iter) # <2> 25 | 26 | 27 | class SentenceIter(): 28 | 29 | def __init__(self, word_iter): 30 | self.word_iter = word_iter # <3> 31 | 32 | def __next__(self): 33 | match = next(self.word_iter) # <4> 34 | return match.group() # <5> 35 | 36 | def __iter__(self): 37 | return self 38 | -------------------------------------------------------------------------------- /Fluent_Python/ch14-it-generator/soapbox/yield_delegate/yield_delegate_fix.py: -------------------------------------------------------------------------------- 1 | """ Example adapted from ``yield_delegate_fail.py`` 2 | 3 | The following program performs a simple abstraction over the process of 4 | yielding. 5 | 6 | """ 7 | 8 | # BEGIN YIELD_DELEGATE_FIX 9 | def f(): 10 | def do_yield(n): 11 | yield n 12 | x = 0 13 | while True: 14 | x += 1 15 | yield from do_yield(x) 16 | # END YIELD_DELEGATE_FIX 17 | 18 | if __name__ == '__main__': 19 | print('Invoking f() now produces a generator') 20 | g = f() 21 | print(next(g)) 22 | print(next(g)) 23 | print(next(g)) 24 | 25 | -------------------------------------------------------------------------------- /Fluent_Python/ch16-coroutine/c_impl/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = main 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | main: CXXFLAGS += -Wall 18 | main: main.cc coro.cc 19 | 20 | -------------------------------------------------------------------------------- /Fluent_Python/ch16-coroutine/c_impl/coro.h: -------------------------------------------------------------------------------- 1 | #ifndef _CORO_H_ 2 | #define _CORO_H_ 3 | 4 | typedef void (*CoroFunType)(const void*); 5 | 6 | void coro_new(CoroFunType f, const void* arg); 7 | void coro_yield(); 8 | void coro_main(); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /Fluent_Python/ch16-coroutine/c_impl/main.cc: -------------------------------------------------------------------------------- 1 | #include "coro.h" 2 | #include 3 | #include 4 | 5 | void co1(const void* msg) 6 | { 7 | for (int i = 0; i != 5; ++i) 8 | { 9 | printf("%s\n", (char*)msg); 10 | coro_yield(); 11 | } 12 | } 13 | 14 | int main(int ac, char** av) 15 | { 16 | assert(sizeof(void*) == sizeof(unsigned long)); 17 | 18 | coro_new(co1, "hello"); 19 | coro_new(co1, "world"); 20 | coro_main(); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Fluent_Python/ch16-coroutine/coroaverager/coroaverager1-prime/coroaverager1.py: -------------------------------------------------------------------------------- 1 | # BEGIN DECORATED_AVERAGER 2 | """ 3 | A coroutine to compute a running average 4 | """ 5 | 6 | from coroutil import coroutine # <4> 7 | 8 | @coroutine # <5> 增加了预激装饰器 9 | def averager(): # <6> 10 | total = 0.0 11 | count = 0 12 | average = None 13 | while True: 14 | term = yield average 15 | total += term 16 | count += 1 17 | average = total/count 18 | # END DECORATED_AVERAGER 19 | 20 | coro_avg = averager() # <1> 21 | from inspect import getgeneratorstate 22 | print(getgeneratorstate(coro_avg)) # <2> 'GEN_SUSPENDED' 23 | print(coro_avg.send(10)) # <3> 10.0 24 | print(coro_avg.send(30)) # 20.0 25 | print(coro_avg.send(5)) # 15.0 26 | 27 | -------------------------------------------------------------------------------- /Fluent_Python/ch16-coroutine/coroaverager/coroaverager1-prime/coroutil.py: -------------------------------------------------------------------------------- 1 | # BEGIN CORO_DECO 2 | from functools import wraps 3 | 4 | def coroutine(func): 5 | """Decorator: primes `func` by advancing to first `yield`""" 6 | @wraps(func) 7 | def primer(*args,**kwargs): # <1> 8 | gen = func(*args,**kwargs) # <2> 9 | next(gen) # <3> 10 | return gen # <4> 11 | return primer 12 | # END CORO_DECO 13 | -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/__pycache__/flags.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/__pycache__/flags.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/bd.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/bd.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/br.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/br.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/cd.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/cd.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/cn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/cn.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/de.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/de.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/eg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/eg.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/et.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/et.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/fr.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/fr.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/id.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/id.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/in.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/in.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/ir.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/ir.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/jp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/jp.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/mx.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/mx.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/ng.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/ng.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/ph.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/ph.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/pk.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/pk.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/ru.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/ru.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/tr.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/tr.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/us.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/us.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags/downloads/vn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags/downloads/vn.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/__pycache__/flags2_common.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/__pycache__/flags2_common.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/__pycache__/flags2_sequential.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/__pycache__/flags2_sequential.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/bd.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/bd.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/br.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/br.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/cd.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/cd.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/cn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/cn.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/de.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/de.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/eg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/eg.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/et.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/et.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/fr.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/fr.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/id.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/id.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/in.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/in.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/ir.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/ir.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/jp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/jp.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/mx.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/mx.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/ng.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/ng.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/ph.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/ph.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/pk.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/pk.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/ru.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/ru.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/tr.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/tr.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/us.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/us.gif -------------------------------------------------------------------------------- /Fluent_Python/ch17-futures/flags2/downloads/vn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch17-futures/flags2/downloads/vn.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/charfinder/http_charfinder.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Charfinder 6 | 7 | 8 | Examples: {links} 9 |

10 |

11 | 12 | {message} 13 |
14 |

15 | 16 | {result} 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/charfinder/requirements.txt: -------------------------------------------------------------------------------- 1 | aiohttp==0.13.1 2 | -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/bd.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/bd.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/br.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/br.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/cd.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/cd.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/cn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/cn.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/de.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/de.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/eg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/eg.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/et.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/et.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/fr.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/fr.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/id.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/id.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/in.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/in.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/ir.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/ir.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/jp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/jp.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/mx.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/mx.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/ng.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/ng.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/ph.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/ph.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/pk.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/pk.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/ru.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/ru.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/tr.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/tr.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/us.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/us.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18-asyncio/flags/downloads/vn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18-asyncio/flags/downloads/vn.gif -------------------------------------------------------------------------------- /Fluent_Python/ch18b-async-await/README.rst: -------------------------------------------------------------------------------- 1 | Refactored sample code for Chapter 18 - "Concurrency with asyncio" 2 | 3 | From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) 4 | http://shop.oreilly.com/product/0636920032519.do 5 | 6 | ################################################################## 7 | NOTE: this "18b" directory contains the examples of chapter 18 8 | rewritten using the new async/await syntax available in Python 3.5 9 | ONLY, instead of the "yield-from" syntax which works since Python 10 | 3.3 (and will still work with Python 3.5). 11 | ################################################################## 12 | -------------------------------------------------------------------------------- /Fluent_Python/ch18b-async-await/charfinder/__pycache__/charfinder.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18b-async-await/charfinder/__pycache__/charfinder.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch18b-async-await/charfinder/http_charfinder.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Charfinder 6 | 7 | 8 | Examples: {links} 9 |

10 |

11 | 12 | {message} 13 |
14 |

15 | 16 | {result} 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /Fluent_Python/ch18b-async-await/flags/imported/__pycache__/flags.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18b-async-await/flags/imported/__pycache__/flags.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch18b-async-await/flags/imported/__pycache__/flags2_common.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch18b-async-await/flags/imported/__pycache__/flags2_common.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch19-dyn-attr-prop/doc_property.py: -------------------------------------------------------------------------------- 1 | """ 2 | Example of property documentation 3 | 4 | >>> f = Foo() 5 | >>> f.bar = 77 6 | >>> f.bar 7 | 77 8 | >>> Foo.bar.__doc__ 9 | 'The bar attribute' 10 | """ 11 | 12 | # BEGIN DOC_PROPERTY 13 | class Foo: 14 | 15 | @property 16 | def bar(self): 17 | '''The bar attribute''' 18 | return self.__dict__['bar'] 19 | 20 | @bar.setter 21 | def bar(self, value): 22 | self.__dict__['bar'] = value 23 | # END DOC_PROPERTY 24 | -------------------------------------------------------------------------------- /Fluent_Python/ch19-dyn-attr-prop/oscon/__pycache__/osconfeed.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/Fluent_Python/ch19-dyn-attr-prop/oscon/__pycache__/osconfeed.cpython-36.pyc -------------------------------------------------------------------------------- /Fluent_Python/ch19-dyn-attr-prop/oscon/demo_schedule2.py: -------------------------------------------------------------------------------- 1 | import shelve 2 | 3 | from schedule2 import DB_NAME, CONFERENCE, load_db 4 | from schedule2 import DbRecord, Event 5 | 6 | with shelve.open(DB_NAME) as db: 7 | if CONFERENCE not in db: 8 | load_db(db) 9 | 10 | DbRecord.set_db(db) 11 | event = DbRecord.fetch('event.33950') 12 | print(event) 13 | print(event.venue) 14 | print(event.venue.name) 15 | for spkr in event.speakers: 16 | print('{0.serial}: {0.name}'.format(spkr)) 17 | 18 | print(repr(Event.venue)) 19 | 20 | event2 = DbRecord.fetch('event.33451') 21 | print(event2) 22 | print(event2.fetch) 23 | print(event2.venue) -------------------------------------------------------------------------------- /Fluent_Python/ch19-dyn-attr-prop/oscon/test_schedule1.py: -------------------------------------------------------------------------------- 1 | import shelve 2 | import pytest 3 | 4 | import schedule1 as schedule 5 | 6 | 7 | @pytest.yield_fixture 8 | def db(): 9 | with shelve.open(schedule.DB_NAME) as the_db: 10 | if schedule.CONFERENCE not in the_db: 11 | schedule.load_db(the_db) 12 | yield the_db 13 | 14 | 15 | def test_record_class(): 16 | rec = schedule.Record(spam=99, eggs=12) 17 | assert rec.spam == 99 18 | assert rec.eggs == 12 19 | 20 | 21 | def test_conference_record(db): 22 | assert schedule.CONFERENCE in db 23 | 24 | 25 | def test_speaker_record(db): 26 | speaker = db['speaker.3471'] 27 | assert speaker.name == 'Anna Martelli Ravenscroft' 28 | 29 | 30 | def test_event_record(db): 31 | event = db['event.33950'] 32 | assert event.name == 'There *Will* Be Bugs' 33 | 34 | 35 | def test_event_venue(db): 36 | event = db['event.33950'] 37 | assert event.venue_serial == 1449 38 | -------------------------------------------------------------------------------- /Fluent_Python/ch19-dyn-attr-prop/pseudo_construction.py: -------------------------------------------------------------------------------- 1 | # pseudo-code for object construction 2 | def object_maker(the_class, some_arg): 3 | new_object = the_class.__new__(some_arg) 4 | if isinstance(new_object, the_class): 5 | the_class.__init__(new_object, some_arg) 6 | return new_object 7 | 8 | # the following statements are roughly equivalent 9 | x = Foo('bar') 10 | x = object_maker(Foo, 'bar') 11 | -------------------------------------------------------------------------------- /Fluent_Python/ch20-descriptor/bulkfood/model_v4c.py: -------------------------------------------------------------------------------- 1 | # BEGIN MODEL_V4 2 | class Quantity: 3 | __counter = 0 4 | 5 | def __init__(self): 6 | cls = self.__class__ 7 | prefix = cls.__name__ 8 | index = cls.__counter 9 | self.storage_name = '_{}#{}'.format(prefix, index) 10 | cls.__counter += 1 11 | 12 | def __get__(self, instance, owner): 13 | if instance is None: 14 | return self 15 | else: 16 | return getattr(instance, self.storage_name) 17 | 18 | def __set__(self, instance, value): 19 | if value > 0: 20 | setattr(instance, self.storage_name, value) 21 | else: 22 | raise ValueError('value must be > 0') 23 | # END MODEL_V4 24 | -------------------------------------------------------------------------------- /Fluent_Python/ch21-class-metaprog/README.rst: -------------------------------------------------------------------------------- 1 | Sample code for Chapter 21 - "Class metaprogramming" 2 | 3 | From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) 4 | http://shop.oreilly.com/product/0636920032519.do 5 | -------------------------------------------------------------------------------- /Fluent_Python/ch21-class-metaprog/evalsupport.py: -------------------------------------------------------------------------------- 1 | print('<[100]> evalsupport module start') 2 | 3 | def deco_alpha(cls): 4 | print('<[200]> deco_alpha') 5 | 6 | def inner_1(self): 7 | print('<[300]> deco_alpha:inner_1') 8 | 9 | cls.method_y = inner_1 10 | return cls 11 | 12 | 13 | class MetaAleph(type): 14 | print('<[400]> MetaAleph body') 15 | 16 | def __init__(cls, name, bases, dic): 17 | print('<[500]> MetaAleph.__init__') 18 | 19 | def inner_2(self): 20 | print('<[600]> MetaAleph.__init__:inner_2') 21 | 22 | cls.method_z = inner_2 23 | 24 | 25 | print('<[700]> evalsupport module end') 26 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch01-moderner_and_simpler/.cc -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-02_auto.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Foo 4 | { 5 | public: 6 | static int get(void) 7 | { 8 | return 0; 9 | } 10 | }; 11 | 12 | class Bar 13 | { 14 | public: 15 | static const char* get(void) 16 | { 17 | return "0"; 18 | } 19 | }; 20 | 21 | template 22 | void func(void) 23 | { 24 | auto val = A::get(); 25 | 26 | //... 27 | } 28 | 29 | int main(void) 30 | { 31 | func(); 32 | func(); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-03_decltype.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Foo 4 | { 5 | public: 6 | static const int Number = 0; 7 | int x; 8 | }; 9 | 10 | int main(void) 11 | { 12 | int n = 0; 13 | volatile const int &x = n; 14 | 15 | decltype(n) a = n; //a -> int 16 | decltype(x) b = n; //b -> const volatile int & 17 | 18 | decltype(Foo::Number) c = 0; //c -> const int 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-04_decltype.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Foo{}; 4 | 5 | int& func_int_r(void); //左值(lvalue,可简单理解为可寻址值) 6 | int&& func_int_rr(void); //x值(xvalue,右值引用本身是一个xvalue) 7 | int func_int(void); //纯右值(pvalue) 8 | 9 | const int& func_cint_r(void); //左值 10 | const int&& func_cint_rr(void); //x值 11 | const int func_cint(void); //纯右值 12 | 13 | const Foo func_cfoo(void); //纯右值 14 | 15 | int main(void) 16 | { 17 | int x = 0; 18 | 19 | decltype(func_int_r()) a1 = x; //a1 -> int& 20 | decltype(func_int_rr()) b1 = 0; //b1 -> int&& 21 | decltype(func_int()) c1 = 0; //c1 -> int 22 | 23 | decltype(func_cint_r()) a2 = x; //a2 -> const int& 24 | decltype(func_cint_rr()) b2 = 0; //b2 -> const int&& 25 | decltype(func_cint()) c2 = 0; //c2 -> int 26 | 27 | decltype(func_cfoo()) ff = Foo(); //ff -> Foo 28 | 29 | system("pause"); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-05_template.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | class Foo 6 | { 7 | //typename ContainerT::iterator it_; //类型定义可能有问题 8 | //若想在 const ContainerT 下通过编译请使用下面it_定义: 9 | decltype(std::declval().begin()) it_; 10 | public: 11 | void func(ContainerT& container) 12 | { 13 | it_ = container.begin(); 14 | } 15 | 16 | //... 17 | }; 18 | 19 | int main(void) 20 | { 21 | typedef const std::vector container_t; //这里有const属性,编译会报一大堆错误信息 22 | container_t arr; 23 | 24 | Foo foo; 25 | foo.func(arr); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-06_》.cc: -------------------------------------------------------------------------------- 1 | template 2 | struct Foo 3 | { 4 | typedef T type; 5 | }; 6 | 7 | template 8 | class A 9 | { 10 | //... 11 | }; 12 | 13 | int main(void) 14 | { 15 | Foo>::type xx; //C++98/03编译出错。 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-07_init.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int i_arr[3] = { 1, 2, 3 }; //普通数组 4 | 5 | struct A 6 | { 7 | int x; 8 | struct B 9 | { 10 | int i; 11 | int j; 12 | } b; 13 | } a = { 1, {2, 3} }; //POD类型 14 | 15 | int i = 0; 16 | 17 | class Foo 18 | { 19 | public: 20 | Foo(int){} 21 | } foo = 123; 22 | 23 | int j(0); 24 | 25 | Foo bar(123); 26 | 27 | int main(void) 28 | { 29 | 30 | system("pause"); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-08_init.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Foo 4 | { 5 | public: 6 | Foo(int){} 7 | 8 | private: 9 | Foo(const Foo&); 10 | }; 11 | 12 | int main(void) 13 | { 14 | Foo a1(123); 15 | //Foo a2 = a1; //error C2248: “Foo::Foo”: 无法访问 private 成员(在“Foo”类中声明) 16 | Foo a3 = { 123 }; 17 | Foo a4{ 123 }; 18 | 19 | int a5 = { 3 }; 20 | int a6 { 3 }; 21 | 22 | system("pause"); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-09_init.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class FooVector 6 | { 7 | std::vector content_; 8 | 9 | public: 10 | FooVector(std::initializer_list list) 11 | { 12 | for (auto it = list.begin(); it != list.end(); ++it) 13 | { 14 | content_.push_back(*it); 15 | } 16 | } 17 | }; 18 | 19 | class FooMap 20 | { 21 | std::map content_; 22 | using pair_t = std::map::value_type; 23 | 24 | public: 25 | FooMap(std::initializer_list list) 26 | { 27 | for (auto it = list.begin(); it != list.end(); ++it) 28 | { 29 | content_.insert(*it); 30 | } 31 | } 32 | }; 33 | 34 | int main(void) 35 | { 36 | FooVector foo1 = { 1, 2, 3, 4, 5 }; 37 | FooMap foo2 = { { 1, 2 }, { 3, 4 }, { 5, 6 } }; 38 | 39 | system("pause"); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-10_init_narrow.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int a = 1.1; 6 | //int b = { 1.1 }; //error 7 | 8 | float fa = 1e40; 9 | //float fb = { 1e40 }; //error 10 | 11 | float fc = (unsigned long long)-1; 12 | //float fd = { (unsigned long long)-1 }; //error 13 | float fe = (unsigned long long)1; 14 | float ff = { (unsigned long long)1 }; //OK 15 | 16 | const int x = 1024; 17 | const int y = 1; 18 | 19 | char c = x; 20 | //char d = { x }; //error 21 | char e = y; 22 | char f = { y }; 23 | 24 | system("pause"); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-11_1-15_for_range/Test.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch01-moderner_and_simpler/1-11_1-15_for_range/Test.cc -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-16_functor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void func(void) 4 | { 5 | //... 6 | } 7 | 8 | struct Foo 9 | { 10 | void operator()(void) 11 | { 12 | //... 13 | } 14 | }; 15 | 16 | struct Bar 17 | { 18 | using fr_t = void(*)(void); 19 | 20 | static void func(void) 21 | { 22 | //... 23 | } 24 | 25 | operator fr_t(void) 26 | { 27 | return func; 28 | } 29 | }; 30 | 31 | struct A 32 | { 33 | int a_; 34 | 35 | void mem_func(void) 36 | { 37 | //... 38 | } 39 | }; 40 | 41 | int main(void) 42 | { 43 | void(*func_ptr)(void) = &func; //函数指针 44 | func_ptr(); 45 | 46 | Foo foo; //仿函数 47 | foo(); 48 | 49 | Bar bar; //可被转换为函数指针的类对象 50 | bar(); 51 | 52 | void(A::*mem_func_ptr)(void) = &A::mem_func; //类成员函数指针 53 | int A::*mem_obj_ptr = &A::a_; //类成员指针 54 | 55 | A aa; 56 | (aa.*mem_func_ptr)(); 57 | aa.*mem_obj_ptr = 123; 58 | 59 | system("pause"); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-17_std_function.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func(void) 5 | { 6 | std::cout << __FUNCTION__ << std::endl; 7 | } 8 | 9 | class Foo 10 | { 11 | public: 12 | static int foo_func(int a) 13 | { 14 | std::cout << __FUNCTION__ << "(" << a << ") ->: "; 15 | return a; 16 | } 17 | }; 18 | 19 | class Bar 20 | { 21 | public: 22 | int operator()(int a) 23 | { 24 | std::cout << __FUNCTION__ << "(" << a << ") ->: "; 25 | return a; 26 | } 27 | }; 28 | 29 | int main(void) 30 | { 31 | std::function fr1 = func; 32 | fr1(); 33 | 34 | //绑定一个类的静态成员函数 35 | std::function fr2 = Foo::foo_func; 36 | std::cout << fr2(123) << std::endl; 37 | 38 | Bar bar; 39 | fr2 = bar; 40 | std::cout << fr2(123) << std::endl; 41 | 42 | system("pause"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-19.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void call_when_even(int x, const std::function& f) 5 | { 6 | if (!(x & 1)) //x % 2 == 0 7 | { 8 | f(x); 9 | } 10 | } 11 | 12 | void output(int x) 13 | { 14 | std::cout << x << " "; 15 | } 16 | 17 | int main(void) 18 | { 19 | for (int i = 0; i < 10; ++i) 20 | { 21 | call_when_even(i, output); 22 | } 23 | 24 | std::cout << std::endl; 25 | 26 | system("pause"); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-20.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void call_when_even(int x, const std::function& f) 5 | { 6 | if (!(x & 1)) //x % 2 == 0 7 | { 8 | f(x); 9 | } 10 | } 11 | 12 | void output(int x) 13 | { 14 | std::cout << x << " "; 15 | } 16 | 17 | void output_add_2(int x) 18 | { 19 | std::cout << x + 2 << " "; 20 | } 21 | 22 | int main(void) 23 | { 24 | { 25 | auto fr = std::bind(output, std::placeholders::_1); 26 | for (int i = 0; i < 10; ++i) 27 | { 28 | call_when_even(i, fr); 29 | } 30 | 31 | std::cout << std::endl; 32 | } 33 | { 34 | auto fr = std::bind(output_add_2, std::placeholders::_1); 35 | 36 | for (int i = 0; i < 10; ++i) 37 | { 38 | call_when_even(i, fr); 39 | } 40 | 41 | std::cout << std::endl; 42 | } 43 | 44 | system("pause"); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-21.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void output(int x, int y) 5 | { 6 | std::cout << x << " " << y << std::endl; 7 | } 8 | 9 | int main(void) 10 | { 11 | std::bind(output, 1, 2)(); 12 | std::bind(output, std::placeholders::_1, 2)(1); 13 | std::bind(output, 2, std::placeholders::_1)(1); 14 | 15 | //std::bind(output, 2, std::placeholders::_2)(1); //error:调用时没有第二个参数 16 | 17 | std::bind(output, 2, std::placeholders::_2)(1, 2); //输出 2 2 调用时第一个参数被吞掉了 18 | 19 | std::bind(output, std::placeholders::_1, std::placeholders::_2)(1, 2); //输出 1 2 20 | std::bind(output, std::placeholders::_2, std::placeholders::_1)(1, 2); //输出 2 1 21 | 22 | 23 | system("pause"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-22.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A 5 | { 6 | public: 7 | int i_ = 0; 8 | 9 | void output(int x, int y) 10 | { 11 | std::cout << x << " " << y << std::endl; 12 | } 13 | }; 14 | 15 | int main(void) 16 | { 17 | A a; 18 | std::function fr = 19 | std::bind(&A::output, &a, std::placeholders::_1, std::placeholders::_2); 20 | fr(1, 2); //输出 1 2 21 | 22 | std::function fr_i = std::bind(&A::i_, &a); //vs13的bug,绑定成员变量要报错 23 | fr_i(); 24 | fr_i() = 123; 25 | 26 | std::cout << a.i_ << std::endl; //输出 123 27 | 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-23.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class A 4 | { 5 | int i_ = 0; 6 | 7 | void func(int x, int y) 8 | { 9 | //auto x1 = []{return i_; }; //error,没有捕获外部变量 10 | auto x2 = [=]{return i_ + x + y; }; 11 | auto x3 = [&]{return i_ + x + y; }; 12 | auto x4 = [this]{return i_; }; 13 | //auto x5 = [this]{return i_ + x + y; }; //error,没有捕获x和y 14 | auto x6 = [this, x, y]{return i_ + x + y; }; 15 | auto x7 = [this]{return i_++; }; 16 | } 17 | }; 18 | 19 | int main(void) 20 | { 21 | { 22 | int a = 0; 23 | int b = 1; 24 | //auto f1 = []{return a; }; //error,没有捕获外部变量 25 | auto f2 = [&]{return a++; }; 26 | auto f3 = [=]{return a; }; 27 | //auto f4 = [=]{return a++; }; //error,a是以复制方式捕获的,无法修改 28 | //auto f5 = [a]{return a + b; }; //error,没有捕获变量b 29 | auto f6 = [a, &b]{return a + (b++); }; 30 | auto f7 = [=, &b]{return a + (b++); }; 31 | } 32 | 33 | system("pause"); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/1-24.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class CountEven 6 | { 7 | int& count_; 8 | 9 | public: 10 | CountEven(int& count) : count_(count){} 11 | 12 | void operator()(int val) 13 | { 14 | if (!(val & 1)) //val % 2 == 0 15 | { 16 | ++count_; 17 | } 18 | } 19 | }; 20 | 21 | 22 | int main(void) 23 | { 24 | std::vector v = { 1, 2, 3, 4, 5, 6 }; 25 | int even_count = 0; 26 | 27 | std::for_each(v.begin(), v.end(), CountEven(even_count)); 28 | std::cout << "The number of even is " << even_count << std::endl; 29 | 30 | system("pause"); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch01-moderner_and_simpler/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = 1-01 1-02 1-03 1-04 1-05 1-06 1-07 1-08 1-09 1-10 1-13 1-16 1-17 1-19 1-20 1-21 1-22 1-23 1-24 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 1-01:1-01_auto.cc 17 | 1-02:1-02_auto.cc 18 | 1-03:1-03_decltype.cc 19 | 1-04:1-04_decltype.cc 20 | 1-05:1-05_template.cc 21 | 1-06:1-06_》.cc 22 | 1-07:1-07_init.cc 23 | 1-08:1-08_init.cc 24 | 1-09:1-09_init.cc 25 | 1-10:1-10_init_narrow.cc 26 | 1-13:1-11_1-15_for_range/Test.cc 27 | 1-16:1-16_functor.cc 28 | 1-17:1-17_std_function.cc 29 | 1-19:1-19.cc 30 | 1-20:1-20.cc 31 | 1-21:1-21.cc 32 | 1-22:1-22.cc 33 | 1-23:1-23.cc 34 | 1-24:1-24.cc 35 | -------------------------------------------------------------------------------- /In-depth_C++11/ch02-rvalue/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = 2-1 2-2 2-3 2-4 2-5 2-6 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 2-1: 2-1.cc 17 | 18 | 2-2: 2-2.cc 19 | 20 | 2-3: CXXFLAGS += -Wall 21 | 2-3: 2-3.cc 22 | 23 | 2-4: 2-4.cc 24 | 25 | 2-5: 2-5.cc 26 | 27 | 2-6: 2-6.cc 28 | 29 | -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | template 6 | struct Construct 7 | { 8 | typedef typename std::remove_reference::type U; 9 | Construct() : m_ptr(new U){} 10 | 11 | typename std::add_lvalue_reference::type Get() const 12 | { 13 | *m_ptr = 100; //特意加的一行,以防*m_ptr是个未定义的值 14 | return *m_ptr.get(); 15 | } 16 | 17 | private: 18 | typename std::unique_ptr m_ptr; 19 | }; 20 | 21 | int main(void) 22 | { 23 | //typename Construct c;//error: typename只能用在模板函数、模板类中 24 | Construct c; 25 | int a = c.Get(); 26 | 27 | std::cout << a << std::endl; 28 | 29 | system("pause"); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-10/3-10.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-10/3-10.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-10/3-10/MyDLL.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-10/3-10/MyDLL.dll -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-10/3-10/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "DllParser.hpp" 2 | 3 | #include 4 | 5 | void TestDllParser() 6 | { 7 | DllParser dllParser; 8 | std::string str("MyDLL.dll"); 9 | 10 | dllParser.Load(str); 11 | 12 | int a = 5; 13 | int b = 8; 14 | 15 | auto max = dllParser.ExcecuteFunc("Max", a, b); 16 | auto add = dllParser.ExcecuteFunc("Add", a, b); 17 | } 18 | 19 | int main(void) 20 | { 21 | TestDllParser(); 22 | 23 | system("pause"); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-12/3-12.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-12/3-12.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-13/3-13.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-13/3-13.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-13/3-13/Any.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-13/3-13/Any.hpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-13/3-13/源.cpp: -------------------------------------------------------------------------------- 1 | #include "Any.hpp" 2 | 3 | #include 4 | #include 5 | 6 | void TestAny() 7 | { 8 | Any n; 9 | auto r = n.IsNull();//true 10 | std::string s1 = "hello"; 11 | n = s1; 12 | n = "world"; 13 | n.AnyCast(); //can not cast int to string 14 | Any n1 = 1; 15 | n1.Is(); //true 16 | } 17 | 18 | 19 | int main(void) 20 | { 21 | TestAny(); 22 | 23 | system("pause"); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-14/3-14.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-14/3-14.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-14/3-14/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct VariantVisitor : public boost::static_visitor 6 | { 7 | void operator()(int a) 8 | { 9 | std::cout << "int" << std::endl; 10 | } 11 | void operator()(short a) 12 | { 13 | std::cout << "short" << std::endl; 14 | } 15 | void operator()(double a) 16 | { 17 | std::cout << "double" << std::endl; 18 | } 19 | void operator()(std::string a) 20 | { 21 | std::cout << "std::string" << std::endl; 22 | } 23 | }; 24 | 25 | 26 | int main(void) 27 | { 28 | VariantVisitor visitor; 29 | boost::variant v = 1; 30 | boost::apply_visitor(visitor, v); 31 | 32 | system("pause"); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-15/3-15.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-15/3-15.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-15/3-15/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-15/3-15/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-15/3-15/Variant.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-15/3-15/Variant.hpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-17/3-17.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-17/3-17.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-17/3-17/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-17/3-17/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-18/3-18.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-18/3-18.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-19/3-19.v11.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-19/3-19.v11.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-19/3-19.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-19/3-19.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-19/3-19/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-19/3-19/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-20/3-20.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-20/3-20.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-20/3-20/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-20/3-20/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-21/3-21.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-21/3-21.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-21/3-21/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-21/3-21/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-22/3-22.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-22/3-22.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-22/3-22/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "TpForeach.hpp" 2 | #include 3 | 4 | struct Functor 5 | { 6 | template 7 | void operator()(T& t) const 8 | { 9 | //t.doSomething(); 10 | std::cout << t << std::endl; 11 | } 12 | }; 13 | 14 | void TestTupleForeach() 15 | { 16 | tp_for_each(Functor(), std::make_tuple(1, 2.5)); 17 | } 18 | 19 | int main(void) 20 | { 21 | TestTupleForeach(); 22 | 23 | system("pause"); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-22/3-22/TpIndexs.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | template 4 | struct IndexTuple {}; 5 | 6 | template 7 | struct make_indexes : make_indexes{}; 8 | 9 | template 10 | struct make_indexes<0, Indexs...> 11 | { 12 | typedef IndexTuple type; 13 | }; -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-23/3-23.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-23/3-23.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-23/3-23/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-23/3-23/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-24/3-24.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-24/3-24.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-24/3-24/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-24/3-24/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-25/3-25.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-25/3-25.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-25/3-25/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "TpZip.hpp" 2 | #include 3 | 4 | void TupleZip() 5 | { 6 | auto tp1 = std::make_tuple(1, 2, 2.5, 'a'); 7 | auto tp2 = std::make_tuple(1.5, 2, 2.5, 'z'); 8 | auto mypairs = Zip(tp1, tp2); 9 | } 10 | 11 | int main(void) 12 | { 13 | TupleZip(); 14 | 15 | system("pause"); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-25/3-25/TpZip.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-25/3-25/TpZip.hpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-3/3-3.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-3/3-3.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-4/3-4.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-4/3-4.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-5/3-5.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-5/3-5.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-5/3-5/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-5/3-5/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-5/3-5/optional.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-5/3-5/optional.hpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-7~3-8/3-7~3-8.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-7~3-8/3-7~3-8.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-7~3-8/3-7~3-8/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-7~3-8/3-7~3-8/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9/MyDLL.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9/MyDLL.dll -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9/MyDLL.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | extern "C" __declspec(dllexport) int Add(int &a,int &b); 4 | 5 | extern "C" __declspec(dllexport) int Max(const int &a, const int &b); 6 | extern "C" __declspec(dllexport) int Get(const int &a); -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9/MyDLL.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9/MyDLL.lib -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3-9/3-9/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "MyDLL.h" 2 | 3 | #include 4 | #include 5 | 6 | void TestDll() 7 | { 8 | typedef int(*pMax)(int a, int b); 9 | typedef int(*pGet)(int a); 10 | 11 | HINSTANCE hDLL = LoadLibrary("MyDll.dll"); 12 | if (hDLL == nullptr) 13 | { 14 | return; 15 | } 16 | 17 | pMax Max = (pMax)GetProcAddress(hDLL, "Max"); 18 | if (Max == nullptr) 19 | { 20 | return; 21 | } 22 | 23 | int ret = Max(5, 8); 24 | 25 | pGet Get = (pGet)GetProcAddress(hDLL, "Get"); 26 | if (Get == nullptr) 27 | { 28 | return; 29 | } 30 | 31 | int ret2 = Get(5); 32 | 33 | FreeLibrary(hDLL); 34 | } 35 | 36 | int main(void) 37 | { 38 | TestDll(); 39 | 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/3.16/Test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ScopeGuard.h" 3 | 4 | #include 5 | #include 6 | 7 | void TestScopeGuard() 8 | { 9 | std::function < void()> f = [](){ std::cout << "cleanup from unnormal exit" << std::endl; }; 10 | //正常退出 11 | { 12 | auto gd = MakeGuard(f); 13 | //... 14 | gd.Dismiss(); //表明前面我是正常的清理了资源,属于正常退出的 15 | } 16 | 17 | //异常退出 18 | try 19 | { 20 | auto gd = MakeGuard(f); 21 | //... 22 | throw 1; 23 | } 24 | catch (...) 25 | { 26 | std::cout << "捕获到了一个异常!!\n\n"; 27 | } 28 | 29 | //非正常退出 30 | { 31 | auto gd = MakeGuard(f); 32 | return; //非正常退出表示资源还没清理呢,,等着ScopeGuard自动清理 33 | //... 34 | } 35 | } 36 | 37 | 38 | int main(void) 39 | { 40 | TestScopeGuard(); 41 | 42 | system("pause"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = 3-1 3-2 enable_if 3-16 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 3-1: 3-1.cc 17 | 18 | 3-2: CXXFLAGS += -Wall 19 | 3-2: 3-2.cc 20 | 21 | enable_if: enable_if.cc 22 | 23 | 24 | 3-16: 3.16/Test.cc 25 | -------------------------------------------------------------------------------- /In-depth_C++11/ch03-type_traits_and_vargs/enable_if.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | typename std::enable_if::value, T>::type foo(T t) 5 | { 6 | return t; 7 | } 8 | 9 | template 10 | typename std::enable_if::value, T>::type foo(T t) 11 | { 12 | return t; 13 | } 14 | 15 | 16 | template 17 | T foo2(T t, typename std::enable_if::value, int>::type =0) 18 | { 19 | return t; 20 | } 21 | 22 | 23 | int main(void) 24 | { 25 | auto r = foo(1); 26 | auto r1= foo(1.2); 27 | auto r2= foo("test"); 28 | 29 | //auto r3=foo2("test",1); 30 | //auto r3=foo2(1.4, 1); 31 | auto r3=foo2(5, 1); 32 | auto r4=foo2(9, 1.4); 33 | auto r5=foo2(6); 34 | //auto r6=foo2(9, 1.4, "test"); 35 | } 36 | -------------------------------------------------------------------------------- /In-depth_C++11/ch04-smart-pointer/4-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch04-smart-pointer/4-1 -------------------------------------------------------------------------------- /In-depth_C++11/ch04-smart-pointer/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = 4-1 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 4-1: 4-1.cc 17 | 18 | 19 | -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-1/5-1.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-1/5-1.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-1/5-1/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | std::mutex g_lock; 7 | 8 | void func() 9 | { 10 | g_lock.lock(); 11 | 12 | std::cout << "entered thread " << std::this_thread::get_id() << std::endl; 13 | std::this_thread::sleep_for(std::chrono::seconds(1)); 14 | std::cout << "leaving thread " << std::this_thread::get_id() << std::endl; 15 | 16 | g_lock.unlock(); 17 | } 18 | 19 | int main(void) 20 | { 21 | std::thread t1(func); 22 | std::thread t2(func); 23 | std::thread t3(func); 24 | 25 | t1.join(); 26 | t2.join(); 27 | t3.join(); 28 | 29 | system("pause"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-10/5-10.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-10/5-10.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-2/5-2.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-2/5-2.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-2/5-2/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Complex 6 | { 7 | std::mutex mutex; 8 | int i; 9 | 10 | Complex() : i(0){} 11 | 12 | void mul(int x) 13 | { 14 | std::lock_guard lock(mutex); 15 | i *= x; 16 | } 17 | 18 | void div(int x) 19 | { 20 | std::lock_guard lock(mutex); 21 | i /= x; 22 | } 23 | 24 | void both(int x, int y) 25 | { 26 | std::lock_guard lock(mutex); 27 | mul(x); 28 | div(y); 29 | } 30 | }; 31 | 32 | int main(void) 33 | { 34 | Complex complex; 35 | 36 | complex.both(32, 23); 37 | 38 | system("pause"); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-3/5-3.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-3/5-3.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-3/5-3/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-3/5-3/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-4/5-4.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-4/5-4.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-5/5-5.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-5/5-5.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-5/5-5/SyncQueue.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-5/5-5/SyncQueue.hpp -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-5/5-5/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "SyncQueue.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | SyncQueue syncQueue(5); 8 | 9 | void PutDatas() 10 | { 11 | for (int i = 0; i < 20; ++i) 12 | { 13 | syncQueue.Put(888); 14 | } 15 | } 16 | 17 | void TakeDatas() 18 | { 19 | int x = 0; 20 | 21 | for (int i = 0; i < 20; ++i) 22 | { 23 | syncQueue.Take(x); 24 | std::cout << x << std::endl; 25 | } 26 | } 27 | 28 | int main(void) 29 | { 30 | std::thread t1(PutDatas); 31 | std::thread t2(TakeDatas); 32 | 33 | t1.join(); 34 | t2.join(); 35 | 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-6/5-6.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-6/5-6.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-6/5-6/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "SimpleSyncQueue.hpp" 2 | 3 | #include 4 | #include 5 | 6 | SimpleSyncQueue syncQueue; 7 | 8 | void PutDatas() 9 | { 10 | for (int i = 0; i < 20; ++i) 11 | { 12 | syncQueue.Put(888); 13 | } 14 | } 15 | 16 | void TakeDatas() 17 | { 18 | int x = 0; 19 | 20 | for (int i = 0; i < 20; ++i) 21 | { 22 | syncQueue.Take(x); 23 | std::cout << x << std::endl; 24 | } 25 | } 26 | 27 | int main(void) 28 | { 29 | std::thread t1(PutDatas); 30 | std::thread t2(TakeDatas); 31 | 32 | t1.join(); 33 | t2.join(); 34 | 35 | system("pause"); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-7/5-7.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-7/5-7.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-7/5-7/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Counter 6 | { 7 | int value = 0; 8 | std::mutex mutex; 9 | 10 | void increment() 11 | { 12 | std::lock_guard lock(mutex); 13 | ++value; 14 | std::cout << value << std::endl; 15 | } 16 | 17 | void decrement() 18 | { 19 | std::lock_guard lock(mutex); 20 | --value; 21 | std::cout << value << std::endl; 22 | } 23 | 24 | int get() 25 | { 26 | return value; 27 | } 28 | }; 29 | 30 | Counter g_counter; 31 | 32 | void Increments() 33 | { 34 | for (int i = 0; i < 10; ++i) 35 | { 36 | g_counter.increment(); 37 | } 38 | } 39 | 40 | void Decrements() 41 | { 42 | for (int i = 0; i < 5; ++i) 43 | { 44 | g_counter.decrement(); 45 | } 46 | } 47 | 48 | int main(void) 49 | { 50 | std::thread t1(Increments); 51 | std::thread t2(Decrements); 52 | 53 | t1.join(); 54 | t2.join(); 55 | 56 | system("pause"); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-8/5-8.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-8/5-8.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-8/5-8/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Counter 6 | { 7 | std::atomic value = 0; 8 | 9 | void increment() 10 | { 11 | ++value; 12 | } 13 | 14 | void decrement() 15 | { 16 | --value; 17 | } 18 | 19 | int get() 20 | { 21 | return value; 22 | } 23 | }; 24 | 25 | Counter g_counter; 26 | 27 | void Increments() 28 | { 29 | for (int i = 0; i < 10; ++i) 30 | { 31 | g_counter.increment(); 32 | std::cout << g_counter.get() << std::endl; 33 | } 34 | } 35 | 36 | void Decrements() 37 | { 38 | for (int i = 0; i < 5; ++i) 39 | { 40 | g_counter.decrement(); 41 | std::cout << g_counter.get() << std::endl; 42 | } 43 | } 44 | 45 | int main(void) 46 | { 47 | std::thread t1(Increments); 48 | std::thread t2(Decrements); 49 | 50 | t1.join(); 51 | t2.join(); 52 | 53 | system("pause"); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-9/5-9.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-9/5-9.v12.suo -------------------------------------------------------------------------------- /In-depth_C++11/ch05-thread/5-9/5-9/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch05-thread/5-9/5-9/Test.cpp -------------------------------------------------------------------------------- /In-depth_C++11/ch06-chrono_and_convert/6-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch06-chrono_and_convert/6-1 -------------------------------------------------------------------------------- /In-depth_C++11/ch06-chrono_and_convert/6-2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch06-chrono_and_convert/6-2 -------------------------------------------------------------------------------- /In-depth_C++11/ch06-chrono_and_convert/6-3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | std::wstring str = L"我是中国人yzb"; 10 | std::wstring_convert> 11 | converter(new std::codecvt()); 12 | 13 | std::string narrowStr = converter.to_bytes(str); 14 | 15 | std::wstring wstr = converter.from_bytes(narrowStr); 16 | std::cout << narrowStr << std::endl; 17 | 18 | std::wcout.imbue(std::locale("chs")); //初始化cout为中文输出 19 | std::wcout << wstr << std::endl; 20 | 21 | system("pause"); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /In-depth_C++11/ch06-chrono_and_convert/6.1/Test.cc: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include "Timer.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void fun() 11 | { 12 | std::cout << "hello world" << std::endl; 13 | } 14 | 15 | void Test() 16 | { 17 | std::cout << "\nTest()\n"; 18 | 19 | Timer t; //开始计时 20 | fun(); 21 | 22 | std::cout << t.elapsed_seconds() << std::endl; //打印fun函数耗时多少秒 23 | std::cout << t.elapsed_nano() << std::endl; //打印纳秒 24 | std::cout << t.elapsed_micro() << std::endl; //打印微秒 25 | std::cout << t.elapsed() << std::endl; //打印毫秒 26 | std::cout << t.elapsed_seconds() << std::endl; //打印秒 27 | std::cout << t.elapsed_minutes() << std::endl; //打印分钟 28 | std::cout << t.elapsed_hours() << std::endl; //打印小时 29 | } 30 | 31 | 32 | int main(void) 33 | { 34 | Test(); 35 | 36 | system("pause"); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /In-depth_C++11/ch06-chrono_and_convert/6.1/Timer.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch06-chrono_and_convert/6.1/Timer.hpp -------------------------------------------------------------------------------- /In-depth_C++11/ch06-chrono_and_convert/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = 6-1 6-2 6-3 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 6-1: 6.1/Test.cc 17 | 18 | 6-2: 6-2.cc 19 | 20 | 6-3: CXXFLAGS += -Wall 21 | 6-3: 6-3.cc 22 | 23 | -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/8-3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch08-Pattern/8-3 -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/8-3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Singleton.h" 5 | 6 | using namespace std; 7 | 8 | struct A 9 | { 10 | A(const string&) { cout << "lvalue" <::Instance(str); 31 | 32 | Singleton::Instance(std::move(str)); 33 | 34 | Singleton::Instance(1,3.14); 35 | 36 | Singleton::GetInstance()->Fun(); 37 | 38 | 39 | // release 40 | Singleton::DestroyInstance(); 41 | Singleton::DestroyInstance(); 42 | Singleton::DestroyInstance(); 43 | } 44 | -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/8-5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/activeion/Books_SourceCode/7c6c23d4c22aa0ad3c8f01b0bba962527bdc599e/In-depth_C++11/ch08-Pattern/8-5 -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/8-5.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Observer.hpp" 3 | 4 | struct stA 5 | { 6 | int a, b; 7 | void print(int a, int b){std::cout << a << ", " << b << std::endl; } 8 | }; 9 | void print(int a, int b){std::cout << a << ", " << b << std::endl; } 10 | 11 | int main(void) 12 | { 13 | Events> myevent; 14 | 15 | auto key=myevent.Connect(print); 16 | stA t; 17 | auto lambdakey = myevent.Connect([&t](int a, int b) { t.a = a; t.b= b; }); 18 | // std::function registration 19 | std::functionf = std::bind(&stA::print, &t, std::placeholders::_1, std::placeholders::_2); 20 | myevent.Connect(f); 21 | int a = 1, b=2; 22 | myevent.Notify(a,b); 23 | myevent.Disconnect(key); 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/Command.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | template 4 | struct CommCommand 5 | { 6 | private: 7 | std::function < Ret()> m_f; 8 | 9 | public: 10 | //接受可调用对象的函数包装器 11 | template< class F, class... Args, class = typename std::enable_if::value>::type> 12 | void Wrap(F && f, Args && ... args) 13 | { 14 | m_f = [&]{return f(args...); }; 15 | } 16 | 17 | //接受常量成员函数的函数包装器 18 | template 19 | void Wrap(R(C::*f)(DArgs...) const, P && p, Args && ... args) 20 | { 21 | m_f = [&, f]{return (*p.*f)(args...); }; 22 | } 23 | 24 | //接受非常量成员函数的函数包装器 25 | template 26 | void Wrap(R(C::*f)(DArgs...), P && p, Args && ... args) 27 | { 28 | m_f = [&, f]{return (*p.*f)(args...); }; 29 | } 30 | 31 | Ret Excecute() 32 | { 33 | return m_f(); 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = 8-3 8-5 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | 8-3: 8-3.cc 18 | 19 | 8-5: CXXFLAGS += -Wall 20 | 8-5: 8-5.cc 21 | 22 | -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/NonCopyable.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class NonCopyable 3 | { 4 | public: 5 | NonCopyable(const NonCopyable&) = delete; // deleted 6 | NonCopyable& operator = (const NonCopyable&) = delete; // deleted 7 | NonCopyable() = default; // available 8 | }; 9 | -------------------------------------------------------------------------------- /In-depth_C++11/ch08-Pattern/Visitor.hpp: -------------------------------------------------------------------------------- 1 | template 2 | struct Visitor; 3 | 4 | template 5 | struct Visitor : Visitor 6 | { 7 | using Visitor::Visit;//通过using避免隐藏基类的visit同名方法 8 | virtual void Visit(const T&) = 0; 9 | }; 10 | 11 | template 12 | struct Visitor 13 | { 14 | virtual void Visit(const T&) = 0; 15 | }; 16 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = ch01 ch02 ch05 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | ch01: CXXFLAGS += -Wall 18 | ch01: ch01.cc 19 | 20 | ch02: CXXFLAGS += -DNDEBUG #使用sleep函数不得不让assert失效 21 | ch02: ch02.cc 22 | 23 | ch05: ch05.cc 24 | 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch06_sudoku/README: -------------------------------------------------------------------------------- 1 | batch.cc muduo客户端 sudoku_client_batch req.txt localhost 18 2 | pipeline.cc muduo客户端 sudoku_client_pipeline req.txt localhost 18 3 | 4 | 5 | server_basic.cc muduo单线程 6 | server_threadpool.cc muduo IO单线程 + threadpool计算多线程 7 | server_multiloop.cc muduo Acceptor单线程 + IO多线程 8 | server_hybrid.cc muduo Acceptor单线程 + IO多线程 + threadpool计算多线程 9 | server_prod.cc 改进server_hybrid.cc: 高低水位 10 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch06_sudoku/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -x 4 | 5 | SOURCE_DIR=`pwd` 6 | BUILD_DIR=${BUILD_DIR:-./build} 7 | BUILD_TYPE=${BUILD_TYPE:-debug} 8 | INSTALL_DIR=${INSTALL_DIR:-../${BUILD_TYPE}-install-cpp11} 9 | BUILD_NO_EXAMPLES=${BUILD_NO_EXAMPLES:-0} 10 | 11 | mkdir -p $BUILD_DIR/$BUILD_TYPE-cpp11 \ 12 | && cd $BUILD_DIR/$BUILD_TYPE-cpp11 \ 13 | && cp ../../req.txt . \ 14 | && cmake \ 15 | -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ 16 | -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \ 17 | -DCMAKE_BUILD_NO_EXAMPLES=$BUILD_NO_EXAMPLES \ 18 | $SOURCE_DIR \ 19 | && make $* 20 | 21 | # Use the following command to run all the unit tests 22 | # at the dir $BUILD_DIR/$BUILD_TYPE : 23 | # CTEST_OUTPUT_ON_FAILURE=TRUE make test 24 | 25 | # cd $SOURCE_DIR && doxygen 26 | 27 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch06_sudoku/req.txt: -------------------------------------------------------------------------------- 1 | 000000010400000000020000000000050407008000300001090000300400200050100000000806000 2 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch06_sudoku/sudoku.h: -------------------------------------------------------------------------------- 1 | #ifndef MUDUO_EXAMPLES_SUDOKU_SUDOKU_H 2 | #define MUDUO_EXAMPLES_SUDOKU_SUDOKU_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | muduo::string solveSudoku(const muduo::StringPiece& puzzle); 9 | const int kCells = 81; 10 | extern const char kNoSolution[]; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/Makefile: -------------------------------------------------------------------------------- 1 | SUBDIRS = s00_EventLoop \ 2 | s01_timerfd \ 3 | s00_TimerQueue \ 4 | s03_runInLoop \ 5 | s04_Acceptor \ 6 | s05_TcpConnection \ 7 | s06_TcpConnection \ 8 | s07_Buffer \ 9 | s08_send \ 10 | s09_SIGPIPE \ 11 | s10_multithread \ 12 | s11_Connector \ 13 | s12_TcpClient \ 14 | s13_epoll 15 | 16 | all: $(SUBDIRS) 17 | 18 | clean: 19 | for d in $(SUBDIRS); do cd $$d && make clean && cd ..; done 20 | 21 | $(SUBDIRS): 22 | cd $@ && make 23 | 24 | .PHONY: all clean $(SUBDIRS) 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/mkdiff.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 产生命令文本的脚本, 列出所有的需要diff文件列表 4 | # 使用方法./mkdiff.sh 5 | 6 | diff_a_file() 7 | { 8 | diff $1 $2 -q |grep differ |grep -v Makefile|grep -v un~ \ 9 | |awk '{\ 10 | split($2, old, "/"); \ 11 | split($4, new, "/"); \ 12 | print "diff -U200", $2, $4, "| awk \" NR>3 {print}\" >", new[1]"-"old[1]"-"new[2]".diff"\ 13 | }' 14 | echo 15 | } 16 | 17 | diff_a_file s00 s01 18 | diff_a_file s01 s02 19 | diff_a_file s02 s03 20 | diff_a_file s03 s04 21 | diff_a_file s04 s05 22 | diff_a_file s05 s06 23 | diff_a_file s06 s07 24 | diff_a_file s07 s08 25 | diff_a_file s08 s09 26 | diff_a_file s09 s10 27 | diff_a_file s10 s11 28 | diff_a_file s11 s12 29 | diff_a_file s12 s13 30 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s00_EventLoop/EventLoop.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOP_H 9 | #define MUDUO_NET_EVENTLOOP_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class EventLoop : muduo::noncopyable 17 | { 18 | public: 19 | 20 | EventLoop(); 21 | ~EventLoop(); 22 | 23 | void loop(); 24 | 25 | void assertInLoopThread() 26 | { 27 | if (!isInLoopThread()) 28 | { 29 | abortNotInLoopThread(); 30 | } 31 | } 32 | 33 | bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); } 34 | 35 | private: 36 | 37 | void abortNotInLoopThread(); 38 | 39 | bool looping_; /* atomic */ 40 | const pid_t threadId_; 41 | }; 42 | 43 | } 44 | 45 | #endif // MUDUO_NET_EVENTLOOP_H 46 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s00_EventLoop/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc # s00 5 | BINARIES = test1 test2 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | 18 | test1: test1.cc 19 | test2: test2.cc 20 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s00_EventLoop/test1.cc: -------------------------------------------------------------------------------- 1 | #include "EventLoop.h" 2 | #include 3 | #include 4 | #include // getpid() 5 | 6 | void threadFunc() 7 | { 8 | printf("threadFunc(): pid = %d, tid = %d\n", 9 | getpid(), muduo::CurrentThread::tid()); 10 | 11 | muduo::EventLoop loop; 12 | loop.loop(); 13 | } 14 | 15 | int main() 16 | { 17 | printf("main(): pid = %d, tid = %d\n", 18 | getpid(), muduo::CurrentThread::tid()); 19 | 20 | muduo::EventLoop loop; 21 | 22 | muduo::Thread thread(threadFunc); 23 | thread.start(); 24 | 25 | loop.loop(); 26 | pthread_exit(NULL); 27 | } 28 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s00_EventLoop/test2.cc: -------------------------------------------------------------------------------- 1 | #include "EventLoop.h" 2 | #include 3 | 4 | muduo::EventLoop* g_loop; 5 | 6 | void threadFunc() 7 | { 8 | g_loop->loop(); 9 | } 10 | 11 | int main() 12 | { 13 | muduo::EventLoop loop; 14 | g_loop = &loop; 15 | muduo::Thread t(threadFunc); 16 | t.start(); 17 | t.join(); 18 | } 19 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s01_timerfd/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc # s01 6 | BINARIES = test3 7 | HEADERS=$(wildcard *.h) 8 | 9 | all: $(BINARIES) 10 | $(BINARIES): $(HEADERS) 11 | $(BINARIES): 12 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 13 | 14 | clean: 15 | rm -f $(BINARIES) core 16 | 17 | test3: test3.cc # s01 18 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s01_timerfd/test3.cc: -------------------------------------------------------------------------------- 1 | #include "Channel.h" 2 | #include "EventLoop.h" 3 | 4 | #include 5 | #include 6 | #include //bzero() 7 | 8 | muduo::EventLoop* g_loop; 9 | 10 | void timeout() 11 | { 12 | printf("Timeout!\n"); 13 | g_loop->quit(); 14 | } 15 | 16 | int main() 17 | { 18 | muduo::EventLoop loop; 19 | g_loop = &loop; 20 | 21 | int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 22 | muduo::Channel channel(&loop, timerfd);//每一个chn都属于一个特定的loop, 对应于一个特定的fd 23 | channel.setReadCallback(timeout); 24 | channel.enableReading();//将本chn加入到Poller::channels_,对应的fd加入到Poller::pollfds_ 25 | 26 | struct itimerspec howlong; 27 | bzero(&howlong, sizeof howlong); 28 | howlong.it_value.tv_sec = 5; 29 | ::timerfd_settime(timerfd, 0, &howlong, NULL); 30 | 31 | loop.loop();//每次::poll()之前, EventLoop::activeChannels_被清零, ::poll()之后调用fillActiveChannel() 32 | 33 | ::close(timerfd); 34 | } 35 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s02_TimerQueue/Callbacks.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_CALLBACKS_H 9 | #define MUDUO_NET_CALLBACKS_H 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | namespace muduo 17 | { 18 | 19 | // All client visible callbacks go here. 20 | 21 | typedef boost::function TimerCallback; 22 | 23 | } 24 | 25 | #endif // MUDUO_NET_CALLBACKS_H 26 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s02_TimerQueue/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc # s02 7 | BINARIES = test4 8 | HEADERS=$(wildcard *.h) 9 | 10 | all: $(BINARIES) 11 | $(BINARIES): $(HEADERS) 12 | $(BINARIES): 13 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 14 | 15 | clean: 16 | rm -f $(BINARIES) core 17 | 18 | 19 | test4: test4.cc # s02 20 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s02_TimerQueue/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s02_TimerQueue/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s03_runInLoop/Callbacks.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_CALLBACKS_H 9 | #define MUDUO_NET_CALLBACKS_H 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | namespace muduo 17 | { 18 | 19 | // All client visible callbacks go here. 20 | 21 | typedef boost::function TimerCallback; 22 | 23 | } 24 | 25 | #endif // MUDUO_NET_CALLBACKS_H 26 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s03_runInLoop/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOPTHREAD_H 9 | #define MUDUO_NET_EVENTLOOPTHREAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace muduo 18 | { 19 | 20 | class EventLoop; 21 | 22 | class EventLoopThread : muduo::noncopyable 23 | { 24 | public: 25 | EventLoopThread(); 26 | ~EventLoopThread(); 27 | EventLoop* startLoop(); 28 | 29 | private: 30 | void threadFunc(); 31 | 32 | EventLoop* loop_; 33 | bool exiting_; 34 | Thread thread_; 35 | MutexLock mutex_; 36 | Condition cond_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_EVENTLOOPTHREAD_H 42 | 43 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s03_runInLoop/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc # s03 7 | BINARIES = test5 test6 8 | HEADERS=$(wildcard *.h) 9 | 10 | all: $(BINARIES) 11 | $(BINARIES): $(HEADERS) 12 | $(BINARIES): 13 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 14 | 15 | clean: 16 | rm -f $(BINARIES) core 17 | 18 | 19 | 20 | test1: test1.cc 21 | test2: test2.cc 22 | 23 | test3: test3.cc 24 | 25 | test4: test4.cc 26 | 27 | test5: test5.cc # s03 28 | test6: test6.cc # s03 29 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s03_runInLoop/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s03_runInLoop/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s04_Acceptor/Callbacks.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_CALLBACKS_H 9 | #define MUDUO_NET_CALLBACKS_H 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | namespace muduo 17 | { 18 | 19 | // All client visible callbacks go here. 20 | 21 | typedef boost::function TimerCallback; 22 | 23 | } 24 | 25 | #endif // MUDUO_NET_CALLBACKS_H 26 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s04_Acceptor/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOPTHREAD_H 9 | #define MUDUO_NET_EVENTLOOPTHREAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace muduo 18 | { 19 | 20 | class EventLoop; 21 | 22 | class EventLoopThread : muduo::noncopyable 23 | { 24 | public: 25 | EventLoopThread(); 26 | ~EventLoopThread(); 27 | EventLoop* startLoop(); 28 | 29 | private: 30 | void threadFunc(); 31 | 32 | EventLoop* loop_; 33 | bool exiting_; 34 | Thread thread_; 35 | MutexLock mutex_; 36 | Condition cond_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_EVENTLOOPTHREAD_H 42 | 43 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s04_Acceptor/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc # s04 8 | BINARIES = test7 9 | HEADERS=$(wildcard *.h) 10 | 11 | all: $(BINARIES) 12 | $(BINARIES): $(HEADERS) 13 | $(BINARIES): 14 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 15 | 16 | clean: 17 | rm -f $(BINARIES) core 18 | 19 | test7: test7.cc # s04 20 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s04_Acceptor/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s04_Acceptor/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s04_Acceptor/test7.cc: -------------------------------------------------------------------------------- 1 | #include "Acceptor.h" 2 | #include "EventLoop.h" 3 | #include "InetAddress.h" 4 | #include "SocketsOps.h" 5 | #include 6 | 7 | void newConnection(int sockfd, const muduo::InetAddress& peerAddr) 8 | { 9 | printf("newConnection(): accepted a new connection from %s\n", 10 | peerAddr.toHostPort().c_str()); 11 | ::write(sockfd, "How are you?\n", 13); 12 | muduo::sockets::close(sockfd); 13 | } 14 | 15 | int main() 16 | { 17 | printf("main(): pid = %d\n", getpid()); 18 | 19 | muduo::InetAddress listenAddr(9981); 20 | muduo::EventLoop loop; 21 | 22 | muduo::Acceptor acceptor(&loop, listenAddr); 23 | acceptor.setNewConnectionCallback(newConnection); 24 | acceptor.listen(); 25 | 26 | loop.loop(); 27 | } 28 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s05_TcpConnection/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 8 | TcpConnection.cc TcpServer.cc # s05 9 | BINARIES = test8 10 | HEADERS=$(wildcard *.h) 11 | 12 | all: $(BINARIES) 13 | $(BINARIES): $(HEADERS) 14 | $(BINARIES): 15 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 16 | 17 | clean: 18 | rm -f $(BINARIES) core 19 | 20 | test8: test8.cc # s05 21 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s05_TcpConnection/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s05_TcpConnection/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s06_TcpConnection/Makefile: -------------------------------------------------------------------------------- 1 | #make -B 强制重新make 2 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 3 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 4 | BASE_SRC = 5 | LIB_SRC = EventLoop.cc \ 6 | Poller.cc Channel.cc \ 7 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 8 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 9 | TcpConnection.cc TcpServer.cc # s05 s06 10 | BINARIES = test8 11 | HEADERS=$(wildcard *.h) 12 | 13 | all: $(BINARIES) 14 | $(BINARIES): $(HEADERS) 15 | $(BINARIES): 16 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 17 | 18 | clean: 19 | rm -f $(BINARIES) core 20 | 21 | test8: test8.cc 22 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s06_TcpConnection/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s06_TcpConnection/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s07_Buffer/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOPTHREAD_H 9 | #define MUDUO_NET_EVENTLOOPTHREAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace muduo 18 | { 19 | 20 | class EventLoop; 21 | 22 | class EventLoopThread : muduo::noncopyable 23 | { 24 | public: 25 | EventLoopThread(); 26 | ~EventLoopThread(); 27 | EventLoop* startLoop(); 28 | 29 | private: 30 | void threadFunc(); 31 | 32 | EventLoop* loop_; 33 | bool exiting_; 34 | Thread thread_; 35 | MutexLock mutex_; 36 | Condition cond_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_EVENTLOOPTHREAD_H 42 | 43 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s07_Buffer/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 8 | TcpConnection.cc TcpServer.cc \ 9 | Buffer.cc # s07 10 | BINARIES = test3 test8 11 | HEADERS=$(wildcard *.h) 12 | 13 | all: $(BINARIES) 14 | $(BINARIES): $(HEADERS) 15 | $(BINARIES): 16 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 17 | 18 | clean: 19 | rm -f $(BINARIES) core 20 | 21 | 22 | 23 | test3: test3.cc 24 | test8: test8.cc 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s07_Buffer/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s07_Buffer/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s07_Buffer/test3.cc: -------------------------------------------------------------------------------- 1 | #include "Channel.h" 2 | #include "EventLoop.h" 3 | 4 | #include 5 | #include 6 | 7 | muduo::EventLoop* g_loop; 8 | 9 | void timeout(muduo::Timestamp receiveTime) 10 | { 11 | printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 12 | g_loop->quit(); 13 | } 14 | 15 | int main() 16 | { 17 | printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 18 | muduo::EventLoop loop; 19 | g_loop = &loop; 20 | 21 | int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 22 | muduo::Channel channel(&loop, timerfd); 23 | channel.setReadCallback(timeout); 24 | channel.enableReading(); 25 | 26 | struct itimerspec howlong; 27 | bzero(&howlong, sizeof howlong); 28 | howlong.it_value.tv_sec = 5; 29 | ::timerfd_settime(timerfd, 0, &howlong, NULL); 30 | 31 | loop.loop(); 32 | 33 | ::close(timerfd); 34 | } 35 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s08_send/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOPTHREAD_H 9 | #define MUDUO_NET_EVENTLOOPTHREAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace muduo 18 | { 19 | 20 | class EventLoop; 21 | 22 | class EventLoopThread : muduo::noncopyable 23 | { 24 | public: 25 | EventLoopThread(); 26 | ~EventLoopThread(); 27 | EventLoop* startLoop(); 28 | 29 | private: 30 | void threadFunc(); 31 | 32 | EventLoop* loop_; 33 | bool exiting_; 34 | Thread thread_; 35 | MutexLock mutex_; 36 | Condition cond_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_EVENTLOOPTHREAD_H 42 | 43 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s08_send/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 8 | TcpConnection.cc TcpServer.cc \ 9 | Buffer.cc # s07 10 | BINARIES = test9 test10 11 | HEADERS=$(wildcard *.h) 12 | 13 | all: $(BINARIES) 14 | $(BINARIES): $(HEADERS) 15 | $(BINARIES): 16 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 17 | 18 | clean: 19 | rm -f $(BINARIES) core 20 | 21 | test9: test9.cc 22 | test10: test10.cc 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s08_send/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s08_send/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s09_SIGPIPE/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOPTHREAD_H 9 | #define MUDUO_NET_EVENTLOOPTHREAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace muduo 18 | { 19 | 20 | class EventLoop; 21 | 22 | class EventLoopThread : muduo::noncopyable 23 | { 24 | public: 25 | EventLoopThread(); 26 | ~EventLoopThread(); 27 | EventLoop* startLoop(); 28 | 29 | private: 30 | void threadFunc(); 31 | 32 | EventLoop* loop_; 33 | bool exiting_; 34 | Thread thread_; 35 | MutexLock mutex_; 36 | Condition cond_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_EVENTLOOPTHREAD_H 42 | 43 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s09_SIGPIPE/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 8 | TcpConnection.cc TcpServer.cc \ 9 | Buffer.cc # s07 10 | BINARIES = test9 test10 test11 11 | HEADERS=$(wildcard *.h) 12 | 13 | all: $(BINARIES) 14 | $(BINARIES): $(HEADERS) 15 | $(BINARIES): 16 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 17 | 18 | clean: 19 | rm -f $(BINARIES) core 20 | 21 | 22 | 23 | test9: test9.cc 24 | test10: test10.cc 25 | test11: test11.cc 26 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s09_SIGPIPE/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s09_SIGPIPE/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s10_multithread/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 8 | TcpConnection.cc TcpServer.cc \ 9 | Buffer.cc \ 10 | EventLoopThreadPool.cc # s10 11 | BINARIES = test8 test9 test10 test11 12 | HEADERS=$(wildcard *.h) 13 | 14 | all: $(BINARIES) 15 | $(BINARIES): $(HEADERS) 16 | $(BINARIES): 17 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 18 | 19 | clean: 20 | rm -f $(BINARIES) core 21 | 22 | 23 | 24 | test8: test8.cc 25 | test9: test9.cc 26 | test10: test10.cc 27 | test11: test11.cc 28 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s10_multithread/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | void Timer::restart(Timestamp now) 13 | { 14 | if (repeat_) 15 | { 16 | expiration_ = addTime(now, interval_); 17 | } 18 | else 19 | { 20 | expiration_ = Timestamp::invalid(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s10_multithread/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | explicit TimerId(Timer* timer) 25 | : value_(timer) 26 | { 27 | } 28 | 29 | // default copy-ctor, dtor and assignment are okay 30 | 31 | private: 32 | Timer* value_; 33 | }; 34 | 35 | } 36 | 37 | #endif // MUDUO_NET_TIMERID_H 38 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s11_Connector/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOPTHREAD_H 9 | #define MUDUO_NET_EVENTLOOPTHREAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace muduo 18 | { 19 | 20 | class EventLoop; 21 | 22 | class EventLoopThread : muduo::noncopyable 23 | { 24 | public: 25 | EventLoopThread(); 26 | ~EventLoopThread(); 27 | EventLoop* startLoop(); 28 | 29 | private: 30 | void threadFunc(); 31 | 32 | EventLoop* loop_; 33 | bool exiting_; 34 | Thread thread_; 35 | MutexLock mutex_; 36 | Condition cond_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_EVENTLOOPTHREAD_H 42 | 43 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s11_Connector/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 8 | TcpConnection.cc TcpServer.cc \ 9 | Buffer.cc \ 10 | EventLoopThreadPool.cc \ 11 | Connector.cc # s11/test12.cc 12 | 13 | BINARIES = test4 test12 14 | HEADERS=$(wildcard *.h) 15 | 16 | all: $(BINARIES) 17 | $(BINARIES): $(HEADERS) 18 | $(BINARIES): 19 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 20 | 21 | clean: 22 | rm -f $(BINARIES) core 23 | 24 | test4: test4.cc 25 | test12: test12.cc 26 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s11_Connector/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | AtomicInt64 Timer::s_numCreated_; 13 | 14 | void Timer::restart(Timestamp now) 15 | { 16 | if (repeat_) 17 | { 18 | expiration_ = addTime(now, interval_); 19 | } 20 | else 21 | { 22 | expiration_ = Timestamp::invalid(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s11_Connector/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | TimerId(Timer* timer = NULL, int64_t seq = 0) 25 | : timer_(timer), 26 | sequence_(seq) 27 | { 28 | } 29 | 30 | // default copy-ctor, dtor and assignment are okay 31 | 32 | friend class TimerQueue; 33 | 34 | private: 35 | Timer* timer_; 36 | int64_t sequence_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_TIMERID_H 42 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s11_Connector/test12.cc: -------------------------------------------------------------------------------- 1 | #include "Connector.h" 2 | #include "EventLoop.h" 3 | 4 | #include 5 | 6 | muduo::EventLoop* g_loop; 7 | 8 | void connectCallback(int sockfd) 9 | { 10 | printf("connected.\n"); 11 | g_loop->quit(); 12 | } 13 | 14 | int main(int argc, char* argv[]) 15 | { 16 | muduo::EventLoop loop; 17 | g_loop = &loop; 18 | muduo::InetAddress addr("127.0.0.1", 9981); 19 | muduo::ConnectorPtr connector(new muduo::Connector(&loop, addr)); 20 | connector->setNewConnectionCallback(connectCallback); 21 | connector->start(); 22 | 23 | loop.loop(); 24 | } 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s12_TcpClient/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g -pthread #-I ../.. -g3可以让gdb展开宏, print命令 2 | LDFLAGS = -lpthread -lmuduo_net -lmuduo_base 3 | BASE_SRC = 4 | LIB_SRC = EventLoop.cc \ 5 | Poller.cc Channel.cc \ 6 | Timer.cc TimerQueue.cc EventLoopThread.cc \ 7 | Acceptor.cc Socket.cc SocketsOps.cc InetAddress.cc \ 8 | TcpConnection.cc TcpServer.cc \ 9 | Buffer.cc \ 10 | EventLoopThreadPool.cc \ 11 | Connector.cc \ 12 | TcpClient.cc # s12 13 | BINARIES = test13 14 | HEADERS=$(wildcard *.h) 15 | 16 | all: $(BINARIES) 17 | $(BINARIES): $(HEADERS) 18 | $(BINARIES): 19 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 20 | 21 | clean: 22 | rm -f $(BINARIES) core 23 | 24 | test13: test13.cc 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s12_TcpClient/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | AtomicInt64 Timer::s_numCreated_; 13 | 14 | void Timer::restart(Timestamp now) 15 | { 16 | if (repeat_) 17 | { 18 | expiration_ = addTime(now, interval_); 19 | } 20 | else 21 | { 22 | expiration_ = Timestamp::invalid(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s12_TcpClient/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | TimerId(Timer* timer = NULL, int64_t seq = 0) 25 | : timer_(timer), 26 | sequence_(seq) 27 | { 28 | } 29 | 30 | // default copy-ctor, dtor and assignment are okay 31 | 32 | friend class TimerQueue; 33 | 34 | private: 35 | Timer* timer_; 36 | int64_t sequence_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_TIMERID_H 42 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_EVENTLOOPTHREAD_H 9 | #define MUDUO_NET_EVENTLOOPTHREAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace muduo 18 | { 19 | 20 | class EventLoop; 21 | 22 | class EventLoopThread : muduo::noncopyable 23 | { 24 | public: 25 | EventLoopThread(); 26 | ~EventLoopThread(); 27 | EventLoop* startLoop(); 28 | 29 | private: 30 | void threadFunc(); 31 | 32 | EventLoop* loop_; 33 | bool exiting_; 34 | Thread thread_; 35 | MutexLock mutex_; 36 | Condition cond_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_EVENTLOOPTHREAD_H 42 | 43 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/Timer.cc: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #include "Timer.h" 9 | 10 | using namespace muduo; 11 | 12 | AtomicInt64 Timer::s_numCreated_; 13 | 14 | void Timer::restart(Timestamp now) 15 | { 16 | if (repeat_) 17 | { 18 | expiration_ = addTime(now, interval_); 19 | } 20 | else 21 | { 22 | expiration_ = Timestamp::invalid(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/TimerId.h: -------------------------------------------------------------------------------- 1 | // excerpts from http://code.google.com/p/muduo/ 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the License file. 5 | // 6 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 7 | 8 | #ifndef MUDUO_NET_TIMERID_H 9 | #define MUDUO_NET_TIMERID_H 10 | 11 | #include 12 | 13 | namespace muduo 14 | { 15 | 16 | class Timer; 17 | 18 | /// 19 | /// An opaque identifier, for canceling Timer. 20 | /// 21 | class TimerId : public muduo::copyable 22 | { 23 | public: 24 | TimerId(Timer* timer = NULL, int64_t seq = 0) 25 | : timer_(timer), 26 | sequence_(seq) 27 | { 28 | } 29 | 30 | // default copy-ctor, dtor and assignment are okay 31 | 32 | friend class TimerQueue; 33 | 34 | private: 35 | Timer* timer_; 36 | int64_t sequence_; 37 | }; 38 | 39 | } 40 | 41 | #endif // MUDUO_NET_TIMERID_H 42 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/test1.cc: -------------------------------------------------------------------------------- 1 | #include "EventLoop.h" 2 | #include 3 | #include 4 | 5 | void threadFunc() 6 | { 7 | printf("threadFunc(): pid = %d, tid = %d\n", 8 | getpid(), muduo::CurrentThread::tid()); 9 | 10 | muduo::EventLoop loop; 11 | loop.loop(); 12 | } 13 | 14 | int main() 15 | { 16 | printf("main(): pid = %d, tid = %d\n", 17 | getpid(), muduo::CurrentThread::tid()); 18 | 19 | muduo::EventLoop loop; 20 | 21 | muduo::Thread thread(threadFunc); 22 | thread.start(); 23 | 24 | loop.loop(); 25 | pthread_exit(NULL); 26 | } 27 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/test12.cc: -------------------------------------------------------------------------------- 1 | #include "Connector.h" 2 | #include "EventLoop.h" 3 | 4 | #include 5 | 6 | muduo::EventLoop* g_loop; 7 | 8 | void connectCallback(int sockfd) 9 | { 10 | printf("connected.\n"); 11 | g_loop->quit(); 12 | } 13 | 14 | int main(int argc, char* argv[]) 15 | { 16 | muduo::EventLoop loop; 17 | g_loop = &loop; 18 | muduo::InetAddress addr("127.0.0.1", 9981); 19 | muduo::ConnectorPtr connector(new muduo::Connector(&loop, addr)); 20 | connector->setNewConnectionCallback(connectCallback); 21 | connector->start(); 22 | 23 | loop.loop(); 24 | } 25 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/test2.cc: -------------------------------------------------------------------------------- 1 | #include "EventLoop.h" 2 | #include 3 | 4 | muduo::EventLoop* g_loop; 5 | 6 | void threadFunc() 7 | { 8 | g_loop->loop(); 9 | } 10 | 11 | int main() 12 | { 13 | muduo::EventLoop loop; 14 | g_loop = &loop; 15 | muduo::Thread t(threadFunc); 16 | t.start(); 17 | t.join(); 18 | } 19 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/test3.cc: -------------------------------------------------------------------------------- 1 | #include "Channel.h" 2 | #include "EventLoop.h" 3 | 4 | #include 5 | #include 6 | 7 | muduo::EventLoop* g_loop; 8 | 9 | void timeout(muduo::Timestamp receiveTime) 10 | { 11 | printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 12 | g_loop->quit(); 13 | } 14 | 15 | int main() 16 | { 17 | printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 18 | muduo::EventLoop loop; 19 | g_loop = &loop; 20 | 21 | int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 22 | muduo::Channel channel(&loop, timerfd); 23 | channel.setReadCallback(timeout); 24 | channel.enableReading(); 25 | 26 | struct itimerspec howlong; 27 | bzero(&howlong, sizeof howlong); 28 | howlong.it_value.tv_sec = 5; 29 | ::timerfd_settime(timerfd, 0, &howlong, NULL); 30 | 31 | loop.loop(); 32 | 33 | ::close(timerfd); 34 | } 35 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/test6.cc: -------------------------------------------------------------------------------- 1 | #include "EventLoop.h" 2 | #include "EventLoopThread.h" 3 | #include 4 | 5 | void runInThread() 6 | { 7 | printf("runInThread(): pid = %d, tid = %d\n", 8 | getpid(), muduo::CurrentThread::tid()); 9 | } 10 | 11 | int main() 12 | { 13 | printf("main(): pid = %d, tid = %d\n", 14 | getpid(), muduo::CurrentThread::tid()); 15 | 16 | muduo::EventLoopThread loopThread; 17 | muduo::EventLoop* loop = loopThread.startLoop(); 18 | loop->runInLoop(runInThread); 19 | sleep(1); 20 | loop->runAfter(2, runInThread); 21 | sleep(3); 22 | loop->quit(); 23 | 24 | printf("exit main().\n"); 25 | } 26 | -------------------------------------------------------------------------------- /Muduo_ChenShuo/ch08_reactor/s13_epoll/test7.cc: -------------------------------------------------------------------------------- 1 | #include "Acceptor.h" 2 | #include "EventLoop.h" 3 | #include "InetAddress.h" 4 | #include "SocketsOps.h" 5 | #include 6 | 7 | void newConnection(int sockfd, const muduo::InetAddress& peerAddr) 8 | { 9 | printf("newConnection(): accepted a new connection from %s\n", 10 | peerAddr.toHostPort().c_str()); 11 | ::write(sockfd, "How are you?\n", 13); 12 | muduo::sockets::close(sockfd); 13 | } 14 | 15 | int main() 16 | { 17 | printf("main(): pid = %d\n", getpid()); 18 | 19 | muduo::InetAddress listenAddr(9981); 20 | muduo::EventLoop loop; 21 | 22 | muduo::Acceptor acceptor(&loop, listenAddr); 23 | acceptor.setNewConnectionCallback(newConnection); 24 | acceptor.listen(); 25 | 26 | loop.loop(); 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Books_SourceCode 2 | 3 | SourceCode collection for Programming Books 4 | 5 | - Effective_Modern_Cpp 《Effective Modern C++》 Chinese version and source code 6 | - Using_STL 《Using the C++ Standard Template Libraries》 中文名《C++标准模板库编程实践》 源码 7 | -------------------------------------------------------------------------------- /Using_STL/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ build active file", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-g", 9 | "${file}", 10 | "-o", 11 | "${fileDirname}/${fileBasenameNoExtension}" 12 | ], 13 | "options": { 14 | "cwd": "/usr/bin" 15 | }, 16 | "problemMatcher": [ 17 | "$gcc" 18 | ], 19 | "group": { 20 | "kind": "build", 21 | "isDefault": true 22 | }, 23 | "detail": "Generated task by Debugger" 24 | } 25 | ], 26 | "version": "2.0.0" 27 | } -------------------------------------------------------------------------------- /Using_STL/Checkout/Checkout_object/Customer.h: -------------------------------------------------------------------------------- 1 | // Defines a customer by their time to checkout 2 | #ifndef CUSTOMER_H 3 | #define CUSTOMER_H 4 | 5 | class Customer 6 | { 7 | protected: 8 | size_t service_t {}; // Time to checkout 9 | public: 10 | //explicit Customer(size_t st = 10) :service_t {st}{} 11 | explicit Customer(size_t st = 10) { 12 | service_t = st; 13 | } 14 | 15 | // Decrement time remaining to checkout 16 | Customer& time_decrement() 17 | { 18 | if (service_t > 0) 19 | --service_t; 20 | return *this; 21 | } 22 | 23 | bool done() const { return service_t == 0; } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Using_STL/Checkout/Checkout_up/Customer.h: -------------------------------------------------------------------------------- 1 | // Defines a customer by their time to checkout 2 | #ifndef CUSTOMER_H 3 | #define CUSTOMER_H 4 | 5 | class Customer 6 | { 7 | private: 8 | size_t checkout_t_ {}; // Time to checkout 9 | 10 | public: 11 | explicit Customer(size_t st = 10) :checkout_t_{st}{} 12 | 13 | // Decrement time remaining to checkout 14 | Customer& time_decrement() 15 | { 16 | --checkout_t_; 17 | return *this; 18 | } 19 | bool done() { return checkout_t_ == 0; } 20 | }; 21 | #endif 22 | -------------------------------------------------------------------------------- /Using_STL/Checkout/Checkout_up/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. # -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = # -lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = code 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | code: CXXFLAGS += -Wall 17 | code: code.cc 18 | -------------------------------------------------------------------------------- /Using_STL/Checkout/customer.h: -------------------------------------------------------------------------------- 1 | // Defines a customer by their time to checkout 2 | #ifndef CUSTOMER_H 3 | #define CUSTOMER_H 4 | 5 | class Customer 6 | { 7 | private: 8 | size_t checkout_time_; // Time to checkout 9 | bool checking_; 10 | size_t line_time_; 11 | 12 | public: 13 | explicit Customer(size_t ct = 10) :checkout_time_(ct), line_time_{0}, checking_(false){} 14 | 15 | bool set_checking(bool flag) { bool old = checking_; checking_ = flag; return old; } 16 | size_t line_time() {return line_time_;} 17 | // Decrement time remaining to checkout 18 | Customer& timeTicks() 19 | { 20 | if(checking_) 21 | --checkout_time_; 22 | ++line_time_; 23 | return *this; 24 | } 25 | bool Done() { return checkout_time_ == 0; } 26 | }; 27 | #endif 28 | -------------------------------------------------------------------------------- /Using_STL/Checkout/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "customer.h" 4 | #include "checkout.h" 5 | 6 | int main(void) 7 | { 8 | Checkout c; 9 | 10 | for(int i=0; i<5; ++i) 11 | c.addCustomer(std::make_unique(2)); 12 | 13 | for(int i=0; i< 100; ++i) { 14 | if(!i%3) c.addCustomer(std::make_unique(2)); 15 | c.timeTicks(); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Using_STL/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -O0 -g3 -I ../.. # -pthread #-g3可以让gdb展开宏, print命令 2 | LDFLAGS = #-lpthread 3 | BASE_SRC = 4 | LIB_SRC = 5 | BINARIES = ch01 ch06 ch07 ch09 ch10 6 | HEADERS=$(wildcard *.h) 7 | 8 | all: $(BINARIES) 9 | $(BINARIES): $(HEADERS) 10 | $(BINARIES): 11 | g++ $(CXXFLAGS) -o $@ $(LIB_SRC) $(BASE_SRC) $(filter %.cc,$^) $(LDFLAGS) 12 | 13 | clean: 14 | rm -f $(BINARIES) core 15 | 16 | 17 | ch01: CXXFLAGS += -Wall 18 | ch01: ch01.cc 19 | 20 | ch06: ch06.cc 21 | 22 | ch07: ch07.cc 23 | 24 | ch09: ch09.cc 25 | 26 | ch09_cin: ch09_cin.cc 27 | 28 | ch10: ch10.cc 29 | 30 | ch10_gaussion: ch10_gaussion.cc 31 | -------------------------------------------------------------------------------- /Using_STL/data/Renewables_vs_kwh_cost.txt: -------------------------------------------------------------------------------- 1 | Austria 353 20.21 2 | Belgium 452 20.97 3 | Denmark 956 30.42 4 | Croatia 89 13.12 5 | Finland 114 15.63 6 | Germany 969 29.81 7 | Greece 424 17.67 8 | Hungary 37 12.02 9 | Ireland 483 24.07 10 | Italy 453 24.46 11 | Netherland 234 18.21 12 | Poland 100 14.21 13 | Portuga1 517 21.75 14 | Spain 596 22.52 15 | Sweden 561 19.67 16 | UK 257 19.18 17 | -------------------------------------------------------------------------------- /Using_STL/data/fibonacci.txt: -------------------------------------------------------------------------------- 1 | 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 12586269025 20365011074 -------------------------------------------------------------------------------- /Using_STL/data/temperatures.txt: -------------------------------------------------------------------------------- 1 | 64.2394 69.2322 61.0484 32.066 70.503 33.7685 47.9791 56.3185 35.5881 61.1607 63.632 63.8291 65.8318 39.9963 46.9024 22.2147 32.9985 45.7904 42.0893 56.0469 50.2636 59.495 55.2646 55.4269 68.2306 48.6161 62.4015 41.8968 44.2749 53.0329 71.4646 81.7624 58.7414 19.5849 45.488 50.1976 44.5752 67.8068 64.447 66.0774 57.4047 30.3737 43.9931 70.2875 68.5318 45.6778 61.845 34.9825 52.1552 18.3537 --------------------------------------------------------------------------------