mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-04-10 18:51:21 +03:00
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.
This commit is contained in:
parent
0ebaa4dbc9
commit
8b381bc3a3
@ -24,6 +24,8 @@
|
|||||||
|
|
||||||
#include <NCollection_StdAllocator.hxx>
|
#include <NCollection_StdAllocator.hxx>
|
||||||
#include <NCollection_IncAllocator.hxx>
|
#include <NCollection_IncAllocator.hxx>
|
||||||
|
#include <Standard_Assert.hxx>
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -41,44 +43,20 @@ static Standard_Integer QANColStdAllocator1(Draw_Interpretor& di, Standard_Integ
|
|||||||
//type definitions
|
//type definitions
|
||||||
typedef Handle_Standard_Transient elem_type;
|
typedef Handle_Standard_Transient elem_type;
|
||||||
typedef NCollection_StdAllocator<elem_type> allocator_type;
|
typedef NCollection_StdAllocator<elem_type> allocator_type;
|
||||||
if ( sizeof (allocator_type::value_type) == sizeof (elem_type) ) {
|
Standard_STATIC_ASSERT (sizeof (allocator_type::value_type) == sizeof (elem_type));
|
||||||
di << "value_type : OK\n";
|
Standard_STATIC_ASSERT (sizeof (allocator_type::pointer) == sizeof (void*));
|
||||||
} else {
|
Standard_STATIC_ASSERT (sizeof (allocator_type::const_pointer) == sizeof (void*));
|
||||||
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";
|
|
||||||
}
|
|
||||||
|
|
||||||
elem_type aDummy;
|
elem_type aDummy;
|
||||||
allocator_type::reference aRef = aDummy;
|
allocator_type::reference aRef = aDummy;
|
||||||
(void)aRef; // avoid compiler warning on unused
|
(void)aRef; // avoid compiler warning on unused
|
||||||
allocator_type::const_reference aConstRef = aDummy;
|
allocator_type::const_reference aConstRef = aDummy;
|
||||||
(void)aConstRef; // avoid compiler warning on unused
|
(void)aConstRef; // avoid compiler warning on unused
|
||||||
if ( sizeof (allocator_type::size_type) == sizeof (size_t) ) {
|
Standard_STATIC_ASSERT (sizeof (allocator_type::size_type) == sizeof (size_t));
|
||||||
di << "size_type : OK\n";
|
Standard_STATIC_ASSERT (sizeof (allocator_type::difference_type) == sizeof (ptrdiff_t));
|
||||||
} 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";
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef int other_elem_type;
|
typedef int other_elem_type;
|
||||||
if ( sizeof (allocator_type::rebind<other_elem_type>::other::value_type) == sizeof (other_elem_type) ) {
|
Standard_STATIC_ASSERT (sizeof (allocator_type::rebind<other_elem_type>::other::value_type) == sizeof (other_elem_type));
|
||||||
di << "other_elem_type : OK\n";
|
|
||||||
} else {
|
|
||||||
di << "other_elem_type : Error\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,13 @@
|
|||||||
//!
|
//!
|
||||||
//! The second argument (message) should be string constant ("...").
|
//! 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.
|
//! The macros are formed as functions and require semicolon at the end.
|
||||||
|
|
||||||
// Stub function used to make macros complete C++ operator
|
// Stub function used to make macros complete C++ operator
|
||||||
@ -155,6 +162,23 @@ inline void Standard_ASSERT_DO_NOTHING() {}
|
|||||||
//! Raise debug message
|
//! Raise debug message
|
||||||
#define Standard_ASSERT_INVOKE(theDesc) Standard_ASSERT_INVOKE_(always, theDesc)
|
#define Standard_ASSERT_INVOKE(theDesc) Standard_ASSERT_INVOKE_(always, theDesc)
|
||||||
|
|
||||||
|
//! Static assert --
|
||||||
|
//! empty default template
|
||||||
|
template <bool condition>
|
||||||
|
struct Standard_Static_Assert { };
|
||||||
|
|
||||||
|
//! Static assert -- specialization for condition being true
|
||||||
|
template <>
|
||||||
|
struct Standard_Static_Assert<true>
|
||||||
|
{
|
||||||
|
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<theExpr>::assert_ok();
|
||||||
|
|
||||||
#endif // Standard_Assert_HeaderFile
|
#endif // Standard_Assert_HeaderFile
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
#include <Standard_MMgrOpt.hxx>
|
#include <Standard_MMgrOpt.hxx>
|
||||||
#include <Standard_OutOfMemory.hxx>
|
#include <Standard_OutOfMemory.hxx>
|
||||||
|
#include <Standard_Assert.hxx>
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
# include <config.h>
|
# include <config.h>
|
||||||
#endif
|
#endif
|
||||||
@ -192,11 +192,7 @@ Standard_MMgrOpt::Standard_MMgrOpt(const Standard_Boolean aClear,
|
|||||||
const Standard_Size aThreshold)
|
const Standard_Size aThreshold)
|
||||||
{
|
{
|
||||||
// check basic assumption
|
// check basic assumption
|
||||||
if ( sizeof(Standard_Size) != sizeof(Standard_Address) )
|
Standard_STATIC_ASSERT(sizeof(Standard_Size) == sizeof(Standard_Address));
|
||||||
{
|
|
||||||
cerr << "Fatal error: Open CASCADE Optimized Memory manager: this platform is not supported!" << endl;
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// clear buffer fields
|
// clear buffer fields
|
||||||
myFreeListMax = 0;
|
myFreeListMax = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user