Files
ResInsight/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmXmlObjectHandleMacros.h
Magne Sjaastad 952e766c2f Update clang-format.yml (#10068)
* Update to clang-format-15
Removed two custom .clang-format files in subfolders of AppFwk

* Fixes by clang-format
2023-04-13 07:05:53 +02:00

84 lines
7.6 KiB
C++

#pragma once
#include "cafPdmDefaultObjectFactory.h"
#include "cafPdmXmlStringValidation.h"
// Taken from gtest.h
//
// Due to C++ preprocessor weirdness, we need double indirection to
// concatenate two tokens when one of them is __LINE__. Writing
//
// foo ## __LINE__
//
// will result in the token foo__LINE__, instead of foo followed by
// the current line number. For more details, see
// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
#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
// To be renamed CAF_PDM_XML_HEADER_INIT
#define CAF_PDM_XML_HEADER_INIT \
public: \
QString classKeyword() const override; \
static QString classKeywordStatic(); \
static std::vector<QString> classKeywordAliases(); \
bool matchesClassKeyword( const QString& keyword ) const override; \
\
static bool 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()
#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::classKeyword() const \
{ \
return classKeywordStatic(); \
} \
QString ClassName::classKeywordStatic() \
{ \
return classKeywordAliases().front(); \
} \
std::vector<QString> ClassName::classKeywordAliases() \
{ \
CAF_PDM_VERIFY_XML_KEYWORD( keyword ) \
return { keyword, ##__VA_ARGS__ }; \
} \
bool ClassName::matchesClassKeyword( const QString& matchKeyword ) const \
{ \
auto aliases = classKeywordAliases(); \
for ( const auto& alias : aliases ) \
{ \
if ( alias == matchKeyword ) return true; \
} \
return false; \
}
/// CAF_PDM_XML_SOURCE_INIT associates the file keyword used for storage with the class and
// initializes the factory
/// Place this in the cpp file, preferably above the constructor
#define CAF_PDM_XML_SOURCE_INIT( ClassName, keyword, ... ) \
CAF_PDM_XML_ABSTRACT_SOURCE_INIT( ClassName, keyword, ##__VA_ARGS__ ) \
static bool PDM_OBJECT_STRING_CONCATENATE( my##ClassName, __LINE__ ) = \
caf::PdmDefaultObjectFactory::instance()->registerCreator<ClassName>()
#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(); \
\
addXmlCapabilityToField( ( field ) ); \
addField( ( field ), ( keyword ) ); \
}