From 004f2f336733df6d67f221f95b94e59e2c6e6d36 Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Thu, 21 Aug 2025 15:41:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E8=A6=81=E5=86=85=E5=AE=B9=E6=98=AF?= =?UTF-8?q?=E6=9B=B4=E5=8A=A0=E8=A7=84=E8=8C=83=E7=9A=84=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=9C=9F=E8=A1=8C=E4=B8=BA=E4=B8=8E=E5=9F=BA=E4=BA=8E=E7=BB=A7?= =?UTF-8?q?=E6=89=BF=E7=9A=84Object=E5=8F=8D=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CP.hpp | 16 ++ Core.hpp | 7 + detail/CP/Arithmetic.hpp | 63 +++++ detail/CP/CBool.hpp | 37 +++ detail/CP/CHash.hpp | 103 ++++++++ detail/CP/CWhile.hpp | 330 ++++++++++++++++++++++++ detail/CP/MaxMin.hpp | 42 ++++ detail/CP/Typen.hpp | 523 +++++++++++++++++++++++++++++++++++++++ detail/Core/Object.hpp | 86 +++++++ detail/Interface.h | 10 + 10 files changed, 1217 insertions(+) create mode 100644 CP.hpp create mode 100644 Core.hpp create mode 100644 detail/CP/Arithmetic.hpp create mode 100644 detail/CP/CBool.hpp create mode 100644 detail/CP/CHash.hpp create mode 100644 detail/CP/CWhile.hpp create mode 100644 detail/CP/MaxMin.hpp create mode 100644 detail/CP/Typen.hpp create mode 100644 detail/Core/Object.hpp create mode 100644 detail/Interface.h diff --git a/CP.hpp b/CP.hpp new file mode 100644 index 0000000..fba9505 --- /dev/null +++ b/CP.hpp @@ -0,0 +1,16 @@ +/** +* Compilation Period +*/ + +#pragma once +#ifndef __FILE_Operator_Hpp +#define __FILE_Operator_Hpp + +#include "detail/CP/CHash.hpp" +#include "detail/CP/CBool.hpp" +#include "detail/CP/CWhile.hpp" +#include "detail/CP/MaxMin.hpp" +#include "detail/CP/Arithmetic.hpp" +#include "detail/CP/Typen.hpp" + +#endif // __FILE_Operator_Hpp diff --git a/Core.hpp b/Core.hpp new file mode 100644 index 0000000..cdf2c90 --- /dev/null +++ b/Core.hpp @@ -0,0 +1,7 @@ +#pragma once +#ifndef __FILE_Core_Hpp +#define __FILE_Core_Hpp + +#include "detail/Core/Object.hpp" + +#endif // !__FILE_Core_Hpp diff --git a/detail/CP/Arithmetic.hpp b/detail/CP/Arithmetic.hpp new file mode 100644 index 0000000..4d65bc2 --- /dev/null +++ b/detail/CP/Arithmetic.hpp @@ -0,0 +1,63 @@ +#pragma once +#ifndef __FILE_Detail_CP_Arithmetic_Hpp +#define __FILE_Detail_CP_Arithmetic_Hpp + +#include "detail/CP/CHash.hpp" + +namespace Internal +{ + /** + * Traits class which tests if a type is arithmetic. + */ + template + struct TIsArithmeticTool + { + constexpr static bool Value = false; + }; + + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + template <> struct TIsArithmeticTool { constexpr static bool Value = true; }; + + template struct TIsArithmeticTool { constexpr static bool Value = TIsArithmeticTool::Value; }; + template struct TIsArithmeticTool< volatile T> { constexpr static bool Value = TIsArithmeticTool::Value; }; + template struct TIsArithmeticTool { constexpr static bool Value = TIsArithmeticTool::Value; }; +} + +template constexpr bool TIsArithmetic = Internal::TIsArithmeticTool::Value; + +namespace Internal +{ + /** + * Traits class which tests if a type is a signed integral type. + */ + template + struct TIsSignedTool + { + constexpr static bool Value = false; + }; + + template <> struct TIsSignedTool { constexpr static bool Value = true; }; + template <> struct TIsSignedTool { constexpr static bool Value = true; }; + template <> struct TIsSignedTool { constexpr static bool Value = true; }; + template <> struct TIsSignedTool { constexpr static bool Value = true; }; + + template struct TIsSignedTool { constexpr static bool Value = TIsSignedTool::Value; }; + template struct TIsSignedTool< volatile T> { constexpr static bool Value = TIsSignedTool::Value; }; + template struct TIsSignedTool { constexpr static bool Value = TIsSignedTool::Value; }; +} + +template constexpr bool TIsSigned = Internal::TIsSignedTool::Value; + +#endif // !__FILE_Detail_CP_Arithmetic_Hpp diff --git a/detail/CP/CBool.hpp b/detail/CP/CBool.hpp new file mode 100644 index 0000000..282df8f --- /dev/null +++ b/detail/CP/CBool.hpp @@ -0,0 +1,37 @@ +#pragma once +#ifndef __FILE_Detail_CP_CBool_Hpp +#define __FILE_Detail_CP_CBool_Hpp + +#define Operator(name,operator,closer)\ +template bool name(const First& first, const Second& second) { return closer(first) operator closer(second); }\ +template bool name(const First& first, const Args&... args) { return closer(first) operator name(args...);} +#define TrueCloser(x) (!!x) + +Operator(And, &&, TrueCloser); +Operator(Or, || , TrueCloser); + +#undef Operator +#undef TrueCloser + +namespace Internal +{ + template class CAnd_t; + template class COr_t; + + template<> class CAnd_t : public std::true_type {}; + template<> class CAnd_t : public std::false_type {}; + template class CAnd_t : public CAnd_t {}; + template class CAnd_t : public std::false_type {}; + + template<> class COr_t : public std::true_type {}; + template<> class COr_t : public std::false_type {}; + template class COr_t : public std::true_type {}; + template class COr_t : public COr_t {}; +} + +template constexpr bool CAnd = Internal::CAnd_t::value; +template constexpr bool COr = Internal::COr_t::value; +template constexpr bool CXor = CAnd != COr; +template constexpr bool CNot = false == Value; + +#endif // !__FILE_Detail_CP_CBool_Hpp diff --git a/detail/CP/CHash.hpp b/detail/CP/CHash.hpp new file mode 100644 index 0000000..346a4db --- /dev/null +++ b/detail/CP/CHash.hpp @@ -0,0 +1,103 @@ +#pragma once +#ifndef __FILE_Detail_CP_CHash_Hpp +#define __FILE_Detail_CP_CHash_Hpp + +// +// Hash functions for common types. +// + +inline uint32_t Hash(const uint8_t A) +{ + return A; +} + +inline uint32_t Hash(const int8_t A) +{ + return A; +} + +inline uint32_t Hash(const uint16_t A) +{ + return A; +} + +inline uint32_t Hash(const int16_t A) +{ + return A; +} + +inline uint32_t Hash(const int32_t A) +{ + return A; +} + +inline uint32_t Hash(const uint32_t A) +{ + return A; +} + +inline uint32_t Hash(const uint64_t A) +{ + return (uint32_t)A + ((uint32_t)(A >> 32) * 23); +} + +inline uint32_t Hash(const int64_t A) +{ + return (uint32_t)A + ((uint32_t)(A >> 32) * 23); +} + +constexpr uint32_t CHash(const uint8_t A) +{ + return A; +} + +constexpr uint32_t CHash(const int8_t A) +{ + return A; +} + +constexpr uint32_t CHash(const uint16_t A) +{ + return A; +} + +constexpr uint32_t CHash(const int16_t A) +{ + return A; +} + +constexpr uint32_t CHash(const int32_t A) +{ + return A; +} + +constexpr uint32_t CHash(const uint32_t A) +{ + return A; +} + +constexpr uint32_t CHash(const uint64_t A) +{ + return (uint32_t)A + ((uint32_t)(A >> 32) * 23); +} + +constexpr uint32_t CHash(const int64_t A) +{ + return (uint32_t)A + ((uint32_t)(A >> 32) * 23); +} + +constexpr uint32_t CHash(const char* str, uint32_t hash = 5381) +{ + return *str == '\0' ? hash : CHash(str + 1, ((hash << 5) + hash) + *str); +} + +constexpr uint32_t CHash(std::string_view str, uint32_t hash = 5381) +{ + for (size_t i = 0; i < str.length(); ++i) + { + hash = ((hash << 5) + hash) + str[i]; + } + return hash; +} + +#endif // !__FILE_Detail_CP_CHash_Hpp diff --git a/detail/CP/CWhile.hpp b/detail/CP/CWhile.hpp new file mode 100644 index 0000000..ff7ea97 --- /dev/null +++ b/detail/CP/CWhile.hpp @@ -0,0 +1,330 @@ +#pragma once +#ifndef __FILE_Detail_CP_CWhile_Hpp +#define __FILE_Detail_CP_CWhile_Hpp + +struct CRange +{ + template class Callable, size_t End> + constexpr static void Range() + { + Range(); + } + + template class Callable, int Start, int Stop, int Step = 1> + constexpr static void Range() + { + if constexpr (Step == 0) + static_assert(Step == 0, "Step not support 0"); + Callable call; + call(); + if constexpr (Start < Stop && Start + Step > Stop) + static_assert(Start < Stop && Start + Step > Stop, "While not stop"); + if constexpr (Start > Stop && Start + Step < Stop) + static_assert(Start > Stop && Start + Step < Stop, "While not stop"); + if constexpr (Start + Step != Stop) + Range(); + } +}; + +template +struct CWhile +{ + template class Callable, template class Pr, size_t MaxDepth, typename... Args> + constexpr static void WhileWithArgs(Args... args) + { + Callable call; + call(args...); + Pr pr; + if constexpr (pr(args...)) + { + if constexpr (MaxDepth != 0) + { + WhileWithArgs(args...); + } + else + { + static_assert(MaxDepth == 0, "WhileWithArgs is touch MaxDepth"); + } + } + } + + template class Pr, size_t MaxDepth, typename... Args> + constexpr static void WhileWithArgs(Args... args) + { + Callable call; + call(); + Pr pr; + if constexpr (pr(args...)) + { + if constexpr (MaxDepth != 0) + { + WhileWithArgs(args...); + } + else + { + static_assert(MaxDepth == 0, "WhileWithArgs is touch MaxDepth"); + } + } + } + + template class Callable, typename Pr, size_t MaxDepth, typename... Args> + constexpr static void WhileWithArgs(Args... args) + { + Callable call; + call(args...); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + WhileWithArgs(args...); + } + else + { + static_assert(MaxDepth == 0, "WhileWithArgs is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, typename Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, typename Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, template class Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } + + template class Callable, typename Pr, size_t MaxDepth, int Start, int Stop, int Step> + constexpr static void WhileWithTemplate() + { + Callable call; + call(); + Pr pr; + if constexpr (pr()) + { + if constexpr (MaxDepth != 0) + { + if constexpr (Start + Step != Stop) + WhileWithTemplate(); + } + else + { + static_assert(MaxDepth == 0, "WhileWithTemplate is touch MaxDepth"); + } + } + } +}; + +#endif // !__FILE_Detail_CP_CWhile_Hpp diff --git a/detail/CP/MaxMin.hpp b/detail/CP/MaxMin.hpp new file mode 100644 index 0000000..451c399 --- /dev/null +++ b/detail/CP/MaxMin.hpp @@ -0,0 +1,42 @@ +#pragma once +#ifndef __FILE_Detail_CP_MaxMin_Hpp +#define __FILE_Detail_CP_MaxMin_Hpp + +namespace Internal +{ + template struct MaxMinFoucsValueConditionalTool + { + constexpr static bool ABS = (!!sizeof(A) > sizeof(B)); + using AF = std::conditional_t, + std::conditional_t < ABS, A, B>, + A + >; + using BF = std::conditional_t, + B, + std::conditional_t < ABS, A, B> + >; + using Type = std::conditional_t < std::is_floating_point_v, AF, BF>; + }; +} + +template using MaxMinFoucsValueConditional = typename Internal::MaxMinFoucsValueConditionalTool::Type; + +template auto Max(const First& first, const Second& second) +{ + return std::max>(first, second); +} +template auto Max(const First& first, const Second& second, const Args&... args) +{ + return Max(std::max>(first, second), args...); +}; +template auto Min(const First& first, const Second& second) +{ + return std::min>(first, second); +} +template auto MIn(const First& first, const Second& second, const Args&... args) +{ + return Min(std::min>(first, second), args...); +}; + +#endif // !__FILE_Detail_CP_MaxMin_Hpp + diff --git a/detail/CP/Typen.hpp b/detail/CP/Typen.hpp new file mode 100644 index 0000000..a3df7ea --- /dev/null +++ b/detail/CP/Typen.hpp @@ -0,0 +1,523 @@ +#pragma once +#ifndef __FILE_Detail_CP_Typen_Hpp +#define __FILE_Detail_CP_Typen_Hpp + +#include "detail/CP/CHash.hpp" + +template constexpr bool TIsBaseOf = std::is_base_of_v; + +template constexpr bool TAreSame = std::is_same_v; + +namespace Internal +{ + template struct TConditionalTool + { + + }; + template + struct TConditionalTool + { + using Type = typename TConditionalTool::Type; + }; + template + struct TConditionalTool + { + using Type = First; + }; + template + struct TConditionalTool + { + using Type = First; + }; + template + struct TConditionalTool + { + using Type = std::conditional_t; + }; +} + +template using TChoose = typename Internal::TConditionalTool::Type; +template using TConditional = typename Internal::TConditionalTool::Type; + +template using TDecay = std::decay_t; + +template constexpr bool TIsP = std::is_pointer_v; +template constexpr bool TIsC = std::is_const_v; +template constexpr bool TIsV = std::is_volatile_v; +template constexpr bool TIsR = std::is_reference_v; +template constexpr bool TIsF = std::is_function_v; +template constexpr bool TIsLR = std::is_lvalue_reference_v; +template constexpr bool TIsRR = std::is_rvalue_reference_v; +template constexpr bool TIsCV = TIsC && TIsV; +template constexpr bool TIsIt = TIsP || TIsLR())>; +template constexpr bool TIsVoid = std::is_same_v; +template constexpr bool THasVirtual = std::has_virtual_destructor_v; +template constexpr bool TIsCStr = TAreSame&& TAreSame; + +template using TRemoveP = std::remove_pointer_t; +template using TRemoveC = std::remove_const_t; +template using TRemoveV = std::remove_volatile_t; +template using TRemoveR = std::remove_reference_t; +template using TRemoveCV = std::remove_cv_t; + +template using TEnableIf = std::enable_if_t; + +namespace Internal +{ + template + struct StringAbleTool + { + template ()))> + static constexpr bool check_std_to_string(U*) { return true; } + + template + static constexpr bool check_std_to_string(...) { return false; } + + template ()))> + static constexpr bool check_to_string(U*) { return true; } + + template + static constexpr bool check_to_string(...) { return false; } + + template ().ToString)> + static constexpr bool check_self_to_string(U*) { return true; } + + template + static constexpr bool check_self_to_string(...) { return false; } + + static constexpr bool Value = check_std_to_string(nullptr) || check_to_string(nullptr) || check_self_to_string(nullptr); + + template ()))> + static constexpr bool check_atoi_parse(U*) { return true; } + + template + static constexpr bool check_atoi_parse(...) { return true; } + + template ()))> + static constexpr bool check_atof_parse(U*) { return true; } + + template + static constexpr bool check_atof_parse(...) { return true; } + + template ()))> + static constexpr bool check_atol_parse(U*) { return true; } + + template + static constexpr bool check_atol_parse(...) { return true; } + + template ()))> + static constexpr bool check_atoll_parse(U*) { return true; } + + template + static constexpr bool check_atoll_parse(...) { return true; } + + template (),(const char*const)nullptr,0))> + static constexpr bool check_parse_string(U*) { return true; } + + template + static constexpr bool check_parse_string(...) { return false; } + + template ().Parse((const char* const)nullptr, 0))> + static constexpr bool check_self_parse(U*) { return true; } + + template + static constexpr bool check_self_parse(...) { return false; } + }; +} + +template constexpr bool THasToString = Internal::StringAbleTool::Value; + +namespace Internal +{ + template + struct TIsConvertibleImpl + { + private: + static uint_fast8_t Test(...); + static uint_fast16_t Test(To); + + public: + enum { Value = sizeof(Test((From)std::declval())) - 1 }; + }; +} + +template constexpr bool TIsConvertible = Internal::TIsConvertibleImpl::Value; +template constexpr bool TIsPointerConvertible = TIsConvertible; +template constexpr bool TIsReferenceConvertible = TIsConvertible; + +namespace Internal +{ + /** + * Copies the cv-qualifiers from one type to another, e.g.: + * + * TCopyQualifiers::Type == const T2 + * TCopyQualifiers::Type == const volatile T2 + */ + template struct TCopyQualifiersTool { typedef To Type; }; + template struct TCopyQualifiersTool { typedef const To Type; }; + template struct TCopyQualifiersTool< volatile From, To> { typedef volatile To Type; }; + template struct TCopyQualifiersTool { typedef const volatile To Type; }; +} + +template using TCopyQualifiers = typename Internal::TCopyQualifiersTool::Type; + +template constexpr bool TLosesQualifiers = !TAreSame, To>; + +namespace Internal +{ + struct PrettyFunctionTag {}; + + template + constexpr std::string_view PrettyFunction() + { + return __FUNCSIG__; + } + + template + constexpr std::string_view PrettyFunction() + { + return __FUNCSIG__; + } + + constexpr size_t GetPrettyFunctionPrefix() + { + auto a = PrettyFunction(); + auto b = PrettyFunction(); + size_t result = 0; + size_t end = std::min(a.size(), b.size()); + for (; result < end; result++) + { + if (a.substr(0, result) != b.substr(0, result)) + return result; + } + return result; + } + + constexpr size_t GetPrettyFunctionSuffix() + { + auto a = PrettyFunction(); + auto b = PrettyFunction(); + size_t result = 0; + size_t end = std::min(a.size(), b.size()); + for (; result < end; result++) + { + if (a.substr(a.size() - result - 1, end) != b.substr(b.size() - result - 1, end)) + return result; + } + return result; + } + + template + constexpr size_t GetPrettyFunctionWithValuePrefix() + { + auto a = PrettyFunction(0)>(); + auto b = PrettyFunction(1)>(); + size_t result = 0; + size_t end = std::min(a.size(), b.size()); + for (; result < end; result++) + { + if (a.substr(0, result) != b.substr(0, result)) + return result; + } + return result; + } + + template + constexpr size_t GetPrettyFunctionWithValueSuffix() + { + auto a = PrettyFunction(0)>(); + auto b = PrettyFunction(1)>(); + size_t result = 0; + size_t end = std::min(a.size(), b.size()); + for (; result < end; result++) + { + if (a.substr(a.size() - result - 1, end) != b.substr(b.size() - result - 1, end)) + return result; + } + return result; + } + + template + constexpr std::string_view SymbolNameTool() + { + auto name = PrettyFunction(); + name.remove_prefix(GetPrettyFunctionPrefix() - 1); + name.remove_suffix(GetPrettyFunctionSuffix()); + return name; + } + + template + constexpr std::string_view ValueNameTool() + { + auto name = PrettyFunction(); + name.remove_prefix(GetPrettyFunctionWithValuePrefix() - 1); + name.remove_suffix(GetPrettyFunctionWithValueSuffix()); + return name; + } + + template struct TraitTool + { + template static constexpr bool Is() { return (TAreSame || (Derived && TIsBaseOf)); } + template static constexpr bool Is(P) { return (TAreSame || (Derived && TIsBaseOf)); } + template static constexpr bool Is(P from, T& to) + { + to = static_cast(from); + return (TAreSame || (Derived && TIsBaseOf)); + } + static bool Is(void* ptr) + { + using TSymbol = decltype(Symbol()); + return Symbol() == *reinterpret_cast(reinterpret_cast(ptr) - sizeof(TSymbol)); + } + static intptr_t Symbol() + { + static auto symbol = typeid(T).hash_code(); + return (intptr_t)&symbol; + } + static T* New(void* memory, size_t capacity) + { + using TSymbol = decltype(Symbol()); + constexpr auto size = sizeof(TSymbol) + sizeof(T); + if (capacity < size || memory == nullptr) + return nullptr; + char* ptr = new(memory) char[size]; + auto&& head = reinterpret_cast(*ptr); + head = Symbol(); + T* result = reinterpret_cast(&(ptr[sizeof(TSymbol)])); + return result; + } + static void Delete(T* ptr) + { + using TSymbol = decltype(Symbol()); + *reinterpret_cast(reinterpret_cast(ptr) - sizeof(TSymbol)) = 0; + ptr->~T(); + } + static T* FirstInMemory(void* memory, size_t capacity) + { + for (char* head = (char*)memory, *end = (char*)memory + capacity; head != end; head++) + { + if (Is(head)) + return (T*)head; + } + return nullptr; + } + + static bool WriteBinary(T* object, char* buffer, size_t capacity) + { + if (sizeof(T) + sizeof(decltype(Symbol())) > capacity) + return false; + *reinterpret_cast(&buffer[0]) = Symbol(); + memcpy_s(&buffer[sizeof(decltype(Symbol()))], capacity, object, sizeof(T)); + return true; + } + static bool Write(T* object, char* buffer, size_t capacity, bool is_allow_binary_write = false) + { + if constexpr (StringAbleTool::check_self_to_string(nullptr)) + { + auto str = object->ToString(); + size_t strCsize = 0; + if constexpr (TIsCStr) + { + constexpr auto _strCsize = sizeof(decltype(*str[0])); + strCsize = _strCsize; + } + else + { + constexpr auto _strCsize = sizeof(decltype(*str.begin())); + strCsize = _strCsize; + } + if (strCsize == 0) + return false; + if (str.size() * strCsize > capacity) + return false; + memcpy_s(buffer, capacity, str.c_str(), str.size() * strCsize); + } + else if constexpr (StringAbleTool::check_to_string(nullptr)) + { + auto str = to_string(*object); + size_t strCsize = 0; + if constexpr (TIsCStr) + { + constexpr auto _strCsize = sizeof(decltype(*str[0])); + strCsize = _strCsize; + } + else + { + constexpr auto _strCsize = sizeof(decltype(*str.begin())); + strCsize = _strCsize; + } + if (strCsize == 0) + return false; + if (str.size() * strCsize > capacity) + return false; + memcpy_s(buffer, capacity, str.c_str(), str.size() * strCsize); + } + else if constexpr (StringAbleTool::check_std_to_string(nullptr)) + { + std::string str = std::to_string(*object); + if (str.size() > capacity) + return false; + memcpy_s(buffer, capacity, str.c_str(), str.size()); + } + else + { + if (is_allow_binary_write == false) + return false; + WriteBinary(object, buffer, capacity); + } + return true; + } + static T* ReadBinaryWithoutToken(T* object, char* buffer, size_t capacity) + { + constexpr auto offset = sizeof(decltype(Symbol())); + if (capacity > sizeof(T) + offset && *reinterpret_cast(&buffer[0]) == Symbol()) + memcpy(object, &buffer[offset], capacity); + else + { + if (capacity > sizeof(T)) + memcpy(object, buffer, capacity); + else + return nullptr; + } + return object; + } + static T* ReadBinary(void* memory, size_t capacity, char* buffer, size_t buffer_capacity) + { + constexpr auto size = sizeof(T) + sizeof(decltype(Symbol())); + if (size > buffer_capacity) + return nullptr; + if (size > capacity) + return nullptr; + if (*reinterpret_cast(&buffer[0]) != Symbol()) + return nullptr; + T* object = reinterpret_cast(&buffer[sizeof(decltype(Symbol()))]); + memcpy(memory, buffer, size); + return object; + } + static bool Parse(T* object, const char* const buffer, size_t capacity) + { + if constexpr (StringAbleTool::check_self_parse(nullptr)) + { + object->Parse(buffer, capacity); + } + else if constexpr (StringAbleTool::check_parse_string(nullptr)) + { + from_string(object, buffer, capacity); + } + else if constexpr (TAreSame) + { + if (strcmp(buffer, "true") == 0) + *object = true; + else if (strcmp(buffer, "false") == 0) + *object = false; + else + return false; + } + else if constexpr (StringAbleTool::check_atoll_parse(nullptr)) + { + *object = static_cast(atoll(buffer)); + } + else if constexpr (StringAbleTool::check_atol_parse(nullptr)) + { + *object = static_cast(atol(buffer)); + } + else if constexpr (StringAbleTool::check_atoi_parse(nullptr)) + { + *object = static_cast(atoi(buffer)); + } + else if constexpr (StringAbleTool::check_atof_parse(nullptr)) + { + *object = static_cast(atof(buffer)); + } + else + { + return false; + } + return true; + } + + constexpr static std::string_view SymbolName() + { + return SymbolNameTool(); + } + template + constexpr static std::string_view ValueName() + { + return ValueNameTool(); + } + + static uint32_t Hash(const T& v) + { + if constexpr (std::is_integral_v) + return GetTypeHash(v); + else + return GetTypeHash(&v); + } + private: + constexpr static uint32_t InjectTypeHash() + { + auto str = SymbolName(); + return CHash(str.data()); + } + public: + constexpr static int TypeHash = InjectTypeHash(); + }; + template struct TraitTool : public TraitTool {}; + template struct TraitTool< volatile T> : public TraitTool {}; + template struct TraitTool : public TraitTool {}; +} + +template using TTrait = Internal::TraitTool; +using Bool = Internal::TraitTool; +using Int = Internal::TraitTool; +using Float = Internal::TraitTool; +using Double = Internal::TraitTool; +using Long = Internal::TraitTool; +using UInt = Internal::TraitTool; +using Int8 = Internal::TraitTool; +using Int16 = Internal::TraitTool; +using Int32 = Internal::TraitTool; +using Int64 = Internal::TraitTool; +using UInt8 = Internal::TraitTool; +using UInt16 = Internal::TraitTool; +using UInt32 = Internal::TraitTool; +using UInt64 = Internal::TraitTool; +using LongDouble = Internal::TraitTool; + +#if !defined(nameofT)&&!defined(nameofEnum) +template constexpr auto __Inject_nameof() +{ + return TTrait::SymbolName(); +} +template constexpr auto __Inject_nameof() +{ + return TTrait::ValueName(); +} +#define nameofT(x) __Inject_nameof(); +#define nameofEnum(x) __Inject_nameof(); +#else +template constexpr auto nameof() +{ + return TTrait::SymbolName(); +} +template constexpr auto nameof() +{ + return TTrait::ValueName(); +} +#endif // !nameof + + +template using TUnwrapped = TEnableIf, TConditional, TRemoveP, decltype(*std::declval())>>; +template > decltype(auto) Unwrapping(T from) +{ + return *from; +} + +template > || TIsConvertible> To Cast(From data) +{ + if constexpr (TIsConvertible) + return static_cast(data); + return static_cast(Unwrapping(data)); +} + +#endif // !__FILE_Detail_CP_Typen_Hpp diff --git a/detail/Core/Object.hpp b/detail/Core/Object.hpp new file mode 100644 index 0000000..ba9b312 --- /dev/null +++ b/detail/Core/Object.hpp @@ -0,0 +1,86 @@ +#pragma once +#ifndef __FILE_Detail_Core_Object_Hpp +#define __FILE_Detail_Core_Object_Hpp + +#include "detail/CP/Typen.hpp" + +/** +* 所有object体系中的非实例类都不包含虚函数和数据结构, 并以私有方式继承 +* @code +* class Base : private Object{}; +* class Derived : public Base, Object{}; +*/ +template struct Object; +template <> struct Object +{ + template + Object& ObjectCast() + { + return *static_cast*>(this); + } +}; +template struct Object : private Object +{ + using TDerivedTerminal = DerivedTerminal; + constexpr DerivedTerminal* operator->() noexcept + { + return static_cast(this); + } + + template || TIsConvertible || TIsBaseOf, + TChoose, T, T&> + > + > + R Cast() + { + if constexpr (std::is_base_of_v && std::has_virtual_destructor_v) + return dynamic_cast(static_cast(*this)); + else + return *static_cast(this); + } +}; + +template struct IInterface; +template struct IInterface: private Object> +{ +protected: + DataType data; +public: + virtual DataType& ReadValue() + { + return data; + } +}; +template struct IInterface : private Object> +{ +protected: + DataType data; +public: + virtual const DataType& ReadValue() + { + return data; + } +}; +template struct IInterface : private Object> +{ +protected: + DataType data; +public: + virtual void SetValue(DataType value) + { + data = value; + } +}; +template struct IInterface : private Object> +{ +protected: + DataType data; +}; + +template +using ITypenInterface = IInterface::TypeHash>; + + +#endif // !__FILE_Detail_Core_Object_Hpp diff --git a/detail/Interface.h b/detail/Interface.h new file mode 100644 index 0000000..acd1784 --- /dev/null +++ b/detail/Interface.h @@ -0,0 +1,10 @@ +#pragma once +#ifndef __FILE_Detail_Interface_H +#define __FILE_Detail_Interface_H + +#include +#include +#include +#include + +#endif // !__FILE_Detail_Interface_H