lsst.afw  14.0-42-g99f9a3040+1
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 {
21 namespace afw {
22 namespace table {
23 
24 namespace detail {
25 
26 class TableImpl;
27 
33 inline int indexCovariance(int i, int j) { return (i < j) ? (i + j * (j + 1) / 2) : (j + i * (i + 1) / 2); }
34 
36 inline int computeCovariancePackedSize(int size) { return size * (size + 1) / 2; }
37 
38 } // namespace detail
39 
43 template <typename T>
44 struct FieldBase {
45  typedef T Value;
46  typedef T &Reference;
47  typedef T const &ConstReference;
48  typedef T Element;
49 
51  int getElementCount() const { return 1; }
52 
54  static std::string getTypeString();
55 
56  // Only the first of these constructors is valid for this specializations, but
57  // it's convenient to be able to instantiate both, since the other is used
58  // by other specializations.
59  FieldBase() = default;
60  FieldBase(int) {
62  "Constructor disabled (this Field type is not sized).");
63  }
64  FieldBase(FieldBase const &) = default;
65  FieldBase(FieldBase &&) = default;
66  FieldBase & operator=(FieldBase const &) = default;
67  FieldBase & operator=(FieldBase &&) = default;
68  ~FieldBase() = default;
69 
70 protected:
72  static FieldBase makeDefault() { return FieldBase(); }
73 
75  void stream(std::ostream &os) const {}
76 
78  Reference getReference(Element *p, ndarray::Manager::Ptr const &) const { return *p; }
79 
81  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
82 
84  Value getValue(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
85 
87  void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const { *p = v; }
88 };
89 
99 template <typename U>
100 struct FieldBase<Array<U> > {
101  typedef ndarray::Array<U const, 1, 1> Value;
102 
104  typedef ndarray::ArrayRef<U, 1, 1> Reference;
105 
107  typedef ndarray::ArrayRef<U const, 1, 1> ConstReference;
108 
109  typedef U Element;
110 
124  FieldBase(int size = 0) : _size(size) {
125  if (size < 0)
127  "A non-negative size must be provided when constructing an array field.");
128  }
129 
130  FieldBase(FieldBase const &) = default;
131  FieldBase(FieldBase &&) = default;
132  FieldBase & operator=(FieldBase const &) = default;
133  FieldBase & operator=(FieldBase &&) = default;
134  ~FieldBase() = default;
135 
137  static std::string getTypeString();
138 
141  int getElementCount() const { return _size; }
142 
145  int getSize() const { return _size; }
146 
148  bool isVariableLength() const { return _size == 0; }
149 
150 protected:
152  static FieldBase makeDefault() { return FieldBase(0); }
153 
155  void stream(std::ostream &os) const { os << ", size=" << _size; }
156 
158  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const {
159  if (isVariableLength()) {
160  return reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p)->deep();
161  }
162  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
163  }
164 
166  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const {
167  if (isVariableLength()) {
168  return reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p)->deep();
169  }
170  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
171  }
172 
174  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const {
175  if (isVariableLength()) {
176  return *reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p);
177  }
178  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
179  }
180 
187  void setValue(Element *p, ndarray::Manager::Ptr const &,
188  ndarray::Array<Element, 1, 1> const &value) 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(Element *p, ndarray::Manager::Ptr const &,
199  ndarray::ExpressionBase<Derived> const &value) const {
200  if (isVariableLength()) {
201  throw LSST_EXCEPT(
203  "Assignment to a variable-length array must use a non-const array of the correct type.");
204  }
205  setValueDeep(p, value);
206  }
207 
208 private:
209  template <typename Derived>
210  void setValueDeep(Element *p, ndarray::ExpressionBase<Derived> const &value) const {
211  if (value.template getSize<0>() != static_cast<std::size_t>(_size)) {
213  "Incorrect size in array field assignment.");
214  }
215  for (int i = 0; i < _size; ++i) p[i] = value[i];
216  }
217 
218  int _size;
219 };
220 
224 template <>
225 struct FieldBase<std::string> {
226  typedef std::string Value;
227 
229  typedef char *Reference;
230 
232  typedef char const *ConstReference;
233 
234  typedef char Element;
235 
249  FieldBase(int size = -1);
250 
251  FieldBase(FieldBase const &) = default;
252  FieldBase(FieldBase &&) = default;
253  FieldBase & operator=(FieldBase const &) = default;
254  FieldBase & operator=(FieldBase &&) = default;
255  ~FieldBase() = default;
256 
258  static std::string getTypeString();
259 
262  int getElementCount() const { return _size; }
263 
266  int getSize() const { return _size; }
267 
269  bool isVariableLength() const { return _size == 0; }
270 protected:
272  static FieldBase makeDefault() { 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 }
311 }
312 } // namespace lsst::afw::table
313 
314 #endif // !AFW_TABLE_FieldBase_h_INCLUDED
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:152
Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:174
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:44
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:166
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:75
bool isVariableLength() const
Return true if the field is variable-length (each record can have a different size array)...
Definition: FieldBase.h:269
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:275
U Element
the type of subfields and array elements
Definition: FieldBase.h:109
STL namespace.
int getSize() const
Return the size of the array (equal to the number of subfield elements), or 0 for a variable-length a...
Definition: FieldBase.h:145
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:155
ndarray::ArrayRef< U, 1, 1 > Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:104
char const * ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:232
int computeCovariancePackedSize(int size)
Defines the packed size of a covariance matrices.
Definition: FieldBase.h:36
ndarray::ArrayRef< U const, 1, 1 > ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:107
int getElementCount() const
Return the number of subfield elements (always one for scalars).
Definition: FieldBase.h:51
STL class.
bool isVariableLength() const
Return true if the field is variable-length (each record can have a different size array)...
Definition: FieldBase.h:148
int getElementCount() const
Return the number of subfield elements (equal to the size of the array), or 0 for a variable-length a...
Definition: FieldBase.h:141
int getSize() const
Return the maximum length of the string, including a null terminator (equal to the number of subfield...
Definition: FieldBase.h:266
FieldBase(int size=0)
Construct a FieldBase with the given size.
Definition: FieldBase.h:124
A base class for image defects.
Reference getReference(Element *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:78
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:81
int getElementCount() const
Return the number of subfield elements (equal to the size of the string, including a null terminator)...
Definition: FieldBase.h:262
int indexCovariance(int i, int j)
Defines the ordering of packed covariance matrices.
Definition: FieldBase.h:33
Tag types used to declare specialized field types.
Definition: misc.h:32
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement BaseRecord::set.
Definition: FieldBase.h:87
ndarray::Array< U const, 1, 1 > Value
the type returned by BaseRecord::get
Definition: FieldBase.h:101
T Element
the type of subfields (the same as the type itself for scalars)
Definition: FieldBase.h:48
#define LSST_EXCEPT(type,...)
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:45
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:84
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:272
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:198
char * Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:229
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:278
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:289
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:187
m
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:158
std::string Value
the type returned by BaseRecord::get
Definition: FieldBase.h:226
T const & ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:47
char Element
the type of subfields and array elements
Definition: FieldBase.h:234
STL class.
T & Reference
the type returned by BaseRecord::operator[] (non-const)
Definition: FieldBase.h:46
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:72