LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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 { namespace afw { namespace table {
10 
11 class BaseRecord;
12 
19 class SchemaMapper {
20 public:
21 
23  Schema const getInputSchema() const { return _impl->_input; }
24 
26  Schema const getOutputSchema() const { return _impl->_output; }
27 
29  Schema & editOutputSchema() { return _impl->_output; }
30 
32  template <typename T>
33  Key<T> addOutputField(Field<T> const & newField, bool doReplace=false) {
34  return _impl->_output.addField(newField, doReplace);
35  }
36 
47  template <typename T>
48  Key<T> addMapping(Key<T> const & inputKey, bool doReplace=false);
49 
60  template <typename T>
61  Key<T> addMapping(Key<T> const & inputKey, Field<T> const & outputField, bool doReplace=false);
62 
73  template <typename T>
74  Key<T> addMapping(Key<T> const & inputKey, std::string const & outputName, bool doReplace=true);
75 
88  template <typename Predicate>
89  void addMappingsWhere(Predicate predicate, bool doReplace=true);
90 
101  void addMinimalSchema(Schema const & minimal, bool doMap=true);
102 
113  static SchemaMapper removeMinimalSchema(Schema const & input, Schema const & minimal);
114 
116  void invert();
117 
119  template <typename T>
120  bool isMapped(Key<T> const & inputKey) const;
121 
123  template <typename T>
124  Key<T> getMapping(Key<T> const & inputKey) const;
125 
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 
200  static std::vector<SchemaMapper> join(
201  std::vector<Schema> const & inputs,
202  std::vector<std::string> const & prefixes = std::vector<std::string>()
203  );
204 
205 private:
206 
207  template <typename Predicate>
209 
210  template <typename T>
211  void operator()(SchemaItem<T> const & item) const {
212  if (predicate(item)) mapper->addMapping(item.key, doReplace);
213  }
214 
215  AddMappingsWhere(SchemaMapper * mapper_, Predicate predicate_, bool doReplace_) :
216  mapper(mapper_), predicate(predicate_), doReplace(doReplace_) {}
217 
219  Predicate predicate;
220  bool doReplace;
221  };
222 
224 
225  std::unique_ptr<Impl> _impl;
226 };
227 
228 template <typename Predicate>
229 void SchemaMapper::addMappingsWhere(Predicate predicate, bool doReplace) {
230  _impl->_input.forEach(AddMappingsWhere<Predicate>(this, predicate, doReplace));
231 }
232 
233 }}} // namespace lsst::afw::table
234 
235 #endif // !AFW_TABLE_SchemaMapper_h_INCLUDED
Defines the fields and offsets for a table.
Definition: Schema.h:44
A functor-wrapper used in the implementation of SchemaMapper::forEach.
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
void invert()
Swap the input and output schemas in-place.
A private implementation class to hide the messy details of SchemaMapper.
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
AddMappingsWhere(SchemaMapper *mapper_, Predicate predicate_, bool doReplace_)
Definition: SchemaMapper.h:215
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.
bool isMapped(Key< T > const &inputKey) const
Return true if the given input Key is mapped to an output Key.
Key< T > getMapping(Key< T > const &inputKey) const
Return the output Key corresponding to the given input Key, or raise NotFoundError.
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:33
A description of a field in a table.
Definition: Field.h:22
detail::SchemaMapperImpl Impl
Definition: SchemaMapper.h:223
void forEach(F &&func) const
Call the given functor for each key pair in the mapper.
Definition: SchemaMapper.h:141
void operator()(SchemaItem< T > const &item) const
Definition: SchemaMapper.h:211
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Definition: SchemaMapper.h:26
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.
Definition: SchemaMapper.h:29
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
void addMappingsWhere(Predicate predicate, bool doReplace=true)
Add mappings for all fields that match criteria defined by a predicate.
Definition: SchemaMapper.h:229
metadata input
std::unique_ptr< Impl > _impl
Definition: SchemaMapper.h:225
SchemaMapper()
Construct an empty mapper; useless unless you assign a fully-constructed one to it.
static SchemaMapper removeMinimalSchema(Schema const &input, Schema const &minimal)
Create a mapper by removing fields from the front of a schema.
SchemaMapper & operator=(SchemaMapper const &other)
Assignement (copy-on-write).
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.
Schema const getInputSchema() const
Return the input schema (copy-on-write).
Definition: SchemaMapper.h:23
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25