aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/include
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-07-14 20:16:19 +0000
committerFelix Morgner <felix.morgner@ost.ch>2025-07-14 20:16:19 +0000
commit576935b6448802138639a324535614e0a966ead1 (patch)
treea3bbabccec415668942085cc9b0f2979f6b66adc /libs/kstd/include
parent763f38fff9336e40fce27565861e85c95d003a12 (diff)
downloadteachos-576935b6448802138639a324535614e0a966ead1.tar.xz
teachos-576935b6448802138639a324535614e0a966ead1.zip
libs: move unique_ptr to kstd
Diffstat (limited to 'libs/kstd/include')
-rw-r--r--libs/kstd/include/kstd/unique_pointer.hpp206
1 files changed, 206 insertions, 0 deletions
diff --git a/libs/kstd/include/kstd/unique_pointer.hpp b/libs/kstd/include/kstd/unique_pointer.hpp
new file mode 100644
index 0000000..2861646
--- /dev/null
+++ b/libs/kstd/include/kstd/unique_pointer.hpp
@@ -0,0 +1,206 @@
+#ifndef KSTD_UNIQUE_POINTER_HPP
+#define KSTD_UNIQUE_POINTER_HPP
+
+#include <utility>
+
+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.
+ *
+ * @tparam T Type of the managed object.
+ */
+ template<typename T>
+ struct unique_pointer
+ {
+ /**
+ * @brief Constructor.
+ *
+ * @param ptr A pointer to an object to manage (default is nullptr).
+ */
+ [[gnu::section(".stl_text")]]
+ explicit unique_pointer(T * ptr = nullptr)
+ : pointer(ptr)
+ {
+ // Nothing to do.
+ }
+
+ /**
+ * @brief Destructor that deletes the managed object.
+ */
+ [[gnu::section(".stl_text")]]
+ ~unique_pointer()
+ {
+ delete pointer;
+ }
+
+ /**
+ * @brief Deleted copy constructor to enforce unique ownership.
+ */
+ unique_pointer(const unique_pointer &) = delete;
+
+ /**
+ * @brief Deleted copy assignment operator to enforce unique ownership.
+ */
+ auto operator=(const unique_pointer &) -> unique_pointer & = delete;
+
+ /**
+ * @brief Move constructor.
+ *
+ * @param other Unique pointer to move from.
+ */
+ [[gnu::section(".stl_text")]]
+ unique_pointer(unique_pointer && other) noexcept
+ : pointer(other.pointer)
+ {
+ other.pointer = nullptr;
+ }
+
+ /**
+ * @brief Move assignment operator. Transfers ownership from other to *this as if by calling reset(r.release()).
+ *
+ * @param other Smart pointer from which ownership will be transferred.
+ * @return Reference to this unique pointer.
+ */
+ [[gnu::section(".stl_text")]]
+ auto operator=(unique_pointer && other) noexcept -> unique_pointer &
+ {
+ if (this != &other)
+ {
+ delete pointer;
+ pointer = other.pointer;
+ other.pointer = nullptr;
+ }
+ return *this;
+ }
+
+ /**
+ * @brief Dereference operator. If get() is a null pointer, the behavior is undefined.
+ *
+ * @return Returns the object owned by *this, equivalent to *get().
+ */
+ [[gnu::section(".stl_text")]]
+ auto operator*() const -> T &
+ {
+ return *pointer;
+ }
+
+ /**
+ * @brief Member access operator.
+ *
+ * @return Returns a pointer to the object owned by *this, i.e. get().
+ */
+ [[gnu::section(".stl_text")]]
+ auto operator->() const -> T *
+ {
+ return pointer;
+ }
+
+ /**
+ * @brief Returns a pointer to the managed object or nullptr if no object is owned.
+ *
+ * @return Pointer to the managed object or nullptr if no object is owned.
+ */
+ [[gnu::section(".stl_text")]]
+ auto get() const -> T *
+ {
+ return pointer;
+ }
+
+ /**
+ * @brief Checks whether *this owns an object, i.e. whether get() != nullptr.
+ *
+ * @return true if *this owns an object, false otherwise.
+ */
+ [[gnu::section(".stl_text")]]
+ explicit operator bool() const noexcept
+ {
+ return pointer != nullptr;
+ }
+
+ /**
+ * @brief Releases the ownership of the managed object, if any.
+ * get() returns nullptr after the call.
+ * The caller is responsible for cleaning up the object (e.g. by use of get_deleter()).
+ *
+ * @return Pointer to the managed object or nullptr if there was no managed object, i.e. the value which would be
+ * returned by get() before the call.
+ */
+ [[gnu::section(".stl_text")]]
+ auto release() -> T *
+ {
+ T * temp = pointer;
+ pointer = nullptr;
+ return temp;
+ }
+
+ /**
+ * @brief Replaces the managed object.
+ *
+ * @note A test for self-reset, i.e. whether ptr points to an object already managed by *this, is not performed,
+ * except where provided as a compiler extension or as a debugging assert. Note that code such as
+ * p.reset(p.release()) does not involve self-reset, only code like p.reset(p.get()) does.
+ *
+ * @param ptr Pointer to a new object to manage (default = nullptr).
+ */
+ [[gnu::section(".stl_text")]]
+ auto reset(T * ptr = nullptr) -> void
+ {
+ delete pointer;
+ pointer = ptr;
+ }
+
+ /**
+ * @brief Swaps the managed objects and associated deleters of *this and another unique_ptr object other.
+ *
+ * @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
+ {
+ using std::swap;
+ swap(pointer, other.pointer);
+ }
+
+ /**
+ * @brief Defaulted three-way comparator operator.
+ */
+ [[gnu::section(".stl_text")]]
+ auto operator<=>(const unique_pointer & other) const = default;
+
+ private:
+ T * pointer; ///< The managed pointer.
+ };
+
+ /**
+ * @brief Specializes the std::swap algorithm for stl::unique_ptr. Swaps the contents of lhs and rhs. Calls
+ * lhs.swap(rhs).
+ *
+ * @tparam T Type of the managed object.
+ * @param lhs, rhs Smart pointers whose contents to swap.
+ */
+ template<typename T>
+ auto swap(unique_pointer<T> & lhs, unique_pointer<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
+ * 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)...)).
+ *
+ * @tparam T Type of the managed object.
+ * @tparam Args Argument types for T's constructor.
+ * @param args List of arguments with which an instance of T will be constructed.
+ * @returns Unique_pointer of an instance of type T.
+ */
+ template<typename T, typename... Args>
+ auto make_unique(Args &&... args) -> unique_pointer<T>
+ {
+ return unique_pointer<T>(new T(std::forward<Args>(args)...));
+ }
+} // namespace kstd
+
+#endif \ No newline at end of file