LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
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
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:94
VisitorWrapper(F func)
Construct the wrapper.
Definition: SchemaImpl.h:172
SchemaImpl()
Default constructor.
Definition: SchemaImpl.h:137
int getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:88
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:167
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.
Citizen(const std::type_info &)
Definition: Citizen.cc:173
A description of a field in a table.
Definition: Field.h:22
int getVersion() const
Return the table&#39;s version.
Definition: SchemaImpl.h:179
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:152
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
void setVersion(int version)
Set the table&#39;s version.
Definition: SchemaImpl.h:182
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:134
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
static int const DEFAULT_VERSION
Definition: SchemaImpl.h:69
void operator()(SchemaItem< T > const &x) const
Call the wrapped function.
Definition: SchemaImpl.h:156
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