LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:75
VisitorWrapper(T &&func)
Construct the wrapper.
Definition: SchemaImpl.h:178
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:143
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
STL namespace.
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:66
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:62
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
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:84
STL class.
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
A base class for image defects.
table::Key< int > type
Definition: Detector.cc:163
A description of a field in a table.
Definition: Field.h:24
Tag types used to declare specialized field types.
Definition: misc.h:32
void operator()(SchemaItem< T > const &x) const
Call the wrapped function.
Definition: SchemaImpl.h:161
double x
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:48
STL class.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:158
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
Key specialization for Flag.
Definition: Flag.h:94
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
std::string prefix
Definition: SchemaMapper.cc:79
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:146
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
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:174
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:78
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:29
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:81
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25