Преглед на файлове

feat(c++): impl. forward and move

greatbridf преди 2 години
родител
ревизия
0d79b8eae9
променени са 4 файла, в които са добавени 23 реда и са изтрити 8 реда
  1. 5 4
      include/types/allocator.hpp
  2. 14 0
      include/types/cplusplus.hpp
  3. 3 3
      include/types/list.hpp
  4. 1 1
      include/types/vector.hpp

+ 5 - 4
include/types/allocator.hpp

@@ -1,5 +1,6 @@
 #pragma once
 #include <kernel/mem.h>
+#include <types/cplusplus.hpp>
 #include <types/types.h>
 
 inline void* operator new(size_t, void* ptr)
@@ -47,13 +48,13 @@ public:
 template <typename T, typename... Args>
 T* kernel_allocator_new(Args&&... args)
 {
-    return allocator_traits<kernel_allocator<T>>::allocate_and_construct(args...);
+    return allocator_traits<kernel_allocator<T>>::allocate_and_construct(forward<Args>(args)...);
 }
 
 template <typename T, typename... Args>
 T* kernel_ident_allocator_new(Args&&... args)
 {
-    return allocator_traits<kernel_ident_allocator<T>>::allocate_and_construct(args...);
+    return allocator_traits<kernel_ident_allocator<T>>::allocate_and_construct(forward<Args>(args)...);
 }
 
 template <typename Allocator>
@@ -71,7 +72,7 @@ public:
     template <typename... Args>
     static value_type* construct(value_type* ptr, Args&&... args)
     {
-        new (ptr) value_type(args...);
+        new (ptr) value_type(forward<Args>(args)...);
         return ptr;
     }
 
@@ -79,7 +80,7 @@ public:
     static value_type* allocate_and_construct(Args&&... args)
     {
         auto* ptr = allocate(1);
-        construct(ptr, args...);
+        construct(ptr, forward<Args>(args)...);
         return ptr;
     }
 

+ 14 - 0
include/types/cplusplus.hpp

@@ -48,6 +48,20 @@ struct add_reference {
     using type = T&;
 };
 
+} // namespace types::traits
+
+namespace types {
+template <typename T>
+T&& move(T& val)
+{
+    return static_cast<T&&>(val);
+}
+template <typename T>
+T&& forward(typename traits::remove_reference<T>::type& val)
+{
+    return static_cast<T&&>(val);
+}
+
 } // namespace types
 
 #endif

+ 3 - 3
include/types/list.hpp

@@ -235,7 +235,7 @@ public:
     // insert the value v in front of the given iterator
     iterator_type insert(const iterator_type& iter, value_type&& v) noexcept
     {
-        node_base_type* new_node = allocator_traits<allocator_type>::allocate_and_construct(v);
+        node_base_type* new_node = allocator_traits<allocator_type>::allocate_and_construct(types::move(v));
         iterator_type ret(new_node);
         iter._node()->prev->connect(new_node);
         new_node->connect(iter._node());
@@ -257,7 +257,7 @@ public:
     template <typename... Args>
     iterator_type emplace_back(Args&&... args)
     {
-        return insert(end(), value_type(args...));
+        return insert(end(), value_type(forward<Args>(args)...));
     }
 
     void push_front(const value_type& v) noexcept
@@ -273,7 +273,7 @@ public:
     template <typename... Args>
     iterator_type emplace_front(Args&&... args)
     {
-        return insert(begin(), value_type(args...));
+        return insert(begin(), value_type(forward<Args>(args)...));
     }
 
     size_t size(void) const noexcept

+ 1 - 1
include/types/vector.hpp

@@ -233,7 +233,7 @@ public:
     template <typename... Args>
     iterator_type emplace_back(Args&&... args)
     {
-        push_back(value_type(args...));
+        push_back(value_type(forward<Args>(args)...));
         return back();
     }