Skip to content

Commit

Permalink
Move namespaces into their own classes, additional code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
SubstituteR committed Mar 10, 2023
1 parent 1a063c4 commit a2abfc3
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 173 deletions.
22 changes: 22 additions & 0 deletions concepts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <concepts>
#include <functional>
#include "moar_ptr.h"

namespace moar
{
namespace concepts
{
template<typename T>
concept Function = std::is_function_v<T>;

template<typename T>
concept CallingConvention = std::derived_from<T, types::calling_convention>;

template<typename T>
concept Variadic = std::is_same_v<T, types::variadic_t> || std::is_same_v<T, void>;

template<typename T>
concept CommonType = CallingConvention<T> || Variadic<T>;
}
}
175 changes: 2 additions & 173 deletions function_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,175 +6,13 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#pragma region Definitions

#if defined(__GNUC__) || defined(__clang__)
#define CC_CDECL __attribute__((cdecl))
#define CC_STDCALL __attribute__((stdcall))
#define CC_THISCALL __attribute__((thiscall))
#define CC_FASTCALL __attribute__((fastcall))
#define CC_VECTORCALL __attribute__((vectorcall))
#if defined(__INTEL_COMPILER)
#define CC_REGCALL __attribute__((regcall))
#else
#define CC_REGCALL CC_CDECL
#endif
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
#define CC_CDECL __cdecl
#define CC_STDCALL __stdcall
#define CC_THISCALL __thiscall
#define CC_FASTCALL __fastcall
#define CC_VECTORCALL __vectorcall
#if defined(__INTEL_COMPILER)
#define CC_REGCALL __regcall
#else
#define CC_REGCALL CC_CDECL
#endif
#else
#define CC_CDECL
#define CC_STDCALL
#define CC_THISCALL
#define CC_FASTCALL
#define CC_VECTORCALL
#define CC_REGCALL
#endif
#if defined(__clang__ ) /* Clang does not support B(A)(C...) syntax, only A B(C...) and refuses to compile otherwise. */
#define FUNCTION_SIGNATURE(x,y,z) x y (z)
#define FUNCTION_SIGNATURE_VA(x,y,z) x y (z, ...)
#elif defined(__GNUC__) || defined(_MSC_VER) || defined(__INTEL_COMPILER) /* GCC Supports A B(C...) and B(A)(C...) but produces wrong T* for T under A B(C...) */
#define FUNCTION_SIGNATURE(x,y,z) y(x)(z)
#define FUNCTION_SIGNATURE_VA(x,y,z) y(x)(z,...)
#endif

#pragma endregion

namespace moar
{
namespace types
{
struct variadic_t {};

struct calling_convention {};
struct cdecl_t : calling_convention {};
struct stdcall_t : calling_convention {};
struct thiscall_t : calling_convention {};
struct fastcall_t : calling_convention {};
struct vectorcall_t : calling_convention {};
struct regcall_t : calling_convention {};

using default_variadic = void;
using default_calling_convention = cdecl_t;
}
namespace concepts
{
template<typename T>
concept Function = std::is_function_v<T>;

template<typename T>
concept CallingConvention = std::derived_from<T, types::calling_convention>;

template<typename T>
concept Variadic = std::is_same_v<T, types::variadic_t> || std::is_same_v<T, void>;

template<typename T>
concept CommonType = CallingConvention<T> || Variadic<T>;
}
#pragma region Helpers
#if defined(__clang__ ) /* Ignore attribute warnings here to shut Clang up. */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wignored-attributes"
#endif
template<concepts::Function T1, concepts::CallingConvention T2, concepts::Variadic T3>
struct signature {
static_assert(sizeof(T2) == 0 || sizeof(T3) == 0, "Illegal Function Signature");
using type = void;
};

template<typename RT, typename ...A>
struct signature<RT(A...), types::cdecl_t, void> { using type = FUNCTION_SIGNATURE(CC_CDECL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::cdecl_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_CDECL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::stdcall_t, void> { using type = FUNCTION_SIGNATURE(CC_STDCALL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::stdcall_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_STDCALL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::thiscall_t, void> { using type = FUNCTION_SIGNATURE(CC_THISCALL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::fastcall_t, void> { using type = FUNCTION_SIGNATURE(CC_FASTCALL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::fastcall_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_FASTCALL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::vectorcall_t, void> { using type = FUNCTION_SIGNATURE(CC_VECTORCALL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::regcall_t, void> { using type = FUNCTION_SIGNATURE(CC_REGCALL, RT, A...); };

template<typename RT, typename ...A>
struct signature<RT(A...), types::regcall_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_REGCALL, RT, A...); };

namespace type_traits
{
/// <summary>
/// Selects T1 or T2 depending on if T1 or T2 is Variadic
/// </summary>
template<concepts::CommonType T1, concepts::CommonType T2>
struct SelectVariadic {
static_assert(sizeof(T1) == 0 || sizeof(T2) == 0, "Incorrect Usage of SelectVariadic (T1 and/or T2 missing.)");
using type = void;
};

template<concepts::CallingConvention T1, concepts::Variadic T2>
struct SelectVariadic<T1, T2> { using type = T2; };

template<concepts::Variadic T1, concepts::CallingConvention T2>
struct SelectVariadic<T1, T2> { using type = T1; };

/// <summary>
/// Selects T1 or T2 depending on if T1 or T2 is a Calling Convention
/// </summary>
template<concepts::CommonType T1, concepts::CommonType T2>
struct SelectCallingConvention {
static_assert(sizeof(T1) == 0 || sizeof(T2) == 0, "Incorrect Usage of SelectCallingConvention (T1 and/or T2 missing.)");
using type = void;
};

template<concepts::CallingConvention T1, concepts::Variadic T2>
struct SelectCallingConvention<T1, T2> { using type = T1; };

template<concepts::Variadic T1, concepts::CallingConvention T2>
struct SelectCallingConvention<T1, T2> { using type = T2; };

template<typename T>
struct SelectDefaultT3 {
static_assert(sizeof(T) == 0, "Incorrect Usage of SelectDefaultT3 (T missing.)");
using type = void;
};

template<concepts::Variadic T>
struct SelectDefaultT3<T> { using type = types::default_calling_convention; };

template<concepts::CallingConvention T>
struct SelectDefaultT3<T> { using type = types::default_variadic; };

}
#if defined(__clang__) /* Clang does not support B(A)(C...) syntax, only A B(C...) and refuses to compile otherwise. */
#pragma clang diagnostic pop
#endif
#pragma endregion

template<concepts::Function T1, concepts::CommonType T2 = types::default_calling_convention, concepts::CommonType T3 = typename type_traits::SelectDefaultT3<T2>::type>
class function_ptr {};

template<typename RT, concepts::CommonType T2, concepts::CommonType T3, typename ...A>
class function_ptr<RT(A...), T2, T3> : public extern_ptr<typename signature<RT(A...), typename type_traits::SelectCallingConvention<T2,T3>::type, typename type_traits::SelectVariadic<T2, T3>::type>::type> /* typename not declared yet. */
class function_ptr<RT(A...), T2, T3> : public extern_ptr<typename function_signature<RT(A...), typename type_traits::SelectCallingConvention<T2,T3>::type, typename type_traits::SelectVariadic<T2, T3>::type>::type> /* typename not declared yet. */
{
public:
/// <summary>
Expand Down Expand Up @@ -260,7 +98,7 @@ namespace moar
return mutable_ptr(args..., vargs...);
}
private:
using base = extern_ptr<typename signature<RT(A...), typename type_traits::SelectCallingConvention<T2, T3>::type, typename type_traits::SelectVariadic<T2, T3>::type>::type>;
using base = extern_ptr<typename function_signature<RT(A...), typename type_traits::SelectCallingConvention<T2, T3>::type, typename type_traits::SelectVariadic<T2, T3>::type>::type>;

base::element_type* mutable_ptr = 0;

Expand All @@ -277,13 +115,4 @@ namespace moar
{
std::size_t operator()(moar::function_ptr<T> const& p) const noexcept { return std::hash<T*>{}(p.get()); }
};

#undef CC_CDECL
#undef CC_STDCALL
#undef CC_THISCALL
#undef CC_FASTCALL
#undef CC_VECTORCALL
#undef CC_REGCALL
#undef FUNCTION_SIGNATURE
#undef FUNCTION_SIGNATURE_VA
}
109 changes: 109 additions & 0 deletions function_signature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#pragma once
#include <concepts>
#include <functional>
#include "moar_ptr.h"

#pragma region Definitions

#if defined(__GNUC__) || defined(__clang__)
#define CC_CDECL __attribute__((cdecl))
#define CC_STDCALL __attribute__((stdcall))
#define CC_THISCALL __attribute__((thiscall))
#define CC_FASTCALL __attribute__((fastcall))
#define CC_VECTORCALL __attribute__((vectorcall))
#if defined(__INTEL_COMPILER)
#define CC_REGCALL __attribute__((regcall))
#else
#define CC_REGCALL CC_CDECL
#endif
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
#define CC_CDECL __cdecl
#define CC_STDCALL __stdcall
#define CC_THISCALL __thiscall
#define CC_FASTCALL __fastcall
#define CC_VECTORCALL __vectorcall
#if defined(__INTEL_COMPILER)
#define CC_REGCALL __regcall
#else
#define CC_REGCALL CC_CDECL
#endif
#else
#define CC_CDECL
#define CC_STDCALL
#define CC_THISCALL
#define CC_FASTCALL
#define CC_VECTORCALL
#define CC_REGCALL
#endif
#if defined(__clang__ ) /* Clang does not support B(A)(C...) syntax, only A B(C...) and refuses to compile otherwise. */
#define FUNCTION_SIGNATURE(x,y,z) x y (z)
#define FUNCTION_SIGNATURE_VA(x,y,z) x y (z, ...)
#elif defined(__GNUC__) || defined(_MSC_VER) || defined(__INTEL_COMPILER) /* GCC Supports A B(C...) and B(A)(C...) but produces wrong T* for T under A B(C...) */
#define FUNCTION_SIGNATURE(x,y,z) y(x)(z)
#define FUNCTION_SIGNATURE_VA(x,y,z) y(x)(z,...)
#endif

#if defined(__clang__ ) /* Ignore attribute warnings here to shut Clang up. */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wignored-attributes"
#endif

#pragma endregion


namespace moar
{
template<concepts::Function T1, concepts::CallingConvention T2, concepts::Variadic T3>
struct function_signature {
static_assert(sizeof(T2) == 0 || sizeof(T3) == 0, "Illegal Function Signature");
using type = void;
};

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::cdecl_t, void> { using type = FUNCTION_SIGNATURE(CC_CDECL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::cdecl_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_CDECL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::stdcall_t, void> { using type = FUNCTION_SIGNATURE(CC_STDCALL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::stdcall_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_STDCALL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::thiscall_t, void> { using type = FUNCTION_SIGNATURE(CC_THISCALL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::fastcall_t, void> { using type = FUNCTION_SIGNATURE(CC_FASTCALL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::fastcall_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_FASTCALL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::vectorcall_t, void> { using type = FUNCTION_SIGNATURE(CC_VECTORCALL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::regcall_t, void> { using type = FUNCTION_SIGNATURE(CC_REGCALL, RT, A...); };

template<typename RT, typename ...A>
struct function_signature<RT(A...), types::regcall_t, types::variadic_t> { using type = FUNCTION_SIGNATURE_VA(CC_REGCALL, RT, A...); };
}


#pragma region Undefinitions

#if defined(__clang__)
#pragma clang diagnostic pop
#endif

#undef CC_CDECL
#undef CC_STDCALL
#undef CC_THISCALL
#undef CC_FASTCALL
#undef CC_VECTORCALL
#undef CC_REGCALL
#undef FUNCTION_SIGNATURE
#undef FUNCTION_SIGNATURE_VA

#pragma endregion
5 changes: 5 additions & 0 deletions moar_ptr.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#pragma once
#include "types.h"
#include "concepts.h"
#include "type_traits.h"

#include "extern_ptr.h"
#include "function_signature.h"
#include "function_ptr.h"
53 changes: 53 additions & 0 deletions type_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include <concepts>
#include <functional>
#include "moar_ptr.h"

namespace moar
{
namespace type_traits
{
/// <summary>
/// Selects T1 or T2 depending on if T1 or T2 is Variadic
/// </summary>
template<concepts::CommonType T1, concepts::CommonType T2>
struct SelectVariadic {
static_assert(sizeof(T1) == 0 || sizeof(T2) == 0, "Incorrect Usage of SelectVariadic (T1 and/or T2 missing.)");
using type = void;
};

template<concepts::CallingConvention T1, concepts::Variadic T2>
struct SelectVariadic<T1, T2> { using type = T2; };

template<concepts::Variadic T1, concepts::CallingConvention T2>
struct SelectVariadic<T1, T2> { using type = T1; };

/// <summary>
/// Selects T1 or T2 depending on if T1 or T2 is a Calling Convention
/// </summary>
template<concepts::CommonType T1, concepts::CommonType T2>
struct SelectCallingConvention {
static_assert(sizeof(T1) == 0 || sizeof(T2) == 0, "Incorrect Usage of SelectCallingConvention (T1 and/or T2 missing.)");
using type = void;
};

template<concepts::CallingConvention T1, concepts::Variadic T2>
struct SelectCallingConvention<T1, T2> { using type = T1; };

template<concepts::Variadic T1, concepts::CallingConvention T2>
struct SelectCallingConvention<T1, T2> { using type = T2; };

template<typename T>
struct SelectDefaultT3 {
static_assert(sizeof(T) == 0, "Incorrect Usage of SelectDefaultT3 (T missing.)");
using type = void;
};

template<concepts::Variadic T>
struct SelectDefaultT3<T> { using type = types::default_calling_convention; };

template<concepts::CallingConvention T>
struct SelectDefaultT3<T> { using type = types::default_variadic; };

}
}
Loading

0 comments on commit a2abfc3

Please sign in to comment.