LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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 {
10 namespace afw {
11 namespace table {
12 
13 class BaseRecord;
14 
21 class SchemaMapper final {
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 
148  template <typename F>
149  void forEach(F&& func) const {
150  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
151  std::for_each(_impl->_map.begin(), _impl->_map.end(), visitor);
152  }
153 
155  explicit SchemaMapper();
156 
172  explicit SchemaMapper(Schema const& input, Schema const& output);
173 
191  explicit SchemaMapper(Schema const& input, bool shareAliasMap = false);
192 
194  SchemaMapper(SchemaMapper const& other);
195  SchemaMapper(SchemaMapper&& other);
196 
198  SchemaMapper& operator=(SchemaMapper const& other);
199  SchemaMapper& operator=(SchemaMapper&& other);
200 
201  ~SchemaMapper();
202 
213  std::vector<Schema> const& inputs,
215 
216 private:
217  template <typename Predicate>
218  struct AddMappingsWhere {
219  template <typename T>
220  void operator()(SchemaItem<T> const& item) const {
221  if (predicate(item)) mapper->addMapping(item.key, doReplace);
222  }
223 
224  AddMappingsWhere(SchemaMapper* mapper_, Predicate predicate_, bool doReplace_)
225  : mapper(mapper_), predicate(predicate_), doReplace(doReplace_) {}
226 
227  SchemaMapper* mapper;
228  Predicate predicate;
229  bool doReplace;
230  };
231 
233 
234  std::unique_ptr<Impl> _impl;
235 };
236 
237 template <typename Predicate>
238 void SchemaMapper::addMappingsWhere(Predicate predicate, bool doReplace) {
239  _impl->_input.forEach(AddMappingsWhere<Predicate>(this, predicate, doReplace));
240 }
241 } // namespace table
242 } // namespace afw
243 } // namespace lsst
244 
245 #endif // !AFW_TABLE_SchemaMapper_h_INCLUDED
SchemaMapper & operator=(SchemaMapper const &other)
Assignment (copy-on-write).
Defines the fields and offsets for a table.
Definition: Schema.h:50
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.
void forEach(F &&func) const
Call the given functor for each key pair in the mapper.
Definition: SchemaMapper.h:149
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.
T end(T... args)
STL class.
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.
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:238
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
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.
T begin(T... args)
Schema minimal
ItemVariant const * other
Definition: Schema.cc:56
void invert()
Swap the input and output schemas in-place.
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.
T for_each(T... args)
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:668
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27