LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
BaseRecord.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <cstring>
4 #include <iostream>
5 
6 #include "lsst/pex/exceptions.h"
9 
10 namespace lsst {
11 namespace afw {
12 namespace table {
13 
14 namespace {
15 
16 // A Schema Functor used to set floating point-fields to NaN and initialize variable-length arrays
17 // using placement new. All other fields are left alone, as they should already be zero.
18 struct RecordInitializer {
19  template <typename T>
20  static void fill(T *element, int size) {} // this matches all non-floating-point-element fields.
21 
22  static void fill(float *element, int size) {
23  std::fill(element, element + size, std::numeric_limits<float>::quiet_NaN());
24  }
25 
26  static void fill(double *element, int size) {
27  std::fill(element, element + size, std::numeric_limits<double>::quiet_NaN());
28  }
29 
30  static void fill(lsst::geom::Angle *element, int size) {
31  fill(reinterpret_cast<double *>(element), size);
32  }
33 
34  template <typename T>
35  void operator()(SchemaItem<T> const &item) const {
36  fill(reinterpret_cast<typename Field<T>::Element *>(data + item.key.getOffset()),
37  item.key.getElementCount());
38  }
39 
40  template <typename T>
41  void operator()(SchemaItem<Array<T> > const &item) const {
42  if (item.key.isVariableLength()) {
43  // Use placement new because the memory (for one ndarray) is already allocated
44  new (data + item.key.getOffset()) ndarray::Array<T, 1, 1>();
45  } else {
46  fill(reinterpret_cast<typename Field<T>::Element *>(data + item.key.getOffset()),
47  item.key.getElementCount());
48  }
49  }
50 
51  void operator()(SchemaItem<std::string> const &item) const {
52  if (item.key.isVariableLength()) {
53  // Use placement new because the memory (for one std::string) is already allocated
54  new (reinterpret_cast<std::string *>(data + item.key.getOffset())) std::string();
55  } else {
56  fill(reinterpret_cast<char *>(data + item.key.getOffset()), item.key.getElementCount());
57  }
58  }
59 
60  void operator()(SchemaItem<Flag> const &item) const {} // do nothing for Flag fields; already 0
61 
62  char *data;
63 };
64 
65 // A Schema::forEach and SchemaMapper::forEach functor that copies data from one record to another.
66 struct CopyValue {
67  template <typename U>
68  void operator()(Key<U> const& inputKey, Key<U> const& outputKey) const {
69  typename Field<U>::Element const* inputElem = _inputRecord->getElement(inputKey);
70  std::copy(inputElem, inputElem + inputKey.getElementCount(), _outputRecord->getElement(outputKey));
71  }
72 
73  template <typename U>
74  void operator()(Key<Array<U> > const& inputKey, Key<Array<U> > const& outputKey) const {
75  if (inputKey.isVariableLength() != outputKey.isVariableLength()) {
77  "At least one input array field is variable-length"
78  " and the correponding output is not, or vice-versa");
79  }
80  if (inputKey.isVariableLength()) {
81  ndarray::Array<U, 1, 1> value = ndarray::copy(_inputRecord->get(inputKey));
82  _outputRecord->set(outputKey, value);
83  return;
84  }
85  typename Field<U>::Element const* inputElem = _inputRecord->getElement(inputKey);
86  std::copy(inputElem, inputElem + inputKey.getElementCount(), _outputRecord->getElement(outputKey));
87  }
88 
89  void operator()(Key<std::string> const& inputKey, Key<std::string> const& outputKey) const {
90  if (inputKey.isVariableLength() != outputKey.isVariableLength()) {
92  "At least one input string field is variable-length "
93  "and the correponding output is not, or vice-versa");
94  }
95  if (inputKey.isVariableLength()) {
96  std::string value = _inputRecord->get(inputKey);
97  _outputRecord->set(outputKey, value);
98  return;
99  }
100  char const* inputElem = _inputRecord->getElement(inputKey);
101  std::copy(inputElem, inputElem + inputKey.getElementCount(), _outputRecord->getElement(outputKey));
102  }
103 
104  void operator()(Key<Flag> const& inputKey, Key<Flag> const& outputKey) const {
105  _outputRecord->set(outputKey, _inputRecord->get(inputKey));
106  }
107 
108  template <typename U>
109  void operator()(SchemaItem<U> const& item) const {
110  (*this)(item.key, item.key);
111  }
112 
113  CopyValue(BaseRecord const* inputRecord, BaseRecord* outputRecord)
114  : _inputRecord(inputRecord), _outputRecord(outputRecord) {}
115 
116 private:
117  BaseRecord const* _inputRecord;
118  BaseRecord* _outputRecord;
119 };
120 
121 } // namespace
122 
124  if (this->getSchema() != other.getSchema()) {
125  throw LSST_EXCEPT(lsst::pex::exceptions::LogicError, "Unequal schemas in record assignment.");
126  }
127  this->getSchema().forEach(CopyValue(&other, this));
128  this->_assign(other); // let derived classes assign their own stuff
129 }
130 
132  if (!other.getSchema().contains(mapper.getInputSchema())) {
134  "Unequal schemas between input record and mapper.");
135  }
136  if (!this->getSchema().contains(mapper.getOutputSchema())) {
138  "Unequal schemas between output record and mapper.");
139  }
140  mapper.forEach(CopyValue(&other, this)); // use the functor we defined above
141  this->_assign(other); // let derived classes assign their own stuff
142 }
143 
145  _data(std::move(data.data)),
146  _table(std::move(data.table)),
147  _manager(std::move(data.manager))
148 {
149  RecordInitializer f = {reinterpret_cast<char *>(_data)};
150  _table->getSchema().forEach(f);
151 }
152 
154  getSchema().forEach([&os, this](auto const& item) {
155  os << item.field.getName() << ": " << this->get(item.key) << std::endl;
156  });
157 }
158 
160  record._stream(os);
161  return os;
162 }
163 
164 } // namespace table
165 } // namespace afw
166 } // namespace lsst
T copy(T... args)
friend std::ostream & operator<<(std::ostream &os, BaseRecord const &record)
Write the record&#39;s content out, one field on each line.
Definition: BaseRecord.cc:159
void forEach(F &&func) const
Call the given functor for each key pair in the mapper.
Definition: SchemaMapper.h:149
Helper struct that contains the information passed from BaseTable to BaseRecord at construction...
Definition: BaseTable.h:32
Schema const getInputSchema() const
Return the input schema (copy-on-write).
Definition: SchemaMapper.h:24
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Definition: SchemaMapper.h:27
T endl(T... args)
STL namespace.
ItemVariant const * other
Definition: Schema.cc:56
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
A class representing an angle.
Definition: Angle.h:127
virtual void _stream(std::ostream &os) const
Called by operator<<.
Definition: BaseRecord.cc:153
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:212
STL class.
double element[2]
Definition: BaseTable.cc:91
A base class for image defects.
char * data
Definition: BaseRecord.cc:62
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:679
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:80
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Base class for all records.
Definition: BaseRecord.h:31
BaseRecord(ConstructionToken const &, detail::RecordData &&data)
Construct a record with uninitialized data.
Definition: BaseRecord.cc:144
FieldBase< T >::Element Element
Type used to store field data in the table (a field may have multiple elements).
Definition: Field.h:26
Reports invalid arguments.
Definition: Runtime.h:66
T fill(T... args)
void assign(BaseRecord const &other)
Copy all field values from other to this, requiring that they have equal schemas. ...
Definition: BaseRecord.cc:123
std::ostream * os
Definition: Schema.cc:746
STL class.