LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
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  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(
77  std::string const & a, std::string const & b, std::string const & c, std::string const & d
78  ) 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 
115  SubSchema operator[](std::string const & name) const;
116 
129  std::set<std::string> getNames(bool topOnly=false) const;
130 
132  int getRecordSize() const { return _impl->getRecordSize(); }
133 
135  int getFieldCount() const { return _impl->getFieldCount(); }
136 
138  int getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
139 
141  int getNonFlagFieldCount() const { return _impl->getNonFlagFieldCount(); }
142 
153  template <typename T>
154  Key<T> addField(Field<T> const & field, bool doReplace=false);
155 
164  template <typename T>
166  std::string const & name, std::string const & doc, std::string const & units = "",
167  FieldBase<T> const & base = FieldBase<T>(), bool doReplace=false
168  ) {
169  return addField(Field<T>(name, doc, units, base), doReplace);
170  }
171 
180  template <typename T>
182  std::string const & name, std::string const & doc, FieldBase<T> const & base,
183  bool doReplace=false
184  ) {
185  return addField(Field<T>(name, doc, base), doReplace);
186  }
187 
189  template <typename T>
190  void replaceField(Key<T> const & key, Field<T> const & field);
191 
203  template <typename F>
204  void forEach(F func) const {
206  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
207  }
208 
210 
217  bool operator==(Schema const & other) const { return compare(other, EQUAL_KEYS); }
218  bool operator!=(Schema const & other) const { return !this->operator==(other); }
220 
230  int compare(Schema const & other, int flags=EQUAL_KEYS) const;
231 
238  int contains(Schema const & other, int flags=EQUAL_KEYS) const;
239 
246  template <typename T>
247  int contains(SchemaItem<T> const & item, int flags=EQUAL_KEYS) const;
248 
258  PTR(AliasMap) getAliasMap() const { return _aliases; }
259 
268  void setAliasMap(PTR(AliasMap) aliases);
269 
271  void disconnectAliases();
272 
274  Schema();
275 
277  explicit Schema(int version);
278 
280  Schema(Schema const & other);
281 
298  explicit Schema(daf::base::PropertyList & metadata, bool stripMetadata);
299 
313  explicit Schema(daf::base::PropertyList const & metadata);
314 
316  friend std::ostream & operator<<(std::ostream & os, Schema const & schema);
317 
320 
322  int getVersion() const { return _impl->getVersion(); }
323 
325  void setVersion(int version) { _edit();_impl->setVersion(version); }
326 
327 private:
328 
329  friend class detail::Access;
330  friend class SubSchema;
331 
333  void _edit();
334 
337 };
338 
372 class SubSchema {
374 public:
375 
377  template <typename T>
378  SchemaItem<T> find(std::string const & name) const;
379 
381  SubSchema operator[](std::string const & name) const;
382 
384  std::string const & getPrefix() const { return _name; }
385 
392  std::set<std::string> getNames(bool topOnly=false) const;
393 
400  template <typename T>
401  operator Key<T>() const { return _impl->find<T>(_aliases->apply(_name)).key; }
402 
409  template <typename T>
410  operator Field<T>() const { return _impl->find<T>(_aliases->apply(_name)).field; }
411 
412 private:
413 
414  friend class Schema;
415 
416  SubSchema(PTR(Impl) impl, PTR(AliasMap) aliases, std::string const & name);
417 
418  PTR(Impl) _impl;
419  PTR(AliasMap) _aliases;
420  std::string _name;
421 };
422 
423 inline SubSchema Schema::operator[](std::string const & name) const {
424  return SubSchema(_impl, _aliases, name);
425 }
426 
427 }}} // namespace lsst::afw::table
428 
429 #endif // !AFW_TABLE_Schema_h_INCLUDED
Defines the fields and offsets for a table.
Definition: Schema.h:46
int getVersion() const
Return the table&#39;s version.
Definition: Schema.h:322
static int const DEFAULT_VERSION
Definition: Schema.h:52
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:218
friend class SubSchema
Definition: Schema.h:330
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:50
Keys have the same types offsets, and sizes.
Definition: Schema.h:61
A proxy type for name lookups in a Schema.
Definition: Schema.h:372
boost::shared_ptr< AliasMap > _aliases
Definition: Schema.h:336
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:217
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:60
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Schemas have identical AliasMaps.
Definition: Schema.h:66
std::string const & _name
Definition: Mask.cc:677
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:165
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
Fields have the same names (ordered).
Definition: Schema.h:62
Everything is the same.
Definition: Schema.h:67
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:423
Mapping class that holds aliases for a Schema.
Definition: AliasMap.h:34
daf::base::Citizen & getCitizen()
Get the Citizen corresponding to this Schema (SchemaImpl is what inherits from Citizen).
Definition: Schema.h:319
boost::shared_ptr< AliasMap > getAliasMap() const
Definition: Schema.h:258
Schema()
Construct an empty Schema.
#define PTR(...)
Definition: base.h:41
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
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:76
int d
Definition: KDTree.cc:89
Fields are identical (but aliases may not be).
Definition: Schema.h:65
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:384
Fields have the same documentation (ordered).
Definition: Schema.h:63
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:141
Fields have the same units (ordered).
Definition: Schema.h:64
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
tbl::Schema schema
Definition: CoaddPsf.cc:324
detail::SchemaImpl Impl
Definition: Schema.h:373
A description of a field in a table.
Definition: Field.h:22
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
boost::shared_ptr< Impl > _impl
Definition: Schema.h:335
void setVersion(int version)
Set the table&#39;s version.
Definition: Schema.h:325
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:56
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:152
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
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.
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
detail::SchemaImpl Impl
Definition: Schema.h:47
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:132
void _edit()
Copy on write; should be called by all mutators (except for alias mutators).
afw::table::Key< double > b
void setAliasMap(boost::shared_ptr< AliasMap > aliases)
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
int getFieldCount() const
The total number of fields.
Definition: Schema.h:135
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:138
static int const DEFAULT_VERSION
Definition: SchemaImpl.h:69
void forEach(F func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:204
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:181
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:73
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27