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
BaseRecord.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_BaseRecord_h_INCLUDED
3 #define AFW_TABLE_BaseRecord_h_INCLUDED
4 
5 #include "lsst/base.h"
6 #include "lsst/afw/table/fwd.h"
10 
11 namespace lsst { namespace afw { namespace table {
12 
28 #ifndef SWIG // swig complains about these not being %shared_ptr, and it doesn't need to know about them
29  : public daf::base::Citizen,
30  private boost::noncopyable
31 #endif
32 {
33 public:
34 
36  typedef BaseTable Table;
37 
40 
43 
46 
48  Schema getSchema() const { return _table->getSchema(); }
49 
51  CONST_PTR(BaseTable) getTable() const { return _table; }
52 
60  template <typename T>
61  typename Field<T>::Element * getElement(Key<T> const & key) {
62  if (!key.isValid()) {
63  throw LSST_EXCEPT(
64  pex::exceptions::LogicError,
65  "Key is not valid (if this is a SourceRecord, make sure slot aliases have been setup)."
66  );
67  }
68  return reinterpret_cast<typename Field<T>::Element*>(
69  reinterpret_cast<char*>(_data) + key.getOffset()
70  );
71  }
72 
80  template <typename T>
81  typename Field<T>::Element const * getElement(Key<T> const & key) const {
82  if (!key.isValid()) {
83  throw LSST_EXCEPT(
84  pex::exceptions::LogicError,
85  "Key is not valid (if this is a SourceRecord, make sure slot aliases have been setup)."
86  );
87  }
88  return reinterpret_cast<typename Field<T>::Element const *>(
89  reinterpret_cast<char const *>(_data) + key.getOffset()
90  );
91  }
92 
100  template <typename T>
101  typename Field<T>::Reference operator[](Key<T> const & key) {
102  return key.getReference(getElement(key), _manager);
103  }
104 
112  template <typename T>
113  typename Field<T>::ConstReference operator[](Key<T> const & key) const {
114  return key.getConstReference(getElement(key), _manager);
115  }
116 
122  template <typename T>
123  typename Field<T>::Value get(Key<T> const & key) const {
124  return key.getValue(getElement(key), _manager);
125  }
126 
135  template <typename T, typename U>
136  void set(Key<T> const & key, U const & value) {
137  key.setValue(getElement(key), _manager, value);
138  }
139 
140 #ifndef SWIG
141 
145  template <typename T>
146  T get(OutputFunctorKey<T> const & key) const {
147  return key.get(*this);
148  }
149 
153  template <typename T, typename U>
154  void set(InputFunctorKey<T> const & key, U const & value) {
155  return key.set(*this, value);
156  }
157 
158  template <typename Ref>
160  return key.getReference(*this);
161  }
162 
163  template <typename ConstRef>
164  ConstRef operator[](ConstReferenceFunctorKey<ConstRef> const & key) const {
165  return key.getConstReference(*this);
166  }
167 
168 #endif // !SWIG
169 
171  void assign(BaseRecord const & other);
172 
174  void assign(BaseRecord const & other, SchemaMapper const & mapper);
175 
177 
178  virtual ~BaseRecord() { _table->_destroy(*this); }
179 
180 protected:
181 
183  virtual void _assign(BaseRecord const & other) {}
184 
186  BaseRecord(PTR(BaseTable) const & table) : daf::base::Citizen(typeid(this)), _table(table) {
187  table->_initialize(*this);
188  }
189 
190 private:
191 
192  friend class BaseTable;
193  friend class BaseColumnView;
194 
195  // All these are definitely private, not protected - we don't want derived classes mucking with them.
196  void * _data; // pointer to field data
197  PTR(BaseTable) _table; // the associated table
198  ndarray::Manager::Ptr _manager; // shared manager for lifetime of _data (like shared_ptr with no pointer)
199 };
200 
201 }}} // namespace lsst::afw::table
202 
203 #endif // !AFW_TABLE_BaseRecord_h_INCLUDED
bool isValid() const
Return true if the key was initialized to valid offset.
Definition: Key.h:83
void set(InputFunctorKey< T > const &key, U const &value)
Set a calculated or aggregate field.
Definition: BaseRecord.h:154
Defines the fields and offsets for a table.
Definition: Schema.h:46
Column-wise view into a sequence of records that have been allocated contiguously.
Field< T >::Reference operator[](Key< T > const &key)
Return a reference (or reference-like type) to the field&#39;s value.
Definition: BaseRecord.h:101
boost::shared_ptr< BaseTable > _table
Definition: BaseRecord.h:197
Field< T >::ConstReference operator[](Key< T > const &key) const
Return a const reference (or const-reference-like type) to the field&#39;s value.
Definition: BaseRecord.h:113
A custom container class for records, based on std::vector.
Definition: Catalog.h:94
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
CatalogT< BaseRecord > Catalog
Template of CatalogT used to hold records of this type.
Definition: BaseRecord.h:42
#define PTR(...)
Definition: base.h:41
virtual void _assign(BaseRecord const &other)
Called by assign() after transferring fields to allow subclass data members to be copied...
Definition: BaseRecord.h:183
ndarray::Manager::Ptr getManager() const
Definition: BaseRecord.h:176
virtual void set(BaseRecord &record, T const &value) const =0
std::map< Citizen const *, CitizenInfo > table
Definition: Citizen.h:93
virtual T getReference(BaseRecord &record) const =0
BaseRecord(boost::shared_ptr< BaseTable > const &table)
Construct a record with uninitialized data.
Definition: BaseRecord.h:186
Field< T >::Element const * getElement(Key< T > const &key) const
Return a pointer to the underlying elements of a field (const).
Definition: BaseRecord.h:81
CatalogT< BaseRecord const > ConstCatalog
Template of CatalogT used to hold const records of this type.
Definition: BaseRecord.h:45
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
boost::shared_ptr< BaseTable const > getTable() const
Return the table this record is associated with.
Definition: BaseRecord.h:51
BaseTable Table
The associated table class.
Definition: BaseRecord.h:36
int getOffset() const
Return the offset (in bytes) of this field within a record.
Definition: Key.h:73
Ref operator[](ReferenceFunctorKey< Ref > const &key)
Definition: BaseRecord.h:159
ConstReference getConstReference(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::operator[] (const).
Definition: FieldBase.h:86
void _initialize(BaseRecord &record)
Field< T >::Element * getElement(Key< T > const &key)
Return a pointer to the underlying elements of a field (non-const).
Definition: BaseRecord.h:61
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
ndarray::Manager::Ptr _manager
Definition: BaseRecord.h:198
Reference getReference(Element *p, ndarray::Manager::Ptr const &) const
Used to implement RecordBase::operator[] (non-const).
Definition: FieldBase.h:83
Base class for all records.
Definition: BaseRecord.h:27
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:50
Citizen(const std::type_info &)
Definition: Citizen.cc:173
ConstRef operator[](ConstReferenceFunctorKey< ConstRef > const &key) const
Definition: BaseRecord.h:164
virtual T getConstReference(BaseRecord const &record) const =0
FieldBase< T >::Element Element
Type used to store field data in the table (a field may have multiple elements).
Definition: Field.h:25
#define CONST_PTR(...)
Definition: base.h:47
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:136
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement RecordBase::set.
Definition: FieldBase.h:92
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
void assign(BaseRecord const &other)
Copy all field values from other to this, requiring that they have equal schemas. ...
T const & ConstReference
the type returned by BaseRecord::operator[] (const)
Definition: FieldBase.h:52
BaseColumnView ColumnView
The associated ColumnView class.
Definition: BaseRecord.h:39
T & Reference
the type returned by BaseRecord::operator[] (non-const)
Definition: FieldBase.h:51
Base class for all tables.
Definition: BaseTable.h:44
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:48