LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
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 
50 class Schema final {
51  typedef detail::SchemaImpl Impl;
52 
53 public:
54  // This variable is defined in SchemaImpl, but is replicated here as
55  // so that it is available to Python.
57 
65  EQUAL_KEYS = 0x01,
66  EQUAL_NAMES = 0x02,
67  EQUAL_DOCS = 0x04,
68  EQUAL_UNITS = 0x08,
69  EQUAL_FIELDS = 0x0F,
70  EQUAL_ALIASES = 0x10,
71  IDENTICAL = 0x1F
72  };
73 
75  std::string join(std::string const& a, std::string const& b) const;
77  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
78  return join(join(a, b), c);
79  }
81  std::string const& d) const {
82  return join(join(a, b), join(c, d));
83  }
85 
93  template <typename T>
94  SchemaItem<T> find(std::string const& name) const;
95 
104  template <typename T>
105  SchemaItem<T> find(Key<T> const& key) const;
106 
115  template <typename F>
116  void findAndApply(std::string const& name, F&& func) const {
117  _impl->findAndApply(_aliases->apply(name), std::forward<F>(func));
118  }
119 
131  SubSchema operator[](std::string const& name) const;
132 
145  std::set<std::string> getNames(bool topOnly = false) const;
146 
148  int getRecordSize() const { return _impl->getRecordSize(); }
149 
151  int getFieldCount() const { return _impl->getFieldCount(); }
152 
154  int getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
155 
157  int getNonFlagFieldCount() const { return _impl->getNonFlagFieldCount(); }
158 
169  template <typename T>
170  Key<T> addField(Field<T> const& field, bool doReplace = false);
171 
179  template <typename T>
180  Key<T> addField(std::string const& name, std::string const& doc, std::string const& units = "",
181  FieldBase<T> const& base = FieldBase<T>(), bool doReplace = false) {
182  return addField(Field<T>(name, doc, units, base), doReplace);
183  }
184 
192  template <typename T>
193  Key<T> addField(std::string const& name, std::string const& doc, FieldBase<T> const& base,
194  bool doReplace = false) {
195  return addField(Field<T>(name, doc, base), doReplace);
196  }
197 
199  template <typename T>
200  void replaceField(Key<T> const& key, Field<T> const& field);
201 
211  template <typename F>
212  void forEach(F&& func) const {
213  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
214  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
215  }
216 
218 
225  bool operator==(Schema const& other) const { return compare(other, EQUAL_KEYS); }
226  bool operator!=(Schema const& other) const { return !this->operator==(other); }
228 
230  std::size_t hash_value() const noexcept;
231 
241  int compare(Schema const& other, int flags = EQUAL_KEYS) const;
242 
249  int contains(Schema const& other, int flags = EQUAL_KEYS) const;
250 
257  template <typename T>
258  int contains(SchemaItem<T> const& item, int flags = EQUAL_KEYS) const;
259 
269  std::shared_ptr<AliasMap> getAliasMap() const { return _aliases; }
270 
280 
282  void disconnectAliases();
283 
285  Schema();
286 
288  Schema(Schema const& other);
289  Schema(Schema&& other);
290 
291  Schema& operator=(Schema const& other);
292  Schema& operator=(Schema&& other);
293  ~Schema();
294 
300  static Schema readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU);
301  static Schema readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU);
302  static Schema readFits(fits::Fits& fitsfile);
303 
309  static Schema fromFitsMetadata(daf::base::PropertyList& header, bool stripMetadata = true);
310 
313 
315  daf::base::Citizen& getCitizen() { return *_impl; }
316 
317 private:
318  friend class detail::Access;
319  friend class SubSchema;
320 
322  void _edit();
323 
324  std::shared_ptr<Impl> _impl;
325  std::shared_ptr<AliasMap> _aliases;
326 };
327 
360 class SubSchema final {
361  typedef detail::SchemaImpl Impl;
362 
363 public:
365  std::string join(std::string const& a, std::string const& b) const;
367  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
368  return join(join(a, b), c);
369  }
370  std::string join(std::string const& a, std::string const& b, std::string const& c,
371  std::string const& d) const {
372  return join(join(a, b), join(c, d));
373  }
375 
377  template <typename T>
378  SchemaItem<T> find(std::string const& name) const;
379 
388  template <typename F>
389  void findAndApply(std::string const& name, F&& func) const {
390  _impl->findAndApply(_aliases->apply(join(_name, name)), std::forward<F>(func));
391  }
392 
404  template <typename F>
405  void apply(F&& func) const {
406  _impl->findAndApply(_aliases->apply(_name), std::forward<F>(func));
407  }
408 
410  SubSchema operator[](std::string const& name) const;
411 
413  std::string const& getPrefix() const { return _name; }
414 
421  std::set<std::string> getNames(bool topOnly = false) const;
422 
430  template <typename T>
431  operator Key<T>() const {
432  return _impl->find<T>(_aliases->apply(_name)).key;
433  }
434 
442  template <typename T>
443  operator Field<T>() const {
444  return _impl->find<T>(_aliases->apply(_name)).field;
445  }
446 
447 private:
448  friend class Schema;
449 
451 
452  std::shared_ptr<Impl> _impl;
453  std::shared_ptr<AliasMap> _aliases;
454  std::string _name;
455 };
456 
457 inline SubSchema Schema::operator[](std::string const& name) const {
458  return SubSchema(_impl, _aliases, name);
459 }
460 } // namespace table
461 } // namespace afw
462 } // namespace lsst
463 
464 namespace std {
465 template <>
466 struct hash<lsst::afw::table::Schema> {
469  size_t operator()(argument_type const& obj) const noexcept { return obj.hash_value(); }
470 };
471 } // namespace std
472 
473 #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:315
Defines the fields and offsets for a table.
Definition: Schema.h:50
friend class SubSchema
Definition: Schema.h:319
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:722
Schema()
Construct an empty Schema.
Definition: Schema.cc:609
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:148
A proxy type for name lookups in a Schema.
Definition: Schema.h:360
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:64
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
Definition: Schema.cc:698
Keys have the same types offsets, and sizes.
Definition: Schema.h:65
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
Basic LSST definitions.
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:641
table::Key< int > b
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Definition: Schema.cc:637
table::Key< int > a
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:729
STL namespace.
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Definition: Schema.cc:619
Schemas have identical AliasMaps.
Definition: Schema.h:70
void findAndApply(std::string const &name, F &&func) const
Find a nested SchemaItem by name and run a functor on it.
Definition: Schema.h:389
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:77
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:405
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:226
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:413
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:212
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:180
Fields have the same units (ordered).
Definition: Schema.h:68
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:656
Fields have the same names (ordered).
Definition: Schema.h:66
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:193
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:679
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
A description of a field in a table.
Definition: Field.h:24
table::Schema schema
Definition: Camera.cc:161
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:154
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:367
int getFieldCount() const
The total number of fields.
Definition: Schema.h:151
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:53
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Schema.cc:709
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:269
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
Definition: Schema.cc:751
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:457
static int const VERSION
Definition: Schema.h:56
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
Key< U > key
Definition: Schema.cc:281
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:225
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
Definition: Schema.cc:674
ItemVariant const * other
Definition: Schema.cc:56
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55
size_t operator()(argument_type const &obj) const noexcept
Definition: Schema.h:469
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:80
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:370
Fields have the same documentation (ordered).
Definition: Schema.h:67
STL class.
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Definition: Schema.cc:653
Schema & operator=(Schema const &other)
Fields are identical (but aliases may not be).
Definition: Schema.h:69
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:668
std::ostream * os
Definition: Schema.cc:746
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:157
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
Everything is the same.
Definition: Schema.h:71
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:116
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27