lsst.afw  13.0-71-gaa0624d+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
45 template <typename T>
46 struct FieldBase {
47  typedef T Value;
48  typedef T &Reference;
49  typedef T const &ConstReference;
50  typedef T Element;
51 
53  int getElementCount() const { return 1; }
54 
56  static std::string getTypeString();
57 
58  // Only the first of these constructors is valid for this specializations, but
59  // it's convenient to be able to instantiate both, since the other is used
60  // by other specializations.
61  FieldBase() {}
62  FieldBase(int) {
63  throw LSST_EXCEPT(lsst::pex::exceptions::LogicError,
64  "Constructor disabled (this Field type is not sized).");
65  }
66 
67 protected:
69  static FieldBase makeDefault() { 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 
96 template <typename U>
97 struct FieldBase<Array<U> > {
98  typedef ndarray::Array<U const, 1, 1> Value;
99 
101  typedef ndarray::ArrayRef<U, 1, 1> Reference;
102 
104  typedef ndarray::ArrayRef<U const, 1, 1> ConstReference;
105 
106  typedef U Element;
107 
121  FieldBase(int size = 0) : _size(size) {
122  if (size < 0)
123  throw LSST_EXCEPT(lsst::pex::exceptions::LogicError,
124  "A non-negative size must be provided when constructing an array field.");
125  }
126 
128  static std::string getTypeString();
129 
131  int getElementCount() const { return _size; }
132 
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 
174  void setValue(Element *p, ndarray::Manager::Ptr const &,
175  ndarray::Array<Element, 1, 1> const &value) const {
176  if (isVariableLength()) {
177  *reinterpret_cast<ndarray::Array<Element, 1, 1> *>(p) = value;
178  } else {
179  setValueDeep(p, value);
180  }
181  }
182 
184  template <typename Derived>
185  void setValue(Element *p, ndarray::Manager::Ptr const &,
186  ndarray::ExpressionBase<Derived> const &value) const {
187  if (isVariableLength()) {
188  throw LSST_EXCEPT(
189  lsst::pex::exceptions::LogicError,
190  "Assignment to a variable-length array must use a non-const array of the correct type.");
191  }
192  setValueDeep(p, value);
193  }
194 
195 private:
196  template <typename Derived>
197  void setValueDeep(Element *p, ndarray::ExpressionBase<Derived> const &value) const {
198  if (value.template getSize<0>() != static_cast<std::size_t>(_size)) {
199  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
200  "Incorrect size in array field assignment.");
201  }
202  for (int i = 0; i < _size; ++i) p[i] = value[i];
203  }
204 
205  int _size;
206 };
207 
211 template <>
212 struct FieldBase<std::string> {
213  typedef std::string Value;
214 
216  typedef char *Reference;
217 
219  typedef char const *ConstReference;
220 
221  typedef char Element;
222 
234  FieldBase(int size = -1);
235 
237  static std::string getTypeString();
238 
241  int getElementCount() const { return _size; }
242 
245  int getSize() const { return _size; }
246 
247 protected:
249  static FieldBase makeDefault() { return FieldBase(0); }
250 
252  void stream(std::ostream &os) const { os << ", size=" << _size; }
253 
255  Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const { return p; }
256 
258  ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const { return p; }
259 
261  Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const;
262 
264  void setValue(Element *p, ndarray::Manager::Ptr const &, std::string const &value) const;
265 
266 private:
267  int _size;
268 };
269 }
270 }
271 } // namespace lsst::afw::table
272 
273 #endif // !AFW_TABLE_FieldBase_h_INCLUDED
Value getValue(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::get.
Definition: FieldBase.h:163
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:141
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:46
int getElementCount() const
Return the number of subfield elements (equal to the size of the string, including a null terminator)...
Definition: FieldBase.h:241
int getSize() const
Return the size of the array (equal to the number of subfield elements).
Definition: FieldBase.h:134
void setValue(Element *p, ndarray::Manager::Ptr const &, ndarray::Array< Element, 1, 1 > const &value) const
Used to implement RecordBase::set; accepts only non-const arrays of the right type, and allows shallow assignment of variable-length arrays (which is the only kind of assignment allowed for variable-length arrays - if you want deep assignment, use operator[] to get a reference and assign to that.
Definition: FieldBase.h:174
U Element
the type of subfields and array elements
Definition: FieldBase.h:106
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:185
int getElementCount() const
Return the number of subfield elements (equal to the size of the array).
Definition: FieldBase.h:131
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:72
ndarray::ArrayRef< U, 1, 1 > Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:101
char const * ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:219
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:104
FieldBase(int size=0)
Construct a FieldBase with the given size.
Definition: FieldBase.h:121
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::operator[] (const).
Definition: FieldBase.h:78
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (const).
Definition: FieldBase.h:155
int getElementCount() const
Return the number of subfield elements (always one for scalars).
Definition: FieldBase.h:53
int indexCovariance(int i, int j)
Defines the ordering of packed covariance matrices.
Definition: FieldBase.h:33
int getSize() const
Return the maximum length of the string, including a null terminator (equal to the number of subfield...
Definition: FieldBase.h:245
Tag types used to declare specialized field types.
Definition: misc.h:32
bool isVariableLength() const
Return true if the field is variable-length (each record can have a different size array)...
Definition: FieldBase.h:137
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:144
ndarray::Array< U const, 1, 1 > Value
the type returned by BaseRecord::get
Definition: FieldBase.h:98
T Element
the type of subfields (the same as the type itself for scalars)
Definition: FieldBase.h:50
Reference getReference(Element *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::operator[] (non-const).
Definition: FieldBase.h:75
void stream(std::ostream &os) const
Defines how Fields are printed.
Definition: FieldBase.h:252
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:47
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:249
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (non-const).
Definition: FieldBase.h:255
char * Reference
the type returned by BaseRecord::operator[]
Definition: FieldBase.h:216
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement RecordBase::set.
Definition: FieldBase.h:84
std::string Value
the type returned by BaseRecord::get
Definition: FieldBase.h:213
T const & ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:49
char Element
the type of subfields and array elements
Definition: FieldBase.h:221
Reference getReference(Element *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (non-const).
Definition: FieldBase.h:147
T & Reference
the type returned by BaseRecord::operator[] (non-const)
Definition: FieldBase.h:48
static FieldBase makeDefault()
Needed to allow Keys to be default-constructed.
Definition: FieldBase.h:69
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &m) const
Used to implement RecordBase::operator[] (const).
Definition: FieldBase.h:258
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::get.
Definition: FieldBase.h:81
static std::string getTypeString()
Return a string description of the field type.