From b1c287f11f5f8aa49e9ce501ffc2814e60099343 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 23 Oct 2022 21:23:25 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +-- docs/source/2022_2023_autumn/index.rst | 2 +- .../week_03/code/lowest_terms_fraction.hpp | 4 ++++ docs/source/2022_2023_autumn/week_06/doc.rst | 2 +- docs/source/2022_2023_autumn/week_07/doc.rst | 8 ++------ .../2022_2023_autumn/week_10/code/finally.hpp | 18 ++++++++++++++++++ docs/source/_todo/concept/doc.rst | 10 ++++++++++ 7 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp create mode 100644 docs/source/2022_2023_autumn/week_10/code/finally.hpp create mode 100644 docs/source/_todo/concept/doc.rst diff --git a/.gitignore b/.gitignore index 6bc0b7a..81daeb2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /.cache/ build .DS_Store -*/.DS_Store -helper.md \ No newline at end of file +*/.DS_Store \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/index.rst b/docs/source/2022_2023_autumn/index.rst index db00576..878172e 100644 --- a/docs/source/2022_2023_autumn/index.rst +++ b/docs/source/2022_2023_autumn/index.rst @@ -9,12 +9,12 @@ week_00/doc.rst week_01/doc.rst week_02/doc.rst + week_03/doc.rst .. toctree:: :maxdepth: 1 :caption: 未完成 - week_03/doc.rst week_04/doc.rst week_05/doc.rst week_06/doc.rst diff --git a/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp b/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp new file mode 100644 index 0000000..7c31e3e --- /dev/null +++ b/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp @@ -0,0 +1,4 @@ +#ifndef LOWEST_TERMS_FRACTION_HPP +#define LOWEST_TERMS_FRACTION_HPP + +#endif \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_06/doc.rst b/docs/source/2022_2023_autumn/week_06/doc.rst index e8b5481..7a76b8a 100644 --- a/docs/source/2022_2023_autumn/week_06/doc.rst +++ b/docs/source/2022_2023_autumn/week_06/doc.rst @@ -3,7 +3,7 @@ .. pull-quote:: - `我只想做县长夫人,谁是县长,我不在乎。——《让子弹飞》` + `我只想做循环变量,谁是循环类型……我不在乎。——改自《让子弹飞》` .. TODO: diff --git a/docs/source/2022_2023_autumn/week_07/doc.rst b/docs/source/2022_2023_autumn/week_07/doc.rst index 9a32092..65b7d30 100644 --- a/docs/source/2022_2023_autumn/week_07/doc.rst +++ b/docs/source/2022_2023_autumn/week_07/doc.rst @@ -1,9 +1,5 @@ -概念:直接表达代码意图 -************************ - -.. pull-quote:: - - `如果一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。——James Whitcomb Riley` +模板:将类型和常量作为参数 +**************************** .. TODO: diff --git a/docs/source/2022_2023_autumn/week_10/code/finally.hpp b/docs/source/2022_2023_autumn/week_10/code/finally.hpp new file mode 100644 index 0000000..6af7c5b --- /dev/null +++ b/docs/source/2022_2023_autumn/week_10/code/finally.hpp @@ -0,0 +1,18 @@ +#ifndef FINALLY_HPP +#define FINALLY_HPP + +#include + +template +class FinalAction { + public: + + private: +}; + +template +FinalAction finally(Function function) { + return FinalAction{std::move(function)}; +} + +#endif \ No newline at end of file diff --git a/docs/source/_todo/concept/doc.rst b/docs/source/_todo/concept/doc.rst new file mode 100644 index 0000000..9a32092 --- /dev/null +++ b/docs/source/_todo/concept/doc.rst @@ -0,0 +1,10 @@ +概念:直接表达代码意图 +************************ + +.. pull-quote:: + + `如果一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。——James Whitcomb Riley` + +.. TODO: + +待完成 \ No newline at end of file -- Gitee From a61f7782bfbe11b1e7d5e5218208ce8567c0b8ad Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 23 Oct 2022 22:00:10 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=8D=E5=8F=98?= =?UTF-8?q?=E5=BC=8F=E6=89=80=E9=9C=80=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/2022_2023_autumn/index.rst | 2 +- .../week_03/code/lowest_terms_fraction.hpp | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/source/2022_2023_autumn/index.rst b/docs/source/2022_2023_autumn/index.rst index 878172e..db00576 100644 --- a/docs/source/2022_2023_autumn/index.rst +++ b/docs/source/2022_2023_autumn/index.rst @@ -9,12 +9,12 @@ week_00/doc.rst week_01/doc.rst week_02/doc.rst - week_03/doc.rst .. toctree:: :maxdepth: 1 :caption: 未完成 + week_03/doc.rst week_04/doc.rst week_05/doc.rst week_06/doc.rst diff --git a/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp b/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp index 7c31e3e..f639889 100644 --- a/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp +++ b/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp @@ -1,4 +1,45 @@ #ifndef LOWEST_TERMS_FRACTION_HPP #define LOWEST_TERMS_FRACTION_HPP +#include // ALERT: remove this +#include + +inline int get_greatest_common_divisor(int numerator, int denominator) { + int res = 0; + while (denominator != 0) { + res = denominator; + denominator = numerator % denominator; + numerator = res; + } + return res; +} + +class LowestTermsFraction { + public: + LowestTermsFraction(int numerator, int denominator) : numerator_(numerator), denominator_{denominator} { + if (denominator_ == 0) { + throw std::domain_error{"the fraction's denominator can't be zero"}; + } + + set_to_lowest_terms(); + } + + void add(LowestTermsFraction const& other) { + numerator_ = numerator_ * other.denominator_ + other.numerator_ * denominator_; + denominator_ *= other.denominator_; + + set_to_lowest_terms(); + } + + private: + void set_to_lowest_terms() { + int const greatest_common_divisor{get_greatest_common_divisor(numerator_, denominator_)}; + numerator_ /= greatest_common_divisor; + denominator_ /= greatest_common_divisor; + } + + int numerator_; // 分子 + int denominator_; // 分母 +}; + #endif \ No newline at end of file -- Gitee From 8105345788a37a60746f3db009d0ade7cf1d6b84 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 23 Oct 2022 22:00:18 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E5=AE=8C=E6=88=90RAII=E6=89=80=E9=9C=80?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../week_04/code/owner_int.hpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 docs/source/2022_2023_autumn/week_04/code/owner_int.hpp diff --git a/docs/source/2022_2023_autumn/week_04/code/owner_int.hpp b/docs/source/2022_2023_autumn/week_04/code/owner_int.hpp new file mode 100644 index 0000000..afaa0e9 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_04/code/owner_int.hpp @@ -0,0 +1,33 @@ +#ifndef OWNER_INT_HPP +#define OWNER_INT_HPP + +#include + +class OwnerInt { + public: + OwnerInt() : data_(nullptr) {} + OwnerInt(OwnerInt const& other) : data_(other.data_ ? new int(*other.data_) : nullptr) {} + OwnerInt(int value) : data_(new int(value)) {} + + OwnerInt& operator=(OwnerInt const& other) { + if (this != &other) { + OwnerInt temp(other); + swap(temp); + } + return *this; + } + + ~OwnerInt() { + delete data_; + } + + void swap(OwnerInt& other) { + using std::swap; + swap(data_, other.data_); + } + + private: + int* data_; +}; + +#endif \ No newline at end of file -- Gitee From 1f8c33bb4cc874445665f4c691513031b4a4f18a Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 23 Oct 2022 22:00:26 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=AE=8C=E6=88=90finally=E6=89=80?= =?UTF-8?q?=E9=9C=80=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022_2023_autumn/week_10/code/finally.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/source/2022_2023_autumn/week_10/code/finally.hpp b/docs/source/2022_2023_autumn/week_10/code/finally.hpp index 6af7c5b..e72286c 100644 --- a/docs/source/2022_2023_autumn/week_10/code/finally.hpp +++ b/docs/source/2022_2023_autumn/week_10/code/finally.hpp @@ -6,8 +6,24 @@ template class FinalAction { public: + FinalAction(Function function) : function_(std::move(function)), should_invoke_(true) {} + + FinalAction(FinalAction const&) = delete; + FinalAction(FinalAction&& other) + : function_(std::move(other.function_)), should_invoke_(std::exchange(other.should_invoke_, false)) {} + + FinalAction& operator=(FinalAction const&) = delete; + FinalAction& operator=(FinalAction const&&) = delete; + + ~FinalAction() { + if (should_invoke_) { + function_(); + } + } private: + Function function_; + bool should_invoke_; }; template -- Gitee From 9142deb6d674e20884493d74b686c4a993f89d46 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 23 Oct 2022 22:02:59 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=89=80=E9=9C=80=E9=83=A8=E5=88=86=E5=BA=94=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/2022_2023_autumn/week_07/code/owner.hpp | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 docs/source/2022_2023_autumn/week_07/code/owner.hpp diff --git a/docs/source/2022_2023_autumn/week_07/code/owner.hpp b/docs/source/2022_2023_autumn/week_07/code/owner.hpp new file mode 100644 index 0000000..9ea6db4 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_07/code/owner.hpp @@ -0,0 +1,7 @@ +#ifndef OWNER_HPP +#define OWNER_HPP + +template +using Owner = T; + +#endif \ No newline at end of file -- Gitee From bd1c46c4c7ac63de7b2d5ab6ae8aced4325df725 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 24 Oct 2022 08:23:28 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=AE=89=E5=85=A8=EF=BC=88=E5=BE=85=E5=AE=8C=E6=88=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../week_03/code/lowest_terms_fraction.hpp | 1 - docs/source/2022_2023_autumn/week_05/doc.rst | 8 ++++++-- .../2022_2023_autumn/{week_07 => week_06}/code/owner.hpp | 0 docs/source/2022_2023_autumn/week_06/doc.rst | 8 ++------ docs/source/2022_2023_autumn/week_07/doc.rst | 4 ++-- docs/source/2022_2023_autumn/week_09/code/owner.hpp | 7 +++++++ docs/source/_todo/free_your_function/doc.rst | 6 ++++++ 7 files changed, 23 insertions(+), 11 deletions(-) rename docs/source/2022_2023_autumn/{week_07 => week_06}/code/owner.hpp (100%) create mode 100644 docs/source/2022_2023_autumn/week_09/code/owner.hpp create mode 100644 docs/source/_todo/free_your_function/doc.rst diff --git a/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp b/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp index f639889..5b419d6 100644 --- a/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp +++ b/docs/source/2022_2023_autumn/week_03/code/lowest_terms_fraction.hpp @@ -1,7 +1,6 @@ #ifndef LOWEST_TERMS_FRACTION_HPP #define LOWEST_TERMS_FRACTION_HPP -#include // ALERT: remove this #include inline int get_greatest_common_divisor(int numerator, int denominator) { diff --git a/docs/source/2022_2023_autumn/week_05/doc.rst b/docs/source/2022_2023_autumn/week_05/doc.rst index 950141d..7a76b8a 100644 --- a/docs/source/2022_2023_autumn/week_05/doc.rst +++ b/docs/source/2022_2023_autumn/week_05/doc.rst @@ -1,5 +1,9 @@ -耦合、内聚、封装:解放你的函数 -********************************* +自动类型推导:分离应当关注的点 +******************************* + +.. pull-quote:: + + `我只想做循环变量,谁是循环类型……我不在乎。——改自《让子弹飞》` .. TODO: diff --git a/docs/source/2022_2023_autumn/week_07/code/owner.hpp b/docs/source/2022_2023_autumn/week_06/code/owner.hpp similarity index 100% rename from docs/source/2022_2023_autumn/week_07/code/owner.hpp rename to docs/source/2022_2023_autumn/week_06/code/owner.hpp diff --git a/docs/source/2022_2023_autumn/week_06/doc.rst b/docs/source/2022_2023_autumn/week_06/doc.rst index 7a76b8a..65b7d30 100644 --- a/docs/source/2022_2023_autumn/week_06/doc.rst +++ b/docs/source/2022_2023_autumn/week_06/doc.rst @@ -1,9 +1,5 @@ -自动类型推导:分离应当关注的点 -******************************* - -.. pull-quote:: - - `我只想做循环变量,谁是循环类型……我不在乎。——改自《让子弹飞》` +模板:将类型和常量作为参数 +**************************** .. TODO: diff --git a/docs/source/2022_2023_autumn/week_07/doc.rst b/docs/source/2022_2023_autumn/week_07/doc.rst index 65b7d30..fc5b5ab 100644 --- a/docs/source/2022_2023_autumn/week_07/doc.rst +++ b/docs/source/2022_2023_autumn/week_07/doc.rst @@ -1,5 +1,5 @@ -模板:将类型和常量作为参数 -**************************** +异常安全:C++ 课程缺失的一课 +******************************* .. TODO: diff --git a/docs/source/2022_2023_autumn/week_09/code/owner.hpp b/docs/source/2022_2023_autumn/week_09/code/owner.hpp new file mode 100644 index 0000000..9ea6db4 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_09/code/owner.hpp @@ -0,0 +1,7 @@ +#ifndef OWNER_HPP +#define OWNER_HPP + +template +using Owner = T; + +#endif \ No newline at end of file diff --git a/docs/source/_todo/free_your_function/doc.rst b/docs/source/_todo/free_your_function/doc.rst new file mode 100644 index 0000000..20755c7 --- /dev/null +++ b/docs/source/_todo/free_your_function/doc.rst @@ -0,0 +1,6 @@ +耦合:解放你的函数 +********************************* + +.. TODO: + +待完成 \ No newline at end of file -- Gitee From 24c99bb866a205166829e1ad754a0a4aa8acea02 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 24 Oct 2022 08:29:30 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E4=B8=BA=E5=BC=82=E5=B8=B8=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E4=BF=9D=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/2022_2023_autumn/week_07/doc.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/2022_2023_autumn/week_07/doc.rst b/docs/source/2022_2023_autumn/week_07/doc.rst index fc5b5ab..8057f18 100644 --- a/docs/source/2022_2023_autumn/week_07/doc.rst +++ b/docs/source/2022_2023_autumn/week_07/doc.rst @@ -1,5 +1,5 @@ -异常安全:C++ 课程缺失的一课 -******************************* +异常安全保证:C++ 课程缺失的一课 +********************************* .. TODO: -- Gitee From 2601e7480f19e0b8985c57d3b3656c45f9893f67 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 24 Oct 2022 08:35:02 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=89=80=E9=9C=80=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/2022_2023_autumn/week_07/doc.rst | 4 +- .../week_09/code/unique_ptr.hpp | 226 ++++++++++++++++++ 2 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 docs/source/2022_2023_autumn/week_09/code/unique_ptr.hpp diff --git a/docs/source/2022_2023_autumn/week_07/doc.rst b/docs/source/2022_2023_autumn/week_07/doc.rst index 8057f18..327e1b3 100644 --- a/docs/source/2022_2023_autumn/week_07/doc.rst +++ b/docs/source/2022_2023_autumn/week_07/doc.rst @@ -3,4 +3,6 @@ .. TODO: -待完成 \ No newline at end of file +待完成 + +.. TODO: noexcept(noexcept(...)) \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_09/code/unique_ptr.hpp b/docs/source/2022_2023_autumn/week_09/code/unique_ptr.hpp new file mode 100644 index 0000000..d480a73 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_09/code/unique_ptr.hpp @@ -0,0 +1,226 @@ +#ifndef UNIQUE_PTR_HPP +#define UNIQUE_PTR_HPP + +#include "owner.hpp" + +#include +#include + +template +class UniquePtr { + public: + UniquePtr() : data_(nullptr) {} + UniquePtr(UniquePtr const&) = delete; + UniquePtr(UniquePtr&& other) noexcept : data_(std::exchange(other.data_, nullptr)) {} + + UniquePtr(std::nullptr_t) noexcept : data_(nullptr) {} + UniquePtr(Owner data) : data_(data) {} + + template + UniquePtr(UniquePtr&& other) noexcept : data_(other.release()) {} + + UniquePtr& operator=(UniquePtr const&) = delete; + UniquePtr& operator=(UniquePtr&& other) noexcept { + if (this != &other) { + reset(other.release()); + } + return *this; + } + + UniquePtr& operator=(std::nullptr_t) noexcept { + reset(); + return *this; + } + + template + UniquePtr& operator=(UniquePtr other) { + reset(other.release()); + return *this; + } + + ~UniquePtr() { + reset(); + } + + void reset(Owner pointer = nullptr) noexcept { + delete std::exchange(data_, pointer); + } + + Owner release() noexcept { + return std::exchange(data_, nullptr); + } + + void swap(UniquePtr& other) noexcept { + using std::swap; + swap(data_, other.data_); + } + + T* get() const noexcept { + return data_; + } + + operator bool() const noexcept { + return get() != nullptr; + } + + T& operator*() const noexcept(noexcept(*data_)) { + return *data_; + } + T* operator->() const noexcept { + return data_; + } + + private: + Owner data_; +}; + +template +class UniquePtr { // NOLINT + public: + UniquePtr() : data_(nullptr) {} + UniquePtr(UniquePtr const&) = delete; + UniquePtr(UniquePtr&& other) noexcept : data_(std::exchange(other.data_, nullptr)) {} + + UniquePtr(std::nullptr_t) noexcept : data_(nullptr) {} + + template + UniquePtr(ArrayPointer pointer) : data_(pointer) {} + + template + UniquePtr(UniquePtr&& other) noexcept : data_(other.release()) {} + + UniquePtr& operator=(UniquePtr const&) = delete; + UniquePtr& operator=(UniquePtr&& other) noexcept { + if (this != &other) { + reset(other.release()); + } + return *this; + } + + UniquePtr& operator=(std::nullptr_t) noexcept { + reset(); + return *this; + } + + template + UniquePtr& operator=(UniquePtr other) { + reset(other.release()); + return *this; + } + + ~UniquePtr() { + reset(); + } + + template + void reset(ArrayPointer pointer) noexcept { + delete[] std::exchange(data_, pointer); // NOLINT + } + + void reset(std::nullptr_t = nullptr) noexcept { + delete[] std::exchange(data_, nullptr); + } + + Owner release() noexcept { + return std::exchange(data_, nullptr); + } + + void swap(UniquePtr& other) noexcept { + std::swap(data_, other.data_); + } + + T* get() const noexcept { + return data_; + } + + operator bool() const noexcept { + return get() != nullptr; + } + + T& operator[](std::size_t index) const { + return *(data_ + index); + } + + private: + Owner data_{}; +}; + +template +bool operator==(UniquePtr const& lhs, UniquePtr const& rhs) { + return lhs.get() == rhs.get(); +} +template +bool operator==(UniquePtr const& lhs, std::nullptr_t) { + return lhs.get() == nullptr; +} +template +bool operator==(std::nullptr_t, UniquePtr const& rhs) { + return nullptr == rhs.get(); +} + +template +bool operator!=(UniquePtr const& lhs, UniquePtr const& rhs) { + return !(lhs == rhs); +} +template +bool operator!=(UniquePtr const& lhs, std::nullptr_t) { + return !(lhs == nullptr); +} +template +bool operator!=(std::nullptr_t, UniquePtr const& rhs) { + return !(nullptr == rhs); +} + +template +bool operator<(UniquePtr const& lhs, UniquePtr const& rhs) { + return lhs.get() < rhs.get(); +} +template +bool operator<(UniquePtr const& lhs, std::nullptr_t) { + return lhs.get() < nullptr; +} +template +bool operator<(std::nullptr_t, UniquePtr const& rhs) { + return nullptr < rhs.get(); +} + +template +bool operator>(UniquePtr const& lhs, UniquePtr const& rhs) { + return rhs < lhs; +} +template +bool operator>(UniquePtr const& lhs, std::nullptr_t) { + return nullptr < lhs; +} +template +bool operator>(std::nullptr_t, UniquePtr const& rhs) { + return rhs < nullptr; +} + +template +bool operator>=(UniquePtr const& lhs, UniquePtr const& rhs) { + return !(lhs < rhs); +} +template +bool operator>=(UniquePtr const& lhs, std::nullptr_t) { + return !(lhs < nullptr); +} +template +bool operator>=(std::nullptr_t, UniquePtr const& rhs) { + return !(nullptr < rhs); +} + +template +bool operator<=(UniquePtr const& lhs, UniquePtr const& rhs) { + return !(rhs < lhs); +} +template +bool operator<=(UniquePtr const& lhs, std::nullptr_t) { + return !(nullptr < lhs); +} +template +bool operator<=(std::nullptr_t, UniquePtr const& rhs) { + return !(rhs < nullptr); +} + +#endif \ No newline at end of file -- Gitee From d79461ff16d92706d82cf6d772cf297bad8e51d8 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 24 Oct 2022 08:46:17 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E5=99=A8=EF=BC=88=E5=BE=85=E5=AE=8C=E6=88=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/2022_2023_autumn/index.rst | 4 +- docs/source/2022_2023_autumn/week_07/doc.rst | 12 +- docs/source/2022_2023_autumn/week_08/doc.rst | 8 +- docs/source/2022_2023_autumn/week_09/doc.rst | 8 +- docs/source/2022_2023_autumn/week_10/doc.rst | 4 +- .../{week_09 => week_11}/code/owner.hpp | 0 .../{week_09 => week_11}/code/unique_ptr.hpp | 0 docs/source/2022_2023_autumn/week_11/doc.rst | 4 +- .../{week_10 => week_12}/code/finally.hpp | 0 docs/source/2022_2023_autumn/week_12/doc.rst | 6 + .../week_13/code/forward_list.hpp | 4 + .../2022_2023_autumn/week_13/code/node.hpp | 12 + .../2022_2023_autumn/week_13/code/owner.hpp | 7 + .../week_13/code/unique_ptr.hpp | 226 ++++++++++++++++++ docs/source/2022_2023_autumn/week_13/doc.rst | 6 + docs/source/_todo/concept/doc.rst | 10 - 16 files changed, 286 insertions(+), 25 deletions(-) rename docs/source/2022_2023_autumn/{week_09 => week_11}/code/owner.hpp (100%) rename docs/source/2022_2023_autumn/{week_09 => week_11}/code/unique_ptr.hpp (100%) rename docs/source/2022_2023_autumn/{week_10 => week_12}/code/finally.hpp (100%) create mode 100644 docs/source/2022_2023_autumn/week_12/doc.rst create mode 100644 docs/source/2022_2023_autumn/week_13/code/forward_list.hpp create mode 100644 docs/source/2022_2023_autumn/week_13/code/node.hpp create mode 100644 docs/source/2022_2023_autumn/week_13/code/owner.hpp create mode 100644 docs/source/2022_2023_autumn/week_13/code/unique_ptr.hpp create mode 100644 docs/source/2022_2023_autumn/week_13/doc.rst delete mode 100644 docs/source/_todo/concept/doc.rst diff --git a/docs/source/2022_2023_autumn/index.rst b/docs/source/2022_2023_autumn/index.rst index db00576..7b81734 100644 --- a/docs/source/2022_2023_autumn/index.rst +++ b/docs/source/2022_2023_autumn/index.rst @@ -22,4 +22,6 @@ week_08/doc.rst week_09/doc.rst week_10/doc.rst - week_11/doc.rst \ No newline at end of file + week_11/doc.rst + week_12/doc.rst + week_13/doc.rst \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_07/doc.rst b/docs/source/2022_2023_autumn/week_07/doc.rst index 327e1b3..486ffe0 100644 --- a/docs/source/2022_2023_autumn/week_07/doc.rst +++ b/docs/source/2022_2023_autumn/week_07/doc.rst @@ -1,8 +1,10 @@ -异常安全保证:C++ 课程缺失的一课 -********************************* +迭代器:尝试编写通用代码(泛型编程) +************************************* -.. TODO: +.. pull-quote:: + + `如果一个类型构造起来像迭代器、解引用起来像迭代器、迭代起来也像迭代器,那么这个类型就可以被称为迭代器。——改自 James Whitcomb Riley` -待完成 +.. TODO: -.. TODO: noexcept(noexcept(...)) \ No newline at end of file +待完成 \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_08/doc.rst b/docs/source/2022_2023_autumn/week_08/doc.rst index ed5e66b..d881c1c 100644 --- a/docs/source/2022_2023_autumn/week_08/doc.rst +++ b/docs/source/2022_2023_autumn/week_08/doc.rst @@ -1,5 +1,9 @@ -移动语义:C++ 的江湖豪情侠胆柔肠之大腿 -****************************************** +概念:直接表达代码意图 +************************ + +.. pull-quote:: + + `` .. TODO: diff --git a/docs/source/2022_2023_autumn/week_09/doc.rst b/docs/source/2022_2023_autumn/week_09/doc.rst index 8b50678..327e1b3 100644 --- a/docs/source/2022_2023_autumn/week_09/doc.rst +++ b/docs/source/2022_2023_autumn/week_09/doc.rst @@ -1,6 +1,8 @@ -智能指针:别了, ``new`` 与 ``delete`` -**************************************** +异常安全保证:C++ 课程缺失的一课 +********************************* .. TODO: -待完成 \ No newline at end of file +待完成 + +.. TODO: noexcept(noexcept(...)) \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_10/doc.rst b/docs/source/2022_2023_autumn/week_10/doc.rst index 241d054..ed5e66b 100644 --- a/docs/source/2022_2023_autumn/week_10/doc.rst +++ b/docs/source/2022_2023_autumn/week_10/doc.rst @@ -1,5 +1,5 @@ -finally:总是在最后执行 -***************************** +移动语义:C++ 的江湖豪情侠胆柔肠之大腿 +****************************************** .. TODO: diff --git a/docs/source/2022_2023_autumn/week_09/code/owner.hpp b/docs/source/2022_2023_autumn/week_11/code/owner.hpp similarity index 100% rename from docs/source/2022_2023_autumn/week_09/code/owner.hpp rename to docs/source/2022_2023_autumn/week_11/code/owner.hpp diff --git a/docs/source/2022_2023_autumn/week_09/code/unique_ptr.hpp b/docs/source/2022_2023_autumn/week_11/code/unique_ptr.hpp similarity index 100% rename from docs/source/2022_2023_autumn/week_09/code/unique_ptr.hpp rename to docs/source/2022_2023_autumn/week_11/code/unique_ptr.hpp diff --git a/docs/source/2022_2023_autumn/week_11/doc.rst b/docs/source/2022_2023_autumn/week_11/doc.rst index 79aab16..8b50678 100644 --- a/docs/source/2022_2023_autumn/week_11/doc.rst +++ b/docs/source/2022_2023_autumn/week_11/doc.rst @@ -1,5 +1,5 @@ -单向链表:运用移动语义、RAII 的示例 -************************************* +智能指针:别了, ``new`` 与 ``delete`` +**************************************** .. TODO: diff --git a/docs/source/2022_2023_autumn/week_10/code/finally.hpp b/docs/source/2022_2023_autumn/week_12/code/finally.hpp similarity index 100% rename from docs/source/2022_2023_autumn/week_10/code/finally.hpp rename to docs/source/2022_2023_autumn/week_12/code/finally.hpp diff --git a/docs/source/2022_2023_autumn/week_12/doc.rst b/docs/source/2022_2023_autumn/week_12/doc.rst new file mode 100644 index 0000000..241d054 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_12/doc.rst @@ -0,0 +1,6 @@ +finally:总是在最后执行 +***************************** + +.. TODO: + +待完成 \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_13/code/forward_list.hpp b/docs/source/2022_2023_autumn/week_13/code/forward_list.hpp new file mode 100644 index 0000000..b714002 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_13/code/forward_list.hpp @@ -0,0 +1,4 @@ +#ifndef FORWARD_LIST_HPP +#define FORWARD_LIST_HPP + +#endif \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_13/code/node.hpp b/docs/source/2022_2023_autumn/week_13/code/node.hpp new file mode 100644 index 0000000..8c2d1f8 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_13/code/node.hpp @@ -0,0 +1,12 @@ +#ifndef NODE_HPP +#define NODE_HPP + +#include "unique_ptr.hpp" + +template +struct Node { + T data; + UniquePtr next; +}; + +#endif \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_13/code/owner.hpp b/docs/source/2022_2023_autumn/week_13/code/owner.hpp new file mode 100644 index 0000000..9ea6db4 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_13/code/owner.hpp @@ -0,0 +1,7 @@ +#ifndef OWNER_HPP +#define OWNER_HPP + +template +using Owner = T; + +#endif \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_13/code/unique_ptr.hpp b/docs/source/2022_2023_autumn/week_13/code/unique_ptr.hpp new file mode 100644 index 0000000..d480a73 --- /dev/null +++ b/docs/source/2022_2023_autumn/week_13/code/unique_ptr.hpp @@ -0,0 +1,226 @@ +#ifndef UNIQUE_PTR_HPP +#define UNIQUE_PTR_HPP + +#include "owner.hpp" + +#include +#include + +template +class UniquePtr { + public: + UniquePtr() : data_(nullptr) {} + UniquePtr(UniquePtr const&) = delete; + UniquePtr(UniquePtr&& other) noexcept : data_(std::exchange(other.data_, nullptr)) {} + + UniquePtr(std::nullptr_t) noexcept : data_(nullptr) {} + UniquePtr(Owner data) : data_(data) {} + + template + UniquePtr(UniquePtr&& other) noexcept : data_(other.release()) {} + + UniquePtr& operator=(UniquePtr const&) = delete; + UniquePtr& operator=(UniquePtr&& other) noexcept { + if (this != &other) { + reset(other.release()); + } + return *this; + } + + UniquePtr& operator=(std::nullptr_t) noexcept { + reset(); + return *this; + } + + template + UniquePtr& operator=(UniquePtr other) { + reset(other.release()); + return *this; + } + + ~UniquePtr() { + reset(); + } + + void reset(Owner pointer = nullptr) noexcept { + delete std::exchange(data_, pointer); + } + + Owner release() noexcept { + return std::exchange(data_, nullptr); + } + + void swap(UniquePtr& other) noexcept { + using std::swap; + swap(data_, other.data_); + } + + T* get() const noexcept { + return data_; + } + + operator bool() const noexcept { + return get() != nullptr; + } + + T& operator*() const noexcept(noexcept(*data_)) { + return *data_; + } + T* operator->() const noexcept { + return data_; + } + + private: + Owner data_; +}; + +template +class UniquePtr { // NOLINT + public: + UniquePtr() : data_(nullptr) {} + UniquePtr(UniquePtr const&) = delete; + UniquePtr(UniquePtr&& other) noexcept : data_(std::exchange(other.data_, nullptr)) {} + + UniquePtr(std::nullptr_t) noexcept : data_(nullptr) {} + + template + UniquePtr(ArrayPointer pointer) : data_(pointer) {} + + template + UniquePtr(UniquePtr&& other) noexcept : data_(other.release()) {} + + UniquePtr& operator=(UniquePtr const&) = delete; + UniquePtr& operator=(UniquePtr&& other) noexcept { + if (this != &other) { + reset(other.release()); + } + return *this; + } + + UniquePtr& operator=(std::nullptr_t) noexcept { + reset(); + return *this; + } + + template + UniquePtr& operator=(UniquePtr other) { + reset(other.release()); + return *this; + } + + ~UniquePtr() { + reset(); + } + + template + void reset(ArrayPointer pointer) noexcept { + delete[] std::exchange(data_, pointer); // NOLINT + } + + void reset(std::nullptr_t = nullptr) noexcept { + delete[] std::exchange(data_, nullptr); + } + + Owner release() noexcept { + return std::exchange(data_, nullptr); + } + + void swap(UniquePtr& other) noexcept { + std::swap(data_, other.data_); + } + + T* get() const noexcept { + return data_; + } + + operator bool() const noexcept { + return get() != nullptr; + } + + T& operator[](std::size_t index) const { + return *(data_ + index); + } + + private: + Owner data_{}; +}; + +template +bool operator==(UniquePtr const& lhs, UniquePtr const& rhs) { + return lhs.get() == rhs.get(); +} +template +bool operator==(UniquePtr const& lhs, std::nullptr_t) { + return lhs.get() == nullptr; +} +template +bool operator==(std::nullptr_t, UniquePtr const& rhs) { + return nullptr == rhs.get(); +} + +template +bool operator!=(UniquePtr const& lhs, UniquePtr const& rhs) { + return !(lhs == rhs); +} +template +bool operator!=(UniquePtr const& lhs, std::nullptr_t) { + return !(lhs == nullptr); +} +template +bool operator!=(std::nullptr_t, UniquePtr const& rhs) { + return !(nullptr == rhs); +} + +template +bool operator<(UniquePtr const& lhs, UniquePtr const& rhs) { + return lhs.get() < rhs.get(); +} +template +bool operator<(UniquePtr const& lhs, std::nullptr_t) { + return lhs.get() < nullptr; +} +template +bool operator<(std::nullptr_t, UniquePtr const& rhs) { + return nullptr < rhs.get(); +} + +template +bool operator>(UniquePtr const& lhs, UniquePtr const& rhs) { + return rhs < lhs; +} +template +bool operator>(UniquePtr const& lhs, std::nullptr_t) { + return nullptr < lhs; +} +template +bool operator>(std::nullptr_t, UniquePtr const& rhs) { + return rhs < nullptr; +} + +template +bool operator>=(UniquePtr const& lhs, UniquePtr const& rhs) { + return !(lhs < rhs); +} +template +bool operator>=(UniquePtr const& lhs, std::nullptr_t) { + return !(lhs < nullptr); +} +template +bool operator>=(std::nullptr_t, UniquePtr const& rhs) { + return !(nullptr < rhs); +} + +template +bool operator<=(UniquePtr const& lhs, UniquePtr const& rhs) { + return !(rhs < lhs); +} +template +bool operator<=(UniquePtr const& lhs, std::nullptr_t) { + return !(nullptr < lhs); +} +template +bool operator<=(std::nullptr_t, UniquePtr const& rhs) { + return !(rhs < nullptr); +} + +#endif \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_13/doc.rst b/docs/source/2022_2023_autumn/week_13/doc.rst new file mode 100644 index 0000000..5f3022c --- /dev/null +++ b/docs/source/2022_2023_autumn/week_13/doc.rst @@ -0,0 +1,6 @@ +单向链表:一个综合示例 +*********************** + +.. TODO: + +待完成 \ No newline at end of file diff --git a/docs/source/_todo/concept/doc.rst b/docs/source/_todo/concept/doc.rst deleted file mode 100644 index 9a32092..0000000 --- a/docs/source/_todo/concept/doc.rst +++ /dev/null @@ -1,10 +0,0 @@ -概念:直接表达代码意图 -************************ - -.. pull-quote:: - - `如果一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。——James Whitcomb Riley` - -.. TODO: - -待完成 \ No newline at end of file -- Gitee From 8d6c95964b8ad24ffa815966cc5852a1e5ffc885 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 24 Oct 2022 08:47:17 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E5=8F=82=E8=80=83=E8=B5=84=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/2022_2023_autumn/week_09/doc.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/source/2022_2023_autumn/week_09/doc.rst b/docs/source/2022_2023_autumn/week_09/doc.rst index 327e1b3..95c91f7 100644 --- a/docs/source/2022_2023_autumn/week_09/doc.rst +++ b/docs/source/2022_2023_autumn/week_09/doc.rst @@ -5,4 +5,5 @@ 待完成 -.. TODO: noexcept(noexcept(...)) \ No newline at end of file +.. TODO: noexcept(noexcept(...)) +.. TODO: https://en.cppreference.com/w/cpp/language/exceptions \ No newline at end of file -- Gitee From 64cb0d7f4ba82a8e0dbb0a3b390a5adb84c8d51a Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 24 Oct 2022 08:55:21 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8D=95=E5=90=91?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E4=BB=A3=E7=A0=81=E6=96=87=E4=BB=B6=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../week_13/code/forward_list.hpp | 10 ++++++++++ .../week_13/code/node_iterator.hpp | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 docs/source/2022_2023_autumn/week_13/code/node_iterator.hpp diff --git a/docs/source/2022_2023_autumn/week_13/code/forward_list.hpp b/docs/source/2022_2023_autumn/week_13/code/forward_list.hpp index b714002..3d9c321 100644 --- a/docs/source/2022_2023_autumn/week_13/code/forward_list.hpp +++ b/docs/source/2022_2023_autumn/week_13/code/forward_list.hpp @@ -1,4 +1,14 @@ #ifndef FORWARD_LIST_HPP #define FORWARD_LIST_HPP +#include "node.hpp" +#include "node_iterator.hpp" + +template +class ForwardList { + public: + + private: +}; + #endif \ No newline at end of file diff --git a/docs/source/2022_2023_autumn/week_13/code/node_iterator.hpp b/docs/source/2022_2023_autumn/week_13/code/node_iterator.hpp new file mode 100644 index 0000000..253047d --- /dev/null +++ b/docs/source/2022_2023_autumn/week_13/code/node_iterator.hpp @@ -0,0 +1,20 @@ +#ifndef NODE_ITERATOR_HPP +#define NODE_ITERATOR_HPP + +#include "node.hpp" + +template +class NodeIterator { + public: + + private: +}; + +template +class NodeConstIterator { + public: + + private: +}; + +#endif \ No newline at end of file -- Gitee