LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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:164
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