lsst.afw  13.0-100-ge0459d59f+2
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() {}
60  FieldBase(int) {
61  throw LSST_EXCEPT(lsst::pex::exceptions::LogicError,
62  "Constructor disabled (this Field type is not sized).");
63  }
64 
65 protected:
67  static FieldBase makeDefault() { return FieldBase(); }
68 
70  void stream(std::ostream &os) const {}
71 
73  Reference getReference(Element *p, ndarray::Manager::Ptr const &) const { return *p; }
74 
76  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
77 
79  Value getValue(Element const *p, ndarray::Manager::Ptr const &) const { return *p; }
80 
82  void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const { *p = v; }
83 };
84 
94 template <typename U>
95 struct FieldBase<Array<U> > {
96  typedef ndarray::Array<U const, 1, 1> Value;
97 
99  typedef ndarray::ArrayRef<U, 1, 1> Reference;
100 
102  typedef ndarray::ArrayRef<U const, 1, 1> ConstReference;
103 
104  typedef U Element;
105 
119  FieldBase(int size = 0) : _size(size) {
120  if (size < 0)
121  throw LSST_EXCEPT(lsst::pex::exceptions::LogicError,
122  "A non-negative size must be provided when constructing an array field.");
123  }
124 
126  static std::string getTypeString();
127 
130  int getElementCount() const { return _size; }
131 
134  int getSize() const { return _size; }
135 
137  bool isVariableLength() const { return _size == 0; }
138 
139 protected:
141  static FieldBase makeDefault() { return FieldBase(0); }
142 
144  void stream(std::ostream &os) const { os << ", size=" << _size; }
145 
147  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const {
148  if (isVariableLength()) {
149  return reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p)->deep();
150  }
151  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
152  }
153 
155  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const {
156  if (isVariableLength()) {
157  return reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p)->deep();
158  }
159  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
160  }
161 
163  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const {
164  if (isVariableLength()) {
165  return *reinterpret_cast<ndarray::Array<Element, 1, 1> const *>(p);
166  }
167  return ndarray::external(p, ndarray::makeVector(_size), ndarray::ROW_MAJOR, m);
168  }
169 
176  void setValue(Element *p, ndarray::Manager::Ptr const &,
177  ndarray::Array<Element, 1, 1> const &value) const {
178  if (isVariableLength()) {
179  *reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p) = value;
180  } else {
181  setValueDeep(p, value);
182  }
183  }
184 
186  template <typename Derived>
187  void setValue(Element *p, ndarray::Manager::Ptr const &,
188  ndarray::ExpressionBase<Derived> const &value) const {
189  if (isVariableLength()) {
190  throw LSST_EXCEPT(
191  lsst::pex::exceptions::LogicError,
192  "Assignment to a variable-length array must use a non-const array of the correct type.");
193  }
194  setValueDeep(p, value);
195  }
196 
197 private:
198  template <typename Derived>
199  void setValueDeep(Element *p, ndarray::ExpressionBase<Derived> const &value) const {
200  if (value.template getSize<0>() != static_cast<std::size_t>(_size)) {
201  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
202  "Incorrect size in array field assignment.");
203  }
204  for (int i = 0; i < _size; ++i) p[i] = value[i];
205  }
206 
207  int _size;
208 };
209 
213 template <>
214 struct FieldBase<std::string> {
215  typedef std::string Value;
216 
218  typedef char *Reference;
219 
221  typedef char const *ConstReference;
222 
223  typedef char Element;
224 
238  FieldBase(int size = -1);
239 
241  static std::string getTypeString();
242 
245  int getElementCount() const { return _size; }
246 
249  int getSize() const { return _size; }
250 
252  bool isVariableLength() const { return _size == 0; }
253 protected:
255  static FieldBase makeDefault() { return FieldBase(0); }
256 
258  void stream(std::ostream &os) const { os << ", size=" << _size; }
259 
261  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const {
262  if (isVariableLength()) {
263  // Can't be done until C++17, which allows read/write access to std::string's internal buffer
264  throw LSST_EXCEPT(lsst::pex::exceptions::LogicError,
265  "non-const operator[] not supported for variable-length strings");
266  } else {
267  return p;
268  }
269  }
270 
272  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const {
273  if (isVariableLength()) {
274  return reinterpret_cast<std::string const *>(p)->data();
275  } else {
276  return p;
277  }
278  }
279 
281  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const;
282 
288  void setValue(Element *p, ndarray::Manager::Ptr const &, std::string const &value) const;
289 
290 private:
291  int _size;
292 };
293 }
294 }
295 } // namespace lsst::afw::table
296 
297 #endif // !AFW_TABLE_FieldBase_h_INCLUDED
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:141
Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:163
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:155
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:70
bool isVariableLength() const
Return true if the field is variable-length (each record can have a different size array)...
Definition: FieldBase.h:252
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:258
U Element
the type of subfields and array elements
Definition: FieldBase.h:104
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:134
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:144
ndarray::ArrayRef< U, 1, 1 > Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:99
char const * ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:221
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:102
int getElementCount() const
Return the number of subfield elements (always one for scalars).
Definition: FieldBase.h:51
bool isVariableLength() const
Return true if the field is variable-length (each record can have a different size array)...
Definition: FieldBase.h:137
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:130
int getSize() const
Return the maximum length of the string, including a null terminator (equal to the number of subfield...
Definition: FieldBase.h:249
FieldBase(int size=0)
Construct a FieldBase with the given size.
Definition: FieldBase.h:119
A base class for image defects.
Definition: cameraGeom.dox:3
Reference getReference(Element *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:73
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:76
int getElementCount() const
Return the number of subfield elements (equal to the size of the string, including a null terminator)...
Definition: FieldBase.h:245
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:82
ndarray::Array< U const, 1, 1 > Value
the type returned by BaseRecord::get
Definition: FieldBase.h:96
T Element
the type of subfields (the same as the type itself for scalars)
Definition: FieldBase.h:48
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:79
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:255
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:187
char * Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:218
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:261
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (const).
Definition: FieldBase.h:272
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:176
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement BaseRecord::operator[] (non-const).
Definition: FieldBase.h:147
std::string Value
the type returned by BaseRecord::get
Definition: FieldBase.h:215
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:223
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:67