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