diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2025-07-14 20:26:05 +0000 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2025-07-14 20:26:05 +0000 |
| commit | 4e99a7586748f9acd7027abc4c86a8df5f0c2e6f (patch) | |
| tree | 4070e954f5a6f7cc218e2a7b160f98a9e306c370 /libs | |
| parent | 23b75cc23b8bab97eb2803e5110641c0f04bfc80 (diff) | |
| download | teachos-4e99a7586748f9acd7027abc4c86a8df5f0c2e6f.tar.xz teachos-4e99a7586748f9acd7027abc4c86a8df5f0c2e6f.zip | |
kstd: improve resemblence of STL
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/include/kstd/bits/shared_ptr.hpp (renamed from libs/kstd/include/kstd/shared_pointer.hpp) | 48 | ||||
| -rw-r--r-- | libs/kstd/include/kstd/bits/unique_ptr.hpp (renamed from libs/kstd/include/kstd/unique_pointer.hpp) | 34 | ||||
| -rw-r--r-- | libs/kstd/include/kstd/memory.hpp | 7 |
3 files changed, 48 insertions, 41 deletions
diff --git a/libs/kstd/include/kstd/shared_pointer.hpp b/libs/kstd/include/kstd/bits/shared_ptr.hpp index 4717117..d41b165 100644 --- a/libs/kstd/include/kstd/shared_pointer.hpp +++ b/libs/kstd/include/kstd/bits/shared_ptr.hpp @@ -1,5 +1,5 @@ -#ifndef KSTD_SHARED_POINTER_HPP -#define KSTD_SHARED_POINTER_HPP +#ifndef KSTD_BITS_SHARED_PTR_HPP +#define KSTD_BITS_SHARED_PTR_HPP #include <atomic> @@ -7,10 +7,10 @@ namespace kstd { /** * @brief Shared_pointer is a smart pointer that retains shared ownership of an object through a pointer. Several - * shared_pointer objects may own the same object. The object is destroyed and its memory deallocated when either of - * the following happens: the last remaining shared_pointer owning the object is destroyed; the last remaining - * shared_pointer owning the object is assigned another pointer via operator= or reset(). A - * shared_pointer can share ownership of an object while storing a pointer to another object. This feature can be used + * shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of + * the following happens: the last remaining shared_ptr owning the object is destroyed; the last remaining + * shared_ptr owning the object is assigned another pointer via operator= or reset(). A + * shared_ptr can share ownership of an object while storing a pointer to another object. This feature can be used * to point to member objects while owning the object they belong to. The stored pointer is the one accessed by get(), * the dereference and the comparison operators. The managed pointer is the one passed to the deleter when use count * reaches zero. @@ -18,7 +18,7 @@ namespace kstd * @tparam T The type of the managed object. */ template<typename T> - struct shared_pointer + struct shared_ptr { /** * @brief Constructor. @@ -26,7 +26,7 @@ namespace kstd * @param pointer A pointer to an object to manage (default is nullptr). */ [[gnu::section(".stl_text")]] - explicit shared_pointer(T * pointer = nullptr) + explicit shared_ptr(T * pointer = nullptr) : pointer(pointer) , ref_count(new std::atomic<std::size_t>(pointer != nullptr ? 1 : 0)) { @@ -36,10 +36,10 @@ namespace kstd /** * @brief Copy constructor. * - * @param other The shared_pointer to copy from. + * @param other The shared_ptr to copy from. */ [[gnu::section(".stl_text")]] - shared_pointer(const shared_pointer & other) + shared_ptr(const shared_ptr & other) : pointer(other.pointer) , ref_count(other.ref_count) { @@ -52,10 +52,10 @@ namespace kstd /** * @brief Move constructor. * - * @param other The shared_pointer to move from. + * @param other The shared_ptr to move from. */ [[gnu::section(".stl_text")]] - shared_pointer(shared_pointer && other) noexcept + shared_ptr(shared_ptr && other) noexcept : pointer(other.pointer) , ref_count(other.ref_count) { @@ -72,7 +72,7 @@ namespace kstd * @return Reference to this shared pointer. */ [[gnu::section(".stl_text")]] - shared_pointer & operator=(const shared_pointer & other) + shared_ptr & operator=(const shared_ptr & other) { if (this != &other) { @@ -97,7 +97,7 @@ namespace kstd * @return Reference to this shared pointer. */ [[gnu::section(".stl_text")]] - shared_pointer & operator=(shared_pointer && other) noexcept + shared_ptr & operator=(shared_ptr && other) noexcept { if (this != &other) { @@ -115,7 +115,7 @@ namespace kstd * @brief Destructor. Cleans up resources if necessary. */ [[gnu::section(".stl_text")]] - ~shared_pointer() + ~shared_ptr() { cleanup(); } @@ -137,10 +137,10 @@ namespace kstd * @brief Exchanges the stored pointer values and the ownerships of *this and r. Reference counts, if any, are not * adjusted. * - * @param other The shared_pointer to swap with. + * @param other The shared_ptr to swap with. */ [[gnu::section(".stl_text")]] - void swap(shared_pointer & other) + void swap(shared_ptr & other) { std::swap(pointer, other.pointer); std::swap(ref_count, other.ref_count); @@ -180,7 +180,7 @@ namespace kstd } /** - * @brief Returns the number of different shared_pointer instances (*this included) managing the current object. If + * @brief Returns the number of different shared_ptr instances (*this included) managing the current object. If * there is no managed object, 0 is returned. * * @note Common use cases include comparison with 0. If use_count returns zero, the shared pointer is empty @@ -216,7 +216,7 @@ namespace kstd * @brief Defaulted three-way comparator operator. */ [[gnu::section(".stl_text")]] - auto operator<=>(const shared_pointer & other) const = default; + auto operator<=>(const shared_ptr & other) const = default; private: /** @@ -244,15 +244,15 @@ namespace kstd * @param lhs, rhs Smart pointers whose contents to swap. */ template<typename T> - auto swap(shared_pointer<T> & lhs, shared_pointer<T> & rhs) -> void + auto swap(shared_ptr<T> & lhs, shared_ptr<T> & rhs) -> void { lhs.swap(rhs); } /** - * @brief Constructs an object of type T and wraps it in a shared_pointer. Constructs a non-array type T. The + * @brief Constructs an object of type T and wraps it in a shared_ptr. Constructs a non-array type T. The * arguments args are passed to the constructor of T. This overload participates in overload resolution only if T is - * not an array type. The function is equivalent to: shared_pointer<T>(new T(std::forward<Args>(args)...)). + * not an array type. The function is equivalent to: shared_ptr<T>(new T(std::forward<Args>(args)...)). * * @tparam T Type of the managed object. * @tparam Args Argument types for T's constructor. @@ -260,9 +260,9 @@ namespace kstd * @returns Shared_pointer of an instance of type T. */ template<typename T, typename... Args> - auto make_shared(Args &&... args) -> shared_pointer<T> + auto make_shared(Args &&... args) -> shared_ptr<T> { - return shared_pointer<T>(new T(std::forward<Args>(args)...)); + return shared_ptr<T>(new T(std::forward<Args>(args)...)); } } // namespace kstd diff --git a/libs/kstd/include/kstd/unique_pointer.hpp b/libs/kstd/include/kstd/bits/unique_ptr.hpp index 2861646..1932913 100644 --- a/libs/kstd/include/kstd/unique_pointer.hpp +++ b/libs/kstd/include/kstd/bits/unique_ptr.hpp @@ -1,5 +1,5 @@ -#ifndef KSTD_UNIQUE_POINTER_HPP -#define KSTD_UNIQUE_POINTER_HPP +#ifndef KSTD_BITS_UNIQUE_POINTER_HPP +#define KSTD_BITS_UNIQUE_POINTER_HPP #include <utility> @@ -7,12 +7,12 @@ namespace kstd { /** * @brief Unique_pointer is a smart pointer that owns (is responsible for) and manages another object via a pointer - * and subsequently disposes of that object when the unique_pointer goes out of scope. + * and subsequently disposes of that object when the unique_ptr goes out of scope. * * @tparam T Type of the managed object. */ template<typename T> - struct unique_pointer + struct unique_ptr { /** * @brief Constructor. @@ -20,7 +20,7 @@ namespace kstd * @param ptr A pointer to an object to manage (default is nullptr). */ [[gnu::section(".stl_text")]] - explicit unique_pointer(T * ptr = nullptr) + explicit unique_ptr(T * ptr = nullptr) : pointer(ptr) { // Nothing to do. @@ -30,7 +30,7 @@ namespace kstd * @brief Destructor that deletes the managed object. */ [[gnu::section(".stl_text")]] - ~unique_pointer() + ~unique_ptr() { delete pointer; } @@ -38,12 +38,12 @@ namespace kstd /** * @brief Deleted copy constructor to enforce unique ownership. */ - unique_pointer(const unique_pointer &) = delete; + unique_ptr(const unique_ptr &) = delete; /** * @brief Deleted copy assignment operator to enforce unique ownership. */ - auto operator=(const unique_pointer &) -> unique_pointer & = delete; + auto operator=(const unique_ptr &) -> unique_ptr & = delete; /** * @brief Move constructor. @@ -51,7 +51,7 @@ namespace kstd * @param other Unique pointer to move from. */ [[gnu::section(".stl_text")]] - unique_pointer(unique_pointer && other) noexcept + unique_ptr(unique_ptr && other) noexcept : pointer(other.pointer) { other.pointer = nullptr; @@ -64,7 +64,7 @@ namespace kstd * @return Reference to this unique pointer. */ [[gnu::section(".stl_text")]] - auto operator=(unique_pointer && other) noexcept -> unique_pointer & + auto operator=(unique_ptr && other) noexcept -> unique_ptr & { if (this != &other) { @@ -157,7 +157,7 @@ namespace kstd * @param other Another unique_ptr object to swap the managed object and the deleter with. */ [[gnu::section(".stl_text")]] - auto swap(unique_pointer & other) -> void + auto swap(unique_ptr & other) -> void { using std::swap; swap(pointer, other.pointer); @@ -167,7 +167,7 @@ namespace kstd * @brief Defaulted three-way comparator operator. */ [[gnu::section(".stl_text")]] - auto operator<=>(const unique_pointer & other) const = default; + auto operator<=>(const unique_ptr & other) const = default; private: T * pointer; ///< The managed pointer. @@ -181,15 +181,15 @@ namespace kstd * @param lhs, rhs Smart pointers whose contents to swap. */ template<typename T> - auto swap(unique_pointer<T> & lhs, unique_pointer<T> & rhs) -> void + auto swap(unique_ptr<T> & lhs, unique_ptr<T> & rhs) -> void { lhs.swap(rhs); } /** - * @brief Constructs an object of type T and wraps it in a unique_pointer. Constructs a non-array type T. The + * @brief Constructs an object of type T and wraps it in a unique_ptr. Constructs a non-array type T. The * arguments args are passed to the constructor of T. This overload participates in overload resolution only if T is - * not an array type. The function is equivalent to: unique_pointer<T>(new T(std::forward<Args>(args)...)). + * not an array type. The function is equivalent to: unique_ptr<T>(new T(std::forward<Args>(args)...)). * * @tparam T Type of the managed object. * @tparam Args Argument types for T's constructor. @@ -197,9 +197,9 @@ namespace kstd * @returns Unique_pointer of an instance of type T. */ template<typename T, typename... Args> - auto make_unique(Args &&... args) -> unique_pointer<T> + auto make_unique(Args &&... args) -> unique_ptr<T> { - return unique_pointer<T>(new T(std::forward<Args>(args)...)); + return unique_ptr<T>(new T(std::forward<Args>(args)...)); } } // namespace kstd diff --git a/libs/kstd/include/kstd/memory.hpp b/libs/kstd/include/kstd/memory.hpp new file mode 100644 index 0000000..cab2fba --- /dev/null +++ b/libs/kstd/include/kstd/memory.hpp @@ -0,0 +1,7 @@ +#ifndef KSTD_SHARED_POINTER_HPP +#define KSTD_SHARED_POINTER_HPP + +#include "kstd/bits/shared_ptr.hpp" // IWYU pragma: export +#include "kstd/bits/unique_ptr.hpp" // IWYU pragma: export + +#endif
\ No newline at end of file |
