LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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/table/Key.h"
11 #include "lsst/afw/table/Field.h"
13 #include "lsst/afw/table/Flag.h"
15 
16 namespace lsst { namespace afw { namespace table {
17 
18 class SubSchema;
19 class BaseRecord;
20 
44 class Schema {
46 public:
47 
48  // This variable is defined in SchemaImpl, but is replicated here as
49  // a static so that it is available to SWIG.
51 
59  EQUAL_KEYS =0x01,
60  EQUAL_NAMES =0x02,
61  EQUAL_DOCS =0x04,
62  EQUAL_UNITS =0x08,
63  EQUAL_FIELDS =0x0F,
64  EQUAL_ALIASES =0x10,
65  IDENTICAL =0x1F
66  };
67 
69  std::string join(std::string const & a, std::string const & b) const;
71  std::string join(std::string const & a, std::string const & b, std::string const & c) const {
72  return join(join(a, b), c);
73  }
74  std::string join(
75  std::string const & a, std::string const & b, std::string const & c, std::string const & d
76  ) const {
77  return join(join(a, b), join(c, d));
78  }
80 
88  template <typename T>
89  SchemaItem<T> find(std::string const & name) const;
90 
99  template <typename T>
100  SchemaItem<T> find(Key<T> const & key) const;
101 
113  SubSchema operator[](std::string const & name) const;
114 
127  std::set<std::string> getNames(bool topOnly=false) const;
128 
130  int getRecordSize() const { return _impl->getRecordSize(); }
131 
133  int getFieldCount() const { return _impl->getFieldCount(); }
134 
136  int getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
137 
139  int getNonFlagFieldCount() const { return _impl->getNonFlagFieldCount(); }
140 
151  template <typename T>
152  Key<T> addField(Field<T> const & field, bool doReplace=false);
153 
162  template <typename T>
164  std::string const & name, std::string const & doc, std::string const & units = "",
165  FieldBase<T> const & base = FieldBase<T>(), bool doReplace=false
166  ) {
167  return addField(Field<T>(name, doc, units, base), doReplace);
168  }
169 
178  template <typename T>
180  std::string const & name, std::string const & doc, FieldBase<T> const & base,
181  bool doReplace=false
182  ) {
183  return addField(Field<T>(name, doc, base), doReplace);
184  }
185 
187  template <typename T>
188  void replaceField(Key<T> const & key, Field<T> const & field);
189 
199  template <typename F>
200  void forEach(F&& func) const {
201  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
202  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
203  }
204 
206 
213  bool operator==(Schema const & other) const { return compare(other, EQUAL_KEYS); }
214  bool operator!=(Schema const & other) const { return !this->operator==(other); }
216 
226  int compare(Schema const & other, int flags=EQUAL_KEYS) const;
227 
234  int contains(Schema const & other, int flags=EQUAL_KEYS) const;
235 
242  template <typename T>
243  int contains(SchemaItem<T> const & item, int flags=EQUAL_KEYS) const;
244 
254  PTR(AliasMap) getAliasMap() const { return _aliases; }
255 
264  void setAliasMap(PTR(AliasMap) aliases);
265 
267  void disconnectAliases();
268 
270  Schema();
271 
273  Schema(Schema const & other);
274 
279  static Schema readFits(std::string const& filename, int hdu=0);
280  static Schema readFits(fits::MemFileManager & manager, int hdu=0);
281  static Schema readFits(fits::Fits & fitsfile);
282 
287  static Schema fromFitsMetadata(daf::base::PropertyList & header, bool stripMetadata=true);
288 
290  friend std::ostream & operator<<(std::ostream & os, Schema const & schema);
291 
294 
295 private:
296 
297  friend class detail::Access;
298  friend class SubSchema;
299 
301  void _edit();
302 
305 };
306 
340 class SubSchema {
342 public:
343 
345  template <typename T>
346  SchemaItem<T> find(std::string const & name) const;
347 
349  SubSchema operator[](std::string const & name) const;
350 
352  std::string const & getPrefix() const { return _name; }
353 
360  std::set<std::string> getNames(bool topOnly=false) const;
361 
368  template <typename T>
369  operator Key<T>() const { return _impl->find<T>(_aliases->apply(_name)).key; }
370 
377  template <typename T>
378  operator Field<T>() const { return _impl->find<T>(_aliases->apply(_name)).field; }
379 
380 private:
381 
382  friend class Schema;
383 
384  SubSchema(PTR(Impl) impl, PTR(AliasMap) aliases, std::string const & name);
385 
386  PTR(Impl) _impl;
387  PTR(AliasMap) _aliases;
388  std::string _name;
389 };
390 
391 inline SubSchema Schema::operator[](std::string const & name) const {
392  return SubSchema(_impl, _aliases, name);
393 }
394 
395 }}} // namespace lsst::afw::table
396 
397 #endif // !AFW_TABLE_Schema_h_INCLUDED
Defines the fields and offsets for a table.
Definition: Schema.h:44
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Schema()
Construct an empty Schema.
friend class SubSchema
Definition: Schema.h:298
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:48
A proxy type for name lookups in a Schema.
Definition: Schema.h:340
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:58
detail::SchemaImpl Impl
Definition: Schema.h:341
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::string const & _name
Definition: Mask.cc:678
Class for storing ordered metadata with comments.
Definition: PropertyList.h:82
afw::table::Schema schema
Definition: GaussianPsf.cc:41
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
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:74
int getFieldCount() const
The total number of fields.
Definition: Schema.h:133
Mapping class that holds aliases for a Schema.
Definition: AliasMap.h:34
boost::shared_ptr< AliasMap > _aliases
Definition: Schema.h:304
Fields are identical (but aliases may not be).
Definition: Schema.h:63
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
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:179
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:71
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:202
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:200
Schemas have identical AliasMaps.
Definition: Schema.h:64
void setAliasMap(boost::shared_ptr< AliasMap > aliases)
Set the alias map.
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:391
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:136
Keys have the same types offsets, and sizes.
Definition: Schema.h:59
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:106
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:163
A description of a field in a table.
Definition: Field.h:22
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:352
static Schema readFits(std::string const &filename, int hdu=0)
Construct from reading a FITS file.
boost::shared_ptr< Impl > _impl
Definition: Schema.h:303
Fields have the same documentation (ordered).
Definition: Schema.h:61
void _edit()
Copy on write; should be called by all mutators (except for alias mutators).
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:54
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:214
boost::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:254
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:154
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
#define PTR(...)
Definition: base.h:41
Fields have the same units (ordered).
Definition: Schema.h:62
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:130
afw::table::Key< double > b
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
static int const VERSION
Definition: Schema.h:50
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53
Everything is the same.
Definition: Schema.h:65
Basic LSST definitions.
daf::base::Citizen & getCitizen()
Get the Citizen corresponding to this Schema (SchemaImpl is what inherits from Citizen).
Definition: Schema.h:293
table::Key< int > field
Definition: ApCorrMap.cc:72
Fields have the same names (ordered).
Definition: Schema.h:60
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:213
detail::SchemaImpl Impl
Definition: Schema.h:45
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:139
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25