LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
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/table/misc.h"
16 #include "lsst/afw/table/KeyBase.h"
17 #include "lsst/afw/table/types.h"
18 
19 namespace lsst {
20 namespace afw {
21 namespace table {
22 
23 namespace detail {
24 
25 class TableImpl;
26 
32 inline int indexCovariance(int i, int j) { return (i < j) ? (i + j * (j + 1) / 2) : (j + i * (i + 1) / 2); }
33 
35 inline int computeCovariancePackedSize(int size) { return size * (size + 1) / 2; }
36 
37 } // namespace detail
38 
42 template <typename T>
43 struct FieldBase {
44  typedef T Value;
45  typedef T &Reference;
46  typedef T const &ConstReference;
47  typedef T Element;
48 
50  int getElementCount() const noexcept { return 1; }
51 
53  static std::string getTypeString();
54 
55  // Only the first of these constructors is valid for this specializations, but
56  // it's convenient to be able to instantiate both, since the other is used
57  // by other specializations.
58  FieldBase() = default;
59  FieldBase(int) {
61  "Constructor disabled (this Field type is not sized).");
62  }
63  FieldBase(FieldBase const &) noexcept = default;
64  FieldBase(FieldBase &&) noexcept = default;
65  FieldBase &operator=(FieldBase const &) noexcept = default;
66  FieldBase &operator=(FieldBase &&) noexcept = default;
67  ~FieldBase() noexcept = default;
68 
69 protected:
71  static FieldBase makeDefault() noexcept { return FieldBase(); }
72 
74  void stream(std::ostream &os) const {}
75 
77  Reference getReference(Element *p, ndarray::Manager::Ptr const &) const { return *p; }
78 
80  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
81 
83  Value getValue(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
84 
86  void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const { *p = v; }
87 };
88 
98 template <typename U>
99 struct FieldBase<Array<U> > {
100  typedef ndarray::Array<U const, 1, 1> Value;
101 
103  typedef ndarray::ArrayRef<U, 1, 1> Reference;
104 
106  typedef ndarray::ArrayRef<U const, 1, 1> ConstReference;
107 
108  typedef U Element;
109 
123  FieldBase(int size = 0) : _size(size) {
124  if (size < 0)
126  "A non-negative size must be provided when constructing an array field.");
127  }
128 
129  FieldBase(FieldBase const &) noexcept = default;
130  FieldBase(FieldBase &&) noexcept = default;
131  FieldBase &operator=(FieldBase const &) noexcept = default;
132  FieldBase &operator=(FieldBase &&) noexcept = default;
133  ~FieldBase() noexcept = default;
134 
136  static std::string getTypeString();
137 
140  int getElementCount() const noexcept { return _size; }
141 
144  int getSize() const noexcept { return _size; }
145 
147  bool isVariableLength() const noexcept { return _size == 0; }
148 
149 protected:
151  static FieldBase makeDefault() noexcept { return FieldBase(0); }
152 
154  void stream(std::ostream &os) const { os << ", size=" << _size; }
155 
157  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const {
158  if (isVariableLength()) {
159  return reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p)->deep();
160  }
161  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
162  }
163 
165  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const {
166  if (isVariableLength()) {
167  return reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p)->deep();
168  }
169  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
170  }
171 
173  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const {
174  if (isVariableLength()) {
175  return *reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p);
176  }
177  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
178  }
179 
186  void setValue(Element *p, ndarray::Manager::Ptr const &,
187  ndarray::Array<Element, 1, 1> const &value) const {
188  if (isVariableLength()) {
189  *reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p) = value;
190  } else {
191  setValueDeep(p, value);
192  }
193  }
194 
196  template <typename Derived>
197  void setValue(Element *p, ndarray::Manager::Ptr const &,
198  ndarray::ExpressionBase<Derived> const &value) const {
199  if (isVariableLength()) {
200  throw LSST_EXCEPT(
202  "Assignment to a variable-length array must use a non-const array of the correct type.");
203  }
204  setValueDeep(p, value);
205  }
206 
207 private:
208  template <typename Derived>
209  void setValueDeep(Element *p, ndarray::ExpressionBase<Derived> const &value) const {
210  if (value.template getSize<0>() != static_cast<std::size_t>(_size)) {
212  "Incorrect size in array field assignment.");
213  }
214  for (int i = 0; i < _size; ++i) p[i] = value[i];
215  }
216 
217  int _size;
218 };
219 
223 template <>
224 struct FieldBase<std::string> {
225  typedef std::string Value;
226 
228  typedef char *Reference;
229 
231  typedef char const *ConstReference;
232 
233  typedef char Element;
234 
248  FieldBase(int size = -1);
249 
250  FieldBase(FieldBase const &) noexcept = default;
251  FieldBase(FieldBase &&) noexcept = default;
252  FieldBase &operator=(FieldBase const &) noexcept = default;
253  FieldBase &operator=(FieldBase &&) noexcept = default;
254  ~FieldBase() noexcept = default;
255 
257  static std::string getTypeString();
258 
261  int getElementCount() const noexcept { return _size; }
262 
265  int getSize() const noexcept { return _size; }
266 
268  bool isVariableLength() const noexcept { return _size == 0; }
269 
270 protected:
272  static FieldBase makeDefault() noexcept { return FieldBase(0); }
273 
275  void stream(std::ostream &os) const { os << ", size=" << _size; }
276 
278  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const {
279  if (isVariableLength()) {
280  // Can't be done until C++17, which allows read/write access to std::string's internal buffer
282  "non-const operator[] not supported for variable-length strings");
283  } else {
284  return p;
285  }
286  }
287 
289  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const {
290  if (isVariableLength()) {
291  return reinterpret_cast<std::string const *>(p)->data();
292  } else {
293  return p;
294  }
295  }
296 
298  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const;
299 
305  void setValue(Element *p, ndarray::Manager::Ptr const &, std::string const &value) const;
306 
307 private:
308  int _size;
309 };
310 } // namespace table
311 } // namespace afw
312 } // namespace lsst
313 
314 #endif // !AFW_TABLE_FieldBase_h_INCLUDED
char * data
Definition: BaseRecord.cc:62
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::ostream * os
Definition: Schema.cc:746
int m
Definition: SpanSet.cc:49
Basic LSST definitions.
Tag types used to declare specialized field types.
Definition: misc.h:32
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
int computeCovariancePackedSize(int size)
Defines the packed size of a covariance matrices.
Definition: FieldBase.h:35
int indexCovariance(int i, int j)
Defines the ordering of packed covariance matrices.
Definition: FieldBase.h:32
A base class for image defects.
STL namespace.
table::Key< table::Array< int > > _size
Definition: PsfexPsf.cc:364
void setValue(Element *p, ndarray::Manager::Ptr const &, ndarray::Array< Element, 1, 1 > const &value) const
Used to implement BaseRecord::set; accepts only non-const arrays of the right type.
Definition: FieldBase.h:186
Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:173
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:154
FieldBase(FieldBase const &) noexcept=default
ndarray::Array< U const, 1, 1 > Value
the type returned by BaseRecord::get
Definition: FieldBase.h:100
U Element
the type of subfields and array elements
Definition: FieldBase.h:108
void setValue(Element *p, ndarray::Manager::Ptr const &, ndarray::ExpressionBase< Derived > const &value) const
Used to implement BaseRecord::set; accepts any ndarray expression.
Definition: FieldBase.h:197
int getSize() const noexcept
Return the size of the array (equal to the number of subfield elements), or 0 for a variable-length a...
Definition: FieldBase.h:144
FieldBase(int size=0)
Construct a FieldBase with the given size.
Definition: FieldBase.h:123
ndarray::ArrayRef< U, 1, 1 > Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:103
bool isVariableLength() const noexcept
Return true if the field is variable-length (each record can have a different size array).
Definition: FieldBase.h:147
FieldBase(FieldBase &&) noexcept=default
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:157
ndarray::ArrayRef< U const, 1, 1 > ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:106
static FieldBase makeDefault() noexcept
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:151
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:165
std::string Value
the type returned by BaseRecord::get
Definition: FieldBase.h:225
char const * ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:231
char Element
the type of subfields and array elements
Definition: FieldBase.h:233
static FieldBase makeDefault() noexcept
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:272
bool isVariableLength() const noexcept
Return true if the field is variable-length (each record can have a different size array).
Definition: FieldBase.h:268
FieldBase(FieldBase &&) noexcept=default
FieldBase(FieldBase const &) noexcept=default
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:289
int getSize() const noexcept
Return the maximum length of the string, including a null terminator (equal to the number of subfield...
Definition: FieldBase.h:265
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:275
char * Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:228
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:278
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
T & Reference
the type returned by BaseRecord::operator[] (non-const)
Definition: FieldBase.h:45
T const & ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:46
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:80
FieldBase(FieldBase const &) noexcept=default
static std::string getTypeString()
Return a string description of the field type.
Definition: FieldBase.cc:56
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:83
T Element
the type of subfields (the same as the type itself for scalars)
Definition: FieldBase.h:47
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:44
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement BaseRecord::set.
Definition: FieldBase.h:86
Reference getReference(Element *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:77
static FieldBase makeDefault() noexcept
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:71
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:74
int getElementCount() const noexcept
Return the number of subfield elements (always one for scalars).
Definition: FieldBase.h:50
FieldBase(FieldBase &&) noexcept=default