LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
SchemaImpl.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_DETAIL_SchemaImpl_h_INCLUDED
3 #define AFW_TABLE_DETAIL_SchemaImpl_h_INCLUDED
4 
5 #include <vector>
6 #include <algorithm>
7 #include <map>
8 #include <set>
9 
10 #include "boost/variant.hpp"
11 #include "boost/mpl/transform.hpp"
12 
13 namespace lsst {
14 namespace afw {
15 namespace table {
16 
17 class Schema;
18 class SubSchema;
19 
24 template <typename T>
25 struct SchemaItem final {
28 
29  SchemaItem(Key<T> const& key_, Field<T> const& field_) : key(key_), field(field_) {}
30 };
31 
32 namespace detail {
33 
34 class Access;
35 
48 class SchemaImpl {
49 private:
51  struct MakeItem {
52  template <typename T>
53  struct apply {
55  };
56  };
57 
58 public:
59  static int const VERSION = 3;
60 
73 
75  int getRecordSize() const { return _recordSize; }
76 
78  int getFieldCount() const { return _names.size(); }
79 
81  int getFlagFieldCount() const { return _flags.size(); }
82 
84  int getNonFlagFieldCount() const { return _offsets.size(); }
85 
87  template <typename T>
88  SchemaItem<T> find(std::string const& name) const;
89 
91  template <typename T>
92  SchemaItem<T> find(Key<T> const& key) const;
93 
95  SchemaItem<Flag> find(Key<Flag> const& key) const;
96 
98  template <typename F>
99  void findAndApply(std::string const& name, F&& func) const {
100  auto iter = _names.find(name);
101  if (iter == _names.end()) {
103  (boost::format("Field with name '%s' not found") % name).str());
104  }
105  VisitorWrapper<F> visitor(std::forward<F>(func));
106  visitor(_items[iter->second]);
107  }
108 
110  std::set<std::string> getNames(bool topOnly) const;
111 
113  std::set<std::string> getNames(bool topOnly, std::string const& prefix) const;
114 
115  template <typename T>
116  int contains(SchemaItem<T> const& item, int flags) const;
117 
119  template <typename T>
120  Key<T> addField(Field<T> const& field, bool doReplace = false);
121 
123  Key<Flag> addField(Field<Flag> const& field, bool doReplace = false);
124 
126  template <typename T>
127  Key<Array<T> > addField(Field<Array<T> > const& field, bool doReplace = false);
128 
130  Key<std::string> addField(Field<std::string> const& field, bool doReplace = false);
131 
133  template <typename T>
134  void replaceField(Key<T> const& key, Field<T> const& field);
135 
143  ItemContainer const& getItems() const { return _items; }
144 
146  explicit SchemaImpl() : _recordSize(0), _lastFlagField(-1), _lastFlagBit(-1), _items() {}
147 
157  template <typename F>
158  struct VisitorWrapper : public boost::static_visitor<> {
160  template <typename T>
161  void operator()(SchemaItem<T> const& x) const {
162  _func(x);
163  };
164 
174  void operator()(ItemVariant const& v) const { boost::apply_visitor(*this, v); }
175 
177  template <typename T>
178  explicit VisitorWrapper(T&& func) : _func(std::forward<T>(func)) {}
179 
180  private:
181  F _func;
182  };
183 
184 private:
185  friend class detail::Access;
186 
187  template <typename T>
188  Key<T> addFieldImpl(int elementSize, int elementCount, Field<T> const& field, bool doReplace);
189 
190  int _recordSize; // Size of a record in bytes.
191  int _lastFlagField; // Offset of the last flag field in bytes.
192  int _lastFlagBit; // Bit of the last flag field.
193  ItemContainer _items; // Vector of variants of SchemaItem<T>.
194  NameMap _names; // Field name to vector-index map.
195  OffsetMap _offsets; // Offset to vector-index map for regular fields.
196  FlagMap _flags; // Offset to vector-index map for flags.
197 };
198 } // namespace detail
199 } // namespace table
200 } // namespace afw
201 } // namespace lsst
202 
203 #endif // !AFW_TABLE_DETAIL_SchemaImpl_h_INCLUDED
lsst::afw::table::detail::SchemaImpl::getFlagFieldCount
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:81
lsst::afw::table::detail::SchemaImpl::getItems
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:143
std::string
STL class.
lsst::afw::table::detail::SchemaImpl::NameMap
std::map< std::string, int > NameMap
A map from field names to position in the vector, so we can do name lookups.
Definition: SchemaImpl.h:68
lsst::afw::table::SchemaItem::key
Key< T > key
Definition: SchemaImpl.h:26
std::vector< ItemVariant >
std::map::find
T find(T... args)
std::map::size
T size(T... args)
lsst.pex::exceptions::NotFoundError
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
lsst::afw::table::detail::SchemaImpl
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:48
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::table::detail::SchemaImpl::OffsetMap
std::map< int, int > OffsetMap
A map from standard field offsets to position in the vector, so we can do field lookups.
Definition: SchemaImpl.h:70
lsst::afw::table::detail::SchemaImpl::VisitorWrapper::operator()
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:174
lsst::afw::table::detail::SchemaImpl::FlagMap
std::map< std::pair< int, int >, int > FlagMap
A map from Flag field offset/bit pairs to position in the vector, so we can do Flag field lookups.
Definition: SchemaImpl.h:72
lsst::afw::table::detail::SchemaImpl::getNonFlagFieldCount
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:84
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::afw::table::detail::SchemaImpl::VisitorWrapper::VisitorWrapper
VisitorWrapper(T &&func)
Construct the wrapper.
Definition: SchemaImpl.h:178
lsst::afw::table::detail::SchemaImpl::ItemContainer
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:66
lsst::afw::table::detail::SchemaImpl::getFieldCount
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:78
lsst::afw::table::detail::SchemaImpl::MakeItem::apply::type
SchemaItem< T > type
Definition: SchemaImpl.h:54
lsst::afw::table::SchemaItem::SchemaItem
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:29
lsst::afw::table::detail::SchemaImpl::ItemVariant
boost::make_variant_over< ItemTypes >::type ItemVariant
A Boost.Variant type that can hold any one of the allowed SchemaItem types.
Definition: SchemaImpl.h:64
x
double x
Definition: ChebyshevBoundedField.cc:277
lsst::afw::table::detail::SchemaImpl::MakeItem::apply
Definition: SchemaImpl.h:53
field
table::Key< int > field
Definition: ApCorrMap.cc:77
lsst::afw::table::SchemaItem
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25
lsst::afw::table::Key
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
lsst::afw::table::detail::SchemaImpl::find
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
Definition: Schema.cc:202
lsst::afw::table::Array
Tag types used to declare specialized field types.
Definition: misc.h:32
std::map< std::string, int >
lsst::afw::table::detail::SchemaImpl::contains
int contains(SchemaItem< T > const &item, int flags) const
Definition: Schema.cc:415
lsst::afw::table::detail::SchemaImpl::VisitorWrapper::operator()
void operator()(SchemaItem< T > const &x) const
Call the wrapped function.
Definition: SchemaImpl.h:161
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::table::Field
A description of a field in a table.
Definition: Field.h:24
lsst::afw::table::detail::Access
Definition: Access.h:31
std
STL namespace.
type
table::Key< int > type
Definition: Detector.cc:163
lsst::afw::table::detail::SchemaImpl::addField
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a field to the schema (used to implement Schema::addField).
Definition: Schema.cc:504
key
Key< U > key
Definition: Schema.cc:281
lsst::afw::table::SchemaItem::field
Field< T > field
Definition: SchemaImpl.h:27
std::map::end
T end(T... args)
lsst::afw::table::detail::SchemaImpl::VERSION
static int const VERSION
Definition: SchemaImpl.h:59
lsst::afw::table::detail::SchemaImpl::SchemaImpl
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:146
lsst::afw::table::detail::SchemaImpl::VisitorWrapper
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:158
astshim.fitsChanContinued.iter
def iter(self)
Definition: fitsChanContinued.py:88
prefix
std::string prefix
Definition: SchemaMapper.cc:79
std::set
STL class.
lsst::afw::table::detail::SchemaImpl::getNames
std::set< std::string > getNames(bool topOnly) const
Return a set of field names (used to implement Schema::getNames).
Definition: Schema.cc:441
lsst::afw::table::Key< Flag >
Key specialization for Flag.
Definition: Flag.h:94
lsst::afw::table::detail::SchemaImpl::findAndApply
void findAndApply(std::string const &name, F &&func) const
Find an item by name and run the given functor on it.
Definition: SchemaImpl.h:99
lsst::afw::table::detail::SchemaImpl::replaceField
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field in an existing SchemaItem without changing the Key.
Definition: Schema.cc:383
lsst::afw::table::detail::SchemaImpl::ItemTypes
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:62
lsst::afw::table::detail::SchemaImpl::getRecordSize
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:75