lsst.afw  21.0.0-44-ge87b71154+532d44eec1
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 <variant>
9 
10 namespace lsst {
11 namespace afw {
12 namespace table {
13 
14 class Schema;
15 class SubSchema;
16 
21 template <typename T>
22 struct SchemaItem final {
25 
26  SchemaItem(Key<T> const& key_, Field<T> const& field_) : key(key_), field(field_) {}
27 };
28 
29 namespace detail {
30 
31 class Access;
32 
45 class SchemaImpl {
46 private:
49  template <typename ...E>
50  static std::variant<SchemaItem<E>...> makeItemVariantType(TypeList<E...>);
51 public:
52  static int const VERSION = 3;
53 
55  using ItemVariant = decltype(makeItemVariantType(FieldTypes{}));
64 
66  int getRecordSize() const { return _recordSize; }
67 
69  int getFieldCount() const { return _names.size(); }
70 
72  int getFlagFieldCount() const { return _flags.size(); }
73 
75  int getNonFlagFieldCount() const { return _offsets.size(); }
76 
78  template <typename T>
79  SchemaItem<T> find(std::string const& name) const;
80 
82  template <typename T>
83  SchemaItem<T> find(Key<T> const& key) const;
84 
86  SchemaItem<Flag> find(Key<Flag> const& key) const;
87 
89  template <typename F>
90  decltype(auto) findAndApply(std::string const& name, F&& func) const {
91  auto iter = _names.find(name);
92  if (iter == _names.end()) {
94  (boost::format("Field with name '%s' not found") % name).str());
95  }
96  return std::visit(std::forward<F>(func), _items[iter->second]);
97  }
98 
100  std::set<std::string> getNames(bool topOnly) const;
101 
103  std::set<std::string> getNames(bool topOnly, std::string const& prefix) const;
104 
105  template <typename T>
106  int contains(SchemaItem<T> const& item, int flags) const;
107 
109  template <typename T>
110  Key<T> addField(Field<T> const& field, bool doReplace = false);
111 
113  Key<Flag> addField(Field<Flag> const& field, bool doReplace = false);
114 
116  template <typename T>
117  Key<Array<T> > addField(Field<Array<T> > const& field, bool doReplace = false);
118 
120  Key<std::string> addField(Field<std::string> const& field, bool doReplace = false);
121 
123  template <typename T>
124  void replaceField(Key<T> const& key, Field<T> const& field);
125 
133  ItemContainer const& getItems() const { return _items; }
134 
136  explicit SchemaImpl() : _recordSize(0), _lastFlagField(-1), _lastFlagBit(-1), _items() {}
137 
138 private:
139  friend class detail::Access;
140 
141  template <typename T>
142  Key<T> addFieldImpl(int elementSize, int elementCount, Field<T> const& field, bool doReplace);
143 
144  int _recordSize; // Size of a record in bytes.
145  int _lastFlagField; // Offset of the last flag field in bytes.
146  int _lastFlagBit; // Bit of the last flag field.
147  ItemContainer _items; // Vector of variants of SchemaItem<T>.
148  NameMap _names; // Field name to vector-index map.
149  OffsetMap _offsets; // Offset to vector-index map for regular fields.
150  FlagMap _flags; // Offset to vector-index map for flags.
151 };
152 } // namespace detail
153 } // namespace table
154 } // namespace afw
155 } // namespace lsst
156 
157 #endif // !AFW_TABLE_DETAIL_SchemaImpl_h_INCLUDED
table::Key< std::string > name
Definition: Amplifier.cc:116
table::Key< int > field
Definition: ApCorrMap.cc:77
#define LSST_EXCEPT(type,...)
std::string prefix
Definition: SchemaMapper.cc:72
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:45
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:63
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:133
std::set< std::string > getNames(bool topOnly) const
Return a set of field names (used to implement Schema::getNames).
Definition: Schema.cc:252
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:136
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a field to the schema (used to implement Schema::addField).
Definition: Schema.cc:315
int contains(SchemaItem< T > const &item, int flags) const
Definition: Schema.cc:226
decltype(auto) findAndApply(std::string const &name, F &&func) const
Find an item by name and run the given functor on it.
Definition: SchemaImpl.h:90
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:66
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:75
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field in an existing SchemaItem without changing the Key.
Definition: Schema.cc:194
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:69
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:59
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
Definition: Schema.cc:93
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:72
decltype(makeItemVariantType(FieldTypes{})) ItemVariant
A Boost.Variant type that can hold any one of the allowed SchemaItem types.
Definition: SchemaImpl.h:55
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:57
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:61
T end(T... args)
T find(T... args)
def iter(self)
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
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:22
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:26