00001 // 00002 // See license.txt for license information. 00003 // 00004 // ref_object.hpp 00005 // 00006 // 4-Jul-2003 phamilton Created 00007 // 00008 00009 #ifndef incCOMMON_REF_OBJECT 00010 #define incCOMMON_REF_OBJECT 00011 00012 // forwards 00013 #include "object.hpp" 00014 #include "deletable_object.hpp" 00015 #include "outerable_object.hpp" 00016 00017 namespace ph { 00018 namespace common { 00019 00020 class ref_object_helper 00021 /** 00022 Helper for an object which refers to another. 00023 */ 00024 { 00025 public: 00026 ref_object_helper() : 00027 _obj(0) 00028 {} 00029 00030 void helper_delete_object(bool owned) 00031 /** 00032 Delete the object attached. Only deletes if the 00033 object is actually owned by this. 00034 */ 00035 { 00036 // only delete sub objects if this is owned. 00037 if (owned) 00038 { 00039 // if the object has a delete interface, then use that. 00040 if (_obj->deletable()) 00041 _obj->deletable()->delete_object(); 00042 else 00043 delete _obj; 00044 } 00045 else 00046 helper_remove(_obj); 00047 } 00048 00049 bool helper_add(object_base *parent, 00050 object_base *obj, bool own, bool owned) 00051 /** 00052 Add a new object to the reference. 00053 */ 00054 { 00055 // ownership policies must match. 00056 // you also can't already have an object. 00057 if (_obj || own != owned) 00058 return false; 00059 if (obj->outerable()) 00060 obj->outerable()->outer(parent); 00061 _obj = obj; 00062 return true; 00063 } 00064 00065 bool helper_remove(object_base *obj) 00066 /** 00067 remove an object from the reference. 00068 */ 00069 { 00070 if (!_obj || obj != _obj) 00071 return false; 00072 _obj = 0; 00073 return true; 00074 } 00075 00076 bool helper_singleton() const 00077 /** 00078 References are singleton objects. 00079 */ 00080 { 00081 return true; 00082 } 00083 00084 bool helper_count() const 00085 /** 00086 Return the number of objects in the reference. 00087 */ 00088 { 00089 return _obj ? 1 : 0; 00090 } 00091 00092 protected: 00093 object_base *_obj; 00094 }; 00095 00096 }; // common 00097 }; // ph 00098 00099 #endif // incCOMMON_REF_OBJECT