From c09f6d8425421ed94931311eb8daa46e3d9c6565 Mon Sep 17 00:00:00 2001 From: Xiaoqiang Wang Date: Mon, 29 May 2023 20:58:41 +0200 Subject: [PATCH 1/2] use C++11 constexpr to replace static typeCode clang-cl has a problem with static memeber variable https://github.com/llvm/llvm-project/issues/54814. But it has no problem with constexpr member variable. --- src/factory/PVDataCreateFactory.cpp | 4 +-- src/pv/pvData.h | 42 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/factory/PVDataCreateFactory.cpp b/src/factory/PVDataCreateFactory.cpp index 6826e62a..d7e0e783 100644 --- a/src/factory/PVDataCreateFactory.cpp +++ b/src/factory/PVDataCreateFactory.cpp @@ -30,7 +30,7 @@ using std::min; namespace epics { namespace pvData { - +#if __cplusplus < 201103L template<> const ScalarType PVBoolean::typeCode = pvBoolean; template<> const ScalarType PVByte::typeCode = pvByte; template<> const ScalarType PVShort::typeCode = pvShort; @@ -56,7 +56,7 @@ template<> const ScalarType PVULongArray::typeCode = pvULong; template<> const ScalarType PVFloatArray::typeCode = pvFloat; template<> const ScalarType PVDoubleArray::typeCode = pvDouble; template<> const ScalarType PVStringArray::typeCode = pvString; - +#endif template PVScalarValue::~PVScalarValue() {} diff --git a/src/pv/pvData.h b/src/pv/pvData.h index a33fb0eb..d7e67e2b 100644 --- a/src/pv/pvData.h +++ b/src/pv/pvData.h @@ -16,6 +16,9 @@ #include #include #include +#if __cplusplus >= 201103L +#include +#endif #include @@ -124,6 +127,36 @@ typedef std::tr1::shared_ptr PVUnionArrayPtrArrayPtr; class PVDataCreate; typedef std::tr1::shared_ptr PVDataCreatePtr; +#if __cplusplus >= 201103L +template +constexpr ScalarType typeToCode() { + if (std::is_same::value) + return pvBoolean; + else if (std::is_same::value) + return pvByte; + else if (std::is_same::value) + return pvUByte; + else if (std::is_same::value) + return pvShort; + else if (std::is_same::value) + return pvUShort; + else if (std::is_same::value) + return pvInt; + else if (std::is_same::value) + return pvUInt; + else if (std::is_same::value) + return pvLong; + else if (std::is_same::value) + return pvULong; + else if (std::is_same::value) + return pvFloat; + else if (std::is_same::value) + return pvDouble; + else if (std::is_same::value) + return pvString; +} +#endif + /** * @brief This class is implemented by code that calls setPostHander * @@ -383,7 +416,11 @@ class epicsShareClass PVScalarValue : public PVScalar { typedef T* pointer; typedef const T* const_pointer; + #if __cplusplus < 201103L static const ScalarType typeCode; + #else + constexpr static const ScalarType typeCode = typeToCode(); + #endif /** * Destructor @@ -1184,8 +1221,11 @@ class epicsShareClass PVValueArray : public detail::PVVectorStorage svector; typedef ::epics::pvData::shared_vector const_svector; - + #if __cplusplus < 201103L static const ScalarType typeCode; + #else + constexpr static const ScalarType typeCode = typeToCode(); + #endif /** * Destructor From c87dcdeef3fe65e2a12ee7c91d5c361608969264 Mon Sep 17 00:00:00 2001 From: Xiaoqiang Wang Date: Mon, 19 Jun 2023 09:41:00 +0200 Subject: [PATCH 2/2] only use constexpr with clang-cl --- src/factory/PVDataCreateFactory.cpp | 2 +- src/pv/pvData.h | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/factory/PVDataCreateFactory.cpp b/src/factory/PVDataCreateFactory.cpp index d7e0e783..883a65f7 100644 --- a/src/factory/PVDataCreateFactory.cpp +++ b/src/factory/PVDataCreateFactory.cpp @@ -30,7 +30,7 @@ using std::min; namespace epics { namespace pvData { -#if __cplusplus < 201103L +#if ! (defined(__clang__) && defined(_MSC_VER)) template<> const ScalarType PVBoolean::typeCode = pvBoolean; template<> const ScalarType PVByte::typeCode = pvByte; template<> const ScalarType PVShort::typeCode = pvShort; diff --git a/src/pv/pvData.h b/src/pv/pvData.h index d7e67e2b..59cc097e 100644 --- a/src/pv/pvData.h +++ b/src/pv/pvData.h @@ -16,7 +16,7 @@ #include #include #include -#if __cplusplus >= 201103L +#if defined(__clang__) && defined(_MSC_VER) #include #endif @@ -127,7 +127,7 @@ typedef std::tr1::shared_ptr PVUnionArrayPtrArrayPtr; class PVDataCreate; typedef std::tr1::shared_ptr PVDataCreatePtr; -#if __cplusplus >= 201103L +#if defined(__clang__) && defined(_MSC_VER) template constexpr ScalarType typeToCode() { if (std::is_same::value) @@ -416,10 +416,10 @@ class epicsShareClass PVScalarValue : public PVScalar { typedef T* pointer; typedef const T* const_pointer; - #if __cplusplus < 201103L - static const ScalarType typeCode; - #else + #if defined(__clang__) && defined(_MSC_VER) constexpr static const ScalarType typeCode = typeToCode(); + #else + static const ScalarType typeCode; #endif /** @@ -1221,10 +1221,10 @@ class epicsShareClass PVValueArray : public detail::PVVectorStorage svector; typedef ::epics::pvData::shared_vector const_svector; - #if __cplusplus < 201103L - static const ScalarType typeCode; - #else + #if defined(__clang__) && defined(_MSC_VER) constexpr static const ScalarType typeCode = typeToCode(); + #else + static const ScalarType typeCode; #endif /**