LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
FieldBase.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_FieldBase_h_INCLUDED
3 #define AFW_TABLE_FieldBase_h_INCLUDED
4 
5 #include <cstring>
6 #include <iostream>
7 
8 #include "boost/mpl/vector.hpp"
9 #include "boost/preprocessor/punctuation/paren.hpp"
10 #include "Eigen/Core"
11 
12 #include "lsst/base.h"
13 #include "lsst/pex/exceptions.h"
14 #include "ndarray.h"
15 #include "lsst/afw/geom.h"
16 #include "lsst/afw/table/misc.h"
17 #include "lsst/afw/table/KeyBase.h"
18 #include "lsst/afw/table/types.h"
19 
20 namespace lsst { namespace afw { namespace table {
21 
22 namespace detail {
23 
24 class TableImpl;
25 
31 inline int indexCovariance(int i, int j) {
32  return (i < j) ? (i + j*(j+1)/2) : (j + i*(i+1)/2);
33 }
34 
36 inline int computeCovariancePackedSize(int size) {
37  return size * (size + 1) / 2;
38 }
39 
40 } // namespace detail
41 
47 template <typename T>
48 struct FieldBase {
49 
50  typedef T Value;
51  typedef T & Reference;
52  typedef T const & ConstReference;
53  typedef T Element;
54 
56  int getElementCount() const { return 1; }
57 
59  static std::string getTypeString();
60 
61 #ifndef SWIG_BUG_3465431_FIXED
62  // SWIG uses this template to define the interface for the other specializations.
63  // We can add other methods to full specializations using %extend, but we can't add
64  // constructors that way.
65  FieldBase() {}
66  FieldBase(int) {
67  throw LSST_EXCEPT(
68  lsst::pex::exceptions::LogicError,
69  "Constructor disabled (it only appears to exist as a workaround for a SWIG bug)."
70  );
71  }
72 #endif
73 
74 protected:
75 
77  static FieldBase makeDefault() { return FieldBase(); }
78 
80  void stream(std::ostream & os) const {}
81 
83  Reference getReference(Element * p, ndarray::Manager::Ptr const &) const { return *p; }
84 
86  ConstReference getConstReference(Element const * p, ndarray::Manager::Ptr const &) const { return *p; }
87 
89  Value getValue(Element const * p, ndarray::Manager::Ptr const &) const { return *p; }
90 
92  void setValue(Element * p, ndarray::Manager::Ptr const &, Value v) const { *p = v; }
93 
94 };
95 
105 template <typename U>
106 struct FieldBase< Array<U> > {
107 
109 
112 
115 
116  typedef U Element;
117 
131  FieldBase(int size=0) : _size(size) {
132  if (size < 0) throw LSST_EXCEPT(
133  lsst::pex::exceptions::LogicError,
134  "A non-negative size must be provided when constructing an array field."
135  );
136  }
137 
139  static std::string getTypeString();
140 
142  int getElementCount() const { return _size; }
143 
145  int getSize() const { return _size; }
146 
148  bool isVariableLength() const { return _size == 0; }
149 
150 protected:
151 
153  static FieldBase makeDefault() { return FieldBase(0); }
154 
156  void stream(std::ostream & os) const { os << ", size=" << _size; }
157 
160  if (isVariableLength()) {
161  return reinterpret_cast< ndarray::Array<Element,1,1> * >(p)->deep();
162  }
164  }
165 
168  if (isVariableLength()) {
169  return reinterpret_cast< ndarray::Array<Element,1,1> const * >(p)->deep();
170  }
172  }
173 
175  Value getValue(Element const * p, ndarray::Manager::Ptr const & m) const {
176  if (isVariableLength()) {
177  return *reinterpret_cast< ndarray::Array<Element,1,1> const * >(p);
178  }
180  }
181 
186  void setValue(
187  Element * p, ndarray::Manager::Ptr const &, ndarray::Array<Element,1,1> const & value
188  ) const {
189  if (isVariableLength()) {
190  *reinterpret_cast< ndarray::Array<Element,1,1>* >(p) = value;
191  } else {
192  setValueDeep(p, value);
193  }
194  }
195 
197  template <typename Derived>
198  void setValue(
200  ) const {
201  if (isVariableLength()) {
202  throw LSST_EXCEPT(
203  lsst::pex::exceptions::LogicError,
204  "Assignment to a variable-length array must use a non-const array of the correct type."
205  );
206  }
207  setValueDeep(p, value);
208  }
209 
210 private:
211 
212  template <typename Derived>
213  void setValueDeep(Element * p, ndarray::ExpressionBase<Derived> const & value) const {
214  if (value.template getSize<0>() != _size) {
215  throw LSST_EXCEPT(
216  lsst::pex::exceptions::LengthError,
217  "Incorrect size in array field assignment."
218  );
219  }
220  for (int i = 0; i < _size; ++i) p[i] = value[i];
221  }
222 
223  int _size;
224 };
225 
229 template <>
230 struct FieldBase< std::string > {
231 
232  typedef std::string Value;
233 
235  typedef char * Reference;
236 
238  typedef char const * ConstReference;
239 
240  typedef char Element;
241 
253  FieldBase(int size=-1);
254 
256  static std::string getTypeString();
257 
260  int getElementCount() const { return _size; }
261 
264  int getSize() const { return _size; }
265 
266 protected:
267 
269  static FieldBase makeDefault() { return FieldBase(0); }
270 
272  void stream(std::ostream & os) const { os << ", size=" << _size; }
273 
276  return p;
277  }
278 
281  return p;
282  }
283 
285  Value getValue(Element const * p, ndarray::Manager::Ptr const & m) const;
286 
288  void setValue(Element * p, ndarray::Manager::Ptr const &, std::string const & value) const;
289 
290 private:
291  int _size;
292 };
293 
294 }}} // namespace lsst::afw::table
295 
296 #endif // !AFW_TABLE_FieldBase_h_INCLUDED
A proxy class for Array with deep assignment operators.
Definition: ArrayRef.h:46
int getSize() const
Return the maximum length of the string, including a null terminator (equal to the number of subfield...
Definition: FieldBase.h:264
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:48
An include file to include the header files for lsst::afw::geom.
char const * ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:238
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:77
int getElementCount() const
Return the number of subfield elements (equal to the size of the array).
Definition: FieldBase.h:142
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:272
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:269
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (non-const).
Definition: FieldBase.h:275
U Element
the type of subfields and array elements
Definition: FieldBase.h:116
detail::ExternalInitializer< T, N, Owner > external(T *data, Vector< int, N > const &shape, Vector< int, N > const &strides, Owner const &owner)
Create an expression that initializes an Array with externally allocated memory.
void setValueDeep(Element *p, ndarray::ExpressionBase< Derived > const &value) const
Definition: FieldBase.h:213
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement RecordBase::set.
Definition: FieldBase.h:92
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (const).
Definition: FieldBase.h:280
int computeCovariancePackedSize(int size)
Defines the packed size of a covariance matrices.
Definition: FieldBase.h:36
T const & ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:52
static std::string getTypeString()
Return a string description of the field type.
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::get.
Definition: FieldBase.h:89
int getElementCount() const
Return the number of subfield elements (always one for scalars).
Definition: FieldBase.h:56
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
std::string Value
the type returned by BaseRecord::get
Definition: FieldBase.h:232
T Element
the type of subfields (the same as the type itself for scalars)
Definition: FieldBase.h:53
int getElementCount() const
Return the number of subfield elements (equal to the size of the string, including a null terminator)...
Definition: FieldBase.h:260
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::operator[] (const).
Definition: FieldBase.h:86
int indexCovariance(int i, int j)
Defines the ordering of packed covariance matrices.
Definition: FieldBase.h:31
Vector< T, N > makeVector(T v1, T v2,..., T vN)
Variadic constructor for Vector.
int getSize() const
Return the size of the array (equal to the number of subfield elements).
Definition: FieldBase.h:145
Tag types used to declare specialized field types.
Definition: misc.h:35
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (non-const).
Definition: FieldBase.h:159
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:50
void setValue(Element *p, ndarray::Manager::Ptr const &, ndarray::ExpressionBase< Derived > const &value) const
Used to implement RecordBase::set; accepts any ndarray expression.
Definition: FieldBase.h:198
void setValue(Element *p, ndarray::Manager::Ptr const &, ndarray::Array< Element, 1, 1 > const &value) const
Definition: FieldBase.h:186
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A multidimensional strided array.
Definition: Array.h:47
tuple m
Definition: lsstimport.py:48
ndarray::ArrayRef< U const, 1, 1 > ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:114
FieldBase(int size=0)
Construct a FieldBase with the given size.
Definition: FieldBase.h:131
T & Reference
the type returned by BaseRecord::operator[] (non-const)
Definition: FieldBase.h:51
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:80
char * Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:235
ndarray::ArrayRef< U, 1, 1 > Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:111
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:156
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (const).
Definition: FieldBase.h:167
ndarray::Array< U const, 1, 1 > Value
the type returned by BaseRecord::get
Definition: FieldBase.h:108
bool isVariableLength() const
Return true if the field is variable-length (each record can have a different size array)...
Definition: FieldBase.h:148
Reference getReference(Element *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::operator[] (non-const).
Definition: FieldBase.h:83
Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::get.
Definition: FieldBase.h:175
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:153
char Element
the type of subfields and array elements
Definition: FieldBase.h:240
Include files required for standard LSST Exception handling.
CRTP base class for all multidimensional expressions.