regina::NScript Class Reference
Basic Packet Types

A packet representing a Python script that can be run. More...

`#include <packet/nscript.h>`

Inheritance diagram for regina::NScript:

## Public Member Functions

| NScript ()
| Initialises to a script with no lines and no variables. More...
unsigned long | getNumberOfLines () const
| Returns the number of lines in this script. More...
const std::string & | getLine (unsigned long index) const
| Returns the requested line of this script. More...
void | addFirst (const std::string &line)
| Adds the given line to the beginning of this script. More...
void | addLast (const std::string &line)
| Adds the given line to the end of this script. More...
void | insertAtPosition (const std::string &line, unsigned long index)
| Inserts the given line into the given position in this script. More...
void | replaceAtPosition (const std::string &line, unsigned long index)
| Replaces a line of this script with the given line. More...
void | removeLineAt (unsigned long index)
| Removes the requested line from this script. More...
void | removeAllLines ()
| Removes all lines from this script. More...
unsigned long | getNumberOfVariables () const
| Returns the number of variables associated with this script. More...
const std::string & | getVariableName (unsigned long index) const
| Returns the name of the requested variable associated with this script. More...
NPacket * | getVariableValue (unsigned long index) const
| Returns the value of the requested variable associated with this script. More...
NPacket * | getVariableValue (const std::string &name) const
| Returns the value of the variable stored with the given name. More...
bool | addVariable (const std::string &name, NPacket *value)
| Adds a new variable to be associated with this script. More...
void | removeVariable (const std::string &name)
| Removes the variable stored with the given name. More...
void | removeAllVariables ()
| Removes all variables associated with this script. More...
virtual void | writeTextShort (std::ostream &out) const
| Writes this object in short text format to the given output stream. More...
virtual void | writeTextLong (std::ostream &out) const
| Writes this object in long text format to the given output stream. More...
virtual bool | dependsOnParent () const
| Determines if this packet depends upon its parent. More...
virtual void | packetToBeDestroyed (NPacket *packet)
| Called before the packet is about to be destroyed. More...
Public Member Functions inherited from regina::NPacket
| NPacket (NPacket *parent=0)
| Constructor that inserts the new packet into the overall tree structure. More...
virtual | ~NPacket ()
| Destructor that also orphans this packet and destroys all of its descendants. More...
virtual PacketType | getPacketType () const =0
| Returns the unique integer ID representing this type of packet. More...
virtual std::string | getPacketTypeName () const =0
| Returns an English name for this type of packet. More...
const std::string & | getPacketLabel () const
| Returns the label associated with this individual packet. More...
std::string | getHumanLabel () const
| Returns the label associated with this individual packet, adjusted if necessary for human-readable output. More...
void | setPacketLabel (const std::string &newLabel)
| Sets the label associated with this individual packet. More...
std::string | getFullName () const
| Returns a descriptive text string for the packet. More...
std::string | makeUniqueLabel (const std::string &base) const
| Returns a new label that cannot be found anywhere in the entire tree structure. More...
bool | makeUniqueLabels (NPacket *reference)
| Ensures that all packet labels in both this and the given packet tree combined are distinct. More...
bool | hasTag (const std::string &tag) const
| Determines whether this packet has the given associated tag. More...
bool | hasTags () const
| Determines whether this packet has any associated tags at all. More...
bool | addTag (const std::string &tag)
| Associates the given tag with this packet. More...
bool | removeTag (const std::string &tag)
| Removes the association of the given tag with this packet. More...
void | removeAllTags ()
| Removes all associated tags from this packet. More...
const std::set< std::string > & | getTags () const
| Returns the set of all tags associated with this packet. More...
bool | listen (NPacketListener *listener)
| Registers the given packet listener to listen for events on this packet. More...
bool | isListening (NPacketListener *listener)
| Determines whether the given packet listener is currently listening for events on this packet. More...
bool | unlisten (NPacketListener *listener)
| Unregisters the given packet listener so that it no longer listens for events on this packet. More...
NPacket * | getTreeParent () const
| Determines the parent packet in the tree structure. More...
NPacket * | getFirstTreeChild () const
| Determines the first child of this packet in the tree structure. More...
NPacket * | getLastTreeChild () const
| Determines the last child of this packet in the tree structure. More...
NPacket * | getNextTreeSibling () const
| Determines the next sibling of this packet in the tree structure. More...
NPacket * | getPrevTreeSibling () const
| Determines the previous sibling of this packet in the tree structure. More...
NPacket * | getTreeMatriarch () const
| Determines the matriarch (the root) of the tree to which this packet belongs. More...
unsigned | levelsDownTo (const NPacket *descendant) const
| Counts the number of levels between this packet and its given descendant in the tree structure. More...
unsigned | levelsUpTo (const NPacket *ancestor) const
| Counts the number of levels between this packet and its given ancestor in the tree structure. More...
bool | isGrandparentOf (const NPacket *descendant) const
| Determines if this packet is equal to or an ancestor of the given packet in the tree structure. More...
unsigned long | getNumberOfChildren () const
| Returns the number of immediate children of this packet. More...
unsigned long | getNumberOfDescendants () const
| Returns the total number of descendants of this packet. More...
unsigned long | getTotalTreeSize () const
| Determines the total number of packets in the tree or subtree for which this packet is matriarch. More...
void | insertChildFirst (NPacket *child)
| Inserts the given packet as the first child of this packet. More...
void | insertChildLast (NPacket *child)
| Inserts the given packet as the last child of this packet. More...
void | insertChildAfter (NPacket *newChild, NPacket *prevChild)
| Inserts the given packet as a child of this packet at the given location in this packet's child list. More...
void | makeOrphan ()
| Cuts this packet away from its parent in the tree structure and instead makes it matriarch of its own tree. More...
void | reparent (NPacket *newParent, bool first=false)
| Cuts this packet away from its parent in the tree structure, and inserts it as a child of the given packet instead. More...
void | swapWithNextSibling ()
| Swaps this packet with its next sibling in the sequence of children beneath their common parent packet. More...
void | moveUp (unsigned steps=1)
| Moves this packet the given number of steps towards the beginning of its sibling list. More...
void | moveDown (unsigned steps=1)
| Moves this packet the given number of steps towards the end of its sibling list. More...
void | moveToFirst ()
| Moves this packet to be the first in its sibling list. More...
void | moveToLast ()
| Moves this packet to be the last in its sibling list. More...
void | sortChildren ()
| Sorts the immediate children of this packet according to their packet labels. More...
NPacket * | nextTreePacket ()
| Finds the next packet after this in a complete depth-first iteration of the entire tree structure to which this packet belongs. More...
const NPacket * | nextTreePacket () const
| Finds the next packet after this in a complete depth-first iteration of the entire tree structure to which this packet belongs. More...
NPacket * | firstTreePacket (const std::string &type)
| Finds the first packet of the requested type in a complete depth-first iteration of the tree structure. More...
const NPacket * | firstTreePacket (const std::string &type) const
| Finds the first packet of the requested type in a complete depth-first iteration of the tree structure. More...
NPacket * | nextTreePacket (const std::string &type)
| Finds the next packet after this of the requested type in a complete depth-first iteration of the entire tree structure. More...
const NPacket * | nextTreePacket (const std::string &type) const
| Finds the next packet after this of the requested type in a complete depth-first iteration of the entire tree structure. More...
NPacket * | findPacketLabel (const std::string &label)
| Finds the packet with the requested label in the tree or subtree for which this packet is matriarch. More...
const NPacket * | findPacketLabel (const std::string &label) const
| Finds the packet with the requested label in the tree or subtree for which this packet is matriarch. More...
bool | isPacketEditable () const
| Determines whether this packet can be altered without invalidating or otherwise upsetting any of its immediate children. More...
NPacket * | clone (bool cloneDescendants=false, bool end=true) const
| Clones this packet (and possibly its descendants), assigns to it a suitable unused label and inserts the clone into the tree as a sibling of this packet. More...
void | writeXMLFile (std::ostream &out) const
| Writes a complete XML file containing the subtree with this packet as matriarch. More...
std::string | internalID () const
| Returns a unique string ID that identifies this packet. More...
Public Member Functions inherited from regina::ShareableObject
| ShareableObject ()
| Default constructor that does nothing. More...
virtual | ~ShareableObject ()
| Default destructor that does nothing. More...
std::string | str () const
| Returns the output from writeTextShort() as a string. More...
std::string | toString () const
| A deprecated alias for str(), which returns the output from writeTextShort() as a string. More...
std::string | detail () const
| Returns the output from writeTextLong() as a string. More...
std::string | toStringLong () const
| A deprecated alias for detail(), which returns the output from writeTextLong() as a string. More...
Public Member Functions inherited from regina::NPacketListener
virtual | ~NPacketListener ()
| Destroys this listener. More...
void | unregisterFromAllPackets ()
| Unregisters this listener from any packets to which it is currently listening. More...
virtual void | packetToBeChanged (NPacket *packet)
| Called before the contents of the packet are to be changed. More...
virtual void | packetWasChanged (NPacket *packet)
| Called after the contents of the packet have been changed. More...
virtual void | packetToBeRenamed (NPacket *packet)
| Called before the packet label or tags are to be changed. More...
virtual void | packetWasRenamed (NPacket *packet)
| Called after the packet label or tags have been changed. More...
virtual void | childToBeAdded (NPacket *packet, NPacket *child)
| Called before a child packet is to be inserted directly beneath the packet. More...
virtual void | childWasAdded (NPacket *packet, NPacket *child)
| Called after a child packet has been inserted directly beneath the packet. More...
virtual void | childToBeRemoved (NPacket *packet, NPacket *child, bool inParentDestructor)
| Called before a child packet is to be removed from directly beneath the packet. More...
virtual void | childWasRemoved (NPacket *packet, NPacket *child, bool inParentDestructor)
| Called after a child packet has been removed from directly beneath the packet. More...
virtual void | childrenToBeReordered (NPacket *packet)
| Called before the child packets directly beneath the packet are to be reordered. More...
virtual void | childrenWereReordered (NPacket *packet)
| Called after the child packets directly beneath the packet have been reordered. More...

## Static Public Member Functions

static NXMLPacketReader * | getXMLReader (NPacket *parent, NXMLTreeResolver &resolver)
Static Public Member Functions inherited from regina::NPacket
static NXMLPacketReader * | getXMLReader (NPacket *parent, NXMLTreeResolver &resolver)
| Returns a newly created XML element reader that will read the contents of a single XML packet element. More...

## Protected Member Functions

virtual NPacket * | internalClonePacket (NPacket *parent) const
| Makes a newly allocated copy of this packet. More...
virtual void | writeXMLPacketData (std::ostream &out) const
| Writes a chunk of XML containing the data for this packet only. More...
Protected Member Functions inherited from regina::NPacket
void | writeXMLPacketTree (std::ostream &out) const
| Writes a chunk of XML containing the subtree with this packet as matriarch. More...
Protected Member Functions inherited from regina::boost::noncopyable
| noncopyable ()
| A constructor which does nothing. More...
| ~noncopyable ()
| A destructor which does nothing. More...

## Additional Inherited Members

Public Types inherited from regina::NPacket
typedef ChangeEventSpan | ChangeEventBlock
| A deprecated typedef for ChangeEventSpan. More...

## Detailed Description

A packet representing a Python script that can be run.

Accessor methods for a script work a line at a time.

As of Regina 4.95, variables are now stored as pointers to packets, not packet labels. This affects how variables react to changes in the packets that they point to. In particular, if a variable V points to some packet P, then as of Regina 4.95:

- if P is renamed then V will still point to it and the script will not notify listeners of any changes (though of course P will still notify its own listeners);
- if P is deleted then V will take the value `None`, and the script will notify listeners of the change.

## Constructor & Destructor Documentation

regina::NScript::NScript | ( |  | ) |

inline

Initialises to a script with no lines and no variables.

## Member Function Documentation

void regina::NScript::addFirst | ( | const std::string & | line | ) |

inline

Adds the given line to the beginning of this script.

Parameters

  line | the line to insert.

void regina::NScript::addLast | ( | const std::string & | line | ) |

inline

Adds the given line to the end of this script.

Parameters

  line | the line to add.

bool regina::NScript::addVariable | ( | const std::string & | name,
|  | NPacket * | value
| ) |  |

inline

Adds a new variable to be associated with this script.

If a variable with the given name is already stored, this routine will do nothing.

Parameters

  name | the name of the new variable.
  value | the value of the new variable; this is allowed to be `null`.

Returns
  `true` if the variable was successfully added, or `false` if a variable with the given name was already stored.

bool regina::NScript::dependsOnParent | ( |  | ) | const

inline virtual

Determines if this packet depends upon its parent.

This is true if the parent cannot be altered without invalidating or otherwise upsetting this packet.

Returns
  `true` if and only if this packet depends on its parent.

Implements regina::NPacket.

const std::string & regina::NScript::getLine | ( | unsigned long | index | ) | const

inline

Returns the requested line of this script.

Parameters

  index | the index of the requested line; this must be between 0 and getNumberOfLines()-1 inclusive.

Returns
  the requested line.

unsigned long regina::NScript::getNumberOfLines | ( |  | ) | const

inline

Returns the number of lines in this script.

Returns
  the number of lines.

unsigned long regina::NScript::getNumberOfVariables | ( |  | ) | const

inline

Returns the number of variables associated with this script.

Returns
  the number of variables.

const std::string& regina::NScript::getVariableName | ( | unsigned long | index | ) | const

Returns the name of the requested variable associated with this script.

Parameters

  index | the index of the requested variable; this must be between 0 and getNumberOfVariables()-1 inclusive.

Returns
  the name of the requested variable.

NPacket* regina::NScript::getVariableValue | ( | unsigned long | index | ) | const

Returns the value of the requested variable associated with this script.

Variables may take the value `null`.

Parameters

  index | the index of the requested variable; this must be between 0 and getNumberOfVariables()-1 inclusive.

Returns
  the value of the requested variable.

NPacket* regina::NScript::getVariableValue | ( | const std::string & | name | ) | const

Returns the value of the variable stored with the given name.

Variables may take the value `null`.

If no variable is stored with the given name, then `null` will likewise be returned.

Parameters

  name | the name of the requested variable; note that names are case sensitive.

Returns
  the value of the requested variable.

void regina::NScript::insertAtPosition | ( | const std::string & | line,
|  | unsigned long | index
| ) |  |

inline

Inserts the given line into the given position in this script.

All subsequent lines will be shifted down to make room.

Parameters

  line | the line to insert.
  index | the index at which the new line will be placed; this must be between 0 and getNumberOfLines() inclusive.

virtual NPacket* regina::NScript::internalClonePacket | ( | NPacket * | parent | ) | const

protected virtual

Makes a newly allocated copy of this packet.

This routine should not insert the new packet into the tree structure, clone the packet's associated tags or give the packet a label. It should also not clone any descendants of this packet.

You may assume that the new packet will eventually be inserted into the tree beneath either the same parent as this packet or a clone of that parent.

Parameters

  parent | the parent beneath which the new packet will eventually be inserted.

Returns
  the newly allocated packet.

Implements regina::NPacket.

virtual void regina::NScript::packetToBeDestroyed | ( | NPacket * | packet | ) |

virtual

Called before the packet is about to be destroyed.

Note that there is no matching function called after the packet is destroyed, since the set of listeners will no longer be available at that stage.

When an entire packet subtree is to be destroyed, child packets will notify their listeners of the impending destruction before parent packets will.

Note that the packet will forcibly unregister this listener immediately before packetToBeDestroyed() is called, to avoid any unpleasant consequences if this listener should also try to unregister itself. This means that, by the time this routine is called, this listener will no longer be registered with the packet in question (and any attempt to unregister it again will be harmless).

The default implementation of this routine is to do nothing.

Parameters

  packet | the packet being listened to.

Reimplemented from regina::NPacketListener.

void regina::NScript::removeAllLines | ( |  | ) |

inline

Removes all lines from this script.

void regina::NScript::removeAllVariables | ( |  | ) |

inline

Removes all variables associated with this script.

void regina::NScript::removeLineAt | ( | unsigned long | index | ) |

inline

Removes the requested line from this script.

Parameters

  index | the index of the requested line; this must be between 0 and getNumberOfLines()-1 inclusive.

void regina::NScript::removeVariable | ( | const std::string & | name | ) |

Removes the variable stored with the given name.

Note that the indices of other variables may change as a result of this action.

If no variable is stored with the given name, this routine will do nothing (but waste time!).

Parameters

  name | the name of the variable to remove; note that names are case sensitive.

void regina::NScript::replaceAtPosition | ( | const std::string & | line,
|  | unsigned long | index
| ) |  |

inline

Replaces a line of this script with the given line.

Parameters

  line | the line to replace the currently stored line.
  index | the index of the line to replace; this must be between 0 and getNumberOfLines()-1 inclusive.

virtual void regina::NScript::writeTextLong | ( | std::ostream & | out | ) | const

virtual

Writes this object in long text format to the given output stream.

The output should provide the user with all the information they could want. The output should be human-readable, should not contain extremely long lines (so users can read the output in a terminal), and should end with a final newline.

The default implementation of this routine merely calls writeTextShort() and adds a newline.

Python:
  The parameter out does not exist; standard output will be used.

Parameters

  out | the output stream to which to write.

Reimplemented from regina::ShareableObject.

void regina::NScript::writeTextShort | ( | std::ostream & | out | ) | const

inline virtual

Writes this object in short text format to the given output stream.

The output should be human-readable, should fit on a single line, and should not end with a newline.

Python:
  The parameter out does not exist; standard output will be used.

Parameters

  out | the output stream to which to write.

Implements regina::ShareableObject.

virtual void regina::NScript::writeXMLPacketData | ( | std::ostream & | out | ) | const

protected virtual

Writes a chunk of XML containing the data for this packet only.

You may assume that the packet opening tag (including the packet type and label) has already been written, and that all child packets followed by the corresponding packet closing tag will be written immediately after this routine is called. This routine need only write the internal data stored in this specific packet.

Parameters

  out | the output stream to which the XML should be written.

Implements regina::NPacket.

The documentation for this class was generated from the following file:

- packet/nscript.h

Copyright © 1999-2013, The Regina development team
This software is released under the GNU General Public License, with some additional permissions; see the source code for details.
For further information, or to submit a bug or other problem, please contact Ben Burton (bab@debian.org).