6 #include "boost/mpl/and.hpp" 7 #include "boost/mpl/bool.hpp" 8 #include "boost/iterator/transform_iterator.hpp" 9 #include "boost/iterator/filter_iterator.hpp" 10 #include "boost/preprocessor/seq/for_each.hpp" 11 #include "boost/preprocessor/tuple/to_seq.hpp" 29 inline char getDelimiter() {
return '_'; }
46 struct KeyHelper :
public boost::static_visitor<bool> {
47 explicit KeyHelper(ItemVariant
const *other_) :
other(other_) {}
50 bool operator()(SchemaItem<T>
const &a)
const {
51 SchemaItem<T>
const *b = boost::get<SchemaItem<T> >(
other);
52 return (b) && a.key == b->key;
59 struct NameHelper :
public boost::static_visitor<std::string const &> {
61 std::string const &operator()(SchemaItem<T>
const &a)
const {
62 return a.field.getName();
67 struct DocHelper :
public boost::static_visitor<std::string const &> {
69 std::string const &operator()(SchemaItem<T>
const &a)
const {
70 return a.field.getDoc();
75 struct UnitsHelper :
public boost::static_visitor<std::string const &> {
77 std::string const &operator()(SchemaItem<T>
const &a)
const {
78 return a.field.getUnits();
83 static bool compareKeys(ItemVariant
const &a, ItemVariant
const &b) {
84 return boost::apply_visitor(KeyHelper(&b), a);
87 static bool compareNames(ItemVariant
const &a, ItemVariant
const &b) {
88 return boost::apply_visitor(NameHelper(), a) == boost::apply_visitor(NameHelper(), b);
91 static bool compareDocs(ItemVariant
const &a, ItemVariant
const &b) {
92 return boost::apply_visitor(DocHelper(), a) == boost::apply_visitor(DocHelper(), b);
95 static bool compareUnits(ItemVariant
const &a, ItemVariant
const &b) {
96 return boost::apply_visitor(UnitsHelper(), a) == boost::apply_visitor(UnitsHelper(), b);
117 template <
typename T>
118 inline int findNamedSubfield(
122 if (name.
size() <= item.field.getName().size())
return -1;
125 name.
compare(0, item.field.getName().size(), item.field.getName()) == 0 &&
126 name[item.field.getName().size()] ==
delimiter) {
127 int const position = item.field.getName().size() + 1;
128 int const size = name.
size() - position;
129 int const nElements = item.field.getElementCount();
130 for (
int i = 0; i < nElements; ++i) {
131 if (name.
compare(position, size, Key<T>::subfields[i]) == 0) {
141 template <
typename T>
142 inline int findNamedSubfield(
143 SchemaItem<T>
const &item,
std::string const &name,
char delimiter,
151 template <
typename T,
typename U>
152 inline void makeSubfieldItem(
157 Field<U>(join(item.field.getName(), Key<T>::subfields[index],
delimiter),
158 item.field.getDoc(), item.field.getUnits())));
162 template <
typename T,
typename U>
163 inline void makeSubfieldItem(
172 template <
typename U>
173 struct ExtractItemByName :
public boost::static_visitor<> {
174 explicit ExtractItemByName(
std::string const &name_,
char delimiter_)
177 template <
typename T>
178 void operator()(SchemaItem<T>
const &item)
const {
183 typedef typename boost::mpl::and_<std::is_same<U, typename Field<T>::Element>,
184 boost::mpl::bool_<KeyBase<T>::HAS_NAMED_SUBFIELDS> >
::type 187 int n = findNamedSubfield(item, name, delimiter, (IsMatchPossible *)0);
189 if (n >= 0) makeSubfieldItem(item, n, delimiter,
result, (IsMatchPossible *)0);
200 template <
typename T>
202 NameMap::const_iterator i = _names.lower_bound(name);
203 if (i != _names.end()) {
204 if (i->first == name) {
207 return boost::get<SchemaItem<T>
const>(_items[i->second]);
208 }
catch (boost::bad_get &err) {
210 (boost::format(
"Field '%s' does not have the given type.") % name).str());
216 ExtractItemByName<T> extractor(name, getDelimiter());
217 while (i != _names.begin()) {
219 boost::apply_visitor(extractor, _items[i->second]);
220 if (extractor.result)
return *extractor.result;
223 (boost::format(
"Field or subfield with name '%s' not found with type '%s'.") % name %
237 template <
typename T,
typename U>
238 inline int findKeySubfield(
242 int n = (key.
getOffset() - item.
key.getOffset()) /
sizeof(U);
243 if (n >= 0 && n < item.
key.getElementCount()) {
251 template <
typename T,
typename U>
252 inline int findKeySubfield(
260 template <
typename U>
261 struct ExtractItemByKey :
public boost::static_visitor<> {
262 explicit ExtractItemByKey(
Key<U> const &key_,
char delimiter_) :
delimiter(delimiter_),
key(key_) {}
264 template <
typename T>
269 typedef typename boost::mpl::and_<std::is_same<U, typename Field<T>::Element>,
270 boost::mpl::bool_<KeyBase<T>::HAS_NAMED_SUBFIELDS> >
::type 273 int n = findKeySubfield(item, key, (IsMatchPossible *)0);
276 if (n >= 0) makeSubfieldItem(item, n, delimiter,
result, (IsMatchPossible *)0);
287 template <
typename T>
289 OffsetMap::const_iterator i = _offsets.lower_bound(key.
getOffset());
290 if (i != _offsets.end()) {
293 return boost::get<SchemaItem<T>
const>(_items[i->second]);
294 }
catch (boost::bad_get &err) {
300 ExtractItemByKey<T> extractor(key, getDelimiter());
301 while (i != _offsets.begin()) {
303 boost::apply_visitor(extractor, _items[i->second]);
304 if (extractor.result)
return *extractor.result;
308 (boost::format(
"Field or subfield with offset %d not found with type '%s'.") %
316 if (i != _flags.end()) {
317 if (i->first.first == key.
getOffset() && i->first.second == key.
getBit()) {
319 return boost::get<SchemaItem<Flag>
const>(_items[i->second]);
320 }
catch (boost::bad_get &err) {
322 (boost::format(
"Flag field with offset %d and bit %d not found.") %
330 (boost::format(
"Flag field with offset %d and bit %d not found.") % key.
getOffset() %
344 template <
typename T>
346 bool throwIfMissing =
true) {
347 SchemaImpl::OffsetMap::const_iterator i = offsets.
find(key.
getOffset());
348 if (i == offsets.
end()) {
349 if (throwIfMissing) {
351 (boost::format(
"Key of type %s with offset %d not found in Schema") %
363 Key<Flag> const &key,
bool throwIfMissing =
true) {
365 if (i == flags.
end()) {
366 if (throwIfMissing) {
369 (boost::format(
"Key of type Flag with offset %d and bit %d not found in Schema") %
381 template <
typename T>
383 NameMap::iterator j = _names.find(field.
getName());
385 if (j != _names.end()) {
388 item = boost::get<SchemaItem<T> >(&_items[j->second]);
389 if (!item || key != item->
key) {
392 (boost::format(
"Field with name '%s' already present in schema with a different key.") %
398 int index = findKey(_offsets, _flags, key);
399 item = boost::get<SchemaItem<T> >(&_items[index]);
402 (boost::format(
"Incorrect key type '%s'.") % key).str());
404 j = _names.find(item->
field.getName());
413 template <
typename T>
417 "Can only check whether item is in schema if flags & EQUAL_KEYS");
420 int index = findKey(_offsets, _flags, item.
key,
false);
422 cmpItem = boost::get<SchemaItem<T> >(&_items[index]);
425 flags &= ~
Schema::EQUAL_NAMES;
428 flags &= ~
Schema::EQUAL_DOCS;
431 flags &= ~
Schema::EQUAL_UNITS;
443 for (NameMap::const_iterator i = _names.begin(); i != _names.end(); ++i) {
445 if (sep == std::string::npos) {
448 result.
insert(result.
end(), i->first.substr(0, sep));
452 for (NameMap::const_iterator i = _names.begin(); i != _names.end(); ++i) {
462 for (NameMap::const_iterator i = _names.lower_bound(prefix); i != _names.end(); ++i) {
463 if (i->first.compare(0, prefix.
size(),
prefix) != 0)
break;
465 if (sep == std::string::npos) {
467 i->first.substr(prefix.
size() + 1, i->first.size() - prefix.
size()));
469 result.
insert(result.
end(), i->first.substr(prefix.
size() + 1, sep - prefix.
size() - 1));
473 for (NameMap::const_iterator i = _names.lower_bound(prefix); i != _names.end(); ++i) {
474 if (i->first.compare(0, prefix.
size(),
prefix) != 0)
break;
476 i->first.substr(prefix.
size() + 1, i->first.size() - prefix.
size() - 1));
482 template <
typename T>
484 if (
field.isVariableLength()) {
486 return addFieldImpl(
sizeof(ndarray::Array<T, 1, 1>), 1,
field, doReplace);
493 if (field.isVariableLength()) {
495 return addFieldImpl(
sizeof(
std::string), 1, field, doReplace);
502 template <
typename T>
511 if (!result.second) {
513 SchemaItem<Flag> *item = boost::get<SchemaItem<Flag> >(&_items[result.first->second]);
517 (boost::format(
"Cannot replace field with name '%s' because types differ.") %
524 (boost::format(
"Cannot replace field with name '%s' because sizes differ.") %
533 (boost::format(
"Field with name '%s' already present in schema.") % field.
getName())
537 if (_lastFlagField < 0 || _lastFlagBit >= ELEMENT_SIZE * 8) {
538 int padding = ELEMENT_SIZE - _recordSize % ELEMENT_SIZE;
539 if (padding != ELEMENT_SIZE) {
540 _recordSize += padding;
542 _lastFlagField = _recordSize;
550 _items.push_back(item);
555 template <
typename T>
556 Key<T> SchemaImpl::addFieldImpl(
int elementSize,
int elementCount,
Field<T> const &
field,
bool doReplace) {
559 if (!result.second) {
561 SchemaItem<T> *item = boost::get<SchemaItem<T> >(&_items[result.first->second]);
565 (boost::format(
"Cannot replace field with name '%s' because types differ.") %
575 (boost::format(
"Cannot replace field with name '%s' because sizes differ.") %
584 (boost::format(
"Field with name '%s' already present in schema.") % field.
getName())
588 int padding = elementSize - _recordSize % elementSize;
589 if (padding != elementSize) {
590 _recordSize += padding;
593 _recordSize += elementCount * elementSize;
595 _items.push_back(item);
642 return afw::table::join(a, b, getDelimiter());
645 void Schema::_edit() {
646 if (!_impl.unique()) {
654 template <
typename T>
657 _aliases->_apply(tmp);
658 return _impl->find<T>(tmp);
661 template <
typename T>
663 return _impl->find(key);
666 template <
typename T>
669 return _impl->addField(field, doReplace);
672 template <
typename T>
675 _impl->replaceField(key, field);
679 if (_impl == other._impl)
return flags;
680 if (_impl->getItems().size() < other._impl->getItems().size())
return 0;
683 for (Impl::ItemContainer::const_iterator i1 = _impl->getItems().begin(),
684 i2 = other._impl->getItems().begin();
685 i2 != other._impl->getItems().end(); ++i1, ++i2) {
686 if ((result &
EQUAL_KEYS) && !ItemFunctors::compareKeys(*i1, *i2)) result &= ~EQUAL_KEYS;
687 if ((result &
EQUAL_NAMES) && !ItemFunctors::compareNames(*i1, *i2)) result &= ~EQUAL_NAMES;
688 if ((result &
EQUAL_DOCS) && !ItemFunctors::compareDocs(*i1, *i2)) result &= ~EQUAL_DOCS;
689 if ((result &
EQUAL_UNITS) && !ItemFunctors::compareUnits(*i1, *i2)) result &= ~EQUAL_UNITS;
699 if (_impl->getItems().size() != other._impl->getItems().size()) {
708 template <
typename T>
710 return _impl->contains(item, flags);
715 aliases = std::make_shared<AliasMap>();
728 typedef void result_type;
730 template <
typename T>
732 *
os <<
" (" << item.
field <<
", " << item.
key <<
"),\n";
746 os <<
" '" << iter->first <<
"'->'" << iter->second <<
"'\n";
757 return afw::table::join(a, b, getDelimiter());
761 : _impl(impl), _aliases(aliases), _name(name) {}
763 template <
typename T>
765 return _impl->find<T>(_aliases->apply(
join(_name, name)));
782 #define INSTANTIATE_LAYOUT(r, data, elem) \ 783 template Key<elem> Schema::addField(Field<elem> const &, bool); \ 784 template SchemaItem<elem> Schema::find(std::string const &) const; \ 785 template SchemaItem<elem> Schema::find(Key<elem> const &) const; \ 786 template SchemaItem<elem> detail::SchemaImpl::find(std::string const &name) const; \ 787 template int Schema::contains(SchemaItem<elem> const &, int) const; \ 788 template void Schema::replaceField(Key<elem> const &, Field<elem> const &); \ 789 template SchemaItem<elem> SubSchema::find(std::string const &) const; Defines the fields and offsets for a table.
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Schema()
Construct an empty Schema.
A proxy type for name lookups in a Schema.
int getOffset() const noexcept
Return the offset (in bytes) of this field within a record.
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
Keys have the same types offsets, and sizes.
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Mapping class that holds aliases for a Schema.
static Key< T > makeKey(int offset)
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Schemas have identical AliasMaps.
boost::make_variant_over< ItemTypes >::type ItemVariant
A Boost.Variant type that can hold any one of the allowed SchemaItem types.
SubSchema operator[](std::string const &name) const
Return a nested proxy.
ItemVariant const * other
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
#define AFW_TABLE_FIELD_TYPE_TUPLE
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Fields have the same units (ordered).
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Fields have the same names (ordered).
A base class for image defects.
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Lifetime-management for memory that goes into FITS memory files.
static Key< typename Key< T >::Element > extractElement(KeyBase< T > const &kb, int n)
A description of a field in a table.
Tag types used to declare specialized field types.
int getBit() const
The index of this field's bit within the integer it shares with other Flag fields.
std::unique_ptr< SchemaItem< U > > result
std::set< std::string > getNames(bool topOnly=false) const
Return a set of nested names that start with the SubSchema's prefix.
A private implementation class to hide the messy details of Schema.
std::string const & getName() const noexcept
Return the name of the field.
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
#define LSST_EXCEPT(type,...)
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
A class used as a handle to a particular field in a table.
Key specialization for Flag.
#define INSTANTIATE_LAYOUT(r, data, elem)
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
FieldBase< T >::Element Element
Type used to store field data in the table (a field may have multiple elements).
int getElementCount() const noexcept
Return the number of subfield elements (always one for scalars).
int getElementCount() const
Return the number of subfield elements (always one for scalars).
int getOffset() const
Return the offset in bytes of the integer element that holds this field's bit.
static std::string getTypeString()
Return a string description of the field type.
Fields have the same documentation (ordered).
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Schema & operator=(Schema const &other)
Fields are identical (but aliases may not be).
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
SchemaItem< T > find(std::string const &name) const
Find a nested SchemaItem by name.
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
#define AFW_TABLE_FIELD_TYPE_N
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...