bbobject.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef BB_OBJECT_H
  2. #define BB_OBJECT_H
  3. #include "bbgc.h"
  4. #include "bbstring.h"
  5. #include "bbdebug.h"
  6. struct bbObject : public bbGCNode{
  7. typedef bbObject *bb_object_type;
  8. bbObject(){
  9. bbGC::beginCtor( this );
  10. }
  11. virtual ~bbObject();
  12. //implemented in bbtypeinfo.h
  13. //
  14. virtual bbTypeInfo *typeof()const;
  15. virtual const char *typeName()const;
  16. void *operator new( size_t size ){
  17. return bbGC::alloc( size );
  18. }
  19. //NOTE! We need this in case ctor throws an exception. delete never otherwise called...
  20. //
  21. void operator delete( void *p ){
  22. bbGC::endCtor( (bbObject*)(p) );
  23. }
  24. };
  25. struct bbThrowable : public bbObject{
  26. };
  27. struct bbInterface{
  28. typedef bbInterface *bb_object_type;
  29. virtual ~bbInterface();
  30. };
  31. struct bbNullCtor_t{
  32. };
  33. extern bbNullCtor_t bbNullCtor;
  34. template<class T,class...A> T *bbGCNew( A...a ){
  35. T *p=new T( a... );
  36. bbGC::endCtor( p );
  37. return p;
  38. }
  39. template<class T,class R=typename T::bb_object_type> void bbGCMark( T *p ){
  40. bbGC::enqueue( dynamic_cast<bbObject*>( p ) );
  41. }
  42. template<class T,class C> T bb_object_cast( const bbGCVar<C> &p ){
  43. return dynamic_cast<T>( p._ptr );
  44. }
  45. template<class T,class C> T bb_object_cast( C *p ){
  46. return dynamic_cast<T>( p );
  47. }
  48. inline void bbDBAssertSelf( void *p ){
  49. bbDebugAssert( p,"'Self' is null" );
  50. }
  51. inline bbString bbDBObjectValue( bbObject *p ){
  52. char buf[64];
  53. sprintf( buf,"@%p",p );
  54. return buf;
  55. }
  56. inline bbString bbDBInterfaceValue( bbInterface *p ){
  57. return bbDBObjectValue( dynamic_cast<bbObject*>( p ) );
  58. }
  59. template<class T> bbString bbDBStructValue( T *p ){
  60. char buf[64];
  61. sprintf( buf,"@%p:%p",p,&T::dbEmit );
  62. return buf;
  63. }
  64. inline bbString bbDBType( bbObject **p ){
  65. return "Object";
  66. }
  67. inline bbString bbDBValue( bbObject **p ){
  68. return bbDBObjectValue( *p );
  69. }
  70. #endif