casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
casa::UDFBase Class Reference

Abstract base class for a user-defined TaQL function. More...

#include <UDFBase.h>

List of all members.

Public Types

typedef UDFBaseMakeUDFObject (const String &functionName)
 The signature of a global or static member function creating an object of the UDF.

Public Member Functions

 UDFBase ()
 Only default constructor is needed.
virtual ~UDFBase ()
 Destructor.
virtual Bool getBool (const TableExprId &id)
 Evaluate the function and return the result.
virtual Int64 getInt (const TableExprId &id)
virtual Double getDouble (const TableExprId &id)
virtual DComplex getDComplex (const TableExprId &id)
virtual String getString (const TableExprId &id)
virtual TaqlRegex getRegex (const TableExprId &id)
virtual MVTime getDate (const TableExprId &id)
virtual Array< BoolgetArrayBool (const TableExprId &id)
virtual Array< Int64getArrayInt (const TableExprId &id)
virtual Array< DoublegetArrayDouble (const TableExprId &id)
virtual Array< DComplex > getArrayDComplex (const TableExprId &id)
virtual Array< StringgetArrayString (const TableExprId &id)
virtual Array< MVTimegetArrayDate (const TableExprId &id)
const StringgetUnit () const
 Get the unit.
void init (const PtrBlock< TableExprNodeRep * > &arg, const Table &table, const TaQLStyle &)
 Initialize the function object.
TableExprNodeRep::NodeDataType dataType () const
 Get the data type.
Int ndim () const
 Get the dimensionality of the results.
const IPositionshape () const
 Get the result shape if the same for all results.
Bool isConstant () const
 Tell if the UDF gives a constant result.

Static Public Member Functions

static void registerUDF (const String &name, MakeUDFObject *func)
 Register a the name and construction function of a UDF (thread-safe).
static UDFBasecreateUDF (const String &name)
 Create a UDF object (thread-safe).

Protected Member Functions

PtrBlock< TableExprNodeRep * > & operands ()
 Get the operands.
void setDataType (TableExprNodeRep::NodeDataType)
 Set the data type.
void setNDim (Int ndim)
 Set the dimensionality of the results.
void setShape (const IPosition &shape)
 Set the shape of the results if it is fixed and known.
void setUnit (const String &unit)
 Set the unit of the result.
void setConstant (Bool isConstant)
 Define if the result is constant (e.g.

Private Member Functions

virtual void setup (const Table &table, const TaQLStyle &)=0
 Set up the function object.

Private Attributes

PtrBlock< TableExprNodeRep * > itsOperands
TableExprNodeRep::NodeDataType itsDataType
Int itsNDim
IPosition itsShape
String itsUnit
Bool itsIsConstant

Static Private Attributes

static map< String,
MakeUDFObject * > 
theirRegistry
static Mutex theirMutex

Detailed Description

Abstract base class for a user-defined TaQL function.

Synopsis

This class makes it possible to add user-defined functions (UDF) to TaQL. A UDF has to be implemented in a class derived from this class. A few functions have to be implemented in the class as described below.

A UDF is a class derived from this base class. It must contain the following member functions. See also the example below.

makeObject a static function to create an object of the UDF class. This function needs to be registered.
setup this virtual function is called after the object has been created. It should initialize the object using the function arguments that can be obtained using the function operands(). The setup function should perform the following:
  • Define the data type of the result using setDataType<src>. The data type should be derived from the data types of the function arguments. The possible data types are defined in class TableExprNodeRep. Note that a UDF can support multiple data types. For example, a function like <src>min can be used for Int, Double, or a mix. Function 'checkDT' in class TableExprNodeMulti can be used to check the data types of the operands and determine the result data type.
  • Define the dimensionality of the result using setNDim. A value of 0 means a scalar. A value of -1 means an array with a dimensionality that can vary from row to row.
  • Optionally use setShape to define the shape if the results are arrays with a shape that is the same for all rows. It will also set ndim if setNDim was not used yet, otherwise it checks if it ndim matches.
  • Optionally set the unit of the result using setUnit. TaQL has full support of units, so UDFs should behave the same. It is possible to change the unit of the function arguments. For example:
    • a function like 'sin' can force its argument to be in radians; TaQL will scale the argument as needed. This can be done like TableExprNodeUnit::adaptUnit (operands()[i], "rad");
    • A function like 'asin' will have a result in radians. Such a UDF should set its result unit to rad.
    • A function like 'min' wants its arguments to have the same unit and will set its result unit to it. It can be done like: setUnit (TableExprFuncNode::makeEqualUnits (operands(), 0, operands().size()));
    • Optionally define if the result is a constant value using setConstant. It means that the function is not dependent on the row number in the table being queried. This is usually the case if all UDF arguments are constant.
    See class TableExprFuncNode for more info about these functions.
getXXX there are virtual get functions for each possible data type. The get functions matching the data types that can be set by the setup function need to be implemented.

A UDF has to be made known to TaQL by adding it to the UDF registry with its name and 'makeObject' function. UDFs will usually reside in a shared library that is loaded dynamically. TaQL will load a UDF in the following way:

Example

      class TestUDF: public UDFBase
      {
      public:
        TestUDF() {}
        static UDFBase* makeObject (const String&)
          { return new TestUDF(); }
        virtual void setup (const Table&, const TaQLStyle&)
        {
          AlwaysAssert (operands().size() == 1, AipsError);
          AlwaysAssert (operands()[0]->dataType() == TableExprNodeRep::NTInt,
                        AipsError);
          AlwaysAssert (operands()[0]->valueType() == TableExprNodeRep::VTScalar,
                        AipsError);
          setDataType (TableExprNodeRep::NTBool);
          setNDim (0);                                 // scalar result
          setConstant (operands()[0].isConstant());    // constant result?
             
        }
        Bool getBool (const TableExprId& id)
          { return operands()[0]->getInt(id) == 1; }
      };

This example returns True if the function argument matches 1. It can be seen that it checks if the argument is an integer scalar.

Definition at line 163 of file UDFBase.h.


Member Typedef Documentation

typedef UDFBase* casa::UDFBase::MakeUDFObject(const String &functionName)

The signature of a global or static member function creating an object of the UDF.

Definition at line 168 of file UDFBase.h.


Constructor & Destructor Documentation

Only default constructor is needed.

virtual casa::UDFBase::~UDFBase ( ) [virtual]

Destructor.


Member Function Documentation

static UDFBase* casa::UDFBase::createUDF ( const String name) [static]

Create a UDF object (thread-safe).

Get the data type.

Definition at line 243 of file UDFBase.h.

References itsDataType.

virtual Array<Bool> casa::UDFBase::getArrayBool ( const TableExprId id) [virtual]
virtual Array<MVTime> casa::UDFBase::getArrayDate ( const TableExprId id) [virtual]
virtual Array<DComplex> casa::UDFBase::getArrayDComplex ( const TableExprId id) [virtual]
virtual Array<Double> casa::UDFBase::getArrayDouble ( const TableExprId id) [virtual]
virtual Array<Int64> casa::UDFBase::getArrayInt ( const TableExprId id) [virtual]
virtual Array<String> casa::UDFBase::getArrayString ( const TableExprId id) [virtual]
virtual Bool casa::UDFBase::getBool ( const TableExprId id) [virtual]

Evaluate the function and return the result.

Their default implementations throw a "not implemented" exception.

virtual MVTime casa::UDFBase::getDate ( const TableExprId id) [virtual]
virtual DComplex casa::UDFBase::getDComplex ( const TableExprId id) [virtual]
virtual Double casa::UDFBase::getDouble ( const TableExprId id) [virtual]
virtual Int64 casa::UDFBase::getInt ( const TableExprId id) [virtual]
virtual TaqlRegex casa::UDFBase::getRegex ( const TableExprId id) [virtual]
virtual String casa::UDFBase::getString ( const TableExprId id) [virtual]
const String& casa::UDFBase::getUnit ( ) const [inline]

Get the unit.

Definition at line 195 of file UDFBase.h.

References itsUnit.

void casa::UDFBase::init ( const PtrBlock< TableExprNodeRep * > &  arg,
const Table table,
const TaQLStyle  
)

Initialize the function object.

Bool casa::UDFBase::isConstant ( ) const [inline]

Tell if the UDF gives a constant result.

Definition at line 256 of file UDFBase.h.

References itsIsConstant.

Int casa::UDFBase::ndim ( ) const [inline]

Get the dimensionality of the results.

(0=scalar, -1=array with variable ndim, >0=array with fixed ndim

Definition at line 248 of file UDFBase.h.

References itsNDim.

Get the operands.

Definition at line 205 of file UDFBase.h.

References itsOperands.

static void casa::UDFBase::registerUDF ( const String name,
MakeUDFObject func 
) [static]

Register a the name and construction function of a UDF (thread-safe).

An exception is thrown if this name already exists with a different construction function.

void casa::UDFBase::setConstant ( Bool  isConstant) [protected]

Define if the result is constant (e.g.

if all arguments are constant). If this function is not called by the setup function of the derived class, the result is not constant.

Set the data type.

This function must be called by the setup function of the derived class.

void casa::UDFBase::setNDim ( Int  ndim) [protected]

Set the dimensionality of the results.


0 means that the results are scalars.
-1 means that the results are arrays with unknown dimensionality.
>0 means that the results are arrays with that dimensionality. This function must be called by the setup function of the derived class.

void casa::UDFBase::setShape ( const IPosition shape) [protected]

Set the shape of the results if it is fixed and known.

void casa::UDFBase::setUnit ( const String unit) [protected]

Set the unit of the result.

If this function is not called by the setup function of the derived class, the result has no unit.

virtual void casa::UDFBase::setup ( const Table table,
const TaQLStyle  
) [private, pure virtual]

Set up the function object.

const IPosition& casa::UDFBase::shape ( ) const [inline]

Get the result shape if the same for all results.

Definition at line 252 of file UDFBase.h.

References itsShape.


Member Data Documentation

Definition at line 265 of file UDFBase.h.

Referenced by dataType().

Definition at line 269 of file UDFBase.h.

Referenced by isConstant().

Definition at line 266 of file UDFBase.h.

Referenced by ndim().

Definition at line 264 of file UDFBase.h.

Referenced by operands().

Definition at line 267 of file UDFBase.h.

Referenced by shape().

Definition at line 268 of file UDFBase.h.

Referenced by getUnit().

Mutex casa::UDFBase::theirMutex [static, private]

Definition at line 271 of file UDFBase.h.

Definition at line 270 of file UDFBase.h.


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