#ifndef __rf_object_ih__ #define __rf_object_ih__ #include "rf_object.hh" #include namespace rftype { using namespace rfrt ; inline Object::Object () : ref_count (0) { if (!allocator.is_dyn_addr(this)) { // // Statically allocated object has one extra reference, so if all other // references are destroyed, the object still remain valid. ref_count++; } } inline Object::~Object () { // // Object destructor is called only in two cases: // 1) It is dynamically allocated and all references to it are destroyed. // 2) It is statically allocated and its life time is expired. if (!allocator.is_dyn_addr(this)) { // // If an object was statically allcated, we remove its extra reference // and check if there are no other references to it. if (--ref_count > 0) { FATAL("Static object to be destroyed has extra references"); } else { #ifdef PARANOIA // // Here an object was dynamically allocated, so its reference counter // should be zero. But if we are paranoid we chek this. if (ref_count > 0) { FATAL("Non-zero reference counter in destructor of dynamic object"); } #endif } } } inline void* Object::operator new (size_t _size) { return allocator.allocate(_size); } inline void* Object::operator new (size_t, void* _ptr) { return _ptr; } inline void Object::operator delete (void* _ptr) { allocator.deallocate(_ptr); } inline int Object::compare (Object const& _obj) const { unsigned t1 = get_type(); unsigned t2 = _obj.get_type(); if (t1 < t2) return -1; else if (t1 > t2) return 1; else FATAL("Unable to compare objects without strict ordering"); return 0; } inline bool Object::operator != (Object const& _obj) const { return !(self == _obj); } inline Ref::Ref (Object* _obj) : object (_obj) { object->ref_count++; } inline Ref::Ref (Ref const& _ref) : object (_ref.object) { object->ref_count++; } inline Ref::~Ref () { if (--object->ref_count == 0) delete object; } inline Ref& Ref::operator = (Ref const& _ref) { if (this != &_ref) { this->~Ref(); new(this) Ref(_ref); } return self; } inline Object& Ref::operator * () { return *object; } } #endif // __rf_object_ih__