LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 "boost/scoped_ptr.hpp"
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 {
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  boost::scoped_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:46
A functor-wrapper used in the implementation of SchemaMapper::forEach.
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Definition: SchemaMapper.h:26
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.
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:23
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
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 private implementation class to hide the messy details of SchemaMapper.
void operator()(SchemaItem< T > const &item) const
Definition: SchemaMapper.h:211
SchemaMapper & operator=(SchemaMapper const &other)
Assignement (copy-on-write).
void addMappingsWhere(Predicate predicate, bool doReplace=true)
Add mappings for all fields that match criteria defined by a predicate.
Definition: SchemaMapper.h:229
A description of a field in a table.
Definition: Field.h:22
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.
Definition: SchemaMapper.h:29
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 class used as a handle to a particular field in a table.
Definition: fwd.h:44
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
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
Key< T > getMapping(Key< T > const &inputKey) const
Return the output Key corresponding to the given input Key, or raise NotFoundError.
void invert()
Swap the input and output schemas in-place.
static SchemaMapper removeMinimalSchema(Schema const &input, Schema const &minimal)
Create a mapper by removing fields from the front of a schema.
bool isMapped(Key< T > const &inputKey) const
Return true if the given input Key is mapped to an output Key.
boost::scoped_ptr< Impl > _impl
Definition: SchemaMapper.h:225
AddMappingsWhere(SchemaMapper *mapper_, Predicate predicate_, bool doReplace_)
Definition: SchemaMapper.h:215
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27