lsst.afw  15.0-17-g0b88e3da8+6
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::forEach and SchemaMapper::forEach functor that copies data from one record to another.
17 struct CopyValue {
18  template <typename U>
19  void operator()(Key<U> const& inputKey, Key<U> const& outputKey) const {
20  typename Field<U>::Element const* inputElem = _inputRecord->getElement(inputKey);
21  std::copy(inputElem, inputElem + inputKey.getElementCount(), _outputRecord->getElement(outputKey));
22  }
23 
24  template <typename U>
25  void operator()(Key<Array<U> > const& inputKey, Key<Array<U> > const& outputKey) const {
26  if (inputKey.isVariableLength() != outputKey.isVariableLength()) {
28  "At least one input array field is variable-length"
29  " and the correponding output is not, or vice-versa");
30  }
31  if (inputKey.isVariableLength()) {
32  ndarray::Array<U, 1, 1> value = ndarray::copy(_inputRecord->get(inputKey));
33  _outputRecord->set(outputKey, value);
34  return;
35  }
36  typename Field<U>::Element const* inputElem = _inputRecord->getElement(inputKey);
37  std::copy(inputElem, inputElem + inputKey.getElementCount(), _outputRecord->getElement(outputKey));
38  }
39 
40  void operator()(Key<std::string> const& inputKey, Key<std::string> const& outputKey) const {
41  if (inputKey.isVariableLength() != outputKey.isVariableLength()) {
43  "At least one input string field is variable-length "
44  "and the correponding output is not, or vice-versa");
45  }
46  if (inputKey.isVariableLength()) {
47  std::string value = _inputRecord->get(inputKey);
48  _outputRecord->set(outputKey, value);
49  return;
50  }
51  char const* inputElem = _inputRecord->getElement(inputKey);
52  std::copy(inputElem, inputElem + inputKey.getElementCount(), _outputRecord->getElement(outputKey));
53  }
54 
55  void operator()(Key<Flag> const& inputKey, Key<Flag> const& outputKey) const {
56  _outputRecord->set(outputKey, _inputRecord->get(inputKey));
57  }
58 
59  template <typename U>
60  void operator()(SchemaItem<U> const& item) const {
61  (*this)(item.key, item.key);
62  }
63 
64  CopyValue(BaseRecord const* inputRecord, BaseRecord* outputRecord)
65  : _inputRecord(inputRecord), _outputRecord(outputRecord) {}
66 
67 private:
68  BaseRecord const* _inputRecord;
69  BaseRecord* _outputRecord;
70 };
71 
72 } // anonymous
73 
75  if (this->getSchema() != other.getSchema()) {
76  throw LSST_EXCEPT(lsst::pex::exceptions::LogicError, "Unequal schemas in record assignment.");
77  }
78  this->getSchema().forEach(CopyValue(&other, this));
79  this->_assign(other); // let derived classes assign their own stuff
80 }
81 
83  if (!other.getSchema().contains(mapper.getInputSchema())) {
85  "Unequal schemas between input record and mapper.");
86  }
87  if (!this->getSchema().contains(mapper.getOutputSchema())) {
89  "Unequal schemas between output record and mapper.");
90  }
91  mapper.forEach(CopyValue(&other, this)); // use the functor we defined above
92  this->_assign(other); // let derived classes assign their own stuff
93 }
94 
95 
97  getSchema().forEach(
98  [&os, this](auto const & item) {
99  os << item.field.getName() << ": " << this->get(item.key) << std::endl;
100  }
101  );
102 }
103 
105  record._stream(os);
106  return os;
107 }
108 
109 
110 }
111 }
112 } // namespace lsst::afw::table
T copy(T... args)
void forEach(F &&func) const
Call the given functor for each key pair in the mapper.
Definition: SchemaMapper.h:141
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)
ItemVariant const * other
Definition: Schema.cc:55
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
virtual void _stream(std::ostream &os) const
Called by operator<<.
Definition: BaseRecord.cc:96
STL class.
A base class for image defects.
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:677
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:54
std::ostream & operator<<(std::ostream &os, BaseRecord const &record)
Definition: BaseRecord.cc:104
#define LSST_EXCEPT(type,...)
Base class for all records.
Definition: BaseRecord.h:31
FieldBase< T >::Element Element
Type used to store field data in the table (a field may have multiple elements).
Definition: Field.h:26
void assign(BaseRecord const &other)
Copy all field values from other to this, requiring that they have equal schemas. ...
Definition: BaseRecord.cc:74
std::ostream * os
Definition: Schema.cc:736
STL class.