bbobject.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef BB_OBJECT_H
  2. #define BB_OBJECT_H
  3. #ifdef BB_THREADS
  4. #include "bbgc_mx.h"
  5. #else
  6. #include "bbgc.h"
  7. #endif
  8. //#include "bbstring.h"
  9. #include "bbdebug.h"
  10. struct bbObject : public bbGCNode{
  11. typedef bbObject *bb_object_type;
  12. bbObject(){
  13. bbGC::beginCtor( this );
  14. }
  15. virtual ~bbObject();
  16. //implemented in bbtypeinfo.h
  17. //
  18. virtual bbTypeInfo *typeof()const;
  19. virtual const char *typeName()const;
  20. void *operator new( size_t size ){
  21. return bbGC::malloc( size );
  22. }
  23. //NOTE! We need this in case ctor throws an exception. delete never otherwise called...
  24. //
  25. void operator delete( void *p ){
  26. bbGC::endCtor( (bbObject*)(p) );
  27. }
  28. };
  29. struct bbThrowable : public bbObject{
  30. };
  31. struct bbInterface{
  32. typedef bbInterface *bb_object_type;
  33. virtual ~bbInterface();
  34. };
  35. struct bbNullCtor_t{
  36. };
  37. extern bbNullCtor_t bbNullCtor;
  38. template<class T,class...A> T *bbGCNew( A...a ){
  39. T *p=new T( a... );
  40. bbGC::endCtor( p );
  41. return p;
  42. }
  43. template<class T,class R=typename T::bb_object_type> void bbGCMark( T *p ){
  44. bbGC::enqueue( dynamic_cast<bbObject*>( p ) );
  45. }
  46. template<class T,class C> T bb_object_cast( C *p ){
  47. return dynamic_cast<T>( p );
  48. }
  49. inline void bbDBAssertSelf( void *p ){
  50. bbDebugAssert( p,"Attempt to invoke method on null instance" );
  51. }
  52. inline bbString bbDBObjectValue( bbObject *p ){
  53. char buf[64];
  54. sprintf( buf,"@%p",p );
  55. return buf;
  56. }
  57. inline bbString bbDBInterfaceValue( bbInterface *p ){
  58. return bbDBObjectValue( dynamic_cast<bbObject*>( p ) );
  59. }
  60. template<class T> bbString bbDBStructValue( T *p ){
  61. char buf[64];
  62. sprintf( buf,"@%p:%p",p,&T::dbEmit );
  63. return buf;
  64. }
  65. inline bbString bbDBType( bbObject **p ){
  66. return "Object";
  67. }
  68. inline bbString bbDBValue( bbObject **p ){
  69. return bbDBObjectValue( *p );
  70. }
  71. template<class T> struct bbGCVar{
  72. public:
  73. T *_ptr;
  74. void enqueue(){
  75. bbGC::enqueue( dynamic_cast<bbGCNode*>( _ptr ) );
  76. }
  77. bbGCVar():_ptr( nullptr ){
  78. }
  79. bbGCVar( T *p ):_ptr( p ){
  80. enqueue();
  81. }
  82. bbGCVar( const bbGCVar &p ):_ptr( p._ptr ){
  83. enqueue();
  84. }
  85. bbGCVar &operator=( T *p ){
  86. _ptr=p;
  87. enqueue();
  88. return *this;
  89. }
  90. bbGCVar &operator=( const bbGCVar &p ){
  91. _ptr=p._ptr;
  92. enqueue();
  93. return *this;
  94. }
  95. void discard(){
  96. _ptr=nullptr;
  97. }
  98. T *get()const{
  99. return _ptr;
  100. }
  101. T *operator->()const{
  102. return _ptr;
  103. }
  104. operator T*()const{
  105. return _ptr;
  106. }
  107. T **operator&(){
  108. return &_ptr;
  109. }
  110. };
  111. template<class T,class C> T bb_object_cast( const bbGCVar<C> &v ){
  112. return dynamic_cast<T>( v._ptr );
  113. }
  114. template<class T> void bbGCMark( const bbGCVar<T> &v ){
  115. bbGCMark( v._ptr );
  116. }
  117. #endif