LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
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
table::Key< std::string > name
Definition: Amplifier.cc:116
table::Key< int > field
Definition: ApCorrMap.cc:77
double x
table::Key< int > type
Definition: Detector.cc:163
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Key< U > key
Definition: Schema.cc:281
std::string prefix
Definition: SchemaMapper.cc:79
Tag types used to declare specialized field types.
Definition: misc.h:32
Key specialization for Flag.
Definition: Flag.h:94
A class used as a handle to a particular field in a table.
Definition: Key.h:53
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:48
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
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:143
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:62
std::set< std::string > getNames(bool topOnly) const
Return a set of field names (used to implement Schema::getNames).
Definition: Schema.cc:441
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:146
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
int contains(SchemaItem< T > const &item, int flags) const
Definition: Schema.cc:415
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:75
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:84
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
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
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:78
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
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
Definition: Schema.cc:202
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:81
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:66
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
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
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
T end(T... args)
T find(T... args)
class[[deprecated("Removed with no replacement (but see lsst::afw::image::TransmissionCurve). Will be " "removed after v22.")]] FilterProperty final
Describe the properties of a Filter (e.g.
Definition: Filter.h:53
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
STL namespace.
T size(T... args)
A description of a field in a table.
Definition: Field.h:24
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:29
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:158
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:174
void operator()(SchemaItem< T > const &x) const
Call the wrapped function.
Definition: SchemaImpl.h:161
VisitorWrapper(T &&func)
Construct the wrapper.
Definition: SchemaImpl.h:178