This commit is contained in:
blueloveTH 2024-05-13 15:16:01 +08:00
parent 7e2c4ea05f
commit f708fc4f98
3 changed files with 10 additions and 10 deletions

View File

@ -5,6 +5,9 @@
namespace pkpy { namespace pkpy {
template<typename T>
inline constexpr bool is_any_sso_v = is_pod_v<T> && sizeof(T) <= sizeof(void*);
struct any{ struct any{
struct vtable{ struct vtable{
const std::type_index type; const std::type_index type;
@ -13,7 +16,7 @@ struct any{
template<typename T> template<typename T>
inline static vtable* get(){ inline static vtable* get(){
static_assert(std::is_same_v<T, std::decay_t<T>>); static_assert(std::is_same_v<T, std::decay_t<T>>);
if constexpr (is_sso_v<T>){ if constexpr (is_any_sso_v<T>){
static vtable vt{ typeid(T), nullptr }; static vtable vt{ typeid(T), nullptr };
return &vt; return &vt;
}else{ }else{
@ -35,7 +38,7 @@ struct any{
using U = std::decay_t<T>; using U = std::decay_t<T>;
static_assert(!std::is_same_v<U, any>, "any(const any&) is deleted"); static_assert(!std::is_same_v<U, any>, "any(const any&) is deleted");
static_assert(sizeof(U) == sizeof(T)); static_assert(sizeof(U) == sizeof(T));
if constexpr (is_sso_v<U>){ if constexpr (is_any_sso_v<U>){
memcpy(&data, &value, sizeof(U)); memcpy(&data, &value, sizeof(U));
}else{ }else{
data = new U(std::forward<T>(value)); data = new U(std::forward<T>(value));
@ -58,7 +61,7 @@ struct any{
template<typename T> template<typename T>
T& _cast() const noexcept{ T& _cast() const noexcept{
static_assert(std::is_same_v<T, std::decay_t<T>>); static_assert(std::is_same_v<T, std::decay_t<T>>);
if constexpr (is_sso_v<T>){ if constexpr (is_any_sso_v<T>){
return *((T*)(&data)); return *((T*)(&data));
}else{ }else{
return *(static_cast<T*>(data)); return *(static_cast<T*>(data));

View File

@ -129,9 +129,6 @@ struct Type {
template<typename T> template<typename T>
inline constexpr bool is_pod_v = std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>; inline constexpr bool is_pod_v = std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>;
template<typename T>
inline constexpr bool is_sso_v = is_pod_v<T> && sizeof(T) <= sizeof(void*);
#define PK_ALWAYS_PASS_BY_POINTER(T) \ #define PK_ALWAYS_PASS_BY_POINTER(T) \
T(const T&) = delete; \ T(const T&) = delete; \
T& operator=(const T&) = delete; \ T& operator=(const T&) = delete; \

View File

@ -124,9 +124,9 @@ struct Mat3x3{
void add_module_linalg(VM* vm); void add_module_linalg(VM* vm);
static_assert(sizeof(Py_<Mat3x3>) <= 64); static_assert(sizeof(Py_<Mat3x3>) <= 64);
static_assert(std::is_trivially_copyable<Vec2>::value); static_assert(is_pod_v<Vec2>);
static_assert(std::is_trivially_copyable<Vec3>::value); static_assert(is_pod_v<Vec3>);
static_assert(std::is_trivially_copyable<Vec4>::value); static_assert(is_pod_v<Vec4>);
static_assert(std::is_trivially_copyable<Mat3x3>::value); static_assert(is_pod_v<Mat3x3>);
} // namespace pkpy } // namespace pkpy