0024191: Static assert functionality should be added to Standard_Assert.hxx
authoromy <omy@opencascade.com>
Thu, 19 Sep 2013 12:12:48 +0000 (16:12 +0400)
committerbugmaster <bugmaster@opencascade.com>
Thu, 26 Sep 2013 13:10:01 +0000 (17:10 +0400)
Added Standard_STATIC_ASSERT macro for compile-time asserts.
The new macro is used in Standard_MMgrOpt and QANCollection.

src/QANCollection/QANCollection4.cxx
src/Standard/Standard_Assert.hxx
src/Standard/Standard_MMgrOpt.cxx

index c3c7df0..3ee72d9 100755 (executable)
@@ -24,6 +24,8 @@
 
 #include <NCollection_StdAllocator.hxx>
 #include <NCollection_IncAllocator.hxx>
+#include <Standard_Assert.hxx>
+
 #include <list>
 #include <vector>
 
@@ -41,44 +43,20 @@ static Standard_Integer QANColStdAllocator1(Draw_Interpretor& di, Standard_Integ
   //type definitions
   typedef Handle_Standard_Transient elem_type;
   typedef NCollection_StdAllocator<elem_type> 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_elem_type>::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_elem_type>::other::value_type) == sizeof (other_elem_type));
 
   return 0;
 }
index 559e6c5..b14ae06 100644 (file)
 //!
 //! 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 <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
 
 #ifdef _MSC_VER
index 7f049b6..0f3e000 100755 (executable)
@@ -20,7 +20,7 @@
 
 #include <Standard_MMgrOpt.hxx>
 #include <Standard_OutOfMemory.hxx>
-
+#include <Standard_Assert.hxx>
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #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;