lsst.afw  15.0-21-g91b8abf62
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  Schema(Schema && other);
284 
285  Schema &operator=(Schema const& other);
286  Schema &operator=(Schema && other);
287  ~Schema();
288 
294  static Schema readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU);
295  static Schema readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU);
296  static Schema readFits(fits::Fits& fitsfile);
297 
303  static Schema fromFitsMetadata(daf::base::PropertyList& header, bool stripMetadata = true);
304 
307 
309  daf::base::Citizen& getCitizen() { return *_impl; }
310 
311 private:
312  friend class detail::Access;
313  friend class SubSchema;
314 
316  void _edit();
317 
318  std::shared_ptr<Impl> _impl;
319  std::shared_ptr<AliasMap> _aliases;
320 };
321 
354 class SubSchema {
355  typedef detail::SchemaImpl Impl;
356 
357 public:
359  std::string join(std::string const& a, std::string const& b) const;
361  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
362  return join(join(a, b), c);
363  }
364  std::string join(std::string const& a, std::string const& b, std::string const& c,
365  std::string const& d) const {
366  return join(join(a, b), join(c, d));
367  }
369 
371  template <typename T>
372  SchemaItem<T> find(std::string const& name) const;
373 
382  template <typename F>
383  void findAndApply(std::string const& name, F&& func) const {
384  _impl->findAndApply(_aliases->apply(join(_name, name)), std::forward<F>(func));
385  }
386 
398  template <typename F>
399  void apply(F&& func) const {
400  _impl->findAndApply(_aliases->apply(_name), std::forward<F>(func));
401  }
402 
404  SubSchema operator[](std::string const& name) const;
405 
407  std::string const& getPrefix() const { return _name; }
408 
415  std::set<std::string> getNames(bool topOnly = false) const;
416 
424  template <typename T>
425  operator Key<T>() const {
426  return _impl->find<T>(_aliases->apply(_name)).key;
427  }
428 
436  template <typename T>
437  operator Field<T>() const {
438  return _impl->find<T>(_aliases->apply(_name)).field;
439  }
440 
441 private:
442  friend class Schema;
443 
445 
446  std::shared_ptr<Impl> _impl;
447  std::shared_ptr<AliasMap> _aliases;
449 };
450 
451 inline SubSchema Schema::operator[](std::string const& name) const {
452  return SubSchema(_impl, _aliases, name);
453 }
454 }
455 }
456 } // namespace lsst::afw::table
457 
458 #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:309
Defines the fields and offsets for a table.
Definition: Schema.h:47
friend class SubSchema
Definition: Schema.h:313
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.
Definition: Schema.cc:712
Schema()
Construct an empty Schema.
Definition: Schema.cc:607
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:354
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.
Definition: Schema.cc:696
Keys have the same types offsets, and sizes.
Definition: Schema.h:62
std::string const & _name
Definition: Mask.cc:576
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:639
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Definition: Schema.cc:635
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:719
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Definition: Schema.cc:617
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:383
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
ItemVariant const * other
Definition: Schema.cc:55
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:399
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:298
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:407
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
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:654
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.
Definition: Schema.cc:677
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: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:361
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
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.
Definition: Schema.cc:741
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:451
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.
Definition: Schema.cc:672
table::Key< int > a
std::ostream * os
Definition: Schema.cc:736
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:364
Fields have the same documentation (ordered).
Definition: Schema.h:64
STL class.
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Definition: Schema.cc:651
Schema & operator=(Schema const &other)
Fields are identical (but aliases may not be).
Definition: Schema.h:66
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:666
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