LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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 { namespace afw { namespace table {
16 
17 class Schema;
18 class SubSchema;
19 
24 template <typename T>
25 struct SchemaItem {
26 #ifndef SWIG // see comment block in tableLib.i; workaround to avoid dangling references
29 #endif
30  SchemaItem(Key<T> const & key_, Field<T> const & field_) : key(key_), field(field_) {}
31 };
32 
33 namespace detail {
34 
35 #ifndef SWIG
36 
37 class Access;
38 
55 private:
56 
58  struct MakeItem {
59  template <typename T>
60  struct apply {
62  };
63  };
64 
65 public:
66 
67  static int const VERSION = 1;
68 
70  typedef boost::mpl::transform<FieldTypes,MakeItem>::type ItemTypes;
72  typedef boost::make_variant_over< ItemTypes >::type ItemVariant;
74  typedef std::vector<ItemVariant> ItemContainer;
76  typedef std::map<std::string,int> NameMap;
78  typedef std::map<int,int> OffsetMap;
80  typedef std::map<std::pair<int,int>,int> FlagMap;
81 
83  int getRecordSize() const { return _recordSize; }
84 
86  int getFieldCount() const { return _names.size(); }
87 
89  int getFlagFieldCount() const { return _flags.size(); }
90 
92  int getNonFlagFieldCount() const { return _offsets.size(); }
93 
95  template <typename T>
96  SchemaItem<T> find(std::string const & name) const;
97 
99  template <typename T>
100  SchemaItem<T> find(Key<T> const & key) const;
101 
103  SchemaItem<Flag> find(Key<Flag> const & key) const;
104 
106  std::set<std::string> getNames(bool topOnly) const;
107 
109  std::set<std::string> getNames(bool topOnly, std::string const & prefix) const;
110 
111  template <typename T>
112  int contains(SchemaItem<T> const & item, int flags) const;
113 
115  template <typename T>
116  Key<T> addField(Field<T> const & field, bool doReplace=false);
117 
119  Key<Flag> addField(Field<Flag> const & field, bool doReplace=false);
120 
122  template <typename T>
123  Key< Array<T> > addField(Field< Array<T> > const & field, bool doReplace=false);
124 
126  template <typename T>
127  void replaceField(Key<T> const & key, Field<T> const & field);
128 
136  ItemContainer const & getItems() const { return _items; }
137 
139  explicit SchemaImpl() :
140  daf::base::Citizen(typeid(this)),
142  {}
143 
153  template <typename F>
154  struct VisitorWrapper : public boost::static_visitor<> {
155 
157  template <typename T>
158  void operator()(SchemaItem<T> const & x) const { _func(x); };
159 
169  void operator()(ItemVariant const & v) const {
170  boost::apply_visitor(*this, v);
171  }
172 
174  template <typename T>
175  explicit VisitorWrapper(T&& func) : _func(std::forward<T>(func)) {}
176 
177  private:
178  F _func;
179  };
180 
181 private:
182 
183  friend class detail::Access;
184 
185  template <typename T>
186  Key<T> addFieldImpl(int elementSize, int elementCount, Field<T> const & field, bool doReplace);
187 
188  int _recordSize; // Size of a record in bytes.
189  int _lastFlagField; // Offset of the last flag field in bytes.
190  int _lastFlagBit; // Bit of the last flag field.
191  ItemContainer _items; // Vector of variants of SchemaItem<T>.
192  NameMap _names; // Field name to vector-index map.
193  OffsetMap _offsets; // Offset to vector-index map for regular fields.
194  FlagMap _flags; // Offset to vector-index map for flags.
195 };
196 
197 #endif
198 
199 }}}} // namespace lsst::afw::table::detail
200 
201 #endif // !AFW_TABLE_DETAIL_SchemaImpl_h_INCLUDED
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:70
Citizen(const std::type_info &)
Definition: Citizen.cc:174
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:74
Boost.MPL metafunction that returns a SchemaItem&lt;T&gt; given a T.
Definition: SchemaImpl.h:58
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:78
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:89
int contains(SchemaItem< T > const &item, int flags) const
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:139
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:136
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:80
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:92
Key< T > addFieldImpl(int elementSize, int elementCount, Field< T > const &field, bool doReplace)
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:76
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field in an existing SchemaItem without changing the Key.
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:169
A description of a field in a table.
Definition: Field.h:22
Tag types used to declare specialized field types.
Definition: misc.h:34
double x
void operator()(SchemaItem< T > const &x) const
Call the wrapped function.
Definition: SchemaImpl.h:158
VisitorWrapper(T &&func)
Construct the wrapper.
Definition: SchemaImpl.h:175
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:54
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:154
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
Key specialization for Flag.
Definition: Flag.h:84
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a field to the schema (used to implement Schema::addField).
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:83
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:30
std::set< std::string > getNames(bool topOnly) const
Return a set of field names (used to implement Schema::getNames).
boost::make_variant_over< ItemTypes >::type ItemVariant
A Boost.Variant type that can hold any one of the allowed SchemaItem types.
Definition: SchemaImpl.h:72
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:86
table::Key< int > field
Definition: ApCorrMap.cc:72
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25