You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.0 KiB
C

1 month ago
#pragma once
#include <vector>
#include <afx.h>
#include "TypeDefine.h"
/**
* CList
*
* \list CList
* \pos POSITION 使 list.GetAt(pos)
*/
#define CListForEach(pos, list) \
for (POSITION pos = (list).GetHeadPosition(); pos != nullptr; (list).GetNext(pos))
/**
* \brief CString
* \param str CString
* \param prefix
* \return CStringtruefalse
*/
inline bool StartsWith(const CString& str, const TCHAR *prefix)
{
assert(prefix != nullptr);
size_t prefixLength = _tcslen(prefix);
if (str.GetLength() < prefixLength)
{
return false;
}
return _tcsncmp(str.GetString(), prefix, prefixLength) == 0;
}
/**
* \brief CStringCString
* \param str CString
* \param prefix CString
* \return CStringtruefalse
*/
inline bool StartsWith(const CString& str, const CString &prefix)
{
if (str.GetLength() < prefix.GetLength())
{
return false;
}
return _tcsncmp(str.GetString(), prefix.GetString(), prefix.GetLength()) == 0;
}
/**
* \brief CStringCString
* \param str CString
* \param suffix CString
* \return CStringtruefalse
*/
inline bool EndsWith(const CString& str, const CString& suffix)
{
int strLength = str.GetLength();
int suffixLength = suffix.GetLength();
if (strLength < suffixLength)
{
return false;
}
return (str.Right(suffixLength) == suffix);
}
/**
* \brief CString
* \param strings CString
* \param delimiter CString
* \return CString
*/
inline CString Join(const std::vector<CString>& strings, const CString& delimiter)
{
CString joinedString;
for (int i = 0; i < strings.size(); i++)
{
joinedString += strings[i];
if (i != strings.size() - 1)
joinedString += delimiter;
}
return joinedString;
}
/**
* \brief path
*
* \param path
* \return
*/
inline CString LayerParentPath(CString& path)
{
int pos = path.ReverseFind(_T('\\'));
if (pos != -1) {
return path.Left(pos);
}
return "";
}
/**
* \brief
*
* \param path1
* \param path2
* \return
*/
inline bool IsSameParentPath(CString& path1, CString& path2)
{
CString parentPath1 = LayerParentPath(path1);
CString parentPath2 = LayerParentPath(path2);
return parentPath1.CompareNoCase(parentPath2) == 0;
}
/**
* \brief (...)
*
* \param childPath
* \param parentPath
* \return
*/
inline bool IsSubPath(const CString& childPath, const CString& parentPath)
{
if (EndsWith(parentPath, _T("\\")))
{
return StartsWith(childPath, parentPath);
}
return StartsWith(childPath, parentPath + _T("\\"));
}
inline void MarkSectionBegin(CArchive& ar, const short& ver)
{
int beginType = SECTION_BEGIN;
pf.WriteElementType(ar, beginType, ver);
}
inline void MarkSectionEnd(CArchive& ar, const short& ver)
{
int endType = SECTION_END;
pf.WriteElementType(ar, endType, ver);
}
inline void VerifySectionBegin(CArchive& ar, const short& ver)
{
int beginType = 0;
pf.ReadElementType(ar, beginType, ver);
if (beginType != SECTION_BEGIN)
{
throw new CArchiveException(CArchiveException::badClass);
}
}
inline void VerifySectionEnd(CArchive& ar, const short& ver)
{
int endType = 0;
pf.ReadElementType(ar, endType, ver);
if (endType != SECTION_END)
{
throw new CArchiveException(CArchiveException::badClass);
}
}
/**
*
*
* \param value
* \param precision < 0
* \return
*/
inline double TruncToPrecision(double value, int precision)
{
if (precision < 0)
{
return value;
}
if (precision == 0)
{
return std::trunc(value);
}
double factor = std::pow(10.0, precision); // 10^n
return std::trunc(value * factor) / factor; // 截断到指定小数位
}
struct CStringHash
{
size_t operator()(const CString& str) const
{
// 使用 CString 的底层字符串进行哈希计算
return std::hash<std::string>()(str.GetString());
}
};
/**
* nullptr
*
* \param pointer
*/
template <typename T>
void SafeDelete(T*& pointer)
{
delete pointer;
pointer = nullptr;
}
/**
* nullptr
*
* \param pointer
*/
template <typename T>
void SafeDeleteArray(T*& pointer)
{
delete[] pointer;
pointer = nullptr;
}
template <typename T>
void ClonePointer(T*& destination, const T* source)
{
delete destination;
destination = nullptr;
if (source != nullptr)
{
destination = new T();
*destination = const_cast<T&>(*source);
}
}