File: ColumnDesc.cc

package info (click to toggle)
casacore 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,680 kB
  • sloc: cpp: 462,815; fortran: 16,372; ansic: 7,403; yacc: 4,626; lex: 2,340; sh: 1,786; python: 629; perl: 531; sed: 499; csh: 34; makefile: 31
file content (329 lines) | stat: -rw-r--r-- 9,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//# ColumnDesc.cc: Envelope class for description of a table column
//# Copyright (C) 1994,1995,1996,1997,1998,2001,2016
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//# $Id$

#include <casacore/tables/Tables/ColumnDesc.h>
#include <casacore/tables/Tables/ScaColDesc.h>
#include <casacore/tables/Tables/ScaRecordColDesc.h>
#include <casacore/tables/Tables/ArrColDesc.h>
#include <casacore/tables/Tables/SubTabDesc.h>
#include <casacore/tables/Tables/TableRecord.h>
#include <casacore/tables/Tables/TableAttr.h>
#include <casacore/casa/Containers/RecordDesc.h>
#include <casacore/casa/BasicSL/Complex.h>
#include <casacore/casa/Utilities/DataType.h>
#include <casacore/casa/IO/AipsIO.h>
#include <casacore/tables/Tables/TableError.h>
#include <casacore/tables/Tables/TableRecord.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/iostream.h>


namespace casacore { //# NAMESPACE CASACORE - BEGIN

// Initialize the statics.
std::mutex ColumnDesc::theirMutex;

  
ColumnDesc::ColumnDesc (const BaseColumnDesc& cold)
: colPtr_p   (cold.clone()),
  allocated_p(True)
{}

ColumnDesc::ColumnDesc (const ColumnDesc& that)
: colPtr_p   (that.colPtr_p),
  allocated_p(True)
{
  if (colPtr_p != 0) {
      colPtr_p = colPtr_p->clone();
  }
}

ColumnDesc::ColumnDesc (BaseColumnDesc* bcdp)
: colPtr_p   (bcdp),
  allocated_p(False)
{}

ColumnDesc::~ColumnDesc()
{
    if (allocated_p) {
	delete colPtr_p;
    }
}


ColumnDesc& ColumnDesc::operator= (const ColumnDesc& that)
{
    if (this != &that) {
	if (allocated_p) {
	    delete colPtr_p;
	}
	colPtr_p = that.colPtr_p;
	if (colPtr_p != 0) {
	    colPtr_p = colPtr_p->clone();
	}
	allocated_p = True;
    }
    return *this;
}


Bool ColumnDesc::operator== (const ColumnDesc& that) const
{
    if (dataType() != that.dataType())
	return False;
    if (options() != that.options())
	return False;
    if (ndim() != that.ndim())
	return False;
    if (isScalar() && that.isScalar())
	return True;
    if (isArray() && that.isArray())
	return True;
    if (isTable() && that.isTable())
	return True;
    return False;
}

Bool ColumnDesc::operator!= (const ColumnDesc& that) const
{
    return !(*this == that);
}


Bool ColumnDesc::isFixedShape() const
{
    if (isScalar()) {
	return True;
    }
    if ((options() & ColumnDesc::FixedShape)  ==  ColumnDesc::FixedShape) {
	return True;
    }
    return False;
}


DataType ColumnDesc::trueDataType() const
{
    DataType dtype = dataType();
    if (! isArray()) {
        return dtype;
    }
    switch (dtype) {
    case TpBool:
	return TpArrayBool;
    case TpChar:
	return TpArrayChar;
    case TpUChar:
	return TpArrayUChar;
    case TpShort:
	return TpArrayShort;
    case TpUShort:
	return TpArrayUShort;
    case TpInt:
	return TpArrayInt;
    case TpUInt:
	return TpArrayUInt;
    case TpInt64:
	return TpArrayInt64;
    case TpFloat:
	return TpArrayFloat;
    case TpDouble:
	return TpArrayDouble;
    case TpComplex:
	return TpArrayComplex;
    case TpDComplex:
	return TpArrayDComplex;
    case TpString:
	return TpArrayString;
    default:
	AlwaysAssert (False, AipsError);
    }
    return TpOther;
}


// Return the column name.
const String& ColumnDesc::name() const
    { return colPtr_p->name(); }


AipsIO& operator<< (AipsIO& ios, const ColumnDesc& cd)
{
    cd.putFile (ios, TableAttr());
    return ios;
}

AipsIO& operator>> (AipsIO& ios, ColumnDesc& cd)
{
    cd.getFile(ios, TableAttr());
    return ios;
}

//# Put into AipsIO.
//# It was felt that putstart takes too much space, so therefore
//# the version is put "manually".
void ColumnDesc::putFile (AipsIO& ios, const TableAttr& parentAttr) const
{
    ios << (uInt)1;                  // class version 1
    //# First write the exact column type, then its data.
    ios << colPtr_p->className();
    colPtr_p->putFile (ios, parentAttr);
}

//# Get from AipsIO.
void ColumnDesc::getFile (AipsIO& ios, const TableAttr& parentAttr)
{
    uInt version;
    ios >> version;
    String tp;
    ios >> tp;
    if (allocated_p) {
	delete colPtr_p;
    }
    // If tp is not in the map, (tp, unknownColumnDesc) is added and called (throws).
    ColumnDesc::ColumnDescCtor* cdFunc = getCtor(tp);
    colPtr_p = (*cdFunc)(tp);
    allocated_p = True;
    colPtr_p->getFile (ios, parentAttr);
}


ostream& operator<< (ostream& ios, const ColumnDesc& cd)
{
    cd.show (ios);
    return ios;
}

void ColumnDesc::show() const
{
    show (cout);
}

void ColumnDesc::show (ostream& os) const
{
    if (colPtr_p) {
        colPtr_p->show (os);
        os << "   #keywords=" << keywordSet().nfields() << endl;
        os << keywordSet().description();
    } else {
        os << "ColumnDesc is empty" << endl;
    }
}


//# Register a mapping.
void ColumnDesc::registerCtor (const String& name,
                               ColumnDesc::ColumnDescCtor* func)
{
    std::lock_guard<std::mutex> lock(theirMutex);
    getRegisterMap().insert (std::make_pair(name, func));
}

//# Get a ColumnDesc constructor.
//# Return default function if undefined.
ColumnDesc::ColumnDescCtor* ColumnDesc::getCtor (const String& name)
{
  std::map<String, ColumnDesc::ColumnDescCtor*>& regMap = getRegisterMap();
  std::map<String, ColumnDesc::ColumnDescCtor*>::iterator iter = regMap.find (name);
  if (iter == regMap.end()) {
    throw;
  }
  return iter->second;
}

std::map<String, ColumnDesc::ColumnDescCtor*>& ColumnDesc::getRegisterMap()
{
  static std::map<String, ColumnDesc::ColumnDescCtor*> regMap(initRegisterMap());
  return regMap;
}

//# Register the main "static constructors" of all XColumnDesc classes.
// No locking since private and only called by ctor of static member init.
std::map<String, ColumnDesc::ColumnDescCtor*> ColumnDesc::initRegisterMap()
{
  std::map<String, ColumnDesc::ColumnDescCtor*> regMap;

  ScalarColumnDesc<Bool>     scdb("x");
  regMap.insert (std::make_pair(scdb.className(), &scdb.makeDesc));
  ScalarColumnDesc<uChar>    scduc("x");
  regMap.insert (std::make_pair(scduc.className(), &scduc.makeDesc));
  ScalarColumnDesc<Short>    scds("x");
  regMap.insert (std::make_pair(scds.className(), &scds.makeDesc));
  ScalarColumnDesc<uShort>   scdus("x");
  regMap.insert (std::make_pair(scdus.className(), &scdus.makeDesc));
  ScalarColumnDesc<Int>      scdi("x");
  regMap.insert (std::make_pair(scdi.className(), &scdi.makeDesc));
  ScalarColumnDesc<uInt>     scdui("x");
  regMap.insert (std::make_pair(scdui.className(), &scdui.makeDesc));
  ScalarColumnDesc<Int64>    scdi64("x");
  regMap.insert (std::make_pair(scdi64.className(), &scdi64.makeDesc));
  ScalarColumnDesc<float>    scdf("x");
  regMap.insert (std::make_pair(scdf.className(), &scdf.makeDesc));
  ScalarColumnDesc<double>   scdd("x");
  regMap.insert (std::make_pair(scdd.className(), &scdd.makeDesc));
  ScalarColumnDesc<Complex>  scdcx("x");
  regMap.insert (std::make_pair(scdcx.className(), &scdcx.makeDesc));
  ScalarColumnDesc<DComplex> scddx("x");
  regMap.insert (std::make_pair(scddx.className(), &scddx.makeDesc));
  ScalarColumnDesc<String>   scdst("x");
  regMap.insert (std::make_pair(scdst.className(), &scdst.makeDesc));

  ScalarRecordColumnDesc     srcd ("x");
  regMap.insert (std::make_pair(srcd.className(), &srcd.makeDesc));

  ArrayColumnDesc<Bool>     acdb("x");
  regMap.insert (std::make_pair(acdb.className(), &acdb.makeDesc));
  ArrayColumnDesc<uChar>    acduc("x");
  regMap.insert (std::make_pair(acduc.className(), &acduc.makeDesc));
  ArrayColumnDesc<Short>    acds("x");
  regMap.insert (std::make_pair(acds.className(), &acds.makeDesc));
  ArrayColumnDesc<uShort>   acdus("x");
  regMap.insert (std::make_pair(acdus.className(), &acdus.makeDesc));
  ArrayColumnDesc<Int>      acdi("x");
  regMap.insert (std::make_pair(acdi.className(), &acdi.makeDesc));
  ArrayColumnDesc<uInt>     acdui("x");
  regMap.insert (std::make_pair(acdui.className(), &acdui.makeDesc));
  ArrayColumnDesc<Int64>    acdi64("x");
  regMap.insert (std::make_pair(acdi64.className(), &acdi64.makeDesc));
  ArrayColumnDesc<float>    acdf("x");
  regMap.insert (std::make_pair(acdf.className(), &acdf.makeDesc));
  ArrayColumnDesc<double>   acdd("x");
  regMap.insert (std::make_pair(acdd.className(), &acdd.makeDesc));
  ArrayColumnDesc<Complex>  acdcx("x");
  regMap.insert (std::make_pair(acdcx.className(), &acdcx.makeDesc));
  ArrayColumnDesc<DComplex> acddx("x");
  regMap.insert (std::make_pair(acddx.className(), &acddx.makeDesc));
  ArrayColumnDesc<String>   acdst("x");
  regMap.insert (std::make_pair(acdst.className(), &acdst.makeDesc));

  SubTableDesc std("x", "", TableDesc());
  regMap.insert (std::make_pair(std.className(), &std.makeDesc));

  return regMap;
}

} //# NAMESPACE CASACORE - END