casacore
Loading...
Searching...
No Matches
FunctionHolder.h
Go to the documentation of this file.
1// # FunctionHolder.h: A holder for Functions to enable record conversions
2// # Copyright (C) 2002,2003,2004
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 SCIMATH_FUNCTIONHOLDER_H
27#define SCIMATH_FUNCTIONHOLDER_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Utilities/RecordTransformable.h>
32#include <casacore/scimath/Functionals/Function.h>
33#include <casacore/casa/Arrays/Vector.h>
34#include <casacore/casa/BasicSL/String.h>
35#include <memory>
36
37namespace casacore { // # NAMESPACE CASACORE - BEGIN
38
39// # Forward Declarations
40
41// <summary> A holder for Functions to enable record conversions </summary>
42
43// <use visibility=export>
44
45// <reviewed reviewer="" date="yyyy/mm/dd" tests="tFunctionHolder" demos="">
46// </reviewed>
47
48// <prerequisite>
49// <li> <linkto class=RecordInterface>RecordInterface</linkto> class
50// <li> <linkto class=Function>Function</linkto> class
51// </prerequisite>
52//
53// <etymology>
54// A Holder of general Measures
55// </etymology>
56//
57// <synopsis>
58// This class can be used to handle heterogeneous collections of Functions,
59// e.g. as a <src>Vector<FunctionHolder></src>. With the aid of the
60// toRecord() and fromRecord() functions it can be used
61// to convert a Function object into or from a record.
62// A FunctionHolder is created from a Function, or can be empty.
63//
64// </synopsis>
65//
66// <example>
67// <srcblock>
68// TableRecord rec;
69// MDirection dir(MVDirection(Quantity(12.5, 'deg'), Quantity(-2, 'deg')),
70// MDirection::J2000);
71// String error; // error message
72// if (!FunctionHolder(dir).toRecord(error, rec)) {
73// cout << error << endl;
74// }
75// Record grec; // a Record
76// if (!FunctionHolder(dir).toRecord(error, grec)) { // make record
77// cout << error << endl;
78// }
79// // Note that for GlishRecords use can be made of the
80// // GlishRecord::to/fromrecord() methods.
81// </srcblock>
82// </example>
83//
84// <motivation>
85// To make general conversions between Functions and records, without knowing
86// the actual Function being converted.
87// </motivation>
88
89template <class T>
91 public:
92 // # Enumerations
93 // Types of functions
111 // # Structures
112 // Structure to hold functional status
113 struct FuncStat {
114 // Name
116 // type
118 // Order (True if needed)
120 };
121
122 // # Constructors
123 // Creates an empty holder
125 // Create from a Function (copy made)
127 // Copy a holder (copy semantics)
129 // # Destructor
131
132 // # Operators
133 // Assignment (copy semantics)
135
136 // # Member Functions
137 // Check the the FunctionHolder holds the specified type. Return
138 // True if if does and False otherwise.
139 // <group>
140 Bool isEmpty() const;
141 // </group>
142 // Get the known names
143 const Vector<String> &names() const;
144 // Get a specific Function from the holder (with lifetime as long
145 // as holder exists).
146 // <thrown>
147 // <li> AipsError if holder empty
148 // <li> AipsError if holder contains wrong Function
149 // </thrown>
150 // <group>
151 const Function<T> &asFunction() const;
152 // </group>
153 // Add a function
155 // Get the type of currently filled holder
156 Types type() const;
157 // Create a Function from a record. An error message is generated, and False
158 // returned if an invalid record is given. A valid record will return True.
159 // A valid record contains at least the following fields (any additional fields are
160 // ignored):
161 // <ul>
162 // <li> tp = TpString: type of Function (gaussian1d, etc; case
163 // insensitive) -- OR an enumeration code
164 // <li> order = TpInt: the order needed to create a Function (-1 if not
165 // necessary or default)
166 // <li> ndim, npar, params are optional
167 // <li> nfunc, funcs are required for COMBI or COMPOUND
168 // </ul>
169 // A Function can be created from a string. In that case the string
170 // will only indicate the type of function (like polynomial), and will
171 // create a default polynomial of that given type.
172 // Error messages are postfixed to error.
173 // <group>
174 virtual Bool fromRecord(String &error, const RecordInterface &in);
175 virtual Bool fromString(String &error, const String &in);
176 template <class U>
178 // </group>
179 // Create a record from a Function. The return will be False and an error
180 // message generated only if the FunctionHolder does not contain a Function.
181 // Error messages are postfixed to error.
182 virtual Bool toRecord(String &error, RecordInterface &out) const;
183 // Get identification of record
184 virtual const String &ident() const;
185
186 private:
187 // # Data Members
188 // Pointer to a Function
189 std::unique_ptr<Function<T>> hold_p;
190 // Aids (only filled after a succesful to/fromRecord
191 // <group>
192 mutable Types nf_p;
193 mutable Int order_p;
194 mutable String text_p;
195 mutable std::unique_ptr<RecordInterface> mode_p;
196 // </group>
197 // List of known names
199 // Filled list?
200 mutable Bool isFilled;
201
202 // # Member functions
203 // Initialise and check the name list
204 void init() const;
205 // Aid for to/from Record, String
206 // <group>
207 Bool putType(String &error, RecordInterface &out) const;
208 template <class U>
210 template <class U>
212 void setParameters(Function<T> *&fn, const Vector<T> &params);
213 void setParameters(Function<AutoDiff<T>> *&fn, const Vector<T> &params);
214 // </group>
215};
216
217} // namespace casacore
218
219#ifndef CASACORE_NO_AUTO_TEMPLATES
220#include <casacore/scimath/Functionals/FunctionHolder.tcc>
221#endif // # CASACORE_NO_AUTO_TEMPLATES
222#endif
FunctionHolder()
Creates an empty holder.
void setParameters(Function< T > *&fn, const Vector< T > &params)
const Vector< String > & names() const
Get the known names.
std::unique_ptr< RecordInterface > mode_p
const Function< T > & asFunction() const
Get a specific Function from the holder (with lifetime as long as holder exists).
Bool isEmpty() const
Check the the FunctionHolder holds the specified type.
virtual const String & ident() const
Get identification of record.
Bool getType(String &error, Function< U > *&fn)
Bool getType(String &error, Function< U > *&fn, const RecordInterface &in)
void setParameters(Function< AutoDiff< T > > *&fn, const Vector< T > &params)
FunctionHolder(const Function< T > &in)
Create from a Function (copy made).
FunctionHolder(const FunctionHolder< T > &other)
Copy a holder (copy semantics).
virtual Bool fromString(String &error, const String &in)
Initialise the class from a String representation.
Bool getRecord(String &error, Function< U > *&fn, const RecordInterface &in)
Bool addFunction(const Function< T > &fnc)
Add a function.
Bool putType(String &error, RecordInterface &out) const
Aid for to/from Record, String.
Types type() const
Get the type of currently filled holder.
Types
Types of functions.
Bool isFilled
Filled list?
std::unique_ptr< Function< T > > hold_p
Pointer to a Function.
Types nf_p
Aids (only filled after a succesful to/fromRecord.
Vector< String > nam_p
List of known names.
void init() const
Initialise and check the name list.
virtual Bool fromRecord(String &error, const RecordInterface &in)
Create a Function from a record.
virtual Bool toRecord(String &error, RecordInterface &out) const
Create a record from a Function.
FunctionHolder & operator=(const FunctionHolder< T > &other)
Assignment (copy semantics).
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
RecordInterface()
The default constructor creates an empty record with a variable structure.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
Structure to hold functional status.
Bool order
Order (True if needed).