#1064 Use constexpr to verify keywords for pdmObject and pdmFields

This commit is contained in:
Magne Sjaastad 2017-01-07 08:57:49 +01:00
parent c65c9b1c58
commit 5d9fd5ddeb
4 changed files with 71 additions and 2 deletions

View File

@ -97,6 +97,8 @@ class PdmObjectCapability;
#define CAF_PDM_InitField(field, keyword, default, uiName, iconResourceName, toolTip, whatsThis) \
{ \
CAF_PDM_VERIFY_XML_KEYWORD(keyword) \
\
static bool chekingThePresenceOfHeaderAndSourceInitMacros = \
Error_You_forgot_to_add_the_macro_CAF_PDM_XML_HEADER_INIT_and_or_CAF_PDM_XML_SOURCE_INIT_to_your_cpp_file_for_this_class(); \
this->isInheritedFromPdmUiObject(); \
@ -113,6 +115,8 @@ class PdmObjectCapability;
#define CAF_PDM_InitFieldNoDefault(field, keyword, uiName, iconResourceName, toolTip, whatsThis) \
{ \
CAF_PDM_VERIFY_XML_KEYWORD(keyword) \
\
static bool chekingThePresenceOfHeaderAndSourceInitMacros = \
Error_You_forgot_to_add_the_macro_CAF_PDM_XML_HEADER_INIT_and_or_CAF_PDM_XML_SOURCE_INIT_to_your_cpp_file_for_this_class(); \
this->isInheritedFromPdmUiObject(); \

View File

@ -33,6 +33,8 @@ set( PROJECT_FILES
cafPdmSettings.h
cafPdmSettings.cpp
cafPdmXmlStringValidation.h
)

View File

@ -1,6 +1,7 @@
#pragma once
#include "cafPdmDefaultObjectFactory.h"
#include "cafPdmXmlStringValidation.h"
// Taken from gtest.h
//
@ -15,6 +16,11 @@
#define PDM_OBJECT_STRING_CONCATENATE(foo, bar) PDM_OBJECT_STRING_CONCATENATE_IMPL_(foo, bar)
#define PDM_OBJECT_STRING_CONCATENATE_IMPL_(foo, bar) foo ## bar
#define CAF_PDM_VERIFY_XML_KEYWORD(keyword) \
static_assert(isFirstCharacterValidInXmlKeyword(keyword), "First character in keyword is invalid"); \
static_assert(!isFirstThreeCharactersXml(keyword), "Keyword starts with invalid sequence xml"); \
static_assert(isValidXmlKeyword(keyword), "Detected invalid character in keyword");
/// CAF_PDM_HEADER_INIT assists the factory used when reading objects from file
/// Place this in the header file inside the class definition of your PdmObject
@ -35,16 +41,25 @@ public: \
#define CAF_PDM_XML_SOURCE_INIT(ClassName, keyword) \
bool ClassName::Error_You_forgot_to_add_the_macro_CAF_PDM_XML_HEADER_INIT_and_or_CAF_PDM_XML_SOURCE_INIT_to_your_cpp_file_for_this_class() { return false;} \
\
QString ClassName::classKeywordStatic() { return keyword; } \
QString ClassName::classKeywordStatic() \
{ \
CAF_PDM_VERIFY_XML_KEYWORD(keyword) \
return keyword; \
} \
static bool PDM_OBJECT_STRING_CONCATENATE(my##ClassName, __LINE__) = caf::PdmDefaultObjectFactory::instance()->registerCreator<ClassName>()
#define CAF_PDM_XML_ABSTRACT_SOURCE_INIT(ClassName, keyword) \
bool ClassName::Error_You_forgot_to_add_the_macro_CAF_PDM_XML_HEADER_INIT_and_or_CAF_PDM_XML_SOURCE_INIT_to_your_cpp_file_for_this_class() { return false;} \
\
QString ClassName::classKeywordStatic() { return keyword; } \
QString ClassName::classKeywordStatic() \
{ \
CAF_PDM_VERIFY_XML_KEYWORD(keyword) \
return keyword; \
} \
#define CAF_PDM_XML_InitField(field, keyword) \
{ \
CAF_PDM_VERIFY_XML_KEYWORD(keyword) \
static bool chekingThePresenceOfHeaderAndSourceInitMacros = \
Error_You_forgot_to_add_the_macro_CAF_PDM_XML_HEADER_INIT_and_or_CAF_PDM_XML_SOURCE_INIT_to_your_cpp_file_for_this_class(); \
this->isInheritedFromPdmXmlSerializable(); \

View File

@ -0,0 +1,48 @@
#pragma once
constexpr bool isLowerCase(char c) {
return (c >= 'a' && c <= 'z');
}
constexpr bool isUpperCase(char c) {
return (c >= 'A' && c <= 'Z');
}
constexpr bool isLetter(char c) {
return isLowerCase(c) || isUpperCase(c);
}
constexpr bool isDigit(char c) {
return (c >= '0' && c <= '9');
}
constexpr bool isValidFirstCharacterInXmlKeyword(char c) {
return (c != '.') && !isDigit(c);
}
constexpr bool isValidCharacterInXmlKeyword(char c) {
return isLetter(c) || isDigit(c) || (c == '_');
}
template< unsigned N > constexpr
bool isValidXmlKeyword(const char(&arr)[N], unsigned i = 0)
{
return (!isValidCharacterInXmlKeyword(arr[i])) ? false :
i == N-2 ? true :
isValidXmlKeyword(arr, i + 1);
}
template< unsigned N > constexpr
bool isFirstCharacterValidInXmlKeyword(const char(&arr)[N])
{
static_assert(N>0, "String literal is too small");
return isValidFirstCharacterInXmlKeyword(arr[0]);
}
template< unsigned N > constexpr
bool isFirstThreeCharactersXml(const char(&arr)[N])
{
return (N < 3) ? false : arr[0] == 'x' && arr[1] == 'm' && arr[2] == 'l';
}