casacore
Loading...
Searching...
No Matches
BitFlagsEngine.h
Go to the documentation of this file.
1//# BitFlagsEngine.h: Templated virtual column engine to map bit flags to a Bool
2//# Copyright (C) 2009
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef TABLES_BITFLAGSENGINE_H
27#define TABLES_BITFLAGSENGINE_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/DataMan/BaseMappedArrayEngine.h>
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35
36 // <summary> Non-templated Helper class to handle the mask. </summary>
37 // <use visibility=local>
38 class BFEngineMask
39 {
40 public:
41 // Form the mask as given.
42 explicit BFEngineMask (uInt mask=0xffffffff);
44 // Form the mask from the given keywords defining the bits.
45 BFEngineMask (const Array<String>& keys, uInt defaultMask);
46
47 // Make the mask from the given keywords defining the bits.
48 void makeMask (const TableColumn& column);
49
50 // Form the read mask from the specification.
51 // If keywords are given, the mask is formed from them.
52 void fromRecord (const RecordInterface& spec, const TableColumn& column,
53 const String& prefix);
54
55 // Store the info in a Record.
56 void toRecord (RecordInterface& spec, const String& prefix) const;
57
58 // Get the mask.
59 uInt getMask() const
60 { return itsMask; }
61
62 // Get the mask keywords.
63 const Array<String>& getKeys() const
64 { return itsMaskKeys; }
65
66 private:
69 };
70
71
72 // <summary>
73 // Templated virtual column engine to map bit flags to a Bool.
74 // </summary>
75
76 // <use visibility=export>
77
78 // <reviewed reviewer="Gareth Hunt" date="94Nov17" tests="">
79 // </reviewed>
80
81 // <prerequisite>
82 //# Classes you should understand before using this one.
83 // <li> VirtualColumnEngine
84 // <li> VirtualArrayColumn
85 // </prerequisite>
86
87 // <synopsis>
88 // BitFlagsEngine is a virtual column engine which maps an integer column
89 // containing flag bits to a Bool column. It can be used in a MeasurementSet
90 // to have multiple flag categories, yet use all existing software that
91 // deals with the Bool FLAG column.
92 //
93 // The engine support read as well as write access.
94 // For both cases a mask can be defined telling which bits have to be taken
95 // into account. For example, when writing to the Bool FLAG column, the data
96 // in the bitflags column twill be or-ed with the bits as defined in the
97 // writemask. Similary when reading FLAG, only the bits of the readmask are
98 // taken into account.
99 //
100 // The masks can be defined in two ways:
101 // <ul>
102 // <li> The mask can be given directly as an integer value.
103 // The default write mask is 1 (thus only bit 0), while the default
104 // read mask is all bits.
105 // <li> Symbolic names for mask bits can be defined as keywords in the
106 // flagbits column. They define the bit value, not the bit number.
107 // It makes it possible to combine bits in a keyword.
108 // The keywords are stored in a subrecord of keyword FLAGSETS.
109 // Example of keyword and their values could be:
110 // <br>RFI=1, CAL=2, CLIP=4, OTHER=8, RFICAL=3
111 // <br>Note that in this example RFICAL is defined such that it
112 // contains RFI and CAL.
113 // </ul>
114 // A mask can be set at construction time, but it can be changed at runtime
115 // using the <src>setProperties</src> function.
116 // The masks are kept in special keywords (which are different from the
117 // keywords defining the flag bits), so it is possible to change a mask
118 // by changing those keywords before opening a table. However, that is
119 // not recommended.
120 //
121 // BitFlagsEngine is known to the table system for data types uChar, Short,
122 // and Int.
123 // </synopsis>
124
125 // <motivation>
126 // The FLAG_CATEGORY defined the Measurement does not work because adding
127 // an extra flag means resizing the entire array which is slow.
128 // This class makes it possible to use an integer column to store flags
129 // and map it directly to a Bool column.
130 // </motivation>
131
132 // <example>
133 // <srcblock>
134 // // Create the table description and 2 columns with indirect arrays in it.
135 // // The Int column will be stored, while the Bool will be used as virtual.
136 // TableDesc tableDesc ("", TableDesc::Scratch);
137 // tableDesc.addColumn (ArrayColumnDesc<Int> ("BitBlags"));
138 // tableDesc.addColumn (ArrayColumnDesc<Bool> ("FLAG"));
139 //
140 // // Create a new table using the table description.
141 // SetupNewTable newtab (tableDesc, "tab.data", Table::New);
142 //
143 // // Create the engine and bind the FLAG column to it.
144 // BitFlagsEngine<Int> flagsEngine("FLAG", "BitFlags");
145 // newtab.bindColumn ("FLAG", flagsEngine);
146 // // Create the table.
147 // Table table (newtab);
148 //
149 // // Store a 3-D array (with dim. 2,3,4) into each row of the column.
150 // // The shape of each array in the column is implicitly set by the put
151 // // function. This will also set the shape of the underlying Int array.
152 // ArrayColumn data (table, "virtualArray");
153 // Array<Bool> someArray(IPosition(4,2,3,4));
154 // someArray = True;
155 // for (rownr_t i=0, i<10; i++) { // table will have 10 rows
156 // table.addRow();
157 // data.put (i, someArray)
158 // }
159 // </srcblock>
160 // The underlying integer array will be stored according to the writemask
161 // which defaults to 1.
162 // </example>
163
164 // <templating arg=StoredType>
165 // <li> only suited for built-in integer data types
166 // </templating>
167
168 template<typename StoredType> class BitFlagsEngine : public BaseMappedArrayEngine<Bool, StoredType>
169 {
170 //# Make members of parent class known.
171 public:
173 protected:
174 using BaseMappedArrayEngine<Bool,StoredType>::storedName;
175 using BaseMappedArrayEngine<Bool,StoredType>::table;
176 using BaseMappedArrayEngine<Bool,StoredType>::column;
177 using BaseMappedArrayEngine<Bool,StoredType>::setNames;
178
179 public:
180 // Construct an engine to map integer arrays in a column to Bool arrays.
181 // StoredColumnName is the name of the column where the integer
182 // data will be put and must have data type StoredType.
183 // The virtual column using this engine must have data type Bool.
184 // <br>A mask can be given that specifies which bits to use in the mapping
185 // from StoredType to Bool. Similarly a mask can be given defining which
186 // bits to set when mapping from Bool to StoredType.
187 BitFlagsEngine (const String& virtualColumnName,
188 const String& storedColumnName,
189 StoredType readMask=StoredType(0xffffffff),
190 StoredType writeMask=1);
191
192 // Construct an engine to map integer arrays in a column to Bool arrays.
193 // StoredColumnName is the name of the column where the scaled
194 // data will be put and must have data type StoredType.
195 // The virtual column using this engine must have data type Bool.
196 // <br>A mask can be given that specifies which bits to use in the mapping
197 // from StoredType to Bool. Similarly a mask can be given defining which
198 // bits to set when mapping from Bool to StoredType.
199 // The masks are given using the values of keywords in the stored column.
200 // Each keyword should be an integer defining one or more bits and can be
201 // seen as a symbolic name. The keyword values are or-ed to form the mask.
202 // The keywords are stored in a subrecord of keyword FLAGSETS.
203 BitFlagsEngine (const String& virtualColumnName,
204 const String& storedColumnName,
205 const Array<String>& readMaskKeys,
206 const Array<String>& writeMaskKeys);
207
208 // Construct from a record specification as created by dataManagerSpec().
209 BitFlagsEngine (const Record& spec);
210
211 // Destructor is mandatory.
213
214 // Assignment is not needed and therefore forbidden
216
217 // Return the type name of the engine (i.e. its class name).
218 virtual String dataManagerType() const;
219
220 // Get the name given to the engine (is the virtual column name).
221 virtual String dataManagerName() const;
222
223 // Record a record containing data manager specifications.
224 virtual Record dataManagerSpec() const;
225
226 // Get data manager properties that can be modified.
227 // These are ReadMask, WriteMask, ReadMaskKeys, and WriteMaskKeys.
228 // It is a subset of the data manager specification.
229 virtual Record getProperties() const;
230
231 // Modify data manager properties.
232 // These are ReadMask, WriteMask, ReadMaskKeys, and/or WriteMaskKeys.
233 // Mask keys should be given as an array of strings giving the keyword
234 // names defining mask bits (similar to the constructor). Mask keys are
235 // only used if not empty.
236 virtual void setProperties (const Record& spec);
237
238 // Return the name of the class.
239 // This includes the names of the template arguments.
241
242 // Register the class name and the static makeObject "constructor".
243 // This will make the engine known to the table system.
244 // The automatically invoked registration function in DataManReg.cc
245 // contains BitFlagsEngine<Int>.
246 // Any other instantiation of this class must be registered "manually"
247 // (or added to DataManReg.cc).
248 static void registerClass();
249
250 private:
251 // Copy constructor is only used by clone().
252 // (so it is made private).
254
255 // Clone the engine object.
257
258 // Initialize the object for a new table.
259 // It defines the keywords containing the engine parameters.
260 void create64 (rownr_t initialNrrow);
261
262 // Preparing consists of setting the writable switch and
263 // adding the initial number of rows in case of create.
264 // Furthermore it reads the keywords containing the engine parameters.
265 void prepare();
266
267 // Get an array in the given row.
268 // This will scale and offset from the underlying array.
270
271 // Put an array in the given row.
272 // This will scale and offset to the underlying array.
273 void putArray (rownr_t rownr, const Array<Bool>& array);
274
275 // Get a section of the array in the given row.
276 // This will scale and offset from the underlying array.
277 void getSlice (rownr_t rownr, const Slicer& slicer, Array<Bool>& array);
278
279 // Put into a section of the array in the given row.
280 // This will scale and offset to the underlying array.
281 void putSlice (rownr_t rownr, const Slicer& slicer,
282 const Array<Bool>& array);
283
284 // Get an entire column.
285 // This will scale and offset from the underlying array.
287
288 // Put an entire column.
289 // This will scale and offset to the underlying array.
291
292 // Get some array values in the column.
293 // This will scale and offset from the underlying array.
294 virtual void getArrayColumnCells (const RefRows& rownrs,
295 Array<Bool>& data);
296
297 // Put some array values in the column.
298 // This will scale and offset to the underlying array.
299 virtual void putArrayColumnCells (const RefRows& rownrs,
300 const Array<Bool>& data);
301
302 // Get a section of all arrays in the column.
303 // This will scale and offset from the underlying array.
304 void getColumnSlice (const Slicer& slicer, Array<Bool>& array);
305
306 // Put a section of all arrays in the column.
307 // This will scale and offset to the underlying array.
308 void putColumnSlice (const Slicer& slicer, const Array<Bool>& array);
309
310 // Get a section of some arrays in the column.
311 // This will scale and offset from the underlying array.
312 virtual void getColumnSliceCells (const RefRows& rownrs,
313 const Slicer& slicer,
314 Array<Bool>& data);
315
316 // Put into a section of some arrays in the column.
317 // This will scale and offset to the underlying array.
318 virtual void putColumnSliceCells (const RefRows& rownrs,
319 const Slicer& slicer,
320 const Array<Bool>& data);
321
322 // Map bit flags array to Bool array.
323 // This is meant when reading an array from the stored column.
325 const Array<StoredType>& stored);
326
327 // Map Bool array to bit flags array.
328 // This is meant when writing an array into the stored column.
330 Array<StoredType>& stored);
331
332 // Functor to and an array and mask and convert to Bool.
334 {
335 explicit FlagsToBool(StoredType readMask) : itsMask(readMask) {}
336 Bool operator() (StoredType value) const
337 { return (value & itsMask) != 0; }
338 private:
339 StoredType itsMask;
340 };
341 // Functor to convert Bools to flags using a mask.
342 // By default only bit 0 is set.
343 // Flag bits not affected are kept.
345 {
346 explicit BoolToFlags(StoredType writeMask) : itsMask(writeMask) {}
347 StoredType operator() (Bool flag, StoredType value) const
348 { return (flag ? value&itsMask : value); }
349 private:
350 StoredType itsMask;
351 };
352
353 public:
354 // Define the "constructor" to construct this engine when a
355 // table is read back.
356 // This "constructor" has to be registered by the user of the engine.
357 // If the engine is commonly used, its registration can be added
358 // to the registerAllCtor function in DataManReg.cc.
359 // That function gets automatically invoked by the table system.
361 const Record& spec);
362
363 private:
366 StoredType itsReadMask;
367 StoredType itsWriteMask;
368 Bool itsIsNew; //# True = new table
369 };
370
371
372} //# NAMESPACE CASACORE - END
373
374#ifndef CASACORE_NO_AUTO_TEMPLATES
375#include <casacore/tables/DataMan/BitFlagsEngine.tcc>
376#endif //# CASACORE_NO_AUTO_TEMPLATES
377#endif
BFEngineMask(const Array< String > &keys, uInt defaultMask)
Form the mask from the given keywords defining the bits.
BFEngineMask(uInt mask=0xffffffff)
Form the mask as given.
void toRecord(RecordInterface &spec, const String &prefix) const
Store the info in a Record.
uInt getMask() const
Get the mask.
const Array< String > & getKeys() const
Get the mask keywords.
Array< String > itsMaskKeys
void makeMask(const TableColumn &column)
Make the mask from the given keywords defining the bits.
void fromRecord(const RecordInterface &spec, const TableColumn &column, const String &prefix)
Form the read mask from the specification.
ArrayColumn< StoredType > & column()
Give access to the stored column.
void setNames(const String &virtualName, const String &storedName)
Set the virtual and stored column name.
const String & storedName() const
Get the stored column name.
const String & virtualName() const
Get the virtual column name.
Templated virtual column engine to map bit flags to a Bool.
void mapOnGet(Array< Bool > &array, const Array< StoredType > &stored)
Map bit flags array to Bool array.
virtual void getArrayColumnCells(const RefRows &rownrs, Array< Bool > &data)
Get some array values in the column.
BitFlagsEngine(const BitFlagsEngine< StoredType > &)
Copy constructor is only used by clone().
void putArrayColumn(const Array< Bool > &array)
Put an entire column.
void putArray(rownr_t rownr, const Array< Bool > &array)
Put an array in the given row.
BitFlagsEngine(const String &virtualColumnName, const String &storedColumnName, StoredType readMask=StoredType(0xffffffff), StoredType writeMask=1)
Construct an engine to map integer arrays in a column to Bool arrays.
virtual Record getProperties() const
Get data manager properties that can be modified.
void putColumnSlice(const Slicer &slicer, const Array< Bool > &array)
Put a section of all arrays in the column.
void getColumnSlice(const Slicer &slicer, Array< Bool > &array)
Get a section of all arrays in the column.
void mapOnPut(const Array< Bool > &array, Array< StoredType > &stored)
Map Bool array to bit flags array.
static void registerClass()
Register the class name and the static makeObject "constructor".
virtual String dataManagerName() const
Get the name given to the engine (is the virtual column name).
void create64(rownr_t initialNrrow)
Initialize the object for a new table.
void getSlice(rownr_t rownr, const Slicer &slicer, Array< Bool > &array)
Get a section of the array in the given row.
void getArray(rownr_t rownr, Array< Bool > &array)
Get an array in the given row.
virtual void putArrayColumnCells(const RefRows &rownrs, const Array< Bool > &data)
Put some array values in the column.
void putSlice(rownr_t rownr, const Slicer &slicer, const Array< Bool > &array)
Put into a section of the array in the given row.
~BitFlagsEngine()
Destructor is mandatory.
virtual void setProperties(const Record &spec)
Modify data manager properties.
virtual void putColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, const Array< Bool > &data)
Put into a section of some arrays in the column.
BitFlagsEngine< StoredType > & operator=(const BitFlagsEngine< StoredType > &)=delete
Assignment is not needed and therefore forbidden.
BitFlagsEngine(const Record &spec)
Construct from a record specification as created by dataManagerSpec().
virtual Record dataManagerSpec() const
Record a record containing data manager specifications.
DataManager * clone() const
Clone the engine object.
virtual void getColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, Array< Bool > &data)
Get a section of some arrays in the column.
static String className()
Return the name of the class.
void getArrayColumn(Array< Bool > &array)
Get an entire column.
static DataManager * makeObject(const String &dataManagerType, const Record &spec)
Define the "constructor" to construct this engine when a table is read back.
virtual String dataManagerType() const
Return the type name of the engine (i.e.
BitFlagsEngine(const String &virtualColumnName, const String &storedColumnName, const Array< String > &readMaskKeys, const Array< String > &writeMaskKeys)
Construct an engine to map integer arrays in a column to Bool arrays.
void prepare()
Preparing consists of setting the writable switch and adding the initial number of rows in case of cr...
Abstract base class for a data manager.
Table & table() const
Get the table this object is associated with.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
Definition ExprNode.h:1933
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44
Functor to convert Bools to flags using a mask.
StoredType operator()(Bool flag, StoredType value) const
Functor to and an array and mask and convert to Bool.
Bool operator()(StoredType value) const