kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

function.hh (3689B)


      1 #ifndef function_hh_INCLUDED
      2 #define function_hh_INCLUDED
      3 
      4 #include "exception.hh"
      5 #include <utility>
      6 #include <type_traits>
      7 
      8 namespace Kakoune
      9 {
     10 
     11 template<typename Res, typename... Args>
     12 struct FunctionVTable
     13 {
     14     Res (*call)(void*, Args...);
     15     void* (*clone)(void*);
     16     void (*destroy)(void*);
     17 };
     18 
     19 template<typename Res, typename... Args>
     20 inline constexpr FunctionVTable<Res, Args...> unset_vtable{
     21     .call = [](void*, Args...) -> Res { throw runtime_error("called an empty function"); },
     22     .clone = [](void*) -> void* { return nullptr; },
     23     .destroy = [](void*) {},
     24 };
     25 
     26 template<bool Copyable, typename Target, typename Res, typename... Args>
     27 inline constexpr FunctionVTable<Res, Args...> vtable_for{
     28     .call = [](void* target, Args... args) -> Res { return (*reinterpret_cast<Target*>(target))(std::forward<Args>(args)...); },
     29     .clone = [](void* target) -> void* { if constexpr (Copyable) { return new Target(*reinterpret_cast<Target*>(target)); } return nullptr; },
     30     .destroy = [](void* target) { delete reinterpret_cast<Target*>(target); }
     31 };
     32 
     33 template<typename Res, typename... Args>
     34 inline constexpr FunctionVTable<Res, Args...> vtable_for_func{
     35     .call = [](void* target, Args... args) -> Res { return (*reinterpret_cast<Res (*)(Args...)>(target))(std::forward<Args>(args)...); },
     36     .clone = [](void* target) -> void* { return target; },
     37     .destroy = [](void* target) {}
     38 };
     39 
     40 template<bool Copyable, typename F>
     41 class FunctionImpl;
     42 
     43 template<bool Copyable, typename Res, typename... Args>
     44 class FunctionImpl<Copyable, Res (Args...)>
     45 {
     46 public:
     47     FunctionImpl() = default;
     48 
     49     template<typename Target>
     50         requires (not std::is_same_v<FunctionImpl, std::remove_cvref_t<Target>>)
     51     FunctionImpl(Target&& target)
     52     {
     53         using EffectiveTarget = std::remove_cvref_t<Target>;
     54         if constexpr (std::is_convertible_v<EffectiveTarget, Res(*)(Args...)>)
     55         {
     56             m_target = reinterpret_cast<void*>(static_cast<Res(*)(Args...)>(target));
     57             m_vtable = &vtable_for_func<Res, Args...>;
     58         }
     59         else
     60         {
     61             m_target = new EffectiveTarget(std::forward<Target>(target));
     62             m_vtable = &vtable_for<Copyable, EffectiveTarget, Res, Args...>;
     63         }
     64     }
     65 
     66     FunctionImpl(const FunctionImpl& other) requires Copyable
     67         : m_target(other.m_vtable->clone(other.m_target)), m_vtable(other.m_vtable)
     68     {
     69     }
     70 
     71     FunctionImpl(FunctionImpl&& other) : m_target(other.m_target), m_vtable(other.m_vtable)
     72     {
     73         other.m_target = nullptr;
     74         other.m_vtable = &unset_vtable<Res, Args...>;
     75     }
     76 
     77     ~FunctionImpl()
     78     {
     79         m_vtable->destroy(m_target);
     80     }
     81 
     82     FunctionImpl& operator=(const FunctionImpl& other) requires Copyable
     83     {
     84         m_vtable->destroy(m_target);
     85         m_target = other.m_vtable->clone(other.m_target);
     86         m_vtable = other.m_vtable;
     87         return *this;
     88     }
     89 
     90     FunctionImpl& operator=(FunctionImpl&& other)
     91     {
     92         m_vtable->destroy(m_target);
     93         m_target = other.m_target;
     94         m_vtable = other.m_vtable;
     95         other.m_target = nullptr;
     96         other.m_vtable = &unset_vtable<Res, Args...>;
     97         return *this;
     98     }
     99 
    100     Res operator()(Args... args) const
    101     {
    102         return m_vtable->call(m_target, std::forward<Args>(args)...);
    103     }
    104 
    105     explicit operator bool() const { return m_vtable != &unset_vtable<Res, Args...>; }
    106 
    107 private:
    108     void* m_target = nullptr;
    109     const FunctionVTable<Res, Args...>* m_vtable = &unset_vtable<Res, Args...>;
    110 };
    111 
    112 template<typename F>
    113 using Function = FunctionImpl<true, F>;
    114 
    115 template<typename F>
    116 using MoveOnlyFunction = FunctionImpl<false, F>;
    117 
    118 }
    119 
    120 #endif // function_hh_INCLUDED