gs_object.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef __GS_OBJECT_H__
  2. #define __GS_OBJECT_H__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "common/gs_types.h"
  7. #include "common/gs_util.h"
  8. // Intropection / Reflection keywords that should "compile away" but can be used for code generation
  9. #define _introspect gs_empty_instruction( _introspect )
  10. #define _non_serializable gs_empty_instruction( _non_serializable )
  11. #define _immutable gs_empty_instruction( _immutable )
  12. #define _read_only gs_empty_instruction( _immutable )
  13. #define _ignore gs_empty_instruction( _ignore )
  14. #define _default( ... ) gs_empty_instruction( _default )
  15. #define _attributes( ... ) gs_empty_instruction( _attributes )
  16. #define _ctor( ... ) gs_empty_instruction( _ctor )
  17. #define _defaults( ... ) gs_empty_instruction( _defaults )
  18. #define _serialize( ... ) gs_empty_instruction( _serialize )
  19. #define _deserialize( ... ) gs_empty_instruction( _deserialize )
  20. #define _components( ... ) gs_empty_instruction( _components )
  21. #define _struct_default( type ) type##_default
  22. // Helper macro for typedefing a struture definition
  23. #define gs_struct_def( name, ... ) typedef struct { __VA_ARGS__ } name
  24. // Definition for derived struct ( based another parent struct )
  25. #define gs_derive_def( name, parent, ... ) gs_struct_def( name, parent _base; __VA_ARGS__ )
  26. #define gs_engine_check( statement ) ( statement ) ? true : false
  27. #define _base( base_type ) base_type _base
  28. /*============================================================
  29. // Object Definition: gs_object
  30. ============================================================*/
  31. // This could, instead, be a way to grab the meta class from the given reflected object
  32. typedef struct gs_object
  33. {
  34. // Function pointer for finding the id for a particular object instance
  35. u32 ( * type_id )();
  36. } gs_object;
  37. // Helper macro for specifically deriving some structure from an object struct
  38. #define gs_object_def( name, ... ) gs_derive_def( name, object, __VA_ARGS__ )
  39. #define gs_construct( type )\
  40. *( type* )__gs_default_object_##type()
  41. #define gs_construct_heap( type )\
  42. ( type* )__gs_default_object_##type##_heap()
  43. _inline u32 __gs_type_id_impl( gs_object* obj )
  44. {
  45. gs_assert( obj->type_id != NULL );
  46. return ( obj->type_id() );
  47. }
  48. const char* gs_type_name_obj( gs_object* obj );
  49. #define gs_type_name( obj )\
  50. gs_type_name_obj( gs_cast( gs_object, obj ) )
  51. #define gs_type_id( obj )\
  52. __gs_type_id_impl( gs_cast( gs_object, obj ) )
  53. #define gs_meta_class( obj )\
  54. __gs_meta_class_impl( gs_cast( gs_object, obj ) )
  55. #define gs_type_id_cls( cls )\
  56. ( u32 )gs_meta_class_id_##cls
  57. // #define gs_type_name_cls( cls )\
  58. // __gs_type_name_cls( gs_type_id_cls( cls ) )
  59. #define gs_type_name_cls( cls )\
  60. __gs_type_name_cls( cls )
  61. void
  62. __gs_object_print_impl( gs_object* obj );
  63. #define gs_object_print( obj )\
  64. ( __gs_object_print_impl( gs_cast( gs_object, ( obj ) ) ) )
  65. #define gs_object_serialize( obj, buffer )\
  66. gs_meta_class( gs_cast( gs_object, obj ) )->serialize_func( gs_cast( gs_object, obj ), buffer )
  67. #define gs_object_deserialize( obj, buffer )\
  68. gs_meta_class( gs_cast( gs_object, obj ) )->deserialize_func( gs_cast( gs_object, obj ), buffer )
  69. #ifdef __cplusplus
  70. }
  71. #endif // c++
  72. #endif // __GS_OBJECT_H__