LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 #include "boost/type_traits/remove_const.hpp"
13 #include "boost/type_traits/remove_reference.hpp"
14 
15 #include "lsst/daf/base/Citizen.h"
16 
17 namespace lsst { namespace afw { namespace table {
18 
19 class Schema;
20 class SubSchema;
21 
26 template <typename T>
27 struct SchemaItem {
28 #ifndef SWIG // see comment block in tableLib.i; workaround to avoid dangling references
31 #endif
32  SchemaItem(Key<T> const & key_, Field<T> const & field_) : key(key_), field(field_) {}
33 };
34 
35 namespace detail {
36 
37 #ifndef SWIG
38 
39 class Access;
40 
57 private:
58 
60  struct MakeItem {
61  template <typename T>
62  struct apply {
64  };
65  };
66 
67 public:
68 
69  static int const VERSION = 1;
70 
72  typedef boost::mpl::transform<FieldTypes,MakeItem>::type ItemTypes;
74  typedef boost::make_variant_over< ItemTypes >::type ItemVariant;
76  typedef std::vector<ItemVariant> ItemContainer;
78  typedef std::map<std::string,int> NameMap;
80  typedef std::map<int,int> OffsetMap;
82  typedef std::map<std::pair<int,int>,int> FlagMap;
83 
85  int getRecordSize() const { return _recordSize; }
86 
88  int getFieldCount() const { return _names.size(); }
89 
91  int getFlagFieldCount() const { return _flags.size(); }
92 
94  int getNonFlagFieldCount() const { return _offsets.size(); }
95 
97  template <typename T>
98  SchemaItem<T> find(std::string const & name) const;
99 
101  template <typename T>
102  SchemaItem<T> find(Key<T> const & key) const;
103 
105  SchemaItem<Flag> find(Key<Flag> const & key) const;
106 
108  std::set<std::string> getNames(bool topOnly) const;
109 
111  std::set<std::string> getNames(bool topOnly, std::string const & prefix) const;
112 
113  template <typename T>
114  int contains(SchemaItem<T> const & item, int flags) const;
115 
117  template <typename T>
118  Key<T> addField(Field<T> const & field, bool doReplace=false);
119 
121  Key<Flag> addField(Field<Flag> const & field, bool doReplace=false);
122 
124  template <typename T>
125  Key< Array<T> > addField(Field< Array<T> > const & field, bool doReplace=false);
126 
128  template <typename T>
129  void replaceField(Key<T> const & key, Field<T> const & field);
130 
138  ItemContainer const & getItems() const { return _items; }
139 
141  explicit SchemaImpl() :
142  daf::base::Citizen(typeid(this)),
144  {}
145 
155  template <typename F>
156  struct VisitorWrapper : public boost::static_visitor<> {
157 
159  template <typename T>
160  void operator()(SchemaItem<T> const & x) const { _func(x); };
161 
171  void operator()(ItemVariant const & v) const {
172  boost::apply_visitor(*this, v);
173  }
174 
176  explicit VisitorWrapper(F func) : _func(func) {}
177 
178  private:
179  F _func;
180  };
181 
182 private:
183 
184  friend class detail::Access;
185 
186  template <typename T>
187  Key<T> addFieldImpl(int elementSize, int elementCount, Field<T> const & field, bool doReplace);
188 
189  int _recordSize; // Size of a record in bytes.
190  int _lastFlagField; // Offset of the last flag field in bytes.
191  int _lastFlagBit; // Bit of the last flag field.
192  ItemContainer _items; // Vector of variants of SchemaItem<T>.
193  NameMap _names; // Field name to vector-index map.
194  OffsetMap _offsets; // Offset to vector-index map for regular fields.
195  FlagMap _flags; // Offset to vector-index map for flags.
196 };
197 
198 #endif
199 
200 }}}} // namespace lsst::afw::table::detail
201 
202 #endif // !AFW_TABLE_DETAIL_SchemaImpl_h_INCLUDED
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:94
VisitorWrapper(F func)
Construct the wrapper.
Definition: SchemaImpl.h:176
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:141
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:88
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:91
int contains(SchemaItem< T > const &item, int flags) const
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:171
std::set< std::string > getNames(bool topOnly) const
Return a set of field names (used to implement Schema::getNames).
Boost.MPL metafunction that returns a SchemaItem&lt;T&gt; given a T.
Definition: SchemaImpl.h:60
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:80
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:85
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
SchemaItem(Key< T > const &key_, Field< T > const &field_)
Definition: SchemaImpl.h:32
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
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.
tbl::Key< int > field
A description of a field in a table.
Definition: Field.h:22
Key< T > addFieldImpl(int elementSize, int elementCount, Field< T > const &field, bool doReplace)
Tag types used to declare specialized field types.
Definition: misc.h:35
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:56
int x
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a field to the schema (used to implement Schema::addField).
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:156
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:173
Key specialization for Flag.
Definition: Flag.h:82
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:82
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:72
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:138
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:78
void operator()(SchemaItem< T > const &x) const
Call the wrapped function.
Definition: SchemaImpl.h:160
boost::make_variant_over< ItemTypes >::type ItemVariant
A Boost.Variant type that can hold any one of the allowed SchemaItem types.
Definition: SchemaImpl.h:74
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27