LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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 #include "lsst/daf/base/Citizen.h"
14 
15 namespace lsst {
16 namespace afw {
17 namespace table {
18 
19 class Schema;
20 class SubSchema;
21 
26 template <typename T>
27 struct SchemaItem final {
30 
31  SchemaItem(Key<T> const& key_, Field<T> const& field_) : key(key_), field(field_) {}
32 };
33 
34 namespace detail {
35 
36 class Access;
37 
54 private:
56  struct MakeItem {
57  template <typename T>
58  struct apply {
60  };
61  };
62 
63 public:
64  static int const VERSION = 3;
65 
78 
80  int getRecordSize() const { return _recordSize; }
81 
83  int getFieldCount() const { return _names.size(); }
84 
86  int getFlagFieldCount() const { return _flags.size(); }
87 
89  int getNonFlagFieldCount() const { return _offsets.size(); }
90 
92  template <typename T>
93  SchemaItem<T> find(std::string const& name) const;
94 
96  template <typename T>
97  SchemaItem<T> find(Key<T> const& key) const;
98 
100  SchemaItem<Flag> find(Key<Flag> const& key) const;
101 
103  template <typename F>
104  void findAndApply(std::string const& name, F&& func) const {
105  auto iter = _names.find(name);
106  if (iter == _names.end()) {
108  (boost::format("Field with name '%s' not found") % name).str());
109  }
110  VisitorWrapper<F> visitor(std::forward<F>(func));
111  visitor(_items[iter->second]);
112  }
113 
115  std::set<std::string> getNames(bool topOnly) const;
116 
118  std::set<std::string> getNames(bool topOnly, std::string const& prefix) const;
119 
120  template <typename T>
121  int contains(SchemaItem<T> const& item, int flags) const;
122 
124  template <typename T>
125  Key<T> addField(Field<T> const& field, bool doReplace = false);
126 
128  Key<Flag> addField(Field<Flag> const& field, bool doReplace = false);
129 
131  template <typename T>
132  Key<Array<T> > addField(Field<Array<T> > const& field, bool doReplace = false);
133 
135  Key<std::string> addField(Field<std::string> const& field, bool doReplace = false);
136 
138  template <typename T>
139  void replaceField(Key<T> const& key, Field<T> const& field);
140 
148  ItemContainer const& getItems() const { return _items; }
149 
151  explicit SchemaImpl()
152  : daf::base::Citizen(typeid(this)),
153  _recordSize(0),
154  _lastFlagField(-1),
155  _lastFlagBit(-1),
156  _items() {}
157 
167  template <typename F>
168  struct VisitorWrapper : public boost::static_visitor<> {
170  template <typename T>
171  void operator()(SchemaItem<T> const& x) const {
172  _func(x);
173  };
174 
184  void operator()(ItemVariant const& v) const { boost::apply_visitor(*this, v); }
185 
187  template <typename T>
188  explicit VisitorWrapper(T&& func) : _func(std::forward<T>(func)) {}
189 
190  private:
191  F _func;
192  };
193 
194 private:
195  friend class detail::Access;
196 
197  template <typename T>
198  Key<T> addFieldImpl(int elementSize, int elementCount, Field<T> const& field, bool doReplace);
199 
200  int _recordSize; // Size of a record in bytes.
201  int _lastFlagField; // Offset of the last flag field in bytes.
202  int _lastFlagBit; // Bit of the last flag field.
203  ItemContainer _items; // Vector of variants of SchemaItem<T>.
204  NameMap _names; // Field name to vector-index map.
205  OffsetMap _offsets; // Offset to vector-index map for regular fields.
206  FlagMap _flags; // Offset to vector-index map for flags.
207 };
208 } // namespace detail
209 } // namespace table
210 } // namespace afw
211 } // namespace lsst
212 
213 #endif // !AFW_TABLE_DETAIL_SchemaImpl_h_INCLUDED
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:80
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
VisitorWrapper(T &&func)
Construct the wrapper.
Definition: SchemaImpl.h:188
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:148
void findAndApply(std::string const &name, F &&func) const
Find an item by name and run the given functor on it.
Definition: SchemaImpl.h:104
STL namespace.
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:71
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:67
std::string prefix
Definition: SchemaMapper.cc:79
boost::make_variant_over< ItemTypes >::type ItemVariant
A Boost.Variant type that can hold any one of the allowed SchemaItem types.
Definition: SchemaImpl.h:69
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:75
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:89
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:167
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
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:171
double x
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:53
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:168
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:73
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:151
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:77
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:184
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:83
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:31
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:86
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27