lsst.afw  14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/table/Key.h"
11 #include "lsst/afw/table/Field.h"
13 #include "lsst/afw/table/Flag.h"
15 
16 namespace lsst {
17 namespace afw {
18 namespace table {
19 
20 class SubSchema;
21 class BaseRecord;
22 
46 class Schema {
47  typedef detail::SchemaImpl Impl;
48 
49 public:
50  // This variable is defined in SchemaImpl, but is replicated here as
51  // so that it is available to Python.
53 
61  EQUAL_KEYS = 0x01,
62  EQUAL_NAMES = 0x02,
63  EQUAL_DOCS = 0x04,
64  EQUAL_UNITS = 0x08,
65  EQUAL_FIELDS = 0x0F,
66  EQUAL_ALIASES = 0x10,
67  IDENTICAL = 0x1F
68  };
69 
71  std::string join(std::string const& a, std::string const& b) const;
73  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
74  return join(join(a, b), c);
75  }
76  std::string join(std::string const& a, std::string const& b, std::string const& c,
77  std::string const& d) const {
78  return join(join(a, b), join(c, d));
79  }
81 
89  template <typename T>
90  SchemaItem<T> find(std::string const& name) const;
91 
100  template <typename T>
101  SchemaItem<T> find(Key<T> const& key) const;
102 
111  template <typename F>
112  void findAndApply(std::string const& name, F&& func) const {
113  _impl->findAndApply(_aliases->apply(name), std::forward<F>(func));
114  }
115 
127  SubSchema operator[](std::string const& name) const;
128 
141  std::set<std::string> getNames(bool topOnly = false) const;
142 
144  int getRecordSize() const { return _impl->getRecordSize(); }
145 
147  int getFieldCount() const { return _impl->getFieldCount(); }
148 
150  int getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
151 
153  int getNonFlagFieldCount() const { return _impl->getNonFlagFieldCount(); }
154 
165  template <typename T>
166  Key<T> addField(Field<T> const& field, bool doReplace = false);
167 
175  template <typename T>
176  Key<T> addField(std::string const& name, std::string const& doc, std::string const& units = "",
177  FieldBase<T> const& base = FieldBase<T>(), bool doReplace = false) {
178  return addField(Field<T>(name, doc, units, base), doReplace);
179  }
180 
188  template <typename T>
189  Key<T> addField(std::string const& name, std::string const& doc, FieldBase<T> const& base,
190  bool doReplace = false) {
191  return addField(Field<T>(name, doc, base), doReplace);
192  }
193 
195  template <typename T>
196  void replaceField(Key<T> const& key, Field<T> const& field);
197 
207  template <typename F>
208  void forEach(F&& func) const {
209  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
210  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
211  }
212 
214 
221  bool operator==(Schema const& other) const { return compare(other, EQUAL_KEYS); }
222  bool operator!=(Schema const& other) const { return !this->operator==(other); }
224 
234  int compare(Schema const& other, int flags = EQUAL_KEYS) const;
235 
242  int contains(Schema const& other, int flags = EQUAL_KEYS) const;
243 
250  template <typename T>
251  int contains(SchemaItem<T> const& item, int flags = EQUAL_KEYS) const;
252 
262  std::shared_ptr<AliasMap> getAliasMap() const { return _aliases; }
263 
272  void setAliasMap(std::shared_ptr<AliasMap> aliases);
273 
275  void disconnectAliases();
276 
278  Schema();
279 
281  Schema(Schema const& other);
282 
288  static Schema readFits(std::string const& filename, int hdu = INT_MIN);
289  static Schema readFits(fits::MemFileManager& manager, int hdu = INT_MIN);
290  static Schema readFits(fits::Fits& fitsfile);
291 
297  static Schema fromFitsMetadata(daf::base::PropertyList& header, bool stripMetadata = true);
298 
300  friend std::ostream& operator<<(std::ostream& os, Schema const& schema);
301 
303  daf::base::Citizen& getCitizen() { return *_impl; }
304 
305 private:
306  friend class detail::Access;
307  friend class SubSchema;
308 
310  void _edit();
311 
312  std::shared_ptr<Impl> _impl;
313  std::shared_ptr<AliasMap> _aliases;
314 };
315 
348 class SubSchema {
349  typedef detail::SchemaImpl Impl;
350 
351 public:
353  std::string join(std::string const& a, std::string const& b) const;
355  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
356  return join(join(a, b), c);
357  }
358  std::string join(std::string const& a, std::string const& b, std::string const& c,
359  std::string const& d) const {
360  return join(join(a, b), join(c, d));
361  }
363 
365  template <typename T>
366  SchemaItem<T> find(std::string const& name) const;
367 
376  template <typename F>
377  void findAndApply(std::string const& name, F&& func) const {
378  _impl->findAndApply(_aliases->apply(join(_name, name)), std::forward<F>(func));
379  }
380 
392  template <typename F>
393  void apply(F&& func) const {
394  _impl->findAndApply(_aliases->apply(_name), std::forward<F>(func));
395  }
396 
398  SubSchema operator[](std::string const& name) const;
399 
401  std::string const& getPrefix() const { return _name; }
402 
409  std::set<std::string> getNames(bool topOnly = false) const;
410 
418  template <typename T>
419  operator Key<T>() const {
420  return _impl->find<T>(_aliases->apply(_name)).key;
421  }
422 
430  template <typename T>
431  operator Field<T>() const {
432  return _impl->find<T>(_aliases->apply(_name)).field;
433  }
434 
435 private:
436  friend class Schema;
437 
438  SubSchema(std::shared_ptr<Impl> impl, std::shared_ptr<AliasMap> aliases, std::string const& name);
439 
440  std::shared_ptr<Impl> _impl;
441  std::shared_ptr<AliasMap> _aliases;
442  std::string _name;
443 };
444 
445 inline SubSchema Schema::operator[](std::string const& name) const {
446  return SubSchema(_impl, _aliases, name);
447 }
448 }
449 }
450 } // namespace lsst::afw::table
451 
452 #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:303
Defines the fields and offsets for a table.
Definition: Schema.h:46
friend class SubSchema
Definition: Schema.h:307
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.
A proxy type for name lookups in a Schema.
Definition: Schema.h:348
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:60
Keys have the same types offsets, and sizes.
Definition: Schema.h:61
std::string const & _name
Definition: Mask.cc:546
SubSchema operator[](std::string const &name) const
Return a nested proxy.
table::Key< int > field
Definition: ApCorrMap.cc:72
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Schemas have identical AliasMaps.
Definition: Schema.h:66
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
void findAndApply(std::string const &name, F &&func) const
Find a nested SchemaItem by name and run a functor on it.
Definition: Schema.h:377
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:199
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:150
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:176
Fields have the same units (ordered).
Definition: Schema.h:64
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:208
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:358
Fields have the same names (ordered).
Definition: Schema.h:62
int getFieldCount() const
The total number of fields.
Definition: Schema.h:147
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:189
SchemaItem< T > find(std::string const &name) const
Find a nested SchemaItem by name.
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:100
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:144
A description of a field in a table.
Definition: Field.h:24
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::set< std::string > getNames(bool topOnly=false) const
Return a set of nested names that start with the SubSchema&#39;s prefix.
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:445
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:53
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:393
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:76
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:222
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:401
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:168
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:153
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
static int const VERSION
Definition: Schema.h:52
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:112
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:355
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:262
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:221
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:73
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Fields have the same documentation (ordered).
Definition: Schema.h:63
Fields are identical (but aliases may not be).
Definition: Schema.h:65
afw::table::Schema schema
Definition: GaussianPsf.cc:43
lsst::daf::base::PropertyList PropertyList
Definition: Wcs.cc:56
static Schema readFits(std::string const &filename, int hdu=INT_MIN)
Construct from reading a FITS file.
Everything is the same.
Definition: Schema.h:67
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27