lsst.afw  15.0-33-g5c41f601f+4
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/fits.h"
11 #include "lsst/afw/fitsDefaults.h"
12 #include "lsst/afw/table/Key.h"
13 #include "lsst/afw/table/Field.h"
15 #include "lsst/afw/table/Flag.h"
17 
18 namespace lsst {
19 namespace afw {
20 namespace table {
21 
22 class SubSchema;
23 class BaseRecord;
24 
48 class Schema {
49  typedef detail::SchemaImpl Impl;
50 
51 public:
52  // This variable is defined in SchemaImpl, but is replicated here as
53  // so that it is available to Python.
55 
63  EQUAL_KEYS = 0x01,
64  EQUAL_NAMES = 0x02,
65  EQUAL_DOCS = 0x04,
66  EQUAL_UNITS = 0x08,
67  EQUAL_FIELDS = 0x0F,
68  EQUAL_ALIASES = 0x10,
69  IDENTICAL = 0x1F
70  };
71 
73  std::string join(std::string const& a, std::string const& b) const;
75  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
76  return join(join(a, b), c);
77  }
79  std::string const& d) const {
80  return join(join(a, b), join(c, d));
81  }
83 
91  template <typename T>
92  SchemaItem<T> find(std::string const& name) const;
93 
102  template <typename T>
103  SchemaItem<T> find(Key<T> const& key) const;
104 
113  template <typename F>
114  void findAndApply(std::string const& name, F&& func) const {
115  _impl->findAndApply(_aliases->apply(name), std::forward<F>(func));
116  }
117 
129  SubSchema operator[](std::string const& name) const;
130 
143  std::set<std::string> getNames(bool topOnly = false) const;
144 
146  int getRecordSize() const { return _impl->getRecordSize(); }
147 
149  int getFieldCount() const { return _impl->getFieldCount(); }
150 
152  int getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
153 
155  int getNonFlagFieldCount() const { return _impl->getNonFlagFieldCount(); }
156 
167  template <typename T>
168  Key<T> addField(Field<T> const& field, bool doReplace = false);
169 
177  template <typename T>
178  Key<T> addField(std::string const& name, std::string const& doc, std::string const& units = "",
179  FieldBase<T> const& base = FieldBase<T>(), bool doReplace = false) {
180  return addField(Field<T>(name, doc, units, base), doReplace);
181  }
182 
190  template <typename T>
191  Key<T> addField(std::string const& name, std::string const& doc, FieldBase<T> const& base,
192  bool doReplace = false) {
193  return addField(Field<T>(name, doc, base), doReplace);
194  }
195 
197  template <typename T>
198  void replaceField(Key<T> const& key, Field<T> const& field);
199 
209  template <typename F>
210  void forEach(F&& func) const {
211  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
212  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
213  }
214 
216 
223  bool operator==(Schema const& other) const { return compare(other, EQUAL_KEYS); }
224  bool operator!=(Schema const& other) const { return !this->operator==(other); }
226 
236  int compare(Schema const& other, int flags = EQUAL_KEYS) const;
237 
244  int contains(Schema const& other, int flags = EQUAL_KEYS) const;
245 
252  template <typename T>
253  int contains(SchemaItem<T> const& item, int flags = EQUAL_KEYS) const;
254 
264  std::shared_ptr<AliasMap> getAliasMap() const { return _aliases; }
265 
275 
277  void disconnectAliases();
278 
280  Schema();
281 
283  Schema(Schema const& other);
284  Schema(Schema&& other);
285 
286  Schema& operator=(Schema const& other);
287  Schema& operator=(Schema&& other);
288  ~Schema();
289 
295  static Schema readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU);
296  static Schema readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU);
297  static Schema readFits(fits::Fits& fitsfile);
298 
304  static Schema fromFitsMetadata(daf::base::PropertyList& header, bool stripMetadata = true);
305 
308 
310  daf::base::Citizen& getCitizen() { return *_impl; }
311 
312 private:
313  friend class detail::Access;
314  friend class SubSchema;
315 
317  void _edit();
318 
319  std::shared_ptr<Impl> _impl;
320  std::shared_ptr<AliasMap> _aliases;
321 };
322 
355 class SubSchema {
356  typedef detail::SchemaImpl Impl;
357 
358 public:
360  std::string join(std::string const& a, std::string const& b) const;
362  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
363  return join(join(a, b), c);
364  }
365  std::string join(std::string const& a, std::string const& b, std::string const& c,
366  std::string const& d) const {
367  return join(join(a, b), join(c, d));
368  }
370 
372  template <typename T>
373  SchemaItem<T> find(std::string const& name) const;
374 
383  template <typename F>
384  void findAndApply(std::string const& name, F&& func) const {
385  _impl->findAndApply(_aliases->apply(join(_name, name)), std::forward<F>(func));
386  }
387 
399  template <typename F>
400  void apply(F&& func) const {
401  _impl->findAndApply(_aliases->apply(_name), std::forward<F>(func));
402  }
403 
405  SubSchema operator[](std::string const& name) const;
406 
408  std::string const& getPrefix() const { return _name; }
409 
416  std::set<std::string> getNames(bool topOnly = false) const;
417 
425  template <typename T>
426  operator Key<T>() const {
427  return _impl->find<T>(_aliases->apply(_name)).key;
428  }
429 
437  template <typename T>
438  operator Field<T>() const {
439  return _impl->find<T>(_aliases->apply(_name)).field;
440  }
441 
442 private:
443  friend class Schema;
444 
446 
447  std::shared_ptr<Impl> _impl;
448  std::shared_ptr<AliasMap> _aliases;
450 };
451 
452 inline SubSchema Schema::operator[](std::string const& name) const {
453  return SubSchema(_impl, _aliases, name);
454 }
455 } // namespace table
456 } // namespace afw
457 } // namespace lsst
458 
459 #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:310
Defines the fields and offsets for a table.
Definition: Schema.h:48
friend class SubSchema
Definition: Schema.h:314
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Definition: Schema.cc:713
Schema()
Construct an empty Schema.
Definition: Schema.cc:608
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:146
A proxy type for name lookups in a Schema.
Definition: Schema.h:355
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:62
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
Definition: Schema.cc:697
Keys have the same types offsets, and sizes.
Definition: Schema.h:63
std::string const & _name
Definition: Mask.cc:569
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.
Definition: Schema.cc:640
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Definition: Schema.cc:636
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:720
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Definition: Schema.cc:618
Schemas have identical AliasMaps.
Definition: Schema.h:68
void findAndApply(std::string const &name, F &&func) const
Find a nested SchemaItem by name and run a functor on it.
Definition: Schema.h:384
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:75
ItemVariant const * other
Definition: Schema.cc:55
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:400
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:224
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:408
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:210
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:178
Fields have the same units (ordered).
Definition: Schema.h:66
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:655
Fields have the same names (ordered).
Definition: Schema.h:64
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:191
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:678
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
table::Key< int > b
A description of a field in a table.
Definition: Field.h:24
Key< U > key
Definition: Schema.cc:280
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:152
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:362
int getFieldCount() const
The total number of fields.
Definition: Schema.h:149
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:53
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:264
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
Definition: Schema.cc:742
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:452
static int const VERSION
Definition: Schema.h:54
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:223
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
Definition: Schema.cc:673
table::Key< int > a
std::ostream * os
Definition: Schema.cc:737
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:78
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:365
Fields have the same documentation (ordered).
Definition: Schema.h:65
STL class.
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Definition: Schema.cc:652
Schema & operator=(Schema const &other)
Fields are identical (but aliases may not be).
Definition: Schema.h:67
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.cc:667
table::Key< int > field
Definition: ApCorrMap.cc:72
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:155
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
Everything is the same.
Definition: Schema.h:69
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:114
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27