CXXR (C++ R) API
Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends
CXXR::Closure Class Reference

Class representing a functional programming closure. More...

#include <Closure.h>

Inheritance diagram for CXXR::Closure:
Inheritance graph
[legend]
Collaboration diagram for CXXR::Closure:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Closure (const PairList *formal_args, RObject *body, Environment *env=Environment::global())
 Closure (const Closure &pattern)
 Copy constructor.
const RObjectbody () const
 Access the body of the Closure.
bool debugging () const
 Is debugging enabled?
Environmentenvironment () const
 Access the environment of the Closure.
RObjectexecute (Environment *env) const
 Evaluate the Closure's body in a given environment.
RObjectinvoke (Environment *env, const ArgList *arglist, const Expression *call, const Frame *method_bindings=0) const
 Invoke the function.
const ArgMatchermatcher () const
 Access the ArgMatcher of this Closure.
void setDebugging (bool on)
 Set debugging status.
void setEnvironment (Environment *new_env)
 Replace the environment of the closure.
void stripFormals (Frame *input_frame) const
 Strip formal argument bindings from a Frame.
RObjectapply (ArgList *arglist, Environment *env, const Expression *call) const
 Apply the function.
Closureclone () const
 Return pointer to a copy of this object.
const char * typeName () const
 Name within R of this type of object.
void visitReferents (const_visitor *v) const
 Conduct a visitor to the nodes referred to by this one.
- Public Member Functions inherited from CXXR::FunctionBase
void maybeTrace (const Expression *call) const
 Produce a tracing report if appropriate.
void setTracing (bool on)
 Set tracing status.
bool traced () const
 Is this function being traced?
- Public Member Functions inherited from CXXR::RObject
virtual const PairListattributes () const
 Get object attributes.
virtual void clearAttributes ()
 Remove all attributes.
void copyAttribute (const Symbol *name, const RObject *source)
 Copy an attribute from one RObject to another.
void copyAttributes (const RObject *source, bool copyS4)
 Copy attributes from one RObject to another.
virtual RObjectevaluate (Environment *env)
 Evaluate object in a specified Environment.
virtual RObjectgetAttribute (const Symbol *name) const
 Get the value a particular attribute.
virtual bool hasAttributes () const
 Has this object any attributes?
bool hasClass () const
 Has this object the class attribute?
bool isS4Object () const
 Is this an S4 object?
void maybeTraceMemory (const RObject *src)
 Carry out memory tracing.
void maybeTraceMemory (const RObject *src1, const RObject *src2)
 Carry out memory tracing.
void maybeTraceMemory (const RObject *src1, const RObject *src2, const RObject *src3)
 Carry out memory tracing.
bool memoryTraced () const
 Is copying etc. of this object being traced?
virtual unsigned int packGPBits () const
 Reproduce the gp bits field used in CR.
virtual void setAttribute (const Symbol *name, RObject *value)
 Set or remove an attribute.
void setAttributes (const PairList *new_attributes)
 Replace the attributes of an object.
void setMemoryTracing (bool on)
 Enable/disable tracing of copying etc.
void setS4Object (bool on)
 Set the status of this RObject as an S4 object.
SEXPTYPE sexptype () const
 Get an object's SEXPTYPE.
virtual void unpackGPBits (unsigned int gpbits)
 Interpret the gp bits field used in CR.
- Public Member Functions inherited from CXXR::GCNode
void expose () const
 Record that construction of a node is complete.
bool isExposed () const
 Has this node been exposed to garbage collection?

Static Public Member Functions

static const char * staticTypeName ()
 The name by which this type is known in R.
- Static Public Member Functions inherited from CXXR::FunctionBase
static void enableTracing (bool on)
 Enable/disable function tracing.
static bool isA (const RObject *obj)
 Is an RObject a FunctionBase?
static bool tracingEnabled ()
 If function tracing currently enabled?
- Static Public Member Functions inherited from CXXR::RObject
template<class T >
static T * clone (const T *pattern)
 Return a pointer to a copy of an object.
- Static Public Member Functions inherited from CXXR::GCNode
static void * operator new (size_t bytes)
 Allocate memory.
static void * operator new (size_t, void *where)
 Placement new for GCNode.
static void operator delete (void *p, size_t bytes)
 Deallocate memory.
static bool check ()
 Integrity check.
template<class T >
static T * expose (T *node)
 Record that construction of a node is complete.
static void gc ()
 Initiate a garbage collection.
static void gclite ()
 Lightweight garbage collection.
static void maybeCheckExposed (const GCNode *node)
 Subject to configuration, check that a GCNode is exposed.
static size_t numNodes ()
 Number of GCNode objects in existence.

Protected Member Functions

void detachReferents ()
 Null out all references from this node to other nodes.
- Protected Member Functions inherited from CXXR::FunctionBase
 FunctionBase (SEXPTYPE stype)
 FunctionBase (const FunctionBase &pattern)
 Copy constructor.
- Protected Member Functions inherited from CXXR::RObject
 RObject (SEXPTYPE stype=CXXSXP)
 RObject (const RObject &pattern)
 Copy constructor.
- Protected Member Functions inherited from CXXR::GCNode
virtual ~GCNode ()

Friends

class boost::serialization::access

Detailed Description

Class representing a functional programming closure.

A closure associates a function definition (the body) with a list of formal arguments and an environment. In evaluating the function, non-local variables within the function definition are interpreted by reference to the specified environment (and its enclosing environments).


Constructor & Destructor Documentation

CXXR::Closure::Closure ( const PairList formal_args,
RObject body,
Environment env = Environment::global() 
)
Parameters:
formal_argsList of formal arguments.
bodyPointer to the body of the Closure. This must be either a null pointer or a pointer to an object of one of the following types: PairList, Expression, Symbol, ExpressionVector, ListVector or ByteCode (checked).
envpointer to the environment in which the Closure is to be evaluated.
CXXR::Closure::Closure ( const Closure pattern)
inline

Copy constructor.

Parameters:
patternClosure to be copied.

Member Function Documentation

RObject* CXXR::Closure::apply ( ArgList arglist,
Environment env,
const Expression call 
) const
virtual

Apply the function.

Parameters:
arglistNon-null pointer to the ArgList containing the list of arguments with which the function is to be invoked.
envPointer to the Environment in which the function is to be evaluated.
callPointer to the Expression calling the function.
Returns:
The result of applying the function.

Implements CXXR::FunctionBase.

const RObject* CXXR::Closure::body ( ) const
inline

Access the body of the Closure.

Returns:
Pointer to the body of the Closure.
Closure* CXXR::Closure::clone ( ) const
virtual

Return pointer to a copy of this object.

This function creates a copy of this object, and returns a pointer to that copy.

Generally this function (and the copy constructors it utilises) will attempt to create a 'deep' copy of the object; this follows standard practice within C++, and it is intended to extend this practice as CXXR development continues.

However, if the pattern object contains unclonable subobjects, then the created copy will at the relevant places simply contain pointers to those subobjects, i.e. to that extent the copy is 'shallow'. This is managed using the smart pointers defined by nested class RObject::Handle.

Returns:
a pointer to a clone of this object, or a null pointer if this object cannot be cloned.
Note:
Derived classes should exploit the covariant return type facility to return a pointer to the type of object being cloned.

Reimplemented from CXXR::RObject.

bool CXXR::Closure::debugging ( ) const
inline

Is debugging enabled?

Returns:
true iff debugging is currently enabled for this Closure.
void CXXR::Closure::detachReferents ( )
protectedvirtual

Null out all references from this node to other nodes.

The referents of this node are those objects (derived from GCNode) designated by a GCEdge within this object. This function changes all GCEdges within this object to encapsulate a null pointer. It is used during the sweep phase of a mark-sweep garbage collection to break up unreachable subgraphs, and in particular to remove reference loops from them. After the application of this method, the GCNode should be regarded as a 'zombie', kept in existence only so other nodes can detach their references to it cleanly (using decRefCount()).

Note:
If this method is reimplemented in a derived class, the reimplemented version must remember to invoke detachReferents() for the immediate base class of the derived class, to ensure that all referents of the object get detached.

Reimplemented from CXXR::RObject.

Environment* CXXR::Closure::environment ( ) const
inline

Access the environment of the Closure.

Returns:
Pointer to the environment of the Closure.
RObject* CXXR::Closure::execute ( Environment env) const

Evaluate the Closure's body in a given environment.

This function evaluates the Closure's body with env as the working environment, handling debugging if currently enabled for this Closure.

Parameters:
envNon-null pointer to the working environment in which evaluation of the body is to be carried out.
Returns:
the result of evaluation.
Note:
This is a low-level function that is called by invoke() but may also be called directly. The function does not carry out argument matching, nor does it create a ClosureContext: both these operations must be handled by the calling code.
RObject* CXXR::Closure::invoke ( Environment env,
const ArgList arglist,
const Expression call,
const Frame method_bindings = 0 
) const

Invoke the function.

This differs from apply() in that it is assumed that any required wrapping of the function arguments in Promise objects will have been carried out before invoke() is called, whereas apply() carries out this wrapping itself.

Parameters:
envNon-null pointer to the Environment in which the function is to be evaluated.
arglistNon-null pointer to the promise-wrapped ArgList containing the arguments with which the function is to be invoked.
callPointer to the Expression calling the function.
method_bindingsThis pointer will be non-null if and only if this invocation represents a method call, in which case it points to a Frame containing Bindings that should be added to the working environment, for example bindings of the Symbols .Generic and .Class.
Returns:
The result of applying the function.
const ArgMatcher* CXXR::Closure::matcher ( ) const
inline

Access the ArgMatcher of this Closure.

Returns:
const pointer to this Closure's ArgMatcher object.
void CXXR::Closure::setDebugging ( bool  on)
inline

Set debugging status.

Parameters:
onThe required new debugging status (true = enabled).
void CXXR::Closure::setEnvironment ( Environment new_env)
inline

Replace the environment of the closure.

Parameters:
new_envPointer to the environment now to be considered as the environment of this Closure. A null pointer is not permissible (not checked).
static const char* CXXR::Closure::staticTypeName ( )
inlinestatic

The name by which this type is known in R.

Returns:
The name by which this type is known in R.

Reimplemented from CXXR::FunctionBase.

void CXXR::Closure::stripFormals ( Frame input_frame) const
inline

Strip formal argument bindings from a Frame.

This function removes from input_frame any bindings of the formal arguments of this Closure. It is used in creating the working environment of an S3 method from the working environment of its generic.

Parameters:
input_frameNon-null pointer to the Frame from which bindings are to be stripped.
const char* CXXR::Closure::typeName ( ) const
virtual

Name within R of this type of object.

Returns:
the name by which this type of object is known within R.

Reimplemented from CXXR::RObject.

void CXXR::Closure::visitReferents ( const_visitor v) const
virtual

Conduct a visitor to the nodes referred to by this one.

The referents of this node are those objects (derived from GCNode) designated by a GCEdge within this object.

Parameters:
vPointer to the visitor object.
Note:
If this method is reimplemented in a derived class, the reimplemented version must remember to invoke visitReferents() for the immediate base class of the derived class, to ensure that all referents of the object get visited. It is suggested that implementations set up stack-based pointers to all the referents of a node before visiting any of them; in that case, if the (recursive) visiting pushes the node out of the processor cache, there is no need to fetch it back in.

Reimplemented from CXXR::RObject.


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