Reference.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /******************************************************************************/
  2. T1(TYPE) struct Reference // Reference, this is a pointer to an object which may be deleted but its memory is still accessible and possibly occupied by another object of similar class
  3. {
  4. // get / set
  5. void clear () {_object=null; _object_id.zero (); } // clear reference to null
  6. Bool is ()C {return _object_id.valid(); } // check if the reference is not empty ( want to point to some object )
  7. Bool empty ()C {return !_object_id.valid(); } // check if the reference is empty (doesn't want to point to any object at all)
  8. Bool valid ()C {return _object && _object->id()==_object_id;} // check if the reference is valid (points to an existing object with matching ID)
  9. TYPE* validPtr ()C {return valid() ? _object : null ;} // return a pointer to object that is valid
  10. C UID & objectID ()C {return _object_id ;} // return object ID
  11. TYPE& operator()()C {return *_object ;} // return a reference to object, usage of this method must be preceded by performing a reference validation using 'valid' method
  12. TYPE* operator->()C {return _object ;} // return a reference to object, usage of this method must be preceded by performing a reference validation using 'valid' method
  13. // compare
  14. T1(OBJECT) Bool operator==( OBJECT *object)C {return T._object== object && T._object_id==(object ? object->id() : UIDZero);} // if reference points to 'object'
  15. T1(OBJECT) Bool operator==(C OBJECT *object)C {return T._object== object && T._object_id==(object ? object->id() : UIDZero);} // if reference points to 'object'
  16. T1(OBJECT) Bool operator==(C OBJECT &object)C {return T._object== &object && T._object_id== object .id() ;} // if reference points to 'object'
  17. T1(TYPE2 ) Bool operator==(C Reference<TYPE2> &ref )C {return T._object==ref._object && T._object_id== ref._object_id ;} // if references are equal
  18. T1(OBJECT) Bool operator!=( OBJECT *object)C {return T._object!= object || T._object_id!=(object ? object->id() : UIDZero);} // if reference doesn't point to 'object'
  19. T1(OBJECT) Bool operator!=(C OBJECT *object)C {return T._object!= object || T._object_id!=(object ? object->id() : UIDZero);} // if reference doesn't point to 'object'
  20. T1(OBJECT) Bool operator!=(C OBJECT &object)C {return T._object!= &object || T._object_id!= object .id() ;} // if reference doesn't point to 'object'
  21. T1(TYPE2 ) Bool operator!=(C Reference<TYPE2> &ref )C {return T._object!=ref._object || T._object_id!= ref._object_id ;} // if references are not equal
  22. // io
  23. Bool save(File &f)C; // save 'object_id', false on fail
  24. Bool load(File &f) ; // load 'object_id' and set 'object' pointer to null, actual linking pointer to the object should be performed later using 'link' method
  25. // link
  26. T1(WORLD) void link(WORLD &world); // link the reference with existing world object
  27. // construct
  28. Reference( ) {clear();}
  29. Reference(TYPE &obj) { T._object=&obj; T._object_id=obj .id(); }
  30. Reference(TYPE *obj) {if(T._object= obj) T._object_id=obj->id();else T._object_id.zero();}
  31. private:
  32. TYPE *_object;
  33. UID _object_id;
  34. };
  35. /******************************************************************************/