Developer's Guide: The class HierarchyNode

The class Bounded | The Document and Graphics Classes | The class Protocols

## The class `HierarchyNode`

In Sketch, a document consists of a hierarchy of objects. Each object has at most one parent and belongs to at most one document. That is, an object may not belong to any compound object or document, but if it belongs to one, it cannot be part of two compound objects or two documents.

An object in this hierarchy knows which parent and document it belongs to.

The class `HierarchyNode` provides the standard interface for managing this hierarchy and the references to the parent and the document for child objects. The default behaviuor for parents is defined by the class `Compound`.

No object derived from this class should override the methods defined here except as documented.

### Instance Variables

`HierarchyNode` uses the following instance variables:

`parent`

  The parent of the object. Since the parent also has a reference to its child this introduces a circular reference. Therefore we must make sure that these references are deleted when the document is destroyed. The `Destroy` method takes care of this.

  If the object is not currently the child of a compound object this variable should be `None`.

`document`

  The document the object belongs to. If the object has just been created and is not yet inserted into the document, or when the object is stored in the application's clipboard, this variable is `None`.

In general, if `parent` is not `None`, `document` should be the same as `parent.document`.

### Methods

Methods defined in `HierarchyNode` (imcomplete):

`__init__`

  A `HierarchyNode`'s `__init__` method (and that of all derived classes) must accept an optional keyword argument called `duplicate` with the default value `None` and it must be callable with just that argument. This is used by the standard `Duplicate` method.

`Destroy()`

  Called by the parent when the document is destroyed. This method should delete any circular references. A Compound object should call the `Destroy` method of each of its children.

  The default implementation just sets `parent` to `None`. Derived classes should extend this method if necessary.

`SetParent(parent)`

  Set self's instance variable `parent` to parent.

`SetDocument(document)`

  Set self's instance variable `document` to document.

`Duplicate()`

  Return a duplicate of self. The default implementation just returns `self.__class__(duplicate = self)`. Therefore, the class's constructor must be callable with a single keyword argument `duplicate`.

The class Bounded | The Document and Graphics Classes | The class Protocols