StaticAssert.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // "$Id$"
  3. //
  4. // Copyright (c)1992-2012, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef DAHUA_INFRA3_STATIC_ASSERT_H__
  11. #define DAHUA_INFRA3_STATIC_ASSERT_H__
  12. #include "Defs.h"
  13. //
  14. // If the compiler issues warnings about old C style casts,
  15. // then enable this:
  16. //
  17. #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
  18. # define DAHUA_STATIC_ASSERT_BOOL_CAST( x ) ((x) == 0 ? false : true)
  19. #else
  20. # define DAHUA_STATIC_ASSERT_BOOL_CAST(x) (bool)(x)
  21. #endif
  22. namespace Dahua {
  23. namespace Infra {
  24. // HP aCC cannot deal with missing names for template value parameters
  25. template <bool x> struct STATIC_ASSERTION_FAILURE;
  26. template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
  27. // HP aCC cannot deal with missing names for template value parameters
  28. template<int x> struct static_assert_test{};
  29. } // namespace Infra
  30. } // namespace Dahua
  31. //
  32. // Implicit instantiation requires that all member declarations be
  33. // instantiated, but that the definitions are *not* instantiated.
  34. //
  35. // It's not particularly clear how this applies to enum's or typedefs;
  36. // both are described as declarations [7.1.3] and [7.2] in the standard,
  37. // however some compilers use "delayed evaluation" of one or more of
  38. // these when implicitly instantiating templates. We use typedef declarations
  39. // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
  40. // version gets better results from your compiler...
  41. //
  42. // Implementation:
  43. // Both of these versions rely on sizeof(incomplete_type) generating an error
  44. // message containing the name of the incomplete type. We use
  45. // "STATIC_ASSERTION_FAILURE" as the type name here to generate
  46. // an eye catching error message. The result of the sizeof expression is either
  47. // used as an enum initialiser, or as a template argument depending which version
  48. // is in use...
  49. // Note that the argument to the assert is explicitly cast to bool using old-
  50. // style casts: too many compilers currently have problems with static_cast
  51. // when used inside integral constant expressions.
  52. //
  53. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  54. // __LINE__ macro broken when -ZI is used see Q199057
  55. // fortunately MSVC ignores duplicate typedef's.
  56. #define DAHUA_STATIC_ASSERT( B ) \
  57. typedef ::Dahua::Infra::static_assert_test<\
  58. sizeof(::Dahua::Infra::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
  59. > dahua_static_assert_typedef_
  60. #elif defined(_MSC_VER)
  61. #define DAHUA_STATIC_ASSERT( B ) \
  62. typedef ::Dahua::Infra::static_assert_test<\
  63. sizeof(::Dahua::Infra::STATIC_ASSERTION_FAILURE< DAHUA_STATIC_ASSERT_BOOL_CAST ( B ) >)>\
  64. DAHUA_JOIN(dahua_static_assert_typedef_, __COUNTER__)
  65. #else
  66. // generic version
  67. #define DAHUA_STATIC_ASSERT( B ) \
  68. typedef ::Dahua::Infra::static_assert_test<\
  69. sizeof(::Dahua::Infra::STATIC_ASSERTION_FAILURE< DAHUA_STATIC_ASSERT_BOOL_CAST( B ) >)>\
  70. DAHUA_JOIN(dahua_static_assert_typedef_, __LINE__)
  71. #endif
  72. #endif // DAHUA_INFRA_STATIC_ASSERT_H__