bbobject.h 1.5 KB

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