lsst.afw  14.0-34-g2d9284d60+1
Schema.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_Schema_h_INCLUDED
3 #define AFW_TABLE_Schema_h_INCLUDED
4 
5 #include <memory>
6 #include <vector>
7 
8 #include "ndarray.h"
9 #include "lsst/base.h"
10 #include "lsst/afw/fitsDefaults.h"
11 #include "lsst/afw/table/Key.h"
12 #include "lsst/afw/table/Field.h"
14 #include "lsst/afw/table/Flag.h"
16 
17 namespace lsst {
18 namespace afw {
19 namespace table {
20 
21 class SubSchema;
22 class BaseRecord;
23 
47 class Schema {
48  typedef detail::SchemaImpl Impl;
49 
50 public:
51  // This variable is defined in SchemaImpl, but is replicated here as
52  // so that it is available to Python.
54 
62  EQUAL_KEYS = 0x01,
63  EQUAL_NAMES = 0x02,
64  EQUAL_DOCS = 0x04,
65  EQUAL_UNITS = 0x08,
66  EQUAL_FIELDS = 0x0F,
67  EQUAL_ALIASES = 0x10,
68  IDENTICAL = 0x1F
69  };
70 
72  std::string join(std::string const& a, std::string const& b) const;
74  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
75  return join(join(a, b), c);
76  }
78  std::string const& d) const {
79  return join(join(a, b), join(c, d));
80  }
82 
90  template <typename T>
91  SchemaItem<T> find(std::string const& name) const;
92 
101  template <typename T>
102  SchemaItem<T> find(Key<T> const& key) const;
103 
112  template <typename F>
113  void findAndApply(std::string const& name, F&& func) const {
114  _impl->findAndApply(_aliases->apply(name), std::forward<F>(func));
115  }
116 
128  SubSchema operator[](std::string const& name) const;
129 
142  std::set<std::string> getNames(bool topOnly = false) const;
143 
145  int getRecordSize() const { return _impl->getRecordSize(); }
146 
148  int getFieldCount() const { return _impl->getFieldCount(); }
149 
151  int getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
152 
154  int getNonFlagFieldCount() const { return _impl->getNonFlagFieldCount(); }
155 
166  template <typename T>
167  Key<T> addField(Field<T> const& field, bool doReplace = false);
168 
176  template <typename T>
177  Key<T> addField(std::string const& name, std::string const& doc, std::string const& units = "",
178  FieldBase<T> const& base = FieldBase<T>(), bool doReplace = false) {
179  return addField(Field<T>(name, doc, units, base), doReplace);
180  }
181 
189  template <typename T>
190  Key<T> addField(std::string const& name, std::string const& doc, FieldBase<T> const& base,
191  bool doReplace = false) {
192  return addField(Field<T>(name, doc, base), doReplace);
193  }
194 
196  template <typename T>
197  void replaceField(Key<T> const& key, Field<T> const& field);
198 
208  template <typename F>
209  void forEach(F&& func) const {
210  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
211  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
212  }
213 
215 
222  bool operator==(Schema const& other) const { return compare(other, EQUAL_KEYS); }
223  bool operator!=(Schema const& other) const { return !this->operator==(other); }
225 
235  int compare(Schema const& other, int flags = EQUAL_KEYS) const;
236 
243  int contains(Schema const& other, int flags = EQUAL_KEYS) const;
244 
251  template <typename T>
252  int contains(SchemaItem<T> const& item, int flags = EQUAL_KEYS) const;
253 
263  std::shared_ptr<AliasMap> getAliasMap() const { return _aliases; }
264 
274 
276  void disconnectAliases();
277 
279  Schema();
280 
282  Schema(Schema const& other);
283 
289  static Schema readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU);
290  static Schema readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU);
291  static Schema readFits(fits::Fits& fitsfile);
292 
298  static Schema fromFitsMetadata(daf::base::PropertyList& header, bool stripMetadata = true);
299 
301  friend std::ostream& operator<<(std::ostream& os, Schema const& schema);
302 
304  daf::base::Citizen& getCitizen() { return *_impl; }
305 
306 private:
307  friend class detail::Access;
308  friend class SubSchema;
309 
311  void _edit();
312 
313  std::shared_ptr<Impl> _impl;
314  std::shared_ptr<AliasMap> _aliases;
315 };
316 
349 class SubSchema {
350  typedef detail::SchemaImpl Impl;
351 
352 public:
354  std::string join(std::string const& a, std::string const& b) const;
356  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
357  return join(join(a, b), c);
358  }
359  std::string join(std::string const& a, std::string const& b, std::string const& c,
360  std::string const& d) const {
361  return join(join(a, b), join(c, d));
362  }
364 
366  template <typename T>
367  SchemaItem<T> find(std::string const& name) const;
368 
377  template <typename F>
378  void findAndApply(std::string const& name, F&& func) const {
379  _impl->findAndApply(_aliases->apply(join(_name, name)), std::forward<F>(func));
380  }
381 
393  template <typename F>
394  void apply(F&& func) const {
395  _impl->findAndApply(_aliases->apply(_name), std::forward<F>(func));
396  }
397 
399  SubSchema operator[](std::string const& name) const;
400 
402  std::string const& getPrefix() const { return _name; }
403 
410  std::set<std::string> getNames(bool topOnly = false) const;
411 
419  template <typename T>
420  operator Key<T>() const {
421  return _impl->find<T>(_aliases->apply(_name)).key;
422  }
423 
431  template <typename T>
432  operator Field<T>() const {
433  return _impl->find<T>(_aliases->apply(_name)).field;
434  }
435 
436 private:
437  friend class Schema;
438 
440 
441  std::shared_ptr<Impl> _impl;
442  std::shared_ptr<AliasMap> _aliases;
444 };
445 
446 inline SubSchema Schema::operator[](std::string const& name) const {
447  return SubSchema(_impl, _aliases, name);
448 }
449 }
450 }
451 } // namespace lsst::afw::table
452 
453 #endif // !AFW_TABLE_Schema_h_INCLUDED
daf::base::Citizen & getCitizen()
Get the Citizen corresponding to this Schema (SchemaImpl is what inherits from Citizen).
Definition: Schema.h:304
Defines the fields and offsets for a table.
Definition: Schema.h:47
friend class SubSchema
Definition: Schema.h:308
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:44
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Schema()
Construct an empty Schema.
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:145
A proxy type for name lookups in a Schema.
Definition: Schema.h:349
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:61
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.
Definition: Schema.h:62
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::string const & _name
Definition: Mask.cc:566
afw::table::Schema schema
Definition: GaussianPsf.cc:43
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
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.
Definition: Schema.h:67
void findAndApply(std::string const &name, F &&func) const
Find a nested SchemaItem by name and run a functor on it.
Definition: Schema.h:378
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:74
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:394
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:280
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:223
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:402
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:209
STL class.
Key< T > addField(std::string const &name, std::string const &doc, std::string const &units="", FieldBase< T > const &base=FieldBase< T >(), bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.h:177
Fields have the same units (ordered).
Definition: Schema.h:65
Fields have the same names (ordered).
Definition: Schema.h:63
A base class for image defects.
Key< T > addField(std::string const &name, std::string const &doc, FieldBase< T > const &base, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.h:190
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.
Definition: fits.h:102
table::Key< int > b
A description of a field in a table.
Definition: Field.h:24
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:151
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:356
int getFieldCount() const
The total number of fields.
Definition: Schema.h:148
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:53
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
STL class.
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:168
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:263
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:446
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
static int const VERSION
Definition: Schema.h:53
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:222
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
table::Key< int > a
T for_each(T... args)
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:77
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:359
Fields have the same documentation (ordered).
Definition: Schema.h:64
STL class.
Fields are identical (but aliases may not be).
Definition: Schema.h:66
table::Key< int > field
Definition: ApCorrMap.cc:72
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:154
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
Everything is the same.
Definition: Schema.h:68
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:113
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27