lsst.afw  14.0-27-gbdf48f848+1
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 
21 class SchemaMapper {
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 
89  template <typename Predicate>
90  void addMappingsWhere(Predicate predicate, bool doReplace = true);
91 
102  void addMinimalSchema(Schema const& minimal, bool doMap = true);
103 
114  static SchemaMapper removeMinimalSchema(Schema const& input, Schema const& minimal);
115 
117  void invert();
118 
120  template <typename T>
121  bool isMapped(Key<T> const& inputKey) const;
122 
124  template <typename T>
125  Key<T> getMapping(Key<T> const& inputKey) const;
126 
140  template <typename F>
141  void forEach(F&& func) const {
142  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
143  std::for_each(_impl->_map.begin(), _impl->_map.end(), visitor);
144  }
145 
147  explicit SchemaMapper();
148 
164  explicit SchemaMapper(Schema const& input, Schema const& output);
165 
183  explicit SchemaMapper(Schema const& input, bool shareAliasMap = false);
184 
186  SchemaMapper(SchemaMapper const& other);
187 
189  SchemaMapper& operator=(SchemaMapper const& other);
190 
201  std::vector<Schema> const& inputs,
203 
204 private:
205  template <typename Predicate>
206  struct AddMappingsWhere {
207  template <typename T>
208  void operator()(SchemaItem<T> const& item) const {
209  if (predicate(item)) mapper->addMapping(item.key, doReplace);
210  }
211 
212  AddMappingsWhere(SchemaMapper* mapper_, Predicate predicate_, bool doReplace_)
213  : mapper(mapper_), predicate(predicate_), doReplace(doReplace_) {}
214 
215  SchemaMapper* mapper;
216  Predicate predicate;
217  bool doReplace;
218  };
219 
221 
222  std::unique_ptr<Impl> _impl;
223 };
224 
225 template <typename Predicate>
226 void SchemaMapper::addMappingsWhere(Predicate predicate, bool doReplace) {
227  _impl->_input.forEach(AddMappingsWhere<Predicate>(this, predicate, doReplace));
228 }
229 }
230 }
231 } // namespace lsst::afw::table
232 
233 #endif // !AFW_TABLE_SchemaMapper_h_INCLUDED
Defines the fields and offsets for a table.
Definition: Schema.h:46
A functor-wrapper used in the implementation of SchemaMapper::forEach.
Key< T > getMapping(Key< T > const &inputKey) const
Return the output Key corresponding to the given input Key, or raise NotFoundError.
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.
void forEach(F &&func) const
Call the given functor for each key pair in the mapper.
Definition: SchemaMapper.h:141
SchemaMapper()
Construct an empty mapper; useless unless you assign a fully-constructed one to it.
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
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
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Definition: SchemaMapper.h:27
A private implementation class to hide the messy details of SchemaMapper.
SchemaMapper & operator=(SchemaMapper const &other)
Assignement (copy-on-write).
STL class.
A base class for image defects.
void addMappingsWhere(Predicate predicate, bool doReplace=true)
Add mappings for all fields that match criteria defined by a predicate.
Definition: SchemaMapper.h:226
A description of a field in a table.
Definition: Field.h:24
bool isMapped(Key< T > const &inputKey) const
Return true if the given input Key is mapped to an output Key.
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.
Definition: SchemaMapper.h:30
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.
STL class.
STL class.
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
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.
T for_each(T... args)
static SchemaMapper removeMinimalSchema(Schema const &input, Schema const &minimal)
Create a mapper by removing fields from the front of a schema.
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27