From 8b381bc3a32cc5ae08e446e08a1f0c3eb2c7f3eb Mon Sep 17 00:00:00 2001 From: omy Date: Thu, 19 Sep 2013 16:12:48 +0400 Subject: [PATCH] 0024191: Static assert functionality should be added to Standard_Assert.hxx Added Standard_STATIC_ASSERT macro for compile-time asserts. The new macro is used in Standard_MMgrOpt and QANCollection. --- src/QANCollection/QANCollection4.cxx | 38 ++++++---------------------- src/Standard/Standard_Assert.hxx | 24 ++++++++++++++++++ src/Standard/Standard_MMgrOpt.cxx | 8 ++---- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/QANCollection/QANCollection4.cxx b/src/QANCollection/QANCollection4.cxx index c3c7df0ad1..3ee72d9091 100755 --- a/src/QANCollection/QANCollection4.cxx +++ b/src/QANCollection/QANCollection4.cxx @@ -24,6 +24,8 @@ #include #include +#include + #include #include @@ -41,44 +43,20 @@ static Standard_Integer QANColStdAllocator1(Draw_Interpretor& di, Standard_Integ //type definitions typedef Handle_Standard_Transient elem_type; typedef NCollection_StdAllocator allocator_type; - if ( sizeof (allocator_type::value_type) == sizeof (elem_type) ) { - di << "value_type : OK\n"; - } else { - di << "value_type : Error\n"; - } - if ( sizeof (allocator_type::pointer) == sizeof (void*) ) { - di << "pointer : OK\n"; - } else { - di << "pointer : Error\n"; - } - if (sizeof (allocator_type::const_pointer) == sizeof (void*) ) { - di << "const_pointer : OK\n"; - } else { - di << "const_pointer : Error\n"; - } + Standard_STATIC_ASSERT (sizeof (allocator_type::value_type) == sizeof (elem_type)); + Standard_STATIC_ASSERT (sizeof (allocator_type::pointer) == sizeof (void*)); + Standard_STATIC_ASSERT (sizeof (allocator_type::const_pointer) == sizeof (void*)); elem_type aDummy; allocator_type::reference aRef = aDummy; (void)aRef; // avoid compiler warning on unused allocator_type::const_reference aConstRef = aDummy; (void)aConstRef; // avoid compiler warning on unused - if ( sizeof (allocator_type::size_type) == sizeof (size_t) ) { - di << "size_type : OK\n"; - } else { - di << "size_type : Error\n"; - } - if ( sizeof (allocator_type::difference_type) == sizeof (ptrdiff_t) ) { - di << "allocator_type : OK\n"; - } else { - di << "allocator_type : Error\n"; - } + Standard_STATIC_ASSERT (sizeof (allocator_type::size_type) == sizeof (size_t)); + Standard_STATIC_ASSERT (sizeof (allocator_type::difference_type) == sizeof (ptrdiff_t)); typedef int other_elem_type; - if ( sizeof (allocator_type::rebind::other::value_type) == sizeof (other_elem_type) ) { - di << "other_elem_type : OK\n"; - } else { - di << "other_elem_type : Error\n"; - } + Standard_STATIC_ASSERT (sizeof (allocator_type::rebind::other::value_type) == sizeof (other_elem_type)); return 0; } diff --git a/src/Standard/Standard_Assert.hxx b/src/Standard/Standard_Assert.hxx index 559e6c564f..b14ae06bba 100644 --- a/src/Standard/Standard_Assert.hxx +++ b/src/Standard/Standard_Assert.hxx @@ -62,6 +62,13 @@ //! //! The second argument (message) should be string constant ("..."). //! +//! The Standard_STATIC_ASSERT macro is to be used for compile time checks. +//! To use this macro, write: +//! +//! Standard_STATIC_ASSERT(const_expression); +//! +//! If const_expression is false, a compiler error occurs. +//! //! The macros are formed as functions and require semicolon at the end. // Stub function used to make macros complete C++ operator @@ -155,6 +162,23 @@ inline void Standard_ASSERT_DO_NOTHING() {} //! Raise debug message #define Standard_ASSERT_INVOKE(theDesc) Standard_ASSERT_INVOKE_(always, theDesc) +//! Static assert -- +//! empty default template +template +struct Standard_Static_Assert { }; + +//! Static assert -- specialization for condition being true +template <> +struct Standard_Static_Assert +{ + static void assert_ok() {} +}; + +//! Cause compiler error if argument is not constant expression or +//! evaluates to false +#define Standard_STATIC_ASSERT(theExpr) \ + Standard_Static_Assert::assert_ok(); + #endif // Standard_Assert_HeaderFile #ifdef _MSC_VER diff --git a/src/Standard/Standard_MMgrOpt.cxx b/src/Standard/Standard_MMgrOpt.cxx index 7f049b67f8..0f3e000d9e 100755 --- a/src/Standard/Standard_MMgrOpt.cxx +++ b/src/Standard/Standard_MMgrOpt.cxx @@ -20,7 +20,7 @@ #include #include - +#include #ifdef HAVE_CONFIG_H # include #endif @@ -192,11 +192,7 @@ Standard_MMgrOpt::Standard_MMgrOpt(const Standard_Boolean aClear, const Standard_Size aThreshold) { // check basic assumption - if ( sizeof(Standard_Size) != sizeof(Standard_Address) ) - { - cerr << "Fatal error: Open CASCADE Optimized Memory manager: this platform is not supported!" << endl; - exit(1); - } + Standard_STATIC_ASSERT(sizeof(Standard_Size) == sizeof(Standard_Address)); // clear buffer fields myFreeListMax = 0;