LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
SchemaMapper.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_SchemaMapper_h_INCLUDED
3 #define AFW_TABLE_SchemaMapper_h_INCLUDED
4 
5 #include <memory>
6 
8 
9 namespace lsst {
10 namespace afw {
11 namespace table {
12 
13 class BaseRecord;
14 
22 public:
24  Schema const getInputSchema() const { return _impl->_input; }
25 
27  Schema const getOutputSchema() const { return _impl->_output; }
28 
30  Schema& editOutputSchema() { return _impl->_output; }
31 
33  template <typename T>
34  Key<T> addOutputField(Field<T> const& newField, bool doReplace = false) {
35  return _impl->_output.addField(newField, doReplace);
36  }
37 
48  template <typename T>
49  Key<T> addMapping(Key<T> const& inputKey, bool doReplace = false);
50 
61  template <typename T>
62  Key<T> addMapping(Key<T> const& inputKey, Field<T> const& outputField, bool doReplace = false);
63 
74  template <typename T>
75  Key<T> addMapping(Key<T> const& inputKey, std::string const& outputName, bool doReplace = true);
76 
82  template <typename T>
83  Key<T> addMapping(Key<T> const& inputKey, char const* outputName, bool doReplace = true) = delete;
84 
97  template <typename Predicate>
98  void addMappingsWhere(Predicate predicate, bool doReplace = true);
99 
110  void addMinimalSchema(Schema const& minimal, bool doMap = true);
111 
122  static SchemaMapper removeMinimalSchema(Schema const& input, Schema const& minimal);
123 
125  void invert();
126 
128  template <typename T>
129  bool isMapped(Key<T> const& inputKey) const;
130 
132  template <typename T>
133  Key<T> getMapping(Key<T> const& inputKey) const;
134 
136 
149  template <typename F>
150  void forEach(F& func) const {
151  for (auto const & item : _impl->_map) {
152  std::visit([&func](auto const & pair) { func(pair.first, pair.second); }, item);
153  }
154  }
155  template <typename F>
156  void forEach(F const& func) const {
157  for (auto const & item : _impl->_map) {
158  std::visit([&func](auto const & pair) { func(pair.first, pair.second); }, item);
159  }
160  }
162 
164  explicit SchemaMapper();
165 
181  explicit SchemaMapper(Schema const& input, Schema const& output);
182 
200  explicit SchemaMapper(Schema const& input, bool shareAliasMap = false);
201 
203  SchemaMapper(SchemaMapper const& other);
204  SchemaMapper(SchemaMapper&& other);
205 
207  SchemaMapper& operator=(SchemaMapper const& other);
209 
211 
222  std::vector<Schema> const& inputs,
224 
225 private:
226  template <typename Predicate>
227  struct AddMappingsWhere {
228  template <typename T>
229  void operator()(SchemaItem<T> const& item) const {
230  if (predicate(item)) mapper->addMapping(item.key, doReplace);
231  }
232 
233  AddMappingsWhere(SchemaMapper* mapper_, Predicate predicate_, bool doReplace_)
234  : mapper(mapper_), predicate(predicate_), doReplace(doReplace_) {}
235 
237  Predicate predicate;
238  bool doReplace;
239  };
240 
241  using Impl = detail::SchemaMapperImpl;
242 
243  std::unique_ptr<Impl> _impl;
244 };
245 
246 template <typename Predicate>
247 void SchemaMapper::addMappingsWhere(Predicate predicate, bool doReplace) {
248  _impl->_input.forEach(AddMappingsWhere<Predicate>(this, predicate, doReplace));
249 }
250 } // namespace table
251 } // namespace afw
252 } // namespace lsst
253 
254 #endif // !AFW_TABLE_SchemaMapper_h_INCLUDED
SchemaMapper * mapper
Definition: SchemaMapper.cc:71
Schema minimal
A class used as a handle to a particular field in a table.
Definition: Key.h:53
Defines the fields and offsets for a table.
Definition: Schema.h:51
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:214
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.cc:479
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
SchemaMapper()
Construct an empty mapper; useless unless you assign a fully-constructed one to it.
Key< T > addOutputField(Field< T > const &newField, bool doReplace=false)
Add a new field to the output Schema that is not connected to the input Schema.
Definition: SchemaMapper.h:34
SchemaMapper & operator=(SchemaMapper const &other)
Assignment (copy-on-write).
void forEach(F &func) const
Call the given functor for each key pair in the mapper.
Definition: SchemaMapper.h:150
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.
Definition: SchemaMapper.h:30
Key< T > addMapping(Key< T > const &inputKey, bool doReplace=false)
Add a new field to the output Schema that is a copy of a field in the input Schema.
static SchemaMapper removeMinimalSchema(Schema const &input, Schema const &minimal)
Create a mapper by removing fields from the front of a schema.
static std::vector< SchemaMapper > join(std::vector< Schema > const &inputs, std::vector< std::string > const &prefixes=std::vector< std::string >())
Combine a sequence of schemas into one, creating a SchemaMapper for each.
void forEach(F const &func) const
Definition: SchemaMapper.h:156
Key< T > getMapping(Key< T > const &inputKey) const
Return the output Key corresponding to the given input Key, or raise NotFoundError.
bool isMapped(Key< T > const &inputKey) const
Return true if the given input Key is mapped to an output Key.
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
void invert()
Swap the input and output schemas in-place.
Key< T > addMapping(Key< T > const &inputKey, char const *outputName, bool doReplace=true)=delete
This deliberately deleted overload ensures we don't accidentally cast string literals to bool.
Schema const getInputSchema() const
Return the input schema (copy-on-write).
Definition: SchemaMapper.h:24
void addMappingsWhere(Predicate predicate, bool doReplace=true)
Add mappings for all fields that match criteria defined by a predicate.
Definition: SchemaMapper.h:247
class[[deprecated("Removed with no replacement (but see lsst::afw::image::TransmissionCurve). Will be " "removed after v22.")]] FilterProperty final
Describe the properties of a Filter (e.g.
Definition: Filter.h:53
A base class for image defects.
A description of a field in a table.
Definition: Field.h:24
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:22