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.

122 lines
3.2 KiB
C++

/**
*
* @file FITKEnumTransformer.hpp
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*
*/
#ifndef __FITKENUMTRANSFORMER_H___
#define __FITKENUMTRANSFORMER_H___
#include <QMetaEnum>
#include <QStringList>
#include <QString>
namespace Core
{
template <typename EnumType>
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
class FITKEnumTransfer
{
public:
/**
* @brief Construct a new FITKEnumTransfer object
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
FITKEnumTransfer()
{
_enumMeta = QMetaEnum::fromType<EnumType>();
}
~FITKEnumTransfer() = default;
/**
* @brief
* @param[i] string
* @param[o] ok
* @return EnumType
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
EnumType fromString(const QString& string, bool& ok)
{
QByteArray ba = string.toLocal8Bit();
EnumType resEnum = (EnumType)_enumMeta.keyToValue(ba.data(), &ok);
return resEnum;
}
/**
* @brief
* @param[i] value
* @param[o] ok
* @return QString
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
QString toStrting(EnumType value, bool& ok)
{
ok = false;
QString result = _enumMeta.valueToKey(value);
if (!result.isEmpty()) ok = true;
return result;
}
/**
* @brief
* @return int
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
int memberCount()
{
return _enumMeta.keyCount();
}
/**
* @brief
* @param[i] v
* @return true
* @return false
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
bool isValid(const QString& v)
{
QByteArray ba = v.toLocal8Bit();
bool ok = false;
_enumMeta.keyToValue(ba.data(), &ok);
return ok;
}
/**
* @brief
* @return QStringList
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
QStringList members()
{
QStringList sl;
const int n = this->memberCount();
for (int i = 0; i< n ; ++i)
{
EnumType value = (EnumType)_enumMeta.value(i);
bool ok = false;
QString v = toStrting(value, ok);
if(ok) sl.append(v);
}
return sl;
}
private:
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-15
*/
QMetaEnum _enumMeta;
};
}
#endif