Kaynağa Gözat

feat(c++): add std::function

greatbridf 2 yıl önce
ebeveyn
işleme
f12a8f26ab
3 değiştirilmiş dosya ile 87 ekleme ve 0 silme
  1. 2 0
      CMakeLists.txt
  2. 75 0
      include/types/function.hpp
  3. 10 0
      src/types/libstdcpp.cpp

+ 2 - 0
CMakeLists.txt

@@ -66,6 +66,7 @@ set(KERNEL_MAIN_SOURCES src/fs/fat.cpp
                         src/kernel/event/event.cpp
                         src/types/bitmap.c
                         src/types/elf.cpp
+                        src/types/libstdcpp.cpp
                         include/asm/boot.h
                         include/asm/port_io.h
                         include/asm/sys.h
@@ -106,6 +107,7 @@ set(KERNEL_MAIN_SOURCES src/fs/fat.cpp
                         include/types/lock.hpp
                         include/types/string.hpp
                         include/types/vector.hpp
+                        include/types/function.hpp
                         include/kernel_main.hpp
                         )
 add_library(kernel_main STATIC ${KERNEL_MAIN_SOURCES})

+ 75 - 0
include/types/function.hpp

@@ -0,0 +1,75 @@
+#pragma once
+#include <types/cplusplus.hpp>
+
+namespace std {
+
+namespace __inner {
+
+    template <typename Ret, typename... Args>
+    class _function_base {
+    public:
+        constexpr _function_base() = default;
+        virtual constexpr ~_function_base() = default;
+        virtual constexpr Ret operator()(Args&&... args) const = 0;
+    };
+
+    template <typename FuncLike, typename Ret, typename... Args>
+    class _function : public _function_base<Ret, Args...> {
+    private:
+        FuncLike func;
+
+    public:
+        constexpr _function(FuncLike&& _func)
+            : func(types::forward<FuncLike>(_func))
+        {
+        }
+        constexpr ~_function() = default;
+
+        constexpr Ret operator()(Args&&... args) const override
+        {
+            return func(types::forward<Args>(args)...);
+        }
+    };
+
+} // namespace __inner
+
+template <typename>
+class function;
+
+template <typename Ret, typename... Args>
+class function<Ret(Args...)> {
+private:
+    char _data[sizeof(void*) * 2];
+    using fb_t = __inner::_function_base<Ret, Args...>;
+    constexpr fb_t* _f(void) const
+    {
+        return (fb_t*)_data;
+    }
+
+public:
+    template <typename FuncLike>
+    constexpr function(FuncLike&& func)
+    {
+        static_assert(sizeof(FuncLike) <= sizeof(_data));
+        new (_f()) __inner::_function<FuncLike, Ret, Args...>(types::forward<FuncLike>(func));
+    }
+
+    template <typename FuncPtr>
+    constexpr function(FuncPtr* funcPtr)
+    {
+        new (_f()) __inner::_function<typename types::traits::decay<FuncPtr>::type, Ret, Args...>(
+            types::forward<typename types::traits::decay<FuncPtr>::type>(funcPtr));
+    }
+
+    constexpr ~function()
+    {
+        _f()->~_function_base();
+    }
+
+    constexpr Ret operator()(Args... args) const
+    {
+        return (*_f())(types::forward<Args>(args)...);
+    }
+};
+
+} // namespace std

+ 10 - 0
src/types/libstdcpp.cpp

@@ -0,0 +1,10 @@
+#include <types/assert.h>
+
+void operator delete(void*)
+{
+    crash();
+}
+void operator delete(void*, unsigned int)
+{
+    crash();
+}