Skip to content

Commit

Permalink
Improve TypeHelp.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
visuve committed Mar 30, 2024
1 parent 45cf7ec commit a5dd564
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
35 changes: 32 additions & 3 deletions HackLib/TypeHelp.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Pointer.hpp"
#include "Exceptions.hpp"

inline size_t SizeOfBasicType(std::type_index type)
{
Expand All @@ -26,7 +27,7 @@ inline size_t SizeOfBasicType(std::type_index type)
if (type == typeid(float*)) return sizeof(float*);
if (type == typeid(double*)) return sizeof(double*);

return 0;
throw ArgumentException("Unsupported type");
}

inline std::string_view BasicTypeToString(std::type_index type)
Expand All @@ -52,8 +53,8 @@ inline std::string_view BasicTypeToString(std::type_index type)
if (type == typeid(int64_t*)) return "int64_t*";
if (type == typeid(float*)) return "float*";
if (type == typeid(double*)) return "double*";
return "unknown type";

throw ArgumentException("Unsupported type");
}

inline bool IsBasicType(std::type_index type)
Expand Down Expand Up @@ -86,4 +87,32 @@ inline bool IsBasicPointer(std::type_index type)
if (type == typeid(double*)) return true;

return false;
}

// This order is completely arbitrary, but allows comparing std::type_index
inline uint8_t HacklibOrder(std::type_index type)
{
if (type == typeid(Pointer)) return 1;
if (type == typeid(uint8_t)) return 2;
if (type == typeid(uint16_t)) return 3;
if (type == typeid(uint32_t)) return 4;
if (type == typeid(uint64_t)) return 5;
if (type == typeid(int8_t)) return 6;
if (type == typeid(int16_t)) return 7;
if (type == typeid(int32_t)) return 8;
if (type == typeid(int64_t)) return 9;
if (type == typeid(float)) return 10;
if (type == typeid(double)) return 11;
if (type == typeid(uint8_t*)) return 12;
if (type == typeid(uint16_t*)) return 13;
if (type == typeid(uint32_t*)) return 14;
if (type == typeid(uint64_t*)) return 15;
if (type == typeid(int8_t*)) return 16;
if (type == typeid(int16_t*)) return 17;
if (type == typeid(int32_t*)) return 18;
if (type == typeid(int64_t*)) return 19;
if (type == typeid(float*)) return 20;
if (type == typeid(double*)) return 21;

throw ArgumentException("Unsupported type");
}
37 changes: 36 additions & 1 deletion HackLibTests/TypeHelpTests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
#include "TypeHelp.hpp"

TEST(TypeHelpTests, SizeOfBasicType)
{
EXPECT_EQ(SizeOfBasicType(typeid(Pointer)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(uint8_t)), 1);
EXPECT_EQ(SizeOfBasicType(typeid(uint16_t)), 2);
EXPECT_EQ(SizeOfBasicType(typeid(uint32_t)), 4);
EXPECT_EQ(SizeOfBasicType(typeid(uint64_t)), 8);
EXPECT_EQ(SizeOfBasicType(typeid(int8_t)), 1);
EXPECT_EQ(SizeOfBasicType(typeid(int16_t)), 2);
EXPECT_EQ(SizeOfBasicType(typeid(int32_t)), 4);
EXPECT_EQ(SizeOfBasicType(typeid(int64_t)), 8);
EXPECT_EQ(SizeOfBasicType(typeid(float)), 4);
EXPECT_EQ(SizeOfBasicType(typeid(double)), 8);
EXPECT_EQ(SizeOfBasicType(typeid(uint8_t*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(uint16_t*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(uint32_t*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(uint64_t*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(int8_t*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(int16_t*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(int64_t*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(float*)), Pointer::Size);
EXPECT_EQ(SizeOfBasicType(typeid(double*)), Pointer::Size);
EXPECT_THROW(SizeOfBasicType(typeid(HANDLE)), std::invalid_argument);
}

TEST(TypeHelpTests, ToString)
{
EXPECT_STREQ(BasicTypeToString(typeid(Pointer)).data(), "\"Pointer\"");
Expand All @@ -23,7 +48,7 @@ TEST(TypeHelpTests, ToString)
EXPECT_STREQ(BasicTypeToString(typeid(int64_t*)).data(), "int64_t*");
EXPECT_STREQ(BasicTypeToString(typeid(float*)).data(), "float*");
EXPECT_STREQ(BasicTypeToString(typeid(double*)).data(), "double*");
EXPECT_STREQ(BasicTypeToString(typeid(HANDLE)).data(), "unknown type");
EXPECT_THROW(BasicTypeToString(typeid(HANDLE)), std::invalid_argument);
}

TEST(TypeHelpTests, IsBasicType)
Expand Down Expand Up @@ -90,4 +115,14 @@ TEST(TypeHelpTests, IsPointer)
EXPECT_FALSE(IsBasicPointer(typeid(double)));

EXPECT_FALSE(IsBasicPointer(typeid(HANDLE)));
}

TEST(TypeHelpTests, Ordering)
{
EXPECT_LT(HacklibOrder(typeid(uint8_t)), HacklibOrder(typeid(uint64_t)));
EXPECT_LT(HacklibOrder(typeid(int8_t)), HacklibOrder(typeid(int64_t)));
EXPECT_LT(HacklibOrder(typeid(float)), HacklibOrder(typeid(double)));
EXPECT_LT(HacklibOrder(typeid(uint8_t*)), HacklibOrder(typeid(uint64_t*)));
EXPECT_LT(HacklibOrder(typeid(int8_t*)), HacklibOrder(typeid(int64_t*)));
EXPECT_LT(HacklibOrder(typeid(float*)), HacklibOrder(typeid(double*)));
}

0 comments on commit a5dd564

Please sign in to comment.