bbobject.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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::malloc( 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( C *p ){
  43. return dynamic_cast<T>( p );
  44. }
  45. inline void bbDBAssertSelf( void *p ){
  46. bbDebugAssert( p,"Attempt to invoke method on null instance" );
  47. }
  48. inline bbString bbDBObjectValue( bbObject *p ){
  49. char buf[64];
  50. sprintf( buf,"@%p",p );
  51. return buf;
  52. }
  53. inline bbString bbDBInterfaceValue( bbInterface *p ){
  54. return bbDBObjectValue( dynamic_cast<bbObject*>( p ) );
  55. }
  56. template<class T> bbString bbDBStructValue( T *p ){
  57. char buf[64];
  58. sprintf( buf,"@%p:%p",p,&T::dbEmit );
  59. return buf;
  60. }
  61. inline bbString bbDBType( bbObject **p ){
  62. return "Object";
  63. }
  64. inline bbString bbDBValue( bbObject **p ){
  65. return bbDBObjectValue( *p );
  66. }
  67. template<class T> struct bbGCVar{
  68. public:
  69. T *_ptr;
  70. void enqueue(){
  71. bbGC::enqueue( dynamic_cast<bbGCNode*>( _ptr ) );
  72. }
  73. bbGCVar():_ptr( nullptr ){
  74. }
  75. bbGCVar( T *p ):_ptr( p ){
  76. enqueue();
  77. }
  78. bbGCVar( const bbGCVar &p ):_ptr( p._ptr ){
  79. enqueue();
  80. }
  81. bbGCVar &operator=( T *p ){
  82. _ptr=p;
  83. enqueue();
  84. return *this;
  85. }
  86. bbGCVar &operator=( const bbGCVar &p ){
  87. _ptr=p._ptr;
  88. enqueue();
  89. return *this;
  90. }
  91. void discard(){
  92. _ptr=nullptr;
  93. }
  94. T *get()const{
  95. return _ptr;
  96. }
  97. T *operator->()const{
  98. return _ptr;
  99. }
  100. operator T*()const{
  101. return _ptr;
  102. }
  103. T **operator&(){
  104. return &_ptr;
  105. }
  106. };
  107. template<class T,class C> T bb_object_cast( const bbGCVar<C> &v ){
  108. return dynamic_cast<T>( v._ptr );
  109. }
  110. template<class T> void bbGCMark( const bbGCVar<T> &v ){
  111. bbGCMark( v._ptr );
  112. }
  113. #endif