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> class FunctionHolder : public RecordTransformable {
90 public:
91 //# Enumerations
92 // Types of functions
110 //# Structures
111 // Structure to hold functional status
112 struct FuncStat {
113 // Name
115 // type
117 // Order (True if needed)
119 };
120
121 //# Constructors
122 // Creates an empty holder
124 // Create from a Function (copy made)
126 // Copy a holder (copy semantics)
128 //# Destructor
130
131 //# Operators
132 // Assignment (copy semantics)
134
135 //# Member Functions
136 // Check the the FunctionHolder holds the specified type. Return
137 // True if if does and False otherwise.
138 // <group>
139 Bool isEmpty() const;
140 // </group>
141 // Get the known names
142 const Vector<String> &names() const;
143 // Get a specific Function from the holder (with lifetime as long
144 // as holder exists).
145 // <thrown>
146 // <li> AipsError if holder empty
147 // <li> AipsError if holder contains wrong Function
148 // </thrown>
149 // <group>
150 const Function<T> &asFunction() const;
151 // </group>
152 // Add a function
154 // Get the type of currently filled holder
155 Types type() const;
156 // Create a Function from a record. An error message is generated, and False
157 // returned if an invalid record is given. A valid record will return True.
158 // A valid record contains at least the following fields (any additional fields are
159 // ignored):
160 // <ul>
161 // <li> tp = TpString: type of Function (gaussian1d, etc; case
162 // insensitive) -- OR an enumeration code
163 // <li> order = TpInt: the order needed to create a Function (-1 if not
164 // necessary or default)
165 // <li> ndim, npar, params are optional
166 // <li> nfunc, funcs are required for COMBI or COMPOUND
167 // </ul>
168 // A Function can be created from a string. In that case the string
169 // will only indicate the type of function (like polynomial), and will
170 // create a default polynomial of that given type.
171 // Error messages are postfixed to error.
172 // <group>
173 virtual Bool fromRecord(String &error, const RecordInterface &in);
174 virtual Bool fromString(String &error, const String &in);
175 template <class U>
177 const RecordInterface &in);
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
186private:
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>
209 Bool getType(String &error, Function<U> *&fn, const RecordInterface &in);
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
218} //# NAMESPACE CASACORE - END
219
220#ifndef CASACORE_NO_AUTO_TEMPLATES
221#include <casacore/scimath/Functionals/FunctionHolder.tcc>
222#endif //# CASACORE_NO_AUTO_TEMPLATES
223#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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
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)