LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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
std::set< std::string > getNames(bool topOnly) const
Return a set of field names (used to implement Schema::getNames).
table::Key< std::string > name
Definition: ApCorrMap.cc:71
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()(SchemaItem< T > const &x) const
Call the wrapped function.
Definition: SchemaImpl.h:158
VisitorWrapper(T &&func)
Construct the wrapper.
Definition: SchemaImpl.h:175
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 getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:89
Boost.MPL metafunction that returns a SchemaItem&lt;T&gt; given a T.
Definition: SchemaImpl.h:58
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:74
A description of a field in a table.
Definition: Field.h:22
Tag types used to declare specialized field types.
Definition: misc.h:34
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:169
double x
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:54
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:136
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
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
Citizen(const std::type_info &)
Definition: Citizen.cc:174
Key specialization for Flag.
Definition: Flag.h:84
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
Key< T > addFieldImpl(int elementSize, int elementCount, Field< T > const &field, bool doReplace)
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:92
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:83
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:139
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:86
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:70
table::Key< int > field
Definition: ApCorrMap.cc:72
int contains(SchemaItem< T > const &item, int flags) const
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a field to the schema (used to implement Schema::addField).
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:30
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25