LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
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 DEFAULT_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  void replaceField(Key<T> const & key, Field<T> const & field);
126 
134  ItemContainer const & getItems() const { return _items; }
135 
137  explicit SchemaImpl() :
138  daf::base::Citizen(typeid(this)),
140  {}
141 
151  template <typename F>
152  struct VisitorWrapper : public boost::static_visitor<> {
153 
155  template <typename T>
156  void operator()(SchemaItem<T> const & x) const { _func(x); };
157 
167  void operator()(ItemVariant const & v) const {
168  boost::apply_visitor(*this, v);
169  }
170 
172  explicit VisitorWrapper(F func) : _func(func) {}
173 
174  private:
175  F _func;
176  };
177 
179  int getVersion() const { return _version; }
180 
182  void setVersion(int version) { _version = version; }
183 
184 
185 private:
186 
187  friend class detail::Access;
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  int _version; // temporary flag for version 0/1 tables
197 };
198 
199 #endif
200 
201 }}}} // namespace lsst::afw::table::detail
202 
203 #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).
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:156
void setVersion(int version)
Set the table&#39;s version.
Definition: SchemaImpl.h:182
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:172
std::vector< ItemVariant > ItemContainer
A std::vector whose elements can be any of the allowed SchemaItem types.
Definition: SchemaImpl.h:76
int x
A description of a field in a table.
Definition: Field.h:22
void operator()(ItemVariant const &v) const
Invoke the visitation.
Definition: SchemaImpl.h:167
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:134
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:152
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
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:137
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 getVersion() const
Return the table&#39;s version.
Definition: SchemaImpl.h:179
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
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