1 | //
|
---|
2 | // C++ Interface: TableTraverse
|
---|
3 | //
|
---|
4 | // Description:
|
---|
5 | //
|
---|
6 | // Copyright: See COPYING file that comes with this distribution
|
---|
7 | //
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * $Id: TableTraverse.h 3106 2016-10-04 07:20:50Z TakeshiNakazato $
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifndef ASAP_TABLETRAVERSE_H
|
---|
14 | #define ASAP_TABLETRAVERSE_H
|
---|
15 |
|
---|
16 | #include <casa/aips.h>
|
---|
17 | #include <tables/Tables/BaseTable.h>
|
---|
18 | #include <tables/Tables/Table.h>
|
---|
19 |
|
---|
20 | namespace asap {
|
---|
21 |
|
---|
22 | class TableVisitor {
|
---|
23 | public:
|
---|
24 | virtual ~TableVisitor() {}
|
---|
25 |
|
---|
26 | // This method is called just before the first <src>visit()<src> call.
|
---|
27 | virtual void start() {}
|
---|
28 |
|
---|
29 | // <p><src>isFirst</src> is True only when the first visit.</p>
|
---|
30 | // <p><src>recordNo</src> indicates which record is currently visited.
|
---|
31 | // <p><src>nCols</src> indicates a number of columns used to sort records.
|
---|
32 | // <src>nCols</src> is equal to a number of elements of
|
---|
33 | // <src>columnNames</src> argument of <src>traverseTable()</src>
|
---|
34 | // excluding the trailing NULL element.</p>
|
---|
35 | // <p><src>colValues</src> is an array of arrays which contains all
|
---|
36 | // column values for each column.
|
---|
37 | // For example, <src>colValues[0]</src> is an array which contains all
|
---|
38 | // values of the first column.
|
---|
39 | // The order of <src>colValues</src> is same as the one of
|
---|
40 | // <src>columnNames</src> argument of <src>traverseTable()</src>.</p>
|
---|
41 | // <p>Returning False causes stopping iteration,
|
---|
42 | // otherwise iterate over all records.</p>
|
---|
43 | // <p>If <src>visit()</src> throws an exception, the iteration stops and
|
---|
44 | // <src>finish()</src> will not be called.</p>
|
---|
45 | virtual casacore::Bool visit(casacore::Bool isFirst, casacore::uInt recordNo,
|
---|
46 | casacore::uInt nCols,
|
---|
47 | void const *const colValues[]) = 0;
|
---|
48 |
|
---|
49 | // This method is called immediately after all visits have done.
|
---|
50 | virtual void finish() {}
|
---|
51 | };
|
---|
52 |
|
---|
53 | // <src>TypeManager</src> describes a type to handle the type dynamically.
|
---|
54 | class TypeManager {
|
---|
55 | public:
|
---|
56 | virtual ~TypeManager() {}
|
---|
57 | virtual casacore::BaseCompare *getComparator() const = 0;
|
---|
58 | virtual size_t sizeOf() const = 0;
|
---|
59 | virtual void *allocArray(size_t size) const = 0;
|
---|
60 | virtual void freeArray(void *array) const = 0;
|
---|
61 | };
|
---|
62 |
|
---|
63 | // This template is applicable to the type which can be applied to
|
---|
64 | // <src>casacore::ObjCompare<T></src>.
|
---|
65 | template<class T> class TypeManagerImpl: public TypeManager {
|
---|
66 | public:
|
---|
67 | TypeManagerImpl(){}
|
---|
68 | virtual casacore::BaseCompare *getComparator() const {
|
---|
69 | static casacore::ObjCompare<T> comparator;
|
---|
70 | return &comparator;
|
---|
71 | }
|
---|
72 | virtual size_t sizeOf() const {
|
---|
73 | return sizeof(T);
|
---|
74 | }
|
---|
75 | virtual void *allocArray(size_t size) const {
|
---|
76 | return new T[size];
|
---|
77 | }
|
---|
78 | virtual void freeArray(void *array) const {
|
---|
79 | delete[] (T*)array;
|
---|
80 | }
|
---|
81 | };
|
---|
82 |
|
---|
83 |
|
---|
84 | // <p>This function iterates over <src>table</src> with calling
|
---|
85 | // <src>visitor->visit()</src> for each record after calling
|
---|
86 | // <src>visitor->start()</src> and, finally, it calls
|
---|
87 | // <src>visitor->finish()</src>.</p>
|
---|
88 | // <p><src>columnNames</src> should be a NULL terminated array of column
|
---|
89 | // names which will be used to sort records to determine the order of visits.
|
---|
90 | // If <src>doSort</src> is False, <src>columnNames</src> is used only to
|
---|
91 | // specify <src>nCols</src> and <src>colValues</src> arguments for calling
|
---|
92 | // <src>visitor->visit()</src>.</p>
|
---|
93 | // <p><src>typeManagers</src> should be a NULL terminated array of
|
---|
94 | // <src>TypeManager</src> too.
|
---|
95 | // It's number of elements and order of elements should be same as
|
---|
96 | // <src>columnNames</src>.
|
---|
97 | // <src>TypeManagerImpl</src> is useful to create instances of
|
---|
98 | // <src>TypeManager</src>.</p>
|
---|
99 | // <p>If <src>doSort</src> is True, the order of visits is ascending order
|
---|
100 | // for each column. <src>columnNames[0]</src> is a primary sort key and
|
---|
101 | // <src>columnNames[1]</src> is a secondary sort key, ...</p>
|
---|
102 | void traverseTable(const casacore::Table &table,
|
---|
103 | const char *const columnNames[],
|
---|
104 | const TypeManager *const typeManagers[],
|
---|
105 | TableVisitor *visitor,
|
---|
106 | casacore::Bool doSort = casacore::True);
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | #endif
|
---|