LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
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 
179  explicit SchemaMapper(Schema const & input);
180 
182  SchemaMapper(SchemaMapper const & other);
183 
185  SchemaMapper & operator=(SchemaMapper const & other);
186 
196  static std::vector<SchemaMapper> join(
197  std::vector<Schema> const & inputs,
198  std::vector<std::string> const & prefixes = std::vector<std::string>()
199  );
200 
201 private:
202 
203  template <typename Predicate>
205 
206  template <typename T>
207  void operator()(SchemaItem<T> const & item) const {
208  if (predicate(item)) mapper->addMapping(item.key, doReplace);
209  }
210 
211  AddMappingsWhere(SchemaMapper * mapper_, Predicate predicate_, bool doReplace_) :
212  mapper(mapper_), predicate(predicate_), doReplace(doReplace_) {}
213 
215  Predicate predicate;
216  bool doReplace;
217  };
218 
220 
221  boost::scoped_ptr<Impl> _impl;
222 };
223 
224 template <typename Predicate>
225 void SchemaMapper::addMappingsWhere(Predicate predicate, bool doReplace) {
226  _impl->_input.forEach(AddMappingsWhere<Predicate>(this, predicate, doReplace));
227 }
228 
229 }}} // namespace lsst::afw::table
230 
231 #endif // !AFW_TABLE_SchemaMapper_h_INCLUDED
void forEach(F func) const
Call the given functor for each key pair in the mapper.
Definition: SchemaMapper.h:141
Defines the fields and offsets for a table.
Definition: Schema.h:46
A functor-wrapper used in the implementation of SchemaMapper::forEach.
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.
Definition: SchemaMapper.h:29
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Definition: SchemaMapper.h:26
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
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:33
A private implementation class to hide the messy details of SchemaMapper.
AddMappingsWhere(SchemaMapper *mapper_, Predicate predicate_, bool doReplace_)
Definition: SchemaMapper.h:211
SchemaMapper & operator=(SchemaMapper const &other)
Assignement (copy-on-write).
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.
A description of a field in a table.
Definition: Field.h:22
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.
void addMappingsWhere(Predicate predicate, bool doReplace=true)
Add mappings for all fields that match criteria defined by a predicate.
Definition: SchemaMapper.h:225
void operator()(SchemaItem< T > const &item) const
Definition: SchemaMapper.h:207
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.
detail::SchemaMapperImpl Impl
Definition: SchemaMapper.h:219
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
boost::scoped_ptr< Impl > _impl
Definition: SchemaMapper.h:221
Schema const getInputSchema() const
Return the input schema (copy-on-write).
Definition: SchemaMapper.h:23
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