LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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
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:78
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:160
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
int getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:91
Boost.MPL metafunction that returns a SchemaItem&lt;T&gt; given a T.
Definition: SchemaImpl.h:60
VisitorWrapper(F func)
Construct the wrapper.
Definition: SchemaImpl.h:176
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:76
A description of a field in a table.
Definition: Field.h:22
Tag types used to declare specialized field types.
Definition: misc.h:35
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:171
double x
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:56
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:138
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 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< int, int > OffsetMap
A map from standard field offsets to position in the vector, so we can do field lookups.
Definition: SchemaImpl.h:80
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:94
int getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:85
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:141
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:56
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:88
boost::mpl::transform< FieldTypes, MakeItem >::type ItemTypes
An MPL sequence of all the allowed SchemaItem templates.
Definition: SchemaImpl.h:72
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:32
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27