casacore
Loading...
Searching...
No Matches
LELInterface.h
Go to the documentation of this file.
1//# LELInterface.h: Abstract base class for lattice expressions
2//# Copyright (C) 1997,1998,1999,2000,2003
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef LATTICES_LELINTERFACE_H
27#define LATTICES_LELINTERFACE_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/lattices/LEL/LELAttribute.h>
33#include <casacore/casa/Arrays/IPosition.h>
34#include <casacore/casa/Utilities/DataType.h>
35#include <casacore/casa/IO/FileLocker.h>
36#include <memory>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40//# Forward Declarations
41template <class T> class LELScalar;
42template <class T> class LELArray;
43template <class T> class LELArrayRef;
44class Slicer;
45
46
47// <summary> This base class provides the interface for Lattice expressions </summary>
48
49// <use visibility=local>
50
51// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
52// </reviewed>
53
54// <prerequisite>
55// <li> <linkto class="Lattice"> Lattice</linkto>
56// <li> <linkto class="LatticeExpr"> LatticeExpr</linkto>
57// <li> <linkto class="LatticeExprNode"> LatticeExprNode</linkto>
58// </prerequisite>
59
60// <etymology>
61// The name means "Lattice Expression Language Interface".
62// This class provides the declaration for the interface for classes
63// that are to provide Lattice expression computational functionality
64// </etymology>
65
66// <synopsis>
67// This class is part of the Letter/envelope scheme which enables
68// the C++ programmer to write mathematical expressions involving
69// Lattices. The envelope class LatticeExpr invokes the bridge
70// class LatticeExprNode. LatticeExprNode activates the letter
71// classes which provide the real functionality.
72//
73// A description of the implementation details of these classes can
74// be found in
75// <a href="../notes/216.html">Note 216</a>
76//
77// This class, LELInterface, is the abstract base class for all of
78// the letter classes. Its purpose is to declare the interface inherited
79// by all of its derived classes which are used polymorphically. The derived
80// classes offer the functionality to create and evaluate the expression
81// tree that results from the compiler parsing the expression.
82// For example, these derived classes are activated by LatticeExprNode to
83// handle operations like reading pixels from a Lattice, applying binary
84// operations to Lattices, applying mathematical functions to Lattices
85// and so on.
86//
87// The heart of the interface is in the functions <src>eval</src> and
88// <src>getScalar</src>. These recursively evaluate the result of the
89// current expression when the result is either an array or a scalar,
90// respectively. The need for recursion can be understood with a simple
91// example.
92//
93// Consider an expression summing two Lattices such as "2*(b+c)".
94// The expression tree consists of nodes (leaves) that 1) get Lattice
95// pixels from the Lattice (expressions "b" and "c"), 2) add the pixel
96// values of the Lattices together (operator "+"), and 3) multiply a Lattice
97// by a scalar (operator "*"). At the top of the tree,
98// we have a scalar (2.0) and a Lattice (the
99// result of "b+c"). The top-of-the-tree expression has to multiply
100// them together. That's what the <src>eval</src> function for the "*"
101// operation needs to do. The key is that each of the "2.0" and
102// "b+c" are really Lattice expressions themselves and they can be evaluated.
103// So before the "*" <src>eval</src> function can
104// multiply its two expressions together, it must individually evaluate them.
105// Thus, it first calls the <src>getScalar</src> function of
106// the object housing the expression "2.0". This will in fact return
107// the scalar value "2.0". Then it calls
108// <src>eval</src> on the expression object housing "b+c". This
109// object in turn first calls <src>eval</src> on the left ("b") and
110// right ("c") expressions which results in the pixels for the Lattices
111// being returned. It then adds them together, returning the result
112// to the top of the tree where they are multiplied by 2. You can see
113// that since all these different expression objects call the
114// <src>eval</src> or <src>getScalar</src> function that they all inherit
115// from LELInterface. Indeed for our example above, the actual classes
116// involved are are LELLattice (get pixels from Lattice) and LELBinary
117// ("+" and "*" operators) which inherit from LELInterface. When these
118// objects are constructed, they work out whether the result of their
119// evaluation is a scalar or not. This is how the classes higher up
120// the tree know whether to call <src>eval</src> or <src>getScalar</src>.
121//
122// The results of the computations are either returned in the buffer in
123// the <src>eval</src> function or by value by <src>getScalar</src>
124//
125// The classes evaluate the expression for each specified Lattice
126// chunk (usually tile by tile). The <src>section</src> argument
127// in the <src>eval</src> function specifies the section of the
128// Lattice being evaluated. The absence of the <src>section</src>
129// argument in the <src>getScalar</src> function emphasises the
130// scalar nature; a scalar expression does not have a shape. For most
131// of the letter classes, the <src>section</src> argument is irrelevant;
132// the only one it really matters for is LELLattice which fetches the
133// pixels from the Lattice. The rest only care about the shape of the
134// buffer in the <src>eval</src> call.
135//
136// </synopsis>
137//
138// <motivation>
139// The many letter classes that actually do the computational work
140// are used polymorphically. Therefore, they must have a base
141// class declaring the interface.
142// </motivation>
143
144// <todo asof="1998/02/20">
145// </todo>
146
147
148template <class T> class LELInterface
149{
150public:
151
152// Virtual destructor
153 virtual ~LELInterface();
154
155// Evaluate the expression and fill the result array
156 virtual void eval (LELArray<T>& result,
157 const Slicer& section) const = 0;
158 virtual void evalRef (LELArrayRef<T>& result,
159 const Slicer& section) const;
160
161// Get the result of a scalar subexpression.
162 virtual LELScalar<T> getScalar() const = 0;
163
164// Get the result of an array subexpression.
165// It does eval for the entire array.
166// An exception is thrown if the shape of the subexpression is unknown.
168
169// Do further preparations (e.g. optimization) on the expression.
170// It returns True if the expression is an invalid scalar
171// (i.e. with a False mask).
172// That can happen if the expression has a component with an invalid
173// scalar value (e.g. min(lattice) where lattice contains no valid elements).
174 virtual Bool prepareScalarExpr() = 0;
175
176// Is the result of evaluating this expression a scalar ?
177 Bool isScalar() const {return attr_p.isScalar();}
178
179// Get the shape of the expression result.
180 const IPosition& shape() const {return attr_p.shape();}
181
182// Get expression attribute
183 const LELAttribute& getAttribute() const {return attr_p;}
184
185// Get class name
186 virtual String className() const = 0;
187
188// If the given expression is a valid scalar, replace it by its result.
189// It returns False if the expression is no scalar or if the expression
190// is an invalid scalar (i.e. with a False mask).
191 static Bool replaceScalarExpr (std::shared_ptr<LELInterface<T>>& expr);
192
193 // Handle locking/syncing of the parts of a lattice expression.
194 // <br>By default the functions do not do anything at all.
195 // lock() and hasLock return True.
196 // <group>
197 virtual Bool lock (FileLocker::LockType, uInt nattempts);
198 virtual void unlock();
200 virtual void resync();
201 // </group>
202
203protected:
204// Set the expression attributes of this object.
205 void setAttr(const LELAttribute& attrib);
206
207private:
209};
210
211
212
213} //# NAMESPACE CASACORE - END
214
215//# There is a problem in including LELInterface.tcc, because it needs
216//# LELUnary.h which in its turn includes LELInterface.h again.
217//# So in a source file including LELUnary.h, LELInterface::replaceScalarExpr
218//# fails to compile, because the LELUnary declarations are not seen yet.
219//# Therefore LELUnary.h is included here, while LELUnary.h includes
220//# LELInterface.tcc.
221#ifndef CASACORE_NO_AUTO_TEMPLATES
222#include <casacore/lattices/LEL/LELUnary.h>
223#endif //# CASACORE_NO_AUTO_TEMPLATES
224#endif
LockType
Define the possible lock types.
Definition FileLocker.h:93
This LEL class holds a possible referenced array with a mask.
Definition LELArray.h:131
Bool isScalar() const
Is expression a scalar?
const IPosition & shape() const
What is the shape of the expression?
const LELAttribute & getAttribute() const
Get expression attribute.
virtual Bool hasLock(FileLocker::LockType) const
const IPosition & shape() const
Get the shape of the expression result.
virtual ~LELInterface()
Virtual destructor.
virtual void resync()
virtual void eval(LELArray< T > &result, const Slicer &section) const =0
Evaluate the expression and fill the result array.
virtual void evalRef(LELArrayRef< T > &result, const Slicer &section) const
virtual String className() const =0
Get class name.
virtual Bool lock(FileLocker::LockType, uInt nattempts)
Handle locking/syncing of the parts of a lattice expression.
void setAttr(const LELAttribute &attrib)
Set the expression attributes of this object.
virtual void unlock()
static Bool replaceScalarExpr(std::shared_ptr< LELInterface< T > > &expr)
If the given expression is a valid scalar, replace it by its result.
Bool isScalar() const
Is the result of evaluating this expression a scalar ?
virtual LELScalar< T > getScalar() const =0
Get the result of a scalar subexpression.
virtual Bool prepareScalarExpr()=0
Do further preparations (e.g.
LELArray< T > getArray() const
Get the result of an array subexpression.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40