Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.1 KiB

  1. #ifndef SCHEMA_VERIFICATION_H_
  2. #define SCHEMA_VERIFICATION_H_
  3. /*
  4. inline int SchemaVerificationMemberSize( const char *pStructName, const char *pMemberName )
  5. {
  6. const CResourceStructIntrospection* pStructIntro = g_pResourceSystem->FindStructIntrospection( pStructName );
  7. if ( !pStructIntro )
  8. {
  9. return -1;
  10. }
  11. const CResourceFieldIntrospection* pFieldIntro = pStructIntro->FindField( pMemberName );
  12. if ( !pFieldIntro )
  13. {
  14. return -1;
  15. }
  16. return pFieldIntro->GetElementSize(0);
  17. }
  18. inline int SchemaVerificationMemberMemoryOffset( const char *pStructName, const char *pMemberName )
  19. {
  20. const CResourceStructIntrospection* pStructIntro = g_pResourceSystem->FindStructIntrospection( pStructName );
  21. if ( !pStructIntro )
  22. {
  23. return -1;
  24. }
  25. const CResourceFieldIntrospection* pFieldIntro = pStructIntro->FindField( pMemberName );
  26. if ( !pFieldIntro )
  27. {
  28. return -1;
  29. }
  30. return pFieldIntro->m_nInMemoryOffset;
  31. }
  32. */
  33. template<class T> class CTAlignmentOfHelper
  34. {
  35. T t; byte b; // the extra byte will force the compiler to pad it out by an additional alignment
  36. };
  37. #define schema_alignmentof( _className ) ( sizeof( CTAlignmentOfHelper<_className> ) - sizeof(_className) )
  38. #define VERIFY_FOR_SCHEMA( _description, _expectedValue, _value ) \
  39. { \
  40. if ( ( (_expectedValue) != (_value) ) ) \
  41. { \
  42. Warning( "[FAILED] - " _description " - Expected %d but got %d\n", (_expectedValue), (_value) ); \
  43. nLocalErrors++; \
  44. } \
  45. else \
  46. { \
  47. Msg( "[ OK ] - " _description "\n" ); \
  48. } \
  49. }
  50. #define VERIFY_SCHEMA_MEMBER_SIZE( _className, _memberName, _expectedSize ) \
  51. VERIFY_FOR_SCHEMA( "Member size of " #_className "::" #_memberName, _expectedSize, sizeof( (( _className *)(0))->_memberName ) );
  52. #define VERIFY_SCHEMA_MEMBER_MEMORY_OFFSET( _className, _memberName, _expectedOffset ) \
  53. VERIFY_FOR_SCHEMA( "Member offset of " #_className "::" #_memberName, _expectedOffset, offsetof( _className , _memberName ) );
  54. #define VERIFY_SCHEMA_TYPE_MEMORY_SIZE( _className, _expectedSize ) \
  55. VERIFY_FOR_SCHEMA( "Struct size of " #_className, _expectedSize, sizeof( _className ) );
  56. #define VERIFY_SCHEMA_TYPE_ALIGNMENT( _className, _expectedAlignment ) \
  57. VERIFY_FOR_SCHEMA( "Struct alignment of " #_className, _expectedAlignment, schema_alignmentof( _className ) );
  58. #define VERIFY_SCHEMA_MEMBER_FIX_ARRAY_ELEMENT_SIZE( _className, _memberName, _expectedSize ) \
  59. VERIFY_FOR_SCHEMA( "Member array element size of " #_className "::" #_memberName, _expectedSize, sizeof( (( _className *)(0))->_memberName [ 0 ] ) );
  60. #define VERIFY_SCHEMA_MEMBER_FIX_ARRAY_LENGTH( _className, _memberName, _expectedLength ) \
  61. VERIFY_FOR_SCHEMA( "Member array length of " #_className "::" #_memberName, _expectedLength, ( sizeof( (( _className *)(0))->_memberName ) / sizeof( (( _className *)(0))->_memberName [ 0 ] ) ) );
  62. #define BEGIN_SCHEMA_CLASS_VERIFY( _className ) \
  63. class CSchemaVerificationFor##_className { \
  64. public: static int DoVerify() { \
  65. int nLocalErrors = 0; \
  66. Msg( "Schema Verification: " #_className "\n" );
  67. #define END_SCHEMA_CLASS_VERIFY( ) \
  68. Msg( "\n\n" ); \
  69. return nLocalErrors; \
  70. } };
  71. #define PERFORM_SCHEMA_CLASS_VERIFY( _className ) nErrors += CSchemaVerificationFor##_className::DoVerify();
  72. #endif