From 5718ab49b459105d7b829606f9a4077a0b16f404 Mon Sep 17 00:00:00 2001 From: "Elizarova, Alina" Date: Mon, 4 Oct 2021 17:59:29 +0300 Subject: [PATCH] Smart pointers reorg and small fixes --- .../10_00_00_whats_wrong_with_raw_ptr.cpp | 0 .../10_00_01_raii_idiom.cpp} | 0 .../10_01_00_unique_ptr_as_raii.cpp} | 0 .../10_01_01_unique_ptr_with_custom_deleter.cpp} | 3 ++- .../10_01_02_unique_ptr_example.cpp} | 0 .../10_01_03_unique_ptr_for_array.cpp} | 0 .../10_02_00_shared_ptr.cpp} | 0 .../10_02_01_shared_ptr_with_custom_deleter.cpp} | 0 .../10_02_02_shared_ptr_control_block.cpp} | 2 ++ .../10_02_03_shared_ptr_control_block_problems.cpp} | 0 .../10_02_04_shared_ptr_from_unique_ptr.cpp} | 0 .../10_03_00_weak_ptr.cpp} | 0 .../10_03_01_weak_ptr_and_control_block.cpp} | 2 ++ .../10_03_02_weak_ptr_example.cpp} | 2 +- .../10_03_03_weak_ptr_example_circles.cpp} | 6 +++--- .../10_03_04_weak_ptr_from_shared.cpp} | 0 .../10_04_00_make_functions.cpp} | 0 .../10_04_01_make_functions_pros.cpp} | 0 .../10_04_02_make_functions_cons.cpp} | 0 19 files changed, 10 insertions(+), 5 deletions(-) rename 10_smart_pointers/{10_00_whats_wrong_with_raw_ptr => 10_00_raii_idiom}/10_00_00_whats_wrong_with_raw_ptr.cpp (100%) rename 10_smart_pointers/{10_01_raii_idiom/10_01_00_raii_idiom.cpp => 10_00_raii_idiom/10_00_01_raii_idiom.cpp} (100%) rename 10_smart_pointers/{10_02_unique_ptr_as_raii/10_02_00_unique_ptr_as_raii.cpp => 10_01_unique_ptr/10_01_00_unique_ptr_as_raii.cpp} (100%) rename 10_smart_pointers/{10_03_unique_ptr_with_custom_deleter/10_03_00_unique_ptr_with_custom_deleter.cpp => 10_01_unique_ptr/10_01_01_unique_ptr_with_custom_deleter.cpp} (89%) rename 10_smart_pointers/{10_04_unique_ptr_example/10_04_00_unique_ptr_example.cpp => 10_01_unique_ptr/10_01_02_unique_ptr_example.cpp} (100%) rename 10_smart_pointers/{10_05_unique_ptr_for_array/10_05_00_unique_ptr_for_array.cpp => 10_01_unique_ptr/10_01_03_unique_ptr_for_array.cpp} (100%) rename 10_smart_pointers/{10_06_shared_ptr/10_06_00_shared_ptr.cpp => 10_02_shared_ptr/10_02_00_shared_ptr.cpp} (100%) rename 10_smart_pointers/{10_07_shared_ptr_with_custom_deleter/10_07_00_shared_ptr_with_custom_deleter.cpp => 10_02_shared_ptr/10_02_01_shared_ptr_with_custom_deleter.cpp} (100%) rename 10_smart_pointers/{10_08_shared_ptr_control_block/10_08_00_shared_ptr_control_block.cpp => 10_02_shared_ptr/10_02_02_shared_ptr_control_block.cpp} (97%) rename 10_smart_pointers/{10_09_shared_ptr_control_block_problems/10_09_00_shared_ptr_control_block_problems.cpp => 10_02_shared_ptr/10_02_03_shared_ptr_control_block_problems.cpp} (100%) rename 10_smart_pointers/{10_10_shared_ptr_from_unique_ptr/10_10_00_shared_ptr_from_unique_ptr.cpp => 10_02_shared_ptr/10_02_04_shared_ptr_from_unique_ptr.cpp} (100%) rename 10_smart_pointers/{10_11_weak_ptr/10_11_00_weak_ptr.cpp => 10_03_weak_ptr/10_03_00_weak_ptr.cpp} (100%) rename 10_smart_pointers/{10_12_weak_ptr_and_control_block/10_12_00_weak_ptr_and_control_block.cpp => 10_03_weak_ptr/10_03_01_weak_ptr_and_control_block.cpp} (97%) rename 10_smart_pointers/{10_13_weak_ptr_example/10_13_00_weak_ptr_example.cpp => 10_03_weak_ptr/10_03_02_weak_ptr_example.cpp} (99%) rename 10_smart_pointers/{10_14_weak_ptr_example_circles/10_14_00_weak_ptr_example_circles.cpp => 10_03_weak_ptr/10_03_03_weak_ptr_example_circles.cpp} (72%) rename 10_smart_pointers/{10_15_weak_ptr_from_shared/10_15_00_weak_ptr_from_shared.cpp => 10_03_weak_ptr/10_03_04_weak_ptr_from_shared.cpp} (100%) rename 10_smart_pointers/{10_16_make_functions/10_16_00_make_functions.cpp => 10_04_make_functions/10_04_00_make_functions.cpp} (100%) rename 10_smart_pointers/{10_17_make_functions_pros/10_17_00_make_functions_pros.cpp => 10_04_make_functions/10_04_01_make_functions_pros.cpp} (100%) rename 10_smart_pointers/{10_18_make_functions_cons/10_18_00_make_functions_cons.cpp => 10_04_make_functions/10_04_02_make_functions_cons.cpp} (100%) diff --git a/10_smart_pointers/10_00_whats_wrong_with_raw_ptr/10_00_00_whats_wrong_with_raw_ptr.cpp b/10_smart_pointers/10_00_raii_idiom/10_00_00_whats_wrong_with_raw_ptr.cpp similarity index 100% rename from 10_smart_pointers/10_00_whats_wrong_with_raw_ptr/10_00_00_whats_wrong_with_raw_ptr.cpp rename to 10_smart_pointers/10_00_raii_idiom/10_00_00_whats_wrong_with_raw_ptr.cpp diff --git a/10_smart_pointers/10_01_raii_idiom/10_01_00_raii_idiom.cpp b/10_smart_pointers/10_00_raii_idiom/10_00_01_raii_idiom.cpp similarity index 100% rename from 10_smart_pointers/10_01_raii_idiom/10_01_00_raii_idiom.cpp rename to 10_smart_pointers/10_00_raii_idiom/10_00_01_raii_idiom.cpp diff --git a/10_smart_pointers/10_02_unique_ptr_as_raii/10_02_00_unique_ptr_as_raii.cpp b/10_smart_pointers/10_01_unique_ptr/10_01_00_unique_ptr_as_raii.cpp similarity index 100% rename from 10_smart_pointers/10_02_unique_ptr_as_raii/10_02_00_unique_ptr_as_raii.cpp rename to 10_smart_pointers/10_01_unique_ptr/10_01_00_unique_ptr_as_raii.cpp diff --git a/10_smart_pointers/10_03_unique_ptr_with_custom_deleter/10_03_00_unique_ptr_with_custom_deleter.cpp b/10_smart_pointers/10_01_unique_ptr/10_01_01_unique_ptr_with_custom_deleter.cpp similarity index 89% rename from 10_smart_pointers/10_03_unique_ptr_with_custom_deleter/10_03_00_unique_ptr_with_custom_deleter.cpp rename to 10_smart_pointers/10_01_unique_ptr/10_01_01_unique_ptr_with_custom_deleter.cpp index d555e2a..18bda8b 100644 --- a/10_smart_pointers/10_03_unique_ptr_with_custom_deleter/10_03_00_unique_ptr_with_custom_deleter.cpp +++ b/10_smart_pointers/10_01_unique_ptr/10_01_01_unique_ptr_with_custom_deleter.cpp @@ -3,6 +3,7 @@ #include #include +#include struct custom_deleter { template @@ -24,7 +25,7 @@ int main() { std::unique_ptr uptr2(new int(2), deleter); - auto file_close = [](FILE* file) { std::fclose(file); } + auto file_close = [](FILE* file) { std::fclose(file); }; std::unique_ptr f_uptr(std::fopen("file.txt", "r"), file_close); diff --git a/10_smart_pointers/10_04_unique_ptr_example/10_04_00_unique_ptr_example.cpp b/10_smart_pointers/10_01_unique_ptr/10_01_02_unique_ptr_example.cpp similarity index 100% rename from 10_smart_pointers/10_04_unique_ptr_example/10_04_00_unique_ptr_example.cpp rename to 10_smart_pointers/10_01_unique_ptr/10_01_02_unique_ptr_example.cpp diff --git a/10_smart_pointers/10_05_unique_ptr_for_array/10_05_00_unique_ptr_for_array.cpp b/10_smart_pointers/10_01_unique_ptr/10_01_03_unique_ptr_for_array.cpp similarity index 100% rename from 10_smart_pointers/10_05_unique_ptr_for_array/10_05_00_unique_ptr_for_array.cpp rename to 10_smart_pointers/10_01_unique_ptr/10_01_03_unique_ptr_for_array.cpp diff --git a/10_smart_pointers/10_06_shared_ptr/10_06_00_shared_ptr.cpp b/10_smart_pointers/10_02_shared_ptr/10_02_00_shared_ptr.cpp similarity index 100% rename from 10_smart_pointers/10_06_shared_ptr/10_06_00_shared_ptr.cpp rename to 10_smart_pointers/10_02_shared_ptr/10_02_00_shared_ptr.cpp diff --git a/10_smart_pointers/10_07_shared_ptr_with_custom_deleter/10_07_00_shared_ptr_with_custom_deleter.cpp b/10_smart_pointers/10_02_shared_ptr/10_02_01_shared_ptr_with_custom_deleter.cpp similarity index 100% rename from 10_smart_pointers/10_07_shared_ptr_with_custom_deleter/10_07_00_shared_ptr_with_custom_deleter.cpp rename to 10_smart_pointers/10_02_shared_ptr/10_02_01_shared_ptr_with_custom_deleter.cpp diff --git a/10_smart_pointers/10_08_shared_ptr_control_block/10_08_00_shared_ptr_control_block.cpp b/10_smart_pointers/10_02_shared_ptr/10_02_02_shared_ptr_control_block.cpp similarity index 97% rename from 10_smart_pointers/10_08_shared_ptr_control_block/10_08_00_shared_ptr_control_block.cpp rename to 10_smart_pointers/10_02_shared_ptr/10_02_02_shared_ptr_control_block.cpp index 0fd23e9..5db91e1 100644 --- a/10_smart_pointers/10_08_shared_ptr_control_block/10_08_00_shared_ptr_control_block.cpp +++ b/10_smart_pointers/10_02_shared_ptr/10_02_02_shared_ptr_control_block.cpp @@ -1,5 +1,7 @@ // shared_ptr underneath +#include + struct control_block { std::size_t ref_counter{}; // May contain other data diff --git a/10_smart_pointers/10_09_shared_ptr_control_block_problems/10_09_00_shared_ptr_control_block_problems.cpp b/10_smart_pointers/10_02_shared_ptr/10_02_03_shared_ptr_control_block_problems.cpp similarity index 100% rename from 10_smart_pointers/10_09_shared_ptr_control_block_problems/10_09_00_shared_ptr_control_block_problems.cpp rename to 10_smart_pointers/10_02_shared_ptr/10_02_03_shared_ptr_control_block_problems.cpp diff --git a/10_smart_pointers/10_10_shared_ptr_from_unique_ptr/10_10_00_shared_ptr_from_unique_ptr.cpp b/10_smart_pointers/10_02_shared_ptr/10_02_04_shared_ptr_from_unique_ptr.cpp similarity index 100% rename from 10_smart_pointers/10_10_shared_ptr_from_unique_ptr/10_10_00_shared_ptr_from_unique_ptr.cpp rename to 10_smart_pointers/10_02_shared_ptr/10_02_04_shared_ptr_from_unique_ptr.cpp diff --git a/10_smart_pointers/10_11_weak_ptr/10_11_00_weak_ptr.cpp b/10_smart_pointers/10_03_weak_ptr/10_03_00_weak_ptr.cpp similarity index 100% rename from 10_smart_pointers/10_11_weak_ptr/10_11_00_weak_ptr.cpp rename to 10_smart_pointers/10_03_weak_ptr/10_03_00_weak_ptr.cpp diff --git a/10_smart_pointers/10_12_weak_ptr_and_control_block/10_12_00_weak_ptr_and_control_block.cpp b/10_smart_pointers/10_03_weak_ptr/10_03_01_weak_ptr_and_control_block.cpp similarity index 97% rename from 10_smart_pointers/10_12_weak_ptr_and_control_block/10_12_00_weak_ptr_and_control_block.cpp rename to 10_smart_pointers/10_03_weak_ptr/10_03_01_weak_ptr_and_control_block.cpp index de975cd..9d292b6 100644 --- a/10_smart_pointers/10_12_weak_ptr_and_control_block/10_12_00_weak_ptr_and_control_block.cpp +++ b/10_smart_pointers/10_03_weak_ptr/10_03_01_weak_ptr_and_control_block.cpp @@ -1,5 +1,7 @@ // weak_ptr underneath +#include + struct control_block { std::size_t ref_counter; std::size_t weak_counter; diff --git a/10_smart_pointers/10_13_weak_ptr_example/10_13_00_weak_ptr_example.cpp b/10_smart_pointers/10_03_weak_ptr/10_03_02_weak_ptr_example.cpp similarity index 99% rename from 10_smart_pointers/10_13_weak_ptr_example/10_13_00_weak_ptr_example.cpp rename to 10_smart_pointers/10_03_weak_ptr/10_03_02_weak_ptr_example.cpp index 65758da..d1c0cfc 100644 --- a/10_smart_pointers/10_13_weak_ptr_example/10_13_00_weak_ptr_example.cpp +++ b/10_smart_pointers/10_03_weak_ptr/10_03_02_weak_ptr_example.cpp @@ -6,7 +6,7 @@ struct widget {/*...*/}; struct id_type {/*...*/}; // This function can be expensive -std::unique_ptr widget_factory( id_type id ) {/*...*/} +std::unique_ptr widget_factory( id_type id ) {/*...*/}; std::shared_ptr widget_factory_with_caching( id_type id ) { static std::unordered_map> cache; diff --git a/10_smart_pointers/10_14_weak_ptr_example_circles/10_14_00_weak_ptr_example_circles.cpp b/10_smart_pointers/10_03_weak_ptr/10_03_03_weak_ptr_example_circles.cpp similarity index 72% rename from 10_smart_pointers/10_14_weak_ptr_example_circles/10_14_00_weak_ptr_example_circles.cpp rename to 10_smart_pointers/10_03_weak_ptr/10_03_03_weak_ptr_example_circles.cpp index d5754d7..025f49c 100644 --- a/10_smart_pointers/10_14_weak_ptr_example_circles/10_14_00_weak_ptr_example_circles.cpp +++ b/10_smart_pointers/10_03_weak_ptr/10_03_03_weak_ptr_example_circles.cpp @@ -4,14 +4,14 @@ class person { public: - person get_partner() const { return *partner; } + std::shared_ptr get_partner() const { return partner; } private: std::shared_ptr partner; }; void make_partnership( std::shared_ptr& person1, std::shared_ptr& person2 ) { - person1->partner = person2; - person2->partner = person1; + person1->get_partner() = person2; + person2->get_partner() = person1; } int main() { diff --git a/10_smart_pointers/10_15_weak_ptr_from_shared/10_15_00_weak_ptr_from_shared.cpp b/10_smart_pointers/10_03_weak_ptr/10_03_04_weak_ptr_from_shared.cpp similarity index 100% rename from 10_smart_pointers/10_15_weak_ptr_from_shared/10_15_00_weak_ptr_from_shared.cpp rename to 10_smart_pointers/10_03_weak_ptr/10_03_04_weak_ptr_from_shared.cpp diff --git a/10_smart_pointers/10_16_make_functions/10_16_00_make_functions.cpp b/10_smart_pointers/10_04_make_functions/10_04_00_make_functions.cpp similarity index 100% rename from 10_smart_pointers/10_16_make_functions/10_16_00_make_functions.cpp rename to 10_smart_pointers/10_04_make_functions/10_04_00_make_functions.cpp diff --git a/10_smart_pointers/10_17_make_functions_pros/10_17_00_make_functions_pros.cpp b/10_smart_pointers/10_04_make_functions/10_04_01_make_functions_pros.cpp similarity index 100% rename from 10_smart_pointers/10_17_make_functions_pros/10_17_00_make_functions_pros.cpp rename to 10_smart_pointers/10_04_make_functions/10_04_01_make_functions_pros.cpp diff --git a/10_smart_pointers/10_18_make_functions_cons/10_18_00_make_functions_cons.cpp b/10_smart_pointers/10_04_make_functions/10_04_02_make_functions_cons.cpp similarity index 100% rename from 10_smart_pointers/10_18_make_functions_cons/10_18_00_make_functions_cons.cpp rename to 10_smart_pointers/10_04_make_functions/10_04_02_make_functions_cons.cpp