1 #include "boost/preprocessor/seq/for_each.hpp"
2 #include "boost/preprocessor/tuple/to_seq.hpp"
14 struct SwapKeyPair :
public boost::static_visitor<> {
16 void operator()(
std::pair<Key<T>, Key<T> > &pair)
const {
26 struct KeyPairCompareEqual :
public boost::static_visitor<bool> {
28 bool operator()(
std::pair<Key<U>, Key<U> >
const &pair)
const {
29 return _target == pair.first;
33 return boost::apply_visitor(*
this, v);
36 KeyPairCompareEqual(Key<T>
const &
target) : _target(
target) {}
39 Key<T>
const &_target;
44 struct MapMinimalSchema {
46 void operator()(SchemaItem<U>
const &item)
const {
50 SchemaItem<U> inputItem =
_mapper->getInputSchema().find(item.key);
51 outputKey =
_mapper->addMapping(item.key);
52 }
catch (pex::exceptions::NotFoundError &) {
53 outputKey =
_mapper->addOutputField(item.field);
56 outputKey =
_mapper->addOutputField(item.field);
58 assert(outputKey == item.key);
61 explicit MapMinimalSchema(SchemaMapper *
mapper,
bool doMap) :
_mapper(
mapper), _doMap(doMap) {}
71 void operator()(SchemaItem<T>
const &item)
const {
72 Field<T>
field(
prefix + item.field.getName(), item.field.getDoc(), item.field.getUnits(), item.field);
76 explicit AddMapped(SchemaMapper *mapper_) :
mapper(mapper_) {}
85 void operator()(SchemaItem<T>
const &item)
const {
86 Field<T>
field(
prefix + item.field.getName(), item.field.getDoc(), item.field.getUnits(), item.field);
90 explicit AddUnmapped(SchemaMapper *mapper_) :
mapper(mapper_) {}
96 struct RemoveMinimalSchema {
98 void operator()(SchemaItem<T>
const &item)
const {
100 mapper->addMapping(item.key);
104 RemoveMinimalSchema(SchemaMapper *mapper_, Schema
const &minimal_) :
mapper(mapper_),
minimal(minimal_) {}
136 template <
typename T>
138 typename Impl::KeyPairMap::iterator i =
139 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
140 Field<T> inputField = _impl->_input.find(inputKey).field;
141 if (i != _impl->_map.end()) {
142 Key<T> const &outputKey = boost::get<std::pair<Key<T>,
Key<T> > >(*i).second;
143 _impl->_output.replaceField(outputKey, inputField);
146 Key<T> outputKey = _impl->_output.addField(inputField, doReplace);
152 template <
typename T>
154 typename Impl::KeyPairMap::iterator i =
155 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
156 if (i != _impl->_map.end()) {
157 Key<T> const &outputKey = boost::get<std::pair<Key<T>,
Key<T> > >(*i).second;
158 _impl->_output.replaceField(outputKey,
field);
161 Key<T> outputKey = _impl->_output.addField(
field, doReplace);
167 template <
typename T>
169 typename Impl::KeyPairMap::iterator i =
170 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
171 if (i != _impl->_map.end()) {
172 Key<T> const &outputKey = boost::get<std::pair<Key<T>,
Key<T> > >(*i).second;
175 _impl->_output.replaceField(outputKey,
field);
178 Field<T> inputField = _impl->_input.find(inputKey).field;
180 Key<T> outputKey = _impl->_output.addField(outputField, doReplace);
189 "Must add minimal schema to mapper before adding any other fields");
191 MapMinimalSchema f(
this, doMap);
203 std::swap(_impl->_input, _impl->_output);
204 std::for_each(_impl->_map.begin(), _impl->_map.end(), SwapKeyPair());
207 template <
typename T>
209 return std::count_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
212 template <
typename T>
214 typename Impl::KeyPairMap::iterator i =
215 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
216 if (i == _impl->_map.end()) {
219 return boost::get<std::pair<Key<T>,
Key<T> > >(*i).second;
228 (boost::format(
"prefix vector size (%d) must be the same as input vector size (%d)") %
239 AddMapped functor(&
result[j]);
240 if (!prefixes.
empty()) functor.prefix = prefixes[i];
241 inputs[i].forEach(functor);
243 AddUnmapped functor(&
result[j]);
244 if (!prefixes.
empty()) functor.prefix = prefixes[i];
245 inputs[i].forEach(functor);
254 #define INSTANTIATE_LAYOUTMAPPER(r, data, elem) \
255 template Key<elem> SchemaMapper::addOutputField(Field<elem> const &, bool); \
256 template Key<elem> SchemaMapper::addMapping(Key<elem> const &, bool); \
257 template Key<elem> SchemaMapper::addMapping(Key<elem> const &, Field<elem> const &, bool); \
258 template Key<elem> SchemaMapper::addMapping(Key<elem> const &, std::string const &, bool); \
259 template bool SchemaMapper::isMapped(Key<elem> const &) const; \
260 template Key<elem> SchemaMapper::getMapping(Key<elem> const &) const;
Key< Flag > const & target
#define LSST_EXCEPT(type,...)
ItemVariant const * other
std::unique_ptr< SchemaItem< U > > result
#define INSTANTIATE_LAYOUTMAPPER(r, data, elem)
A class used as a handle to a particular field in a table.
Defines the fields and offsets for a table.
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
A mapping between the keys of two Schemas, used to copy data between them.
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
SchemaMapper()
Construct an empty mapper; useless unless you assign a fully-constructed one to it.
SchemaMapper & operator=(SchemaMapper const &other)
Assignment (copy-on-write).
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified 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.
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.
Key< T > getMapping(Key< T > const &inputKey) const
Return the output Key corresponding to the given input Key, or raise NotFoundError.
bool isMapped(Key< T > const &inputKey) const
Return true if the given input Key is mapped to an output Key.
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.
A private implementation class to hide the messy details of SchemaMapper.
boost::make_variant_over< KeyPairTypes >::type KeyPairVariant
A Boost.Variant type that can hold any one of the allowed pair types.
BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_SCALAR, _, BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_SCALAR_FIELD_TYPE_N, AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE)) BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_ARRAY
A base class for image defects.
A description of a field in a table.
Field< T > copyRenamed(std::string const &newName) const
Return a new Field with a new name and other properties the same as this.
#define AFW_TABLE_FIELD_TYPE_N
#define AFW_TABLE_FIELD_TYPE_TUPLE