mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Enable the use of the C++11 enum class in caf::AppEnum
This commit is contained in:
parent
d4b7797bb1
commit
c4951a5a03
@ -4,6 +4,7 @@
|
||||
#include "cafFilePath.h"
|
||||
#include "cafPdmPointer.h"
|
||||
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
|
||||
@ -60,12 +61,13 @@ public:
|
||||
static QVariant convert(const caf::AppEnum<T>& value)
|
||||
{
|
||||
T enumValue = value;
|
||||
return QVariant(enumValue);
|
||||
// Explicit cast to an int before storage in a QVariant. This allows the use of enum class instead of enum
|
||||
return QVariant(static_cast<int>(enumValue));
|
||||
}
|
||||
|
||||
static void setFromVariant(const QVariant& variantValue, caf::AppEnum<T>& value)
|
||||
{
|
||||
value = (T)variantValue.toInt();
|
||||
value = static_cast<T>(variantValue.toInt());
|
||||
}
|
||||
|
||||
static bool isEqual(const QVariant& variantValue, const QVariant& variantValue2)
|
||||
|
@ -6,6 +6,8 @@
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace caf
|
||||
{
|
||||
|
||||
@ -151,8 +153,9 @@ public:
|
||||
/// Convert the field value into a QVariant
|
||||
static QVariant convert(const caf::AppEnum<T>& value)
|
||||
{
|
||||
int enumIntVal = value;
|
||||
return QVariant(enumIntVal);
|
||||
T enumVal = value;
|
||||
// Explicit cast to an int for storage in a QVariant. This allows the use of enum class instead of enum
|
||||
return QVariant(static_cast<int>(enumVal));
|
||||
}
|
||||
|
||||
/// Set the field value from a QVariant
|
||||
@ -175,8 +178,8 @@ public:
|
||||
|
||||
for (size_t i = 0; i < caf::AppEnum<T>::size(); ++i)
|
||||
{
|
||||
int enumIntVal = caf::AppEnum<T>::fromIndex(i);
|
||||
optionList.push_back(PdmOptionItemInfo(caf::AppEnum<T>::uiTextFromIndex(i), enumIntVal));
|
||||
T enumVal = caf::AppEnum<T>::fromIndex(i);
|
||||
optionList.push_back(PdmOptionItemInfo(caf::AppEnum<T>::uiTextFromIndex(i), static_cast<int>(enumVal)));
|
||||
}
|
||||
|
||||
return optionList;
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <set>
|
||||
#include <type_traits>
|
||||
|
||||
namespace caf
|
||||
{
|
||||
@ -111,6 +112,14 @@ private:
|
||||
class PdmOptionItemInfo
|
||||
{
|
||||
public:
|
||||
// Template pass-through for enum types, ensuring the T type gets cast to an int before storing in the QVariant
|
||||
// Note the extra dummy parameter. This ensures compilation fails for non-enum types and these variants get removed
|
||||
// due to SFINA (https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error)
|
||||
template<typename T>
|
||||
PdmOptionItemInfo(const QString& anOptionUiText, T aValue, bool isReadOnly = false, const QIconProvider& anIcon = QIconProvider(), typename std::enable_if<std::is_enum<T>::value>::type* = 0)
|
||||
: PdmOptionItemInfo(anOptionUiText, QVariant(static_cast<int>(aValue)), isReadOnly, anIcon)
|
||||
{
|
||||
}
|
||||
PdmOptionItemInfo(const QString& anOptionUiText, const QVariant& aValue, bool isReadOnly = false, const QIconProvider& anIcon = QIconProvider());
|
||||
PdmOptionItemInfo(const QString& anOptionUiText, caf::PdmObjectHandle* obj, bool isReadOnly = false, const QIconProvider& anIcon = QIconProvider());
|
||||
|
||||
@ -143,6 +152,7 @@ private:
|
||||
};
|
||||
|
||||
class PdmUiEditorHandle;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Finds the indexes into the optionList that the field value(s) corresponds to.
|
||||
/// In the case where the field is some kind of array, several indexes might be returned
|
||||
|
Loading…
Reference in New Issue
Block a user