PLearn 0.1
|
Object which maintains a "parent" pointer as part of an object graph. More...
#include <ParentableObject.h>
Public Types | |
enum | ParentKind { AnyParent, WeakParent, UniqueParent } |
Kind of parenting relationship; see class help for details. More... | |
Public Member Functions | |
ParentableObject (bool adoptive_parent=false, ParentKind=WeakParent) | |
Default constructor. | |
Object * | parent () |
Accessor for parent object. | |
const Object * | parent () const |
virtual void | updateChildrensParent (Object *parent) |
Function that actually does the heavy work of traversing the descendants and setting _their_ backpointers to point to the specified parent. | |
virtual void | setParent (Object *parent) |
Setter for the parent object; virtual since "transparent parentables" might wish to forward it to children. | |
virtual void | checkParent () const |
After the m_parent field of a child has been set, this function is called so that the child has a chance to check the parent (e.g. | |
virtual ParentableObject * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static string | _classname_ () |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
static void | declareMethods (RemoteMethodMap &rmm) |
Declare the methods that are remote-callable. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Attributes | |
Object * | m_parent |
Backpointer to parent. | |
bool | m_adoptive_parent |
If true, don't set the subobjects' parent to this. | |
ParentKind | m_parent_kind |
The kind of parent. | |
Private Types | |
typedef Object | inherited |
Private Member Functions | |
void | build_ () |
Simply call updateChildrensParent with this. |
Object which maintains a "parent" pointer as part of an object graph.
The purpose of ParentableObject
is to facilitate the building of complex graphs of Objects
. The basic ideas are as follows:
parent
, which is simply a backpointer to the "parent" object in the graph. This is a dumb pointer to avoid cycles as the forward pointers will usually be PP's.build_()
method looks at all the options of itself, and for those objects that are ParentableObject
, it sets their parent
pointer to this
. (This mechanism could conceptually be put in Object
itself, but out of caution we leave it in ParentableObject
for now).In other words, this class both provides the backpointer in an object graph, and provides the mechanism to update the backpointer according to arbitrary forward pointers (as long as the forward pointers are accessible as options through an ObjectOptionsIterator
.)
We want some objects to be able to HAVE A PARENT (hence be a ParentableObject), but not to act as the parent for somebody else. They are 'adoptive', since they are not the biological parents of their children. An option can be specified upon construction of Parentable object that disables the parent-setting aspect, i.e. none of its subobjects can have this as their parent. This is useful if some subobjects are pointed to by multiple ParentableObjects, and we want to control who gets to be the legitimate parent.
Each ParentableObject can be marked with the kind of parent it wants. The following kinds are supported:
Definition at line 93 of file ParentableObject.h.
typedef Object PLearn::ParentableObject::inherited [private] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 95 of file ParentableObject.h.
Kind of parenting relationship; see class help for details.
Definition at line 99 of file ParentableObject.h.
{ AnyParent, WeakParent, UniqueParent };
PLearn::ParentableObject::ParentableObject | ( | bool | adoptive_parent = false , |
ParentKind | pk = WeakParent |
||
) |
Default constructor.
Definition at line 85 of file ParentableObject.cc.
: m_parent(0), m_adoptive_parent(adoptive_parent), m_parent_kind(pk) { }
string PLearn::ParentableObject::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 83 of file ParentableObject.cc.
OptionList & PLearn::ParentableObject::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 83 of file ParentableObject.cc.
RemoteMethodMap & PLearn::ParentableObject::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 83 of file ParentableObject.cc.
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 83 of file ParentableObject.cc.
StaticInitializer ParentableObject::_static_initializer_ & PLearn::ParentableObject::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 83 of file ParentableObject.cc.
void PLearn::ParentableObject::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 90 of file ParentableObject.cc.
References PLearn::Object::build(), and build_().
Referenced by PLearn::TypedParentableObject< ParentT >::build(), and PLearn::TransparentParentable::build().
{ inherited::build(); build_(); }
void PLearn::ParentableObject::build_ | ( | ) | [private] |
Simply call updateChildrensParent with this.
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 125 of file ParentableObject.cc.
References updateChildrensParent().
Referenced by build().
{ updateChildrensParent(this); }
void PLearn::ParentableObject::checkParent | ( | ) | const [virtual] |
After the m_parent field of a child has been set, this function is called so that the child has a chance to check the parent (e.g.
dynamic type checking). By default it just asserts that there is a parent.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 203 of file ParentableObject.cc.
References m_parent, and PLASSERT.
Referenced by declareMethods(), and updateChildrensParent().
void PLearn::ParentableObject::declareMethods | ( | RemoteMethodMap & | rmm | ) | [static] |
Declare the methods that are remote-callable.
Reimplemented from PLearn::Object.
Definition at line 103 of file ParentableObject.cc.
References PLearn::Object::_getRemoteMethodMap_(), checkParent(), PLearn::declareMethod(), PLearn::RemoteMethodMap::inherited(), parent(), and setParent().
{ // Insert a backpointer to remote methods; note that this // different than for declareOptions() rmm.inherited(inherited::_getRemoteMethodMap_()); declareMethod( rmm, "getParent", (const Object* (ParentableObject::*)() const)&ParentableObject::parent, (BodyDoc("Return the parent object"))); declareMethod( rmm, "setParent", &ParentableObject::setParent, (BodyDoc("Setter for the parent object"), ArgDoc ("parent", "Pointer to the new parent of this object"))); declareMethod( rmm, "checkParent", &ParentableObject::checkParent, (BodyDoc("After the m_parent field of a child has been set, this function is\n" "called so that the child has a chance to check the parent (e.g. dynamic\n" "type checking). By default it just asserts that there is a parent."))); }
static const PPath& PLearn::ParentableObject::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 133 of file ParentableObject.h.
:
ParentableObject * PLearn::ParentableObject::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 83 of file ParentableObject.cc.
void PLearn::ParentableObject::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TransparentParentable.
Definition at line 96 of file ParentableObject.cc.
References PLearn::deepCopyField(), m_parent, and PLearn::Object::makeDeepCopyFromShallowCopy().
Referenced by PLearn::TransparentParentable::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(m_parent, copies); }
Object* PLearn::ParentableObject::parent | ( | ) | [inline] |
Accessor for parent object.
Reimplemented in PLearn::TypedParentableObject< ParentT >.
Definition at line 112 of file ParentableObject.h.
References m_parent.
Referenced by declareMethods(), setParent(), and PLearn::TransparentParentable::updateChildrensParent().
{ return m_parent; }
const Object* PLearn::ParentableObject::parent | ( | ) | const [inline] |
Reimplemented in PLearn::TypedParentableObject< ParentT >.
Definition at line 113 of file ParentableObject.h.
References m_parent.
{ return m_parent; }
void PLearn::ParentableObject::setParent | ( | Object * | parent | ) | [virtual] |
Setter for the parent object; virtual since "transparent parentables" might wish to forward it to children.
Reimplemented in PLearn::TransparentParentable.
Definition at line 150 of file ParentableObject.cc.
References AnyParent, PLearn::Object::classname(), PLearn::endl(), PLearn::left(), m_parent, m_parent_kind, parent(), PLERROR, UniqueParent, and WeakParent.
Referenced by declareMethods(), PLearn::TransparentParentable::setParent(), and updateChildrensParent().
{ if (m_parent == parent) return; // Behave appropriately according to the type of parent we want switch (m_parent_kind) { case AnyParent: break; case WeakParent: if (m_parent) { MODULE_LOG << "Object at " << ((void*)this) << " (" << left(classname(),30) << ") " << " ignoring candidate weak parent " << ((void*)parent) << " (" << (parent? parent->classname() : string("NULL")) << ") " << endl; return; } break; case UniqueParent: if (m_parent) // Because of test above, m_parent != parent PLERROR("ParentableObject::setParent: for object at 0x%p (%s),\n" "trying to override existing parent at 0x%p (%s) with\n" "new parent at 0x%p (%s) -- parenting mode set to UniqueParent.", (void*)this, classname().c_str(), (void*)m_parent, m_parent->classname().c_str(), (void*)parent, (parent? parent->classname().c_str() : "NULL")); break; } // Do some logging if (m_parent) { MODULE_LOG << "Object at " << ((void*)this) << " (" << left(classname(),30) << ") " << " getting parent " << ((void*)parent) << " (" << (parent? parent->classname() : string("NULL")) << ") " << " overriding previous " << ((void*)m_parent) << " (" << m_parent->classname() << ") " << endl; } else { MODULE_LOG << "Object at " << ((void*)this) << " (" << left(classname(),30) << ") " << " getting parent " << ((void*)parent) << " (" << (parent? parent->classname() : string("NULL")) << ") " << endl; } m_parent = parent; }
void PLearn::ParentableObject::updateChildrensParent | ( | Object * | parent | ) | [virtual] |
Function that actually does the heavy work of traversing the descendants and setting _their_ backpointers to point to the specified parent.
Reimplemented in PLearn::TransparentParentable.
Definition at line 130 of file ParentableObject.cc.
References checkParent(), m_adoptive_parent, PLearn::OptionBase::nonparentable, and setParent().
Referenced by build_(), and PLearn::TransparentParentable::updateChildrensParent().
{ if (! m_adoptive_parent) { // Set the backpointers of sub-objects under the current object to // this. Skip nonparentable options. for (ObjectOptionsIterator it(this), end ; it != end ; ++it) { if (it.getCurrentOptionFlags() & OptionBase::nonparentable) continue; if (const ParentableObject* cpo = dynamic_cast<const ParentableObject*>(*it)) { ParentableObject* po = const_cast<ParentableObject*>(cpo); po->setParent(parent); if (parent) po->checkParent(); } } } }
Reimplemented from PLearn::Object.
Reimplemented in PLearn::TypedParentableObject< ParentT >, and PLearn::TransparentParentable.
Definition at line 133 of file ParentableObject.h.
bool PLearn::ParentableObject::m_adoptive_parent [protected] |
If true, don't set the subobjects' parent to this.
Definition at line 149 of file ParentableObject.h.
Referenced by updateChildrensParent().
Object* PLearn::ParentableObject::m_parent [protected] |
Backpointer to parent.
Definition at line 146 of file ParentableObject.h.
Referenced by PLearn::TypedParentableObject< ParentT >::build_(), PLearn::TypedParentableObject< ParentT >::checkParent(), checkParent(), makeDeepCopyFromShallowCopy(), PLearn::TypedParentableObject< ParentT >::parent(), parent(), and setParent().
ParentKind PLearn::ParentableObject::m_parent_kind [protected] |