LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
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 "Eigen/Core"
9 
10 #include "lsst/base.h"
11 #include "lsst/pex/exceptions.h"
12 #include "ndarray.h"
13 #include "lsst/afw/table/misc.h"
14 #include "lsst/afw/table/KeyBase.h"
15 #include "lsst/afw/table/types.h"
16 
17 namespace lsst {
18 namespace afw {
19 namespace table {
20 
21 namespace detail {
22 
23 class TableImpl;
24 
30 inline std::size_t indexCovariance(std::size_t i, std::size_t j) { return (i < j) ? (i + j * (j + 1) / 2) : (j + i * (i + 1) / 2); }
31 
33 inline std::size_t computeCovariancePackedSize(std::size_t size) { return size * (size + 1) / 2; }
34 
35 } // namespace detail
36 
40 template <typename T>
41 struct FieldBase {
42  using Value = T;
43  using Reference = T &;
44  using ConstReference = const T &;
45  using Element = T;
46 
48  std::size_t getElementCount() const noexcept { return 1; }
49 
51  static std::string getTypeString();
52 
53  // Only the first of these constructors is valid for this specializations, but
54  // it's convenient to be able to instantiate both, since the other is used
55  // by other specializations.
56  FieldBase() = default;
59  "Constructor disabled (this Field type is not sized).");
60  }
61  FieldBase(FieldBase const &) noexcept = default;
62  FieldBase(FieldBase &&) noexcept = default;
63  FieldBase &operator=(FieldBase const &) noexcept = default;
64  FieldBase &operator=(FieldBase &&) noexcept = default;
65  ~FieldBase() noexcept = default;
66 
67 protected:
69  static FieldBase makeDefault() noexcept { return FieldBase(); }
70 
72  void stream(std::ostream &os) const {}
73 
75  Reference getReference(Element *p, ndarray::Manager::Ptr const &) const { return *p; }
76 
78  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
79 
81  Value getValue(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
82 
84  void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const { *p = v; }
85 };
86 
95 template <typename U>
96 struct FieldBase<Array<U> > {
97  using Value = ndarray::Array<const U, 1, 1>;
98 
100  using Reference = ndarray::ArrayRef<U, 1, 1>;
101 
103  using ConstReference = ndarray::ArrayRef<const U, 1, 1>;
104 
105  using Element = U;
106 
120  FieldBase(size_t size = 0) : _size(size) {
121  }
122 
123  FieldBase(FieldBase const &) noexcept = default;
124  FieldBase(FieldBase &&) noexcept = default;
125  FieldBase &operator=(FieldBase const &) noexcept = default;
126  FieldBase &operator=(FieldBase &&) noexcept = default;
127  ~FieldBase() noexcept = default;
128 
130  static std::string getTypeString();
131 
134  std::size_t getElementCount() const noexcept { return _size; }
135 
138  std::size_t getSize() const noexcept { return _size; }
139 
141  bool isVariableLength() const noexcept { return _size == 0; }
142 
143 protected:
145  static FieldBase makeDefault() noexcept { return FieldBase(0); }
146 
148  void stream(std::ostream &os) const { os << ", size=" << _size; }
149 
151  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const {
152  if (isVariableLength()) {
153  return reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p)->deep();
154  }
155  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
156  }
157 
159  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const {
160  if (isVariableLength()) {
161  return reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p)->deep();
162  }
163  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
164  }
165 
167  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const {
168  if (isVariableLength()) {
169  return *reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p);
170  }
171  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
172  }
173 
180  void setValue(Element *p, ndarray::Manager::Ptr const &,
181  ndarray::Array<Element, 1, 1> const &value) const {
182  if (isVariableLength()) {
183  *reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p) = value;
184  } else {
185  setValueDeep(p, value);
186  }
187  }
188 
190  template <typename Derived>
191  void setValue(Element *p, ndarray::Manager::Ptr const &,
192  ndarray::ExpressionBase<Derived> const &value) const {
193  if (isVariableLength()) {
194  throw LSST_EXCEPT(
196  "Assignment to a variable-length array must use a non-const array of the correct type.");
197  }
198  setValueDeep(p, value);
199  }
200 
201 private:
202  template <typename Derived>
203  void setValueDeep(Element *p, ndarray::ExpressionBase<Derived> const &value) const {
204  if (value.template getSize<0>() != static_cast<std::size_t>(_size)) {
206  "Incorrect size in array field assignment.");
207  }
208  for (std::size_t i = 0; i < _size; ++i) p[i] = value[i];
209  }
210 
212 };
213 
217 template <>
218 struct FieldBase<std::string> {
219  using Value = std::string;
220 
222  using Reference = char *;
223 
225  using ConstReference = const char *;
226 
227  using Element = char;
228 
243 
244  FieldBase(FieldBase const &) noexcept = default;
245  FieldBase(FieldBase &&) noexcept = default;
246  FieldBase &operator=(FieldBase const &) noexcept = default;
247  FieldBase &operator=(FieldBase &&) noexcept = default;
248  ~FieldBase() noexcept = default;
249 
251  static std::string getTypeString();
252 
255  std::size_t getElementCount() const noexcept { return _size; }
256 
259  std::size_t getSize() const noexcept { return _size; }
260 
262  bool isVariableLength() const noexcept { return _size == 0; }
263 
264 protected:
266  static FieldBase makeDefault() noexcept { return FieldBase(0); }
267 
269  void stream(std::ostream &os) const { os << ", size=" << _size; }
270 
272  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const {
273  if (isVariableLength()) {
274  // Can't be done until C++17, which allows read/write access to std::string's internal buffer
276  "non-const operator[] not supported for variable-length strings");
277  } else {
278  return p;
279  }
280  }
281 
283  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const {
284  if (isVariableLength()) {
285  return reinterpret_cast<std::string const *>(p)->data();
286  } else {
287  return p;
288  }
289  }
290 
292  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const;
293 
299  void setValue(Element *p, ndarray::Manager::Ptr const &, std::string const &value) const;
300 
301 private:
303 };
304 } // namespace table
305 } // namespace afw
306 } // namespace lsst
307 
308 #endif // !AFW_TABLE_FieldBase_h_INCLUDED
char * data
Definition: BaseRecord.cc:61
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::ostream * os
Definition: Schema.cc:557
int m
Definition: SpanSet.cc:48
Basic LSST definitions.
Tag types used to declare specialized field types.
Definition: misc.h:31
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
std::size_t computeCovariancePackedSize(std::size_t size)
Defines the packed size of a covariance matrices.
Definition: FieldBase.h:33
std::size_t indexCovariance(std::size_t i, std::size_t j)
Defines the ordering of packed covariance matrices.
Definition: FieldBase.h:30
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:180
ndarray::ArrayRef< U, 1, 1 > Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:100
std::size_t 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:138
Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:167
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:148
FieldBase(FieldBase const &) noexcept=default
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:191
U Element
the type of subfields and array elements
Definition: FieldBase.h:105
ndarray::ArrayRef< const U, 1, 1 > ConstReference
the type returned by BaseRecord::operator[] (const)
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:141
FieldBase(FieldBase &&) noexcept=default
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:151
FieldBase(size_t size=0)
Construct a FieldBase with the given size.
Definition: FieldBase.h:120
ndarray::Array< const U, 1, 1 > Value
the type returned by BaseRecord::get
Definition: FieldBase.h:97
static FieldBase makeDefault() noexcept
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:145
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:159
char Element
the type of subfields and array elements
Definition: FieldBase.h:227
char * Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:222
const char * ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:225
static FieldBase makeDefault() noexcept
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:266
bool isVariableLength() const noexcept
Return true if the field is variable-length (each record can have a different size array).
Definition: FieldBase.h:262
FieldBase(FieldBase &&) noexcept=default
std::size_t getSize() const noexcept
Return the maximum length of the string, including a null terminator (equal to the number of subfield...
Definition: FieldBase.h:259
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:283
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:269
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:272
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:41
const T & ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:44
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:78
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:81
std::size_t getElementCount() const noexcept
Return the number of subfield elements (always one for scalars).
Definition: FieldBase.h:48
T & Reference
the type returned by BaseRecord::operator[] (non-const)
Definition: FieldBase.h:43
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement BaseRecord::set.
Definition: FieldBase.h:84
Reference getReference(Element *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:75
static FieldBase makeDefault() noexcept
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:69
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:42
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:72
T Element
the type of subfields (the same as the type itself for scalars)
Definition: FieldBase.h:45
FieldBase(FieldBase &&) noexcept=default