LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.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
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