LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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  Schema(Schema const & other);
278 
280  friend std::ostream & operator<<(std::ostream & os, Schema const & schema);
281 
284 
285 private:
286 
287  friend class detail::Access;
288  friend class SubSchema;
289 
291  void _edit();
292 
295 };
296 
330 class SubSchema {
332 public:
333 
335  template <typename T>
336  SchemaItem<T> find(std::string const & name) const;
337 
339  SubSchema operator[](std::string const & name) const;
340 
342  std::string const & getPrefix() const { return _name; }
343 
350  std::set<std::string> getNames(bool topOnly=false) const;
351 
358  template <typename T>
359  operator Key<T>() const { return _impl->find<T>(_aliases->apply(_name)).key; }
360 
367  template <typename T>
368  operator Field<T>() const { return _impl->find<T>(_aliases->apply(_name)).field; }
369 
370 private:
371 
372  friend class Schema;
373 
374  SubSchema(PTR(Impl) impl, PTR(AliasMap) aliases, std::string const & name);
375 
376  PTR(Impl) _impl;
377  PTR(AliasMap) _aliases;
378  std::string _name;
379 };
380 
381 inline SubSchema Schema::operator[](std::string const & name) const {
382  return SubSchema(_impl, _aliases, name);
383 }
384 
385 }}} // namespace lsst::afw::table
386 
387 #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:283
Defines the fields and offsets for a table.
Definition: Schema.h:46
friend class SubSchema
Definition: Schema.h:288
Field base class default implementation (used for numeric scalars and Angle).
Definition: FieldBase.h:48
Schema()
Construct an empty Schema.
void forEach(F func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:204
A proxy type for name lookups in a Schema.
Definition: Schema.h:330
boost::shared_ptr< Impl > _impl
Definition: Schema.h:293
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:60
Keys have the same types offsets, and sizes.
Definition: Schema.h:61
table::Key< std::string > name
Definition: ApCorrMap.cc:71
boost::shared_ptr< AliasMap > getAliasMap() const
Definition: Schema.h:258
std::string const & _name
Definition: Mask.cc:678
afw::table::Schema schema
Definition: GaussianPsf.cc:41
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.
#define PTR(...)
Definition: base.h:41
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.
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:138
boost::shared_ptr< AliasMap > _aliases
Definition: Schema.h:294
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
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:135
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
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:132
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:381
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.
Definition: Schema.h:76
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:218
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:342
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:156
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:141
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:44
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
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:217
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
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
void setAliasMap(boost::shared_ptr< AliasMap > aliases)
Fields have the same documentation (ordered).
Definition: Schema.h:63
Fields are identical (but aliases may not be).
Definition: Schema.h:65
detail::SchemaImpl Impl
Definition: Schema.h:331
table::Key< int > field
Definition: ApCorrMap.cc:72
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