Compare commits

...

6 Commits

Author SHA1 Message Date
256e789c9b Update CP 2025-09-18 15:38:52 +08:00
6e1eaef2e6 准备新增字符串的增强结构,包括编译期字符串 2025-08-28 16:49:48 +08:00
651c890442 开放Origin方法 2025-08-23 17:15:57 +08:00
2c25c8fa1f Merge branch 'main' of https://github.com/NINEMINEsigma/ModernCPP 2025-08-22 14:37:23 +08:00
f533bb6a14 Trait增强 2025-08-22 14:37:16 +08:00
9c55bd43e1 Update Interface.h 2025-08-21 17:54:21 +08:00
10 changed files with 1246 additions and 89 deletions

3
CP.hpp
View File

@@ -6,10 +6,7 @@
#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"

View File

@@ -3,5 +3,6 @@
#define __FILE_Core_Hpp
#include "detail/Core/Object.hpp"
#include "detail\Core\String.hpp"
#endif // !__FILE_Core_Hpp

View File

@@ -2,7 +2,9 @@
#ifndef __FILE_Detail_CP_Arithmetic_Hpp
#define __FILE_Detail_CP_Arithmetic_Hpp
#include "detail/CP/CHash.hpp"
#include "CHash.hpp"
#pragma region TIsArithmetic
namespace Internal
{
@@ -37,6 +39,10 @@ namespace Internal
template <typename T> constexpr bool TIsArithmetic = Internal::TIsArithmeticTool<T>::Value;
#pragma endregion
#pragma region TIsSigned
namespace Internal
{
/**
@@ -60,4 +66,339 @@ namespace Internal
template <typename T> constexpr bool TIsSigned = Internal::TIsSignedTool<T>::Value;
#pragma endregion
#pragma region TIsIntegral
namespace Internal
{
/**
* Traits class which tests if a type is integral.
*/
template <typename T>
struct TIsIntegralTool
{
constexpr static bool Value = false;
};
template <> struct TIsIntegralTool< bool> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< char> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool<signed char> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool<unsigned char> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< char16_t> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< char32_t> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< wchar_t> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< short> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool<unsigned short> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< int> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool<unsigned int> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< long> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool<unsigned long> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool< long long> { constexpr static bool Value = true; };
template <> struct TIsIntegralTool<unsigned long long> { constexpr static bool Value = true; };
template <typename T> struct TIsIntegralTool<const T> { constexpr static bool Value = TIsIntegralTool<T>::Value; };
template <typename T> struct TIsIntegralTool< volatile T> { constexpr static bool Value = TIsIntegralTool<T>::Value; };
template <typename T> struct TIsIntegralTool<const volatile T> { constexpr static bool Value = TIsIntegralTool<T>::Value; };
}
template <typename T> constexpr bool TIsIntegral = Internal::TIsIntegralTool<T>::Value;
#pragma endregion
#pragma region TIsFloatingPoint
///////////////////////////////
// TIsFloatingPoint
namespace Internal
{
/**
* Traits class which tests if a type is floating point.
*/
template <typename T>
struct TIsFloatingPointTool
{
constexpr static bool Value = false;
};
template <> struct TIsFloatingPointTool<float> { constexpr static bool Value = true; };
template <> struct TIsFloatingPointTool<double> { constexpr static bool Value = true; };
template <> struct TIsFloatingPointTool<long double> { constexpr static bool Value = true; };
template <typename T> struct TIsFloatingPointTool<const T> { constexpr static bool Value = TIsFloatingPointTool<T>::Value; };
template <typename T> struct TIsFloatingPointTool< volatile T> { constexpr static bool Value = TIsFloatingPointTool<T>::Value; };
template <typename T> struct TIsFloatingPointTool<const volatile T> { constexpr static bool Value = TIsFloatingPointTool<T>::Value; };
}
template <typename T> constexpr bool TIsFloatingPoint = Internal::TIsFloatingPointTool<T>::Value;
// TIsFloatingPoint
//////////////////////////////////
#pragma endregion
#pragma region About Align
///////////////////////
// About Align Toolkit
#pragma region Align
/**
* Aligns a value to the nearest higher multiple of 'Alignment', which must be a power of two.
*
* @param Val The value to align.
* @param Alignment The alignment value, must be a power of two.
*
* @return The value aligned up to the specified alignment.
*/
template <typename T>
FORCEINLINE constexpr T CAlign(T Val, uint64_t Alignment)
{
return (T)(((uint64_t)Val + Alignment - 1) & ~(Alignment - 1));
}
/**
* Aligns a value to the nearest higher multiple of 'Alignment', which must be a power of two.
*
* @param Val The value to align.
* @param Alignment The alignment value, must be a power of two.
*
* @return The value aligned up to the specified alignment.
*/
template <typename T>
FORCEINLINE T Align(T Val, uint64_t Alignment)
{
return (T)(((uint64_t)Val + Alignment - 1) & ~(Alignment - 1));
}
#pragma endregion
#pragma region AlignDown
/**
* Aligns a value to the nearest lower multiple of 'Alignment', which must be a power of two.
*
* @param Val The value to align.
* @param Alignment The alignment value, must be a power of two.
*
* @return The value aligned down to the specified alignment.
*/
template <typename T>
FORCEINLINE constexpr T CAlignDown(T Val, uint64_t Alignment)
{
return (T)(((uint64_t)Val) & ~(Alignment - 1));
}
/**
* Aligns a value to the nearest lower multiple of 'Alignment', which must be a power of two.
*
* @param Val The value to align.
* @param Alignment The alignment value, must be a power of two.
*
* @return The value aligned down to the specified alignment.
*/
template <typename T>
FORCEINLINE T AlignDown(T Val, uint64_t Alignment)
{
return (T)(((uint64_t)Val) & ~(Alignment - 1));
}
#pragma endregion
#pragma region IsAligned
/**
* Checks if a pointer is aligned to the specified alignment.
*
* @param Val The value to align.
* @param Alignment The alignment value, must be a power of two.
*
* @return true if the pointer is aligned to the specified alignment, false otherwise.
*/
template <typename T>
FORCEINLINE constexpr bool CIsAligned(T Val, uint64_t Alignment)
{
return !((uint64_t)Val & (Alignment - 1));
}
/**
* Checks if a pointer is aligned to the specified alignment.
*
* @param Val The value to align.
* @param Alignment The alignment value, must be a power of two.
*
* @return true if the pointer is aligned to the specified alignment, false otherwise.
*/
template <typename T>
FORCEINLINE bool IsAligned(T Val, uint64_t Alignment)
{
return !((uint64_t)Val & (Alignment - 1));
}
#pragma endregion
#pragma region AlignArbitrary
/**
* Aligns a value to the nearest higher multiple of 'Alignment'.
*
* @param Val The value to align.
* @param Alignment The alignment value, can be any arbitrary value.
*
* @return The value aligned up to the specified alignment.
*/
template <typename T>
FORCEINLINE constexpr T CAlignArbitrary(T Val, uint64_t Alignment)
{
return (T)((((uint64_t)Val + Alignment - 1) / Alignment) * Alignment);
}
/**
* Aligns a value to the nearest higher multiple of 'Alignment'.
*
* @param Val The value to align.
* @param Alignment The alignment value, can be any arbitrary value.
*
* @return The value aligned up to the specified alignment.
*/
template <typename T>
FORCEINLINE T AlignArbitrary(T Val, uint64_t Alignment)
{
return (T)((((uint64_t)Val + Alignment - 1) / Alignment) * Alignment);
}
#pragma endregion
// Align Toolkit
////////////////////////
#pragma endregion
#pragma region About Functors
#pragma region Less
///////////////////
// Less
/**
* Binary predicate class for sorting elements in order. Assumes < operator is defined for the template type.
* Forward declaration exists in ContainersFwd.h
*
* See: http://en.cppreference.com/w/cpp/utility/functional/less
*/
struct TLess
{
template <typename T, typename U>
FORCEINLINE bool operator()(const T& A, const U& B) const
{
return A < B;
}
};
// Less
////////////////////
#pragma endregion
#pragma region Greater
////////////////////////
// Greater
/**
* Binary predicate class for sorting elements in reverse order. Assumes < operator is defined for the template type.
*
* See: http://en.cppreference.com/w/cpp/utility/functional/greater
*/
struct TGreater
{
template <typename T, typename U>
FORCEINLINE bool operator()(const T& A, const U& B) const
{
return B < A;
}
};
// Greater
//////////////////////////
#pragma endregion
#pragma region EqualTo
/////////////////////////////
// EqualTo
/**
* Binary predicate class for performing equality comparisons.
*
* Uses operator== when available.
*
* See: https://en.cppreference.com/w/cpp/utility/functional/equal_to
*/
struct TEqualTo
{
template <typename T, typename U>
constexpr auto operator()(const T& Lhs, const U& Rhs) const -> decltype(Lhs == Rhs)
{
return Lhs == Rhs;
}
};
// EqualTo
/////////////////////////////
#pragma endregion
#pragma region IdentityFunctor
/////////////////////////////
// IdentityFunctor
/**
* A functor which returns whatever is passed to it. Mainly used for generic composition.
*/
struct TIdentityFunctor
{
template <typename T>
FORCEINLINE T&& operator()(T&& Val) const
{
return (T&&)Val;
}
};
// IdentityFunctor
///////////////////////////////
#pragma endregion
#pragma endregion
#pragma region IntegralConstant
///////////////////////////////
// IntegralConstant
/**
* Defines a value metafunction of the given Value.
*/
template <typename T, T Val>
struct TIntegralConstant
{
static constexpr T Value = Val;
constexpr operator T() const
{
return Value;
}
};
// IntegralConstant
///////////////////////////////
#pragma endregion
#endif // !__FILE_Detail_CP_Arithmetic_Hpp

View File

@@ -18,13 +18,13 @@ namespace Internal
template<bool First, bool... Value> class CAnd_t;
template<bool First, bool... Value> class COr_t;
template<> class CAnd_t<true> : public std::true_type {};
template<> class CAnd_t<false> : public std::false_type {};
template< > class CAnd_t<true> : public std::true_type {};
template< > class CAnd_t<false> : public std::false_type {};
template<bool... Value> class CAnd_t<true, Value...> : public CAnd_t<Value...> {};
template<bool... Value> class CAnd_t<false, Value...> : public std::false_type {};
template<> class COr_t<true> : public std::true_type {};
template<> class COr_t<false> : public std::false_type {};
template< > class COr_t<true> : public std::true_type {};
template< > class COr_t<false> : public std::false_type {};
template<bool... Value> class COr_t<true, Value...> : public std::true_type {};
template<bool... Value> class COr_t<false, Value...> : public COr_t<Value...> {};
}

View File

@@ -2,86 +2,116 @@
#ifndef __FILE_Detail_CP_CHash_Hpp
#define __FILE_Detail_CP_CHash_Hpp
#ifndef FORCEINLINE
#define FORCEINLINE __forceinline
#endif // !FORCEINLINE
//
// Hash functions for common types.
//
inline uint32_t Hash(const uint8_t A)
FORCEINLINE uint32_t Hash(const uint8_t A)
{
return A;
}
inline uint32_t Hash(const int8_t A)
FORCEINLINE uint32_t Hash(const int8_t A)
{
return A;
}
inline uint32_t Hash(const uint16_t A)
FORCEINLINE uint32_t Hash(const uint16_t A)
{
return A;
}
inline uint32_t Hash(const int16_t A)
FORCEINLINE uint32_t Hash(const int16_t A)
{
return A;
}
inline uint32_t Hash(const int32_t A)
FORCEINLINE uint32_t Hash(const int32_t A)
{
return A;
}
inline uint32_t Hash(const uint32_t A)
FORCEINLINE uint32_t Hash(const uint32_t A)
{
return A;
}
inline uint32_t Hash(const uint64_t A)
FORCEINLINE uint32_t Hash(const uint64_t A)
{
return (uint32_t)A + ((uint32_t)(A >> 32) * 23);
}
inline uint32_t Hash(const int64_t A)
FORCEINLINE uint32_t Hash(const int64_t A)
{
return (uint32_t)A + ((uint32_t)(A >> 32) * 23);
}
constexpr uint32_t CHash(const uint8_t A)
uint32_t Hash(const char* str, uint32_t hash = 5381)
{
return *str == '\0' ? hash : Hash(str + 1, ((hash << 5) + hash) + *str);
}
template<typename StringView>
uint32_t Hash(StringView str, uint32_t hash = 5381)
{
for (size_t i = 0; i < str.length(); ++i)
{
hash = ((hash << 5) + hash) + str[i];
}
return hash;
}
uint32_t THash(const char* const object, unsigned int length, uint32_t hash = 5381)
{
return (length == 0) ? hash : THash(object + 1, length - 1, ((hash << 5) + hash) + *object);
}
template<typename T>
uint32_t THash(T* object, uint32_t hash = 5381)
{
return THash((char*)object, sizeof(T), hash);
}
FORCEINLINE constexpr uint32_t CHash(const uint8_t A)
{
return A;
}
constexpr uint32_t CHash(const int8_t A)
FORCEINLINE constexpr uint32_t CHash(const int8_t A)
{
return A;
}
constexpr uint32_t CHash(const uint16_t A)
FORCEINLINE constexpr uint32_t CHash(const uint16_t A)
{
return A;
}
constexpr uint32_t CHash(const int16_t A)
FORCEINLINE constexpr uint32_t CHash(const int16_t A)
{
return A;
}
constexpr uint32_t CHash(const int32_t A)
FORCEINLINE constexpr uint32_t CHash(const int32_t A)
{
return A;
}
constexpr uint32_t CHash(const uint32_t A)
FORCEINLINE constexpr uint32_t CHash(const uint32_t A)
{
return A;
}
constexpr uint32_t CHash(const uint64_t A)
FORCEINLINE 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)
FORCEINLINE constexpr uint32_t CHash(const int64_t A)
{
return (uint32_t)A + ((uint32_t)(A >> 32) * 23);
}
@@ -91,7 +121,8 @@ 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)
template<typename StringView>
constexpr uint32_t CHash(StringView str, uint32_t hash = 5381)
{
for (size_t i = 0; i < str.length(); ++i)
{

7
detail/CP/CPString.hpp Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#ifndef __FILE_Detail_CP_CPString_H
#define __FILE_Detail_CP_CPString_H
#endif // !__FILE_Detail_CP_CPString_H

View File

@@ -2,14 +2,79 @@
#ifndef __FILE_Detail_CP_Typen_Hpp
#define __FILE_Detail_CP_Typen_Hpp
#include "detail/CP/CHash.hpp"
#include "CHash.hpp"
#pragma region TIdentity
////////////////////////
// TIdentity
/**
* Returns the same type passed to it. This is useful in a few cases, but mainly for inhibiting template argument deduction in function arguments, e.g.:
*
* template <typename T>
* void Func1(T Val); // Can be called like Func(123) or Func<int>(123);
*
* template <typename T>
* void Func2(typename TIdentity<T>::Type Val); // Must be called like Func<int>(123)
*/
template <typename T>
struct TIdentity
{
typedef T Type;
};
// TIdentity
//////////////////////////
#pragma endregion
template <typename Base, typename Derived> constexpr bool TIsBaseOf = std::is_base_of_v<Base, Derived>;
template <typename A, typename B> constexpr bool TAreSame = std::is_same_v<A, B>;
#pragma region TAreSame
/////////////////////////
// TAreSame
namespace Internal
{
template <typename A, typename B> struct TAreSameTool { static constexpr bool Value = false; };
template <typename A > struct TAreSameTool<A,A> { static constexpr bool Value = true; };
}
template <typename A, typename B> constexpr bool TAreSame = Internal::TAreSameTool<A, B>::Value;
// TAreSame
////////////////////////
#pragma endregion
#pragma region Conditional Type
///////////////////////////////
// About Conditional
namespace Internal
{
/** Chooses between two different classes based on a boolean. */
template<bool Predicate, typename TrueClass, typename FalseClass>
class TChooseClass;
template<typename TrueClass, typename FalseClass>
class TChooseClass<true, TrueClass, FalseClass>
{
public:
typedef TrueClass Result;
};
template<typename TrueClass, typename FalseClass>
class TChooseClass<false, TrueClass, FalseClass>
{
public:
typedef FalseClass Result;
};
template<typename IndexType, IndexType Index, typename... Types> struct TConditionalTool
{
@@ -32,27 +97,167 @@ namespace Internal
template<bool Pr, typename First, typename Second>
struct TConditionalTool<bool, Pr, First, Second>
{
using Type = std::conditional_t<Pr, First, Second>;
using Type = typename TChooseClass<Pr, First, Second>::Result;
};
}
template<bool Pr, typename First, typename Second> using TChoose = typename Internal::TConditionalTool<bool, Pr, First, Second>::Type;
template<int Index, typename... Types> using TConditional = typename Internal::TConditionalTool<int, Index, Types...>::Type;
template <typename T> using TDecay = std::decay_t<T>;
// Conditional
///////////////////////////////
template <typename T> constexpr bool TIsP = std::is_pointer_v<T>;
template <typename T> constexpr bool TIsC = std::is_const_v<T>;
template <typename T> constexpr bool TIsV = std::is_volatile_v<T>;
template <typename T> constexpr bool TIsR = std::is_reference_v<T>;
template <typename T> constexpr bool TIsF = std::is_function_v<T>;
#pragma endregion
template <typename T> constexpr bool TIsPointer = std::is_pointer_v<T>;
template <typename T> constexpr bool TIsConst = std::is_const_v<T>;
template <typename T> constexpr bool TIsVolatile = std::is_volatile_v<T>;
template <typename T> constexpr bool TIsRef = std::is_reference_v<T>;
template <typename T> constexpr bool TIsFunc = std::is_function_v<T>;
template <typename T> constexpr bool TIsLR = std::is_lvalue_reference_v<T>;
template <typename T> constexpr bool TIsRR = std::is_rvalue_reference_v<T>;
template <typename T> constexpr bool TIsCV = TIsC<T> && TIsV<T>;
template <typename T> constexpr bool TIsIt = TIsP<T> || TIsLR<decltype(*std::declval<T>())>;
template <typename T> constexpr bool TIsVoid = std::is_same_v<void, T>;
template <typename T> constexpr bool THasVirtual = std::has_virtual_destructor_v<T>;
template <typename T, typename C> constexpr bool TIsCStr = TAreSame<T, const C*>&& TAreSame<T, C*>;
template <typename T> constexpr bool TIsCV = TIsConst<T> && TIsVolatile<T>;
template <typename T> constexpr bool TIsIt = TIsPointer<T> || TIsLR<decltype(*std::declval<T>())>;
template <typename T> constexpr bool TIsVoid = TAreSame<void, T>;
template <typename T> constexpr bool TIsVirtual = std::has_virtual_destructor_v<T>;
template <typename T> constexpr bool TIsPolymorphic = std::is_polymorphic_v<T>;
template <typename T> constexpr bool TIsAbstract = std::is_abstract_v<T>;
template <typename T> constexpr bool TIsClass = std::is_class_v<T>;
template <typename T> constexpr bool TIsPOD = std::is_pod_v<T>;
template <typename T, typename C = char> constexpr bool TIsCStr = TAreSame<T, const C*> && TAreSame<T, C*>;
template <typename T, typename... Args> constexpr bool TIsConstructible = std::is_constructible_v<T, Args...>;
#pragma region About Enum
template <typename T> constexpr bool TIsEnum = std::is_enum_v<T>;
/////////////////////////
// TIsEnumClass
namespace Internal
{
template <typename T>
struct TIsEnumConvertibleToIntTool
{
static char(&Resolve(int))[2];
static char Resolve(...);
enum { Value = sizeof(Resolve(T())) - 1 };
};
}
/**
* Traits class which tests if a type is arithmetic.
*/
template <typename T> constexpr bool TIsEnumClass = TIsEnum<T> && !Internal::TIsEnumConvertibleToIntTool<T>;
// TIsEnumClass
///////////////////////////
#pragma endregion
#pragma region TIsTrivial
/////////////////////////
// TIsTriviallyDestructible
template <typename T> constexpr bool TIsTriviallyDestructible = std::is_trivially_destructible_v<T>;
// TIsTriviallyDestructible
///////////////////////////
/////////////////////////
// TIsTriviallyCopyConstructible
template <typename T> constexpr bool TIsTriviallyCopyConstructible = std::is_trivially_constructible_v<T>;
// TIsTriviallyCopyConstructible
//////////////////////////
//////////////////////////
// TIsTriviallyCopyAssignable
template <typename T> constexpr bool TIsTriviallyCopyAssignable = std::is_trivially_copyable_v<T>;
// TIsTriviallyCopyAssignable
///////////////////////////
///////////////////////////
// TIsTrivial
template <typename T> constexpr bool TIsTrivial = std::is_trivial_v<T>;
// TIsTrivial
////////////////////////////
#pragma endregion
#pragma region TIsMemberPointerTool
///////////////////////////////
// TIsMemberPointer
namespace Internal
{
/**
* Traits class which tests if a type is a pointer to member (data member or member function).
*/
template <typename T>
struct TIsMemberPointerTool
{
constexpr static bool Value = false;
};
template <typename T, typename U> struct TIsMemberPointerTool<T U::*> { constexpr static bool Value = true; };
template <typename T> struct TIsMemberPointerTool<const T> { constexpr static bool Value = TIsMemberPointerTool<T>::Value; };
template <typename T> struct TIsMemberPointerTool< volatile T> { constexpr static bool Value = TIsMemberPointerTool<T>::Value; };
template <typename T> struct TIsMemberPointerTool<const volatile T> { constexpr static bool Value = TIsMemberPointerTool<T>::Value; };
}
template <typename T> constexpr bool TIsMemberPointer = Internal::TIsMemberPointerTool<T>::Value;
// TIsMemberPointer
///////////////////////////////
#pragma endregion
#pragma region TIsInitializerList
////////////////////////////////////
// TIsInitializerList
namespace Internal
{
/**
* Traits class which tests if a type is an initializer list.
*/
template <typename T>
struct TIsInitializerListTool
{
static constexpr bool Value = false;
};
template <typename T>
struct TIsInitializerListTool<std::initializer_list<T>>
{
static constexpr bool Value = true;
};
template <typename T> struct TIsInitializerListTool<const T> { static constexpr Value = TIsInitializerListTool<T>::Value; };
template <typename T> struct TIsInitializerListTool< volatile T> { static constexpr Value = TIsInitializerListTool<T>::Value; };
template <typename T> struct TIsInitializerListTool<const volatile T> { static constexpr Value = TIsInitializerListTool<T>::Value; };
}
template <typename T> constexpr bool TIsInitializerList = Internal::TIsInitializerListTool<T>::Value;
// TIsInitializerList
////////////////////////////////////
#pragma endregion
#pragma region Remove Type
template <typename T> using TRemoveP = std::remove_pointer_t<T>;
template <typename T> using TRemoveC = std::remove_const_t<T>;
@@ -60,7 +265,205 @@ template <typename T> using TRemoveV = std::remove_volatile_t<T>;
template <typename T> using TRemoveR = std::remove_reference_t<T>;
template <typename T> using TRemoveCV = std::remove_cv_t<T>;
template <bool Pr, typename T = void> using TEnableIf = std::enable_if_t<Pr, T>;
#pragma endregion
#pragma region About Array
#pragma region IsArray
/**
* Traits class which tests if a type is a C++ array.
*/
template <typename T> struct TIsArray { constexpr static bool Value = false; };
template <typename T> struct TIsArray<T[]> { constexpr static bool Value = true; };
template <typename T, uint32_t N> struct TIsArray<T[N]> { constexpr static bool Value = true; };
/**
* Traits class which tests if a type is a bounded C++ array.
*/
template <typename T> struct TIsBoundedArray { constexpr static bool Value = false; };
template <typename T, uint32_t N> struct TIsBoundedArray<T[N]> { constexpr static bool Value = true; };
/**
* Traits class which tests if a type is an unbounded C++ array.
*/
template <typename T> struct TIsUnboundedArray { constexpr static bool Value = false; };
template <typename T> struct TIsUnboundedArray<T[]> { constexpr static bool Value = true; };
#pragma endregion
#pragma region Ref Version
/**
* Type trait which returns true if the type T is an array or a reference to an array of ArrType.
*/
template <typename T, typename ArrType>
struct TIsArrayOrRefOfType
{
constexpr static bool Value = false;
};
template <typename ArrType> struct TIsArrayOrRefOfType< ArrType[], ArrType> { constexpr static bool Value = true; };
template <typename ArrType> struct TIsArrayOrRefOfType<const ArrType[], ArrType> { constexpr static bool Value = true; };
template <typename ArrType> struct TIsArrayOrRefOfType< volatile ArrType[], ArrType> { constexpr static bool Value = true; };
template <typename ArrType> struct TIsArrayOrRefOfType<const volatile ArrType[], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType< ArrType[N], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType<const ArrType[N], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType< volatile ArrType[N], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType<const volatile ArrType[N], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType< ArrType(&)[N], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType<const ArrType(&)[N], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType< volatile ArrType(&)[N], ArrType> { constexpr static bool Value = true; };
template <typename ArrType, unsigned int N> struct TIsArrayOrRefOfType<const volatile ArrType(&)[N], ArrType> { constexpr static bool Value = true; };
#pragma endregion
#pragma endregion
#pragma region TEnableIf
////////////////////////
// TEnableIf
namespace Internal
{
/**
* Includes a function in an overload set if the predicate is true. It should be used similarly to this:
*
* // This function will only be instantiated if SomeTrait<T>::Value is true for a particular T
* template <typename T>
* typename TEnableIf<SomeTrait<T>::Value, ReturnType>::Type Function(const T& Obj)
* {
* ...
* }
*
* ReturnType is the real return type of the function.
*/
template <bool Predicate, typename Result = void>
class TEnableIfTool;
template <typename Result>
class TEnableIfTool<true, Result>
{
public:
using type = Result;
using Type = Result;
};
template <typename Result>
class TEnableIfTool<false, Result>
{
};
}
template <bool Pr, typename T = void> using TEnableIf = typename Internal::TEnableIfTool<Pr, T>::Type;
// TEnableIf
////////////////////////
////////////////////////
// TLazyEnableIf
namespace Internal
{
/**
* This is a variant of the above that will determine the return type 'lazily', i.e. only if the function is enabled.
* This is useful when the return type isn't necessarily legal code unless the enabling condition is true.
*
* // This function will only be instantiated if SomeTrait<T>::Value is true for a particular T.
* // The function's return type is typename Transform<T>::Type.
* template <typename T>
* typename TLazyEnableIf<SomeTrait<T>::Value, Transform<T>>::Type Function(const T& Obj)
* {
* ...
* }
*
* See boost::lazy_enable_if for more details.
*/
template <bool Predicate, typename Func>
class TLazyEnableIfTool;
template <typename Func>
class TLazyEnableIfTool<true, Func>
{
public:
using type = typename Func::Type;
using Type = typename Func::Type;
};
template <typename Func>
class TLazyEnableIfTool<false, Func>
{
};
}
template <bool Predicate, typename Func> using TLazyEnableIf = Internal::TLazyEnableIfTool<Predicate, Func>;
// TLazyEnableIf
////////////////////////
#pragma endregion
#pragma region TDecay
/////////////////////
// TDeacy
namespace Internal
{
template <typename T>
struct TDecayNonReference
{
using Type = TRemoveCV<T>;
};
template <typename T>
struct TDecayNonReference<T[]>
{
using Type = T*;
};
template <typename T, uint32_t N>
struct TDecayNonReference<T[N]>
{
using Type = T*;
};
template <typename RetType, typename... Params>
struct TDecayNonReference<RetType(Params...)>
{
using Type = RetType(*)(Params...);
};
/**
* Returns the decayed type of T, meaning it removes all references, qualifiers and
* applies array-to-pointer and function-to-pointer conversions.
*
* http://en.cppreference.com/w/cpp/types/decay
*/
template <typename T>
struct TDecayTool
{
typedef typename TDecayNonReference<typename TRemoveReference<T>::Type>::Type Type;
};
}
template <typename T> using TDecay = typename Internal::TDecayTool<T>::Type;
// TDeacy
//////////////////////
#pragma endregion
#pragma region THasToString
////////////////////////////
// THasToString
namespace Internal
{
@@ -127,6 +530,16 @@ namespace Internal
template <typename T> constexpr bool THasToString = Internal::StringAbleTool<T>::Value;
// THasToString
////////////////////////////
#pragma endregion
#pragma region TIsConvertible
/////////////////////////////
// TIsConvertible
namespace Internal
{
template <typename From, typename To>
@@ -145,6 +558,16 @@ template <typename From, typename To> constexpr bool TIsConvertible = Internal::
template <typename From, typename To> constexpr bool TIsPointerConvertible = TIsConvertible<From*, To*>;
template <typename From, typename To> constexpr bool TIsReferenceConvertible = TIsConvertible<From&, To&>;
// TIsConvertible
/////////////////////////////
#pragma endregion
#pragma region TCopyQualifiers
///////////////////////////////
// TCopyQualifiers
namespace Internal
{
/**
@@ -161,10 +584,73 @@ namespace Internal
template <typename From, typename To> using TCopyQualifiers = typename Internal::TCopyQualifiersTool<From, To>::Type;
template <typename From, typename To> constexpr bool TLosesQualifiers = !TAreSame<TCopyQualifiers<From, To>, To>;
// TCopyQualifiers
////////////////////////////////
////////////////////////////////
// TCopyQualifiersAndRefs
namespace Internal
{
/**
* Copies the cv-qualifiers and references from one type to another
*/
template <typename From, typename To> struct TCopyQualifiersAndRefsTool { using Type = typename TCopyQualifiersTool<From, To>::Type; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From, To& > { using Type = typename TCopyQualifiersTool<From, To>::Type&; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From, To&&> { using Type = typename TCopyQualifiersTool<From, To>::Type&&; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From&, To > { using Type = typename TCopyQualifiersTool<From, To>::Type&; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From&, To& > { using Type = typename TCopyQualifiersTool<From, To>::Type&; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From&, To&&> { using Type = typename TCopyQualifiersTool<From, To>::Type&; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From&&, To > { using Type = typename TCopyQualifiersTool<From, To>::Type&&; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From&&, To& > { using Type = typename TCopyQualifiersTool<From, To>::Type&; };
template <typename From, typename To> struct TCopyQualifiersAndRefsTool<From&&, To&&> { using Type = typename TCopyQualifiersTool<From, To>::Type&&; };
}
template <typename From, typename To> using TCopyQualifiersAndRefsFromTo_T = typename Internal::TCopyQualifiersAndRefsTool<From, To>::Type;
// TCopyQualifiersAndRefs
////////////////////////////////
template <typename From, typename To> constexpr bool TLosesQualifiers = !TAreSame<TCopyQualifiers<From, To>, To>;
#pragma endregion
#pragma region TLosesQualifiersFromTo
///////////////////////
// TLosesQualifiersFromTo
namespace Internal
{
/**
* Tests if qualifiers are lost between one type and another, e.g.:
*
* TLosesQualifiersFromTo<const T1, T2>::Value == true
* TLosesQualifiersFromTo<volatile T1, const volatile T2>::Value == false
*/
template <typename From, typename To>
struct TLosesQualifiersFromToTool
{
constexpr static bool Value = !TAreSame<TCopyQualifiersFromTo<From, To>, To>;
};
}
template <typename From, typename To> using TLosesQualifiersFromTo = Internal::TLosesQualifiersFromToTool<From, To>;
// TLosesQualifiersFromTo
///////////////////////////
#pragma endregion
#pragma region TTrait
//////////////////////
// TTrait
namespace Internal
{
#pragma region Tools
struct PrettyFunctionTag {};
template<typename T>
@@ -255,8 +741,30 @@ namespace Internal
return name;
}
template <typename T> struct TraitTool
#pragma endregion
template <typename T, bool IsIntegral> struct TraitTool;
template <typename T> struct TraitTool<T,false>
{
public:
constexpr static bool IsPointer = TIsPointer<T>;
constexpr static bool IsRef = TIsRef<T>;
constexpr static bool IsVoid = TIsVoid<T>;
constexpr static bool IsVirtual = TIsVirtual<T>;
constexpr static bool IsPolymorphic = TIsPolymorphic<T>;
constexpr static bool IsAbstract = TIsAbstract<T>;
constexpr static bool IsClass = TIsClass<T>;
constexpr static bool IsPOD = TIsPOD<T>;
constexpr static bool IsEnum = TIsEnum<T>;
constexpr static bool IsEnumClass = TIsEnum<T>;
constexpr static bool IsTriviallyDestructible = TIsTriviallyDestructible<T>;
constexpr static bool IsTriviallyCopyConstructible = TIsTriviallyCopyConstructible<T>;
constexpr static bool IsTriviallyCopyAssignable = TIsTriviallyCopyAssignable<T>;
constexpr static bool IsTrivial = TIsTrivial<T>;
constexpr static bool IsMemberPointer = TIsMemberPointer<T>;
constexpr static bool IsInitializerList = TIsInitializerList<T>;
constexpr static bool HasToString = THasToString<T>;
public:
template<typename P, bool Derived = true> static constexpr bool Is() { return (TAreSame<T, P> || (Derived && TIsBaseOf<T, P>)); }
template<typename P, bool Derived = true> static constexpr bool Is(P) { return (TAreSame<T, P> || (Derived && TIsBaseOf<T, P>)); }
template<typename P, bool Derived = true> static constexpr bool Is(P from, T& to)
@@ -274,10 +782,13 @@ namespace Internal
static auto symbol = typeid(T).hash_code();
return (intptr_t)&symbol;
}
constexpr static uint32_t NewPtrMemorySize = sizeof(decltype(Symbol())) + sizeof(T);
static T* New(void* memory, size_t capacity)
{
using TSymbol = decltype(Symbol());
constexpr auto size = sizeof(TSymbol) + sizeof(T);
constexpr auto size = NewPtrMemorySize;
if (capacity < size || memory == nullptr)
return nullptr;
char* ptr = new(memory) char[size];
@@ -440,19 +951,22 @@ namespace Internal
{
return SymbolNameTool<T>();
}
template<T Value>
constexpr static std::string_view ValueName()
{
return ValueNameTool<T, Value>();
}
static uint32_t Hash(const T& v)
{
if constexpr (std::is_integral_v<T>)
return GetTypeHash(v);
return Hash(v);
else
return GetTypeHash(&v);
return THash(&v);
}
static uint32_t Hash(T* v)
{
if constexpr (std::is_integral_v<T>)
return Hash(*v);
else
return THash(v);
}
private:
constexpr static uint32_t InjectTypeHash()
{
@@ -462,58 +976,144 @@ namespace Internal
public:
constexpr static int TypeHash = InjectTypeHash();
};
template <typename T> struct TraitTool<const T> : public TraitTool<T> {};
template <typename T> struct TraitTool< volatile T> : public TraitTool<T> {};
template <typename T> struct TraitTool<const volatile T> : public TraitTool<T> {};
template <typename T> struct TraitTool<T, true> : public TraitTool<T, false>
{
template<T Value>
constexpr static std::string_view ValueName()
{
return ValueNameTool<T, Value>();
}
};
template <typename T, bool IsIntegral> struct TraitTool<const T, IsIntegral> : public TraitTool<T,IsIntegral> {};
template <typename T, bool IsIntegral> struct TraitTool< volatile T, IsIntegral> : public TraitTool<T,IsIntegral> {};
template <typename T, bool IsIntegral> struct TraitTool<const volatile T, IsIntegral> : public TraitTool<T,IsIntegral> {};
}
template <typename T> using TTrait = Internal::TraitTool<T>;
using Bool = Internal::TraitTool<bool>;
using Int = Internal::TraitTool<int>;
using Float = Internal::TraitTool<float>;
using Double = Internal::TraitTool<double>;
using Long = Internal::TraitTool<long>;
using UInt = Internal::TraitTool<unsigned int>;
using Int8 = Internal::TraitTool<int8_t>;
using Int16 = Internal::TraitTool<int16_t>;
using Int32 = Internal::TraitTool<int32_t>;
using Int64 = Internal::TraitTool<int64_t>;
using UInt8 = Internal::TraitTool<uint8_t>;
using UInt16 = Internal::TraitTool<uint16_t>;
using UInt32 = Internal::TraitTool<uint32_t>;
using UInt64 = Internal::TraitTool<uint64_t>;
using LongDouble = Internal::TraitTool<long double>;
template <typename T> using TTrait = Internal::TraitTool<T, std::is_integral_v<T>>;
#if !defined(nameofT)&&!defined(nameofEnum)
template <typename T> constexpr auto __Inject_nameof()
// TTrait
////////////////////////
////////////////////////
// ValueTrait
namespace Internal
{
return TTrait<T>::SymbolName();
template<typename T> class ValueClass : public TTrait<T>
{
private:
using _Mybase = TTrait<T>;
char MyMemoryInside[NewPtrMemorySize];
T* MyCachePtr;
public:
template<typename = TEnableIf<TIsConstructible<T>>>
ValueClass()
{
MyCachePtr = _Mybase::New(MyMemoryInside, NewPtrMemorySize);
new(MyCachePtr) T();
}
template<typename First, typename = TEnableIf<TIsConstructible<T, First>>>
ValueClass(First first)
{
MyCachePtr = _Mybase::New(MyMemoryInside, NewPtrMemorySize);
new(MyCachePtr) T(first);
}
template<typename First, typename Second,
typename = TEnableIf<TIsConstructible<T, First, Second>>>
ValueClass(First first, Second second)
{
MyCachePtr = _Mybase::New(MyMemoryInside, NewPtrMemorySize);
new(MyCachePtr) T(first, second);
}
template<typename First, typename Second, typename Third,
typename = TEnableIf<TIsConstructible<T, First, Second, Third>>>
ValueClass(First first, Second second, Third third)
{
MyCachePtr = _Mybase::New(MyMemoryInside, NewPtrMemorySize);
new(MyCachePtr) T(first, second, third);
}
template<typename First, typename Second, typename Third, typename Other,
typename = TEnableIf<TIsConstructible<T, First, Second, Third, Other>>>
ValueClass(First first, Second second, Third third, Other other)
{
MyCachePtr = _Mybase::New(MyMemoryInside, NewPtrMemorySize);
new(MyCachePtr) T(first, second, third, other);
}
T& ReadValue()
{
return *MyCachePtr;
}
const T& ReadValue() const
{
return *MyCachePtr;
}
constexpr operator T& ()
{
return *MyCachePtr;
}
constexpr operator const T& () const
{
return *MyCachePtr;
}
uint32_t Hash()
{
return _Mybase::Hash(MyCachePtr);
}
};
}
template <typename T, T Value> constexpr auto __Inject_nameof()
// ValueTrait
////////////////////////
using Bool = Internal::ValueClass<bool>;
using Int = Internal::ValueClass<int>;
using Float = Internal::ValueClass<float>;
using Double = Internal::ValueClass<double>;
using Long = Internal::ValueClass<long>;
using UInt = Internal::ValueClass<unsigned int>;
using Int8 = Internal::ValueClass<int8_t>;
using Int16 = Internal::ValueClass<int16_t>;
using Int32 = Internal::ValueClass<int32_t>;
using Int64 = Internal::ValueClass<int64_t>;
using UInt8 = Internal::ValueClass<uint8_t>;
using UInt16 = Internal::ValueClass<uint16_t>;
using UInt32 = Internal::ValueClass<uint32_t>;
using UInt64 = Internal::ValueClass<uint64_t>;
using LongDouble = Internal::ValueClass<long double>;
#pragma endregion
#pragma region nameof
namespace Internal
{
return TTrait<T>::ValueName<Value>();
template <typename T> constexpr auto __Inject_nameof_type()
{
return TTrait<T>::SymbolName();
}
template <typename T, T Value> constexpr auto __Inject_nameof_value()
{
return TTrait<T>::ValueName<Value>();
}
}
#define nameofT(x) __Inject_nameof<x>();
#define nameofEnum(x) __Inject_nameof<decltype(x),x>();
#else
template <typename T> constexpr auto nameof()
{
return TTrait<T>::SymbolName();
}
template <typename T, T Value> constexpr auto nameof()
{
return TTrait<T>::ValueName<Value>();
}
#endif // !nameof
#define nameof(x) Internal::__Inject_nameof_type<x>();
#define nameofEnum(x) Internal::__Inject_nameof_value<decltype(x),x>();
#pragma endregion
template <typename T> using TUnwrapped = TEnableIf<TIsIt<T>, TConditional<TIsP<T>, TRemoveP<T>, decltype(*std::declval<T>())>>;
template <typename T> using TUnwrapped = TEnableIf<TIsIt<T>, TConditional<TIsPointer<T>, TRemoveP<T>, decltype(*std::declval<T>())>>;
template <typename T, typename Unwrapped = TUnwrapped<T>> decltype(auto) Unwrapping(T from)
{
return *from;
}
template <typename From, typename To, bool = TIsConvertible<TUnwrapped<From, To>> || TIsConvertible<From, To>> To Cast(From data)
template <typename From, typename To, bool = TIsConvertible<TUnwrapped<From>, To> || TIsConvertible<From, To>> To Cast(From data)
{
if constexpr (TIsConvertible<From, To>)
return static_cast<To>(data);

View File

@@ -21,10 +21,15 @@ template <> struct Object<void>
};
template <typename DerivedTerminal> struct Object : private Object<void>
{
constexpr DerivedTerminal* Origin() noexcept
{
return static_cast<DerivedTerminal*>(this);
}
using TDerivedTerminal = DerivedTerminal;
constexpr DerivedTerminal* operator->() noexcept
{
return static_cast<DerivedTerminal*>(this);
return Origin();
}
template<typename T,

174
detail/Core/String.hpp Normal file
View File

@@ -0,0 +1,174 @@
#pragma once
#ifndef __FILE_Detail_Core_String_H
#define __FILE_Detail_Core_String_H
namespace Internal
{
template<typename Char>
class PrefixStringTree
{
public:
PrefixStringTree<Char>* parent;
PrefixStringTree<Char>* child_head;
PrefixStringTree<Char>* child_end;
PrefixStringTree<Char>* next;
using my_string_view = std::basic_string_view<Char>;
private:
Char mine;
Char* data;
my_string_view rdata;
public:
my_string_view ReadValue() const
{
return rdata;
}
constexpr operator my_string_view() const
{
return rdata;
}
PrefixStringTree<Char>& PopBack()
{
if (parent)
return *parent;
return *this;
}
private:
void RegetDataFromChild(PrefixStringTree<Char>* target)
{
auto size = this->rdata.size();
if (data)
{
delete this->data;
this->data = target->data;
}
this->rdata = target->rdata.substr(0, size);
if (parent)
parent->RegetDataFromChild(target);
}
PrefixStringTree<Char>* AddChild(Char ch)
{
auto next = child_head;
child_head = new PrefixStringTree<Char>();
if (next == nullptr)
{
child_end = child_head;
}
child_head->mine = ch;
const auto size = rdata.size();
child_head->data = new Char[size + 1];
::strcpy_s(child_head->data, size, data);
child_head->data[size] = ch;
child_head->rdata = child_head->data;
child_head->next = next;
RegetDataFromChild(child_head);
return child_head;
}
PrefixStringTree<Char>* FindChild(Char ch)
{
for (auto head = child_head; head; head = head->next)
{
if (head->mine)
return head;
}
return nullptr;
}
public:
PrefixStringTree<Char>& operator+(Char ch)
{
if (ch == 0)
return *this;
auto result = FindChild(ch);
if (result)
return *result;
return *AddChild(ch);
}
PrefixStringTree<Char>& operator+(my_string_view view)
{
if (view.size() == 0 || view.front() == 0)
return *this;
return this->operator+(view.front()).operator+(view.substr(1));
}
bool operator==(my_string_view str)
{
return rdata == str;
}
bool operator!=(my_string_view str)
{
return rdata != str;
}
static PrefixStringTree<Char>& GetRoot()
{
static PrefixStringTree<Char> root;
return root;
}
decltype(auto) GetSize()
{
return rdata.size();
}
PrefixStringTree<Char>& GetPrefix(size_t size = -1)
{
if (size >= rdata.size())
return *this;
return parent->GetPrefix(size);
}
PrefixStringTree<Char>& GetSamePrefix(PrefixStringTree<Char>& other)
{
if (rdata.front() != other.rdata.front())
return GetRoot();
auto i = 0;
for (auto e = std::min<size_t>(rdata.size(), other, rdata.size()); i <= e; i++)
{
if (rdata[i] != other.rdata[i])
break;
}
return this->GetPrefix(i);
}
void Clear()
{
for (auto head = child_head; head; head = head->next)
{
head->Clear();
}
if (data)
{
delete data;
}
}
};
}
template<typename Char>
class PrefixString
{
using _Mychar = Char;
using my_string_view = std::basic_string_view<Char>;
Internal::PrefixStringTree<Char>& str;
public:
PrefixString() :str(Internal::PrefixStringTree<Char>::GetRoot()) {};
PrefixString(my_string_view view) :str(Internal::PrefixStringTree<Char>::GetRoot() + view) {};
PrefixString(const PrefixString& other) :str(other.str) {};
my_string_view ReadValue() const
{
return str;
}
operator my_string_view() const
{
return str;
}
};
#endif // !__FILE_Detail_Core_String_H

View File

@@ -6,5 +6,6 @@
#include <type_traits>
#include <math.h>
#include <algorithm>
#include <string_view>
#endif // !__FILE_Detail_Interface_H