LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
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 <vector>
6 
7 #include "boost/shared_ptr.hpp"
8 #include "boost/ref.hpp"
9 
10 #include "ndarray.h"
11 #include "lsst/base.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 { namespace afw { namespace table {
19 
20 class SubSchema;
21 class BaseRecord;
22 
46 class Schema {
48 public:
49 
50  // This variable is defined in SchemaImpl, but is replicated here as
51  // a static so that it is available to SWIG.
53 
61  EQUAL_KEYS =0x01,
62  EQUAL_NAMES =0x02,
63  EQUAL_DOCS =0x04,
64  EQUAL_UNITS =0x08,
65  IDENTICAL =0x0F
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 
201  template <typename F>
202  void forEach(F func) const {
204  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
205  }
206 
208 
215  bool operator==(Schema const & other) const { return compare(other, EQUAL_KEYS); }
216  bool operator!=(Schema const & other) const { return !this->operator==(other); }
218 
228  int compare(Schema const & other, int flags=EQUAL_KEYS) const;
229 
236  int contains(Schema const & other, int flags=EQUAL_KEYS) const;
237 
244  template <typename T>
245  int contains(SchemaItem<T> const & item, int flags=EQUAL_KEYS) const;
246 
256  PTR(AliasMap) getAliasMap() const { return _aliases; }
257 
266  void setAliasMap(PTR(AliasMap) aliases);
267 
269  void disconnectAliases();
270 
272  Schema();
273 
275  explicit Schema(int version);
276 
278  Schema(Schema const & other);
279 
296  explicit Schema(daf::base::PropertyList & metadata, bool stripMetadata);
297 
311  explicit Schema(daf::base::PropertyList const & metadata);
312 
314  friend std::ostream & operator<<(std::ostream & os, Schema const & schema);
315 
318 
320  int getVersion() const { return _impl->getVersion(); }
321 
323  void setVersion(int version) { _edit();_impl->setVersion(version); }
324 
325 private:
326 
327  friend class detail::Access;
328  friend class SubSchema;
329 
331  void _edit();
332 
335 };
336 
370 class SubSchema {
372 public:
373 
375  template <typename T>
376  SchemaItem<T> find(std::string const & name) const;
377 
379  SubSchema operator[](std::string const & name) const;
380 
382  std::string const & getPrefix() const { return _name; }
383 
390  std::set<std::string> getNames(bool topOnly=false) const;
391 
398  template <typename T>
399  operator Key<T>() const { return _impl->find<T>(_aliases->apply(_name)).key; }
400 
407  template <typename T>
408  operator Field<T>() const { return _impl->find<T>(_aliases->apply(_name)).field; }
409 
410 private:
411 
412  friend class Schema;
413 
414  SubSchema(PTR(Impl) impl, PTR(AliasMap) aliases, std::string const & name);
415 
416  PTR(Impl) _impl;
417  PTR(AliasMap) _aliases;
418  std::string _name;
419 };
420 
421 inline SubSchema Schema::operator[](std::string const & name) const {
422  return SubSchema(_impl, _aliases, name);
423 }
424 
425 }}} // namespace lsst::afw::table
426 
427 #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:317
Defines the fields and offsets for a table.
Definition: Schema.h:46
friend class SubSchema
Definition: Schema.h:328
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:50
Schema()
Construct an empty Schema.
void forEach(F func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:202
A proxy type for name lookups in a Schema.
Definition: Schema.h:370
boost::shared_ptr< Impl > _impl
Definition: Schema.h:333
#define PTR(...)
Definition: base.h:41
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:60
Keys have the same types offsets, and sizes.
Definition: Schema.h:61
boost::shared_ptr< AliasMap > getAliasMap() const
Definition: Schema.h:256
std::string const & _name
Definition: Mask.cc:677
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
Mapping class that holds aliases for a Schema.
Definition: AliasMap.h:34
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
int getVersion() const
Return the table&#39;s version.
Definition: Schema.h:320
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
void _edit()
Copy on write; should be called by all mutators (except for alias mutators).
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:136
int d
Definition: KDTree.cc:89
boost::shared_ptr< AliasMap > _aliases
Definition: Schema.h:334
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
Fields have the same units (ordered).
Definition: Schema.h:64
Fields have the same names (ordered).
Definition: Schema.h:62
int getFieldCount() const
The total number of fields.
Definition: Schema.h:133
tbl::Schema schema
Definition: CoaddPsf.cc:324
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
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:130
A description of a field in a table.
Definition: Field.h:22
detail::SchemaImpl Impl
Definition: Schema.h:47
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:421
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:56
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&#39;s version.
Definition: Schema.h:74
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:216
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:382
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:152
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:139
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
void setVersion(int version)
Set the table&#39;s version.
Definition: Schema.h:323
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema&#39;s version.
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:215
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&#39;s version.
Definition: Schema.h:71
afw::table::Key< double > b
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
static int const DEFAULT_VERSION
Definition: Schema.h:52
void setAliasMap(boost::shared_ptr< AliasMap > aliases)
Fields have the same documentation (ordered).
Definition: Schema.h:63
detail::SchemaImpl Impl
Definition: Schema.h:371
Everything is the same.
Definition: Schema.h:65
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27