//# ValueHolder.h: A holder object for the standard Casacore data types //# Copyright (C) 2005 //# 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$ #ifndef CASA_VALUEHOLDER_H #define CASA_VALUEHOLDER_H //# Includes #include #include #include #include namespace casacore { //# NAMESPACE CASACORE - BEGIN // // A holder for a value of any basic Casacore data type. // // // // // // Class ValueHolder is meant to be used for holding a single Casacore value. // The value can be scalar or an array of any basic type (including complex // and string). Also a Record value is possible. // In this way varying typed data (e.g. the result of getCell in the table DO) // can be packed in a strongly typed variable. //
All unsigned integer type values are kept as signed 32-bit integers // because scripting languages usually only support those types. // // ValueHolder is an envelope class that holds a counted-referenced letter // object ValueHolderRep. //
// // This class comes handy in passing arbitrary values from a DO to // its environment. // class ValueHolder { public: // Construct a null object. ValueHolder() {} // Create the object for the given value. // explicit ValueHolder (Bool value); explicit ValueHolder (uChar value); explicit ValueHolder (Short value); explicit ValueHolder (uShort value); explicit ValueHolder (Int value); explicit ValueHolder (uInt value); explicit ValueHolder (Int64 value); explicit ValueHolder (Float value); explicit ValueHolder (Double value); explicit ValueHolder (const Complex& value); explicit ValueHolder (const DComplex& value); explicit ValueHolder (const Char* value); explicit ValueHolder (const String& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Array& value); explicit ValueHolder (const Record& value); // // Create an empty N-dim array (gets type TpOther). ValueHolder (uInt ndim, Bool dummy); // Create a ValueHolder from a ValueHolderRep. // It takes over the pointer and deletes it in the destructor. explicit ValueHolder (ValueHolderRep* rep) : itsRep (rep) {} // Copy constructor (reference semantics). ValueHolder (const ValueHolder&); // Destructor. ~ValueHolder() {} // Assignment (reference semantics). ValueHolder& operator= (const ValueHolder&); // Is this a null object? Bool isNull() const { return itsRep.null(); } // Get the data type (as defined in DataType.h). // Note that TpOther is returned for an empty untyped array. DataType dataType() const; // Get the value. // If possible, it converts the data as needed. // Bool asBool () const; uChar asuChar () const; Short asShort () const; uShort asuShort () const; Int asInt () const; uInt asuInt () const; Int64 asInt64 () const; Float asFloat () const; Double asDouble () const; Complex asComplex () const; DComplex asDComplex() const; const String& asString () const; const Array asArrayBool () const; const Array asArrayuChar () const; const Array asArrayShort () const; const Array asArrayuShort () const; const Array asArrayInt () const; const Array asArrayuInt () const; const Array asArrayInt64 () const; const Array asArrayFloat () const; const Array asArrayDouble () const; const Array asArrayComplex () const; const Array asArrayDComplex() const; const Array asArrayString () const; const Record& asRecord () const; // // Get the data in a way useful for templates. // If possible, it converts the the data as needed. // void getValue (Bool& value) const { value = asBool(); } void getValue (uChar& value) const { value = asuChar(); } void getValue (Short& value) const { value = asShort(); } void getValue (uShort& value) const { value = asuShort(); } void getValue (Int& value) const { value = asInt(); } void getValue (uInt& value) const { value = asuInt(); } void getValue (Int64& value) const { value = asInt64(); } void getValue (Float& value) const { value = asFloat(); } void getValue (Double& value) const { value = asDouble(); } void getValue (Complex& value) const { value = asComplex(); } void getValue (DComplex& value) const { value = asDComplex(); } void getValue (String& value) const { value = asString(); } void getValue (Array& value) const { value.reference(asArrayBool()); } void getValue (Array& value) const { value.reference(asArrayuChar()); } void getValue (Array& value) const { value.reference(asArrayShort()); } void getValue (Array& value) const { value.reference(asArrayuShort()); } void getValue (Array& value) const { value.reference(asArrayInt()); } void getValue (Array& value) const { value.reference(asArrayuInt()); } void getValue (Array& value) const { value.reference(asArrayInt64()); } void getValue (Array& value) const { value.reference(asArrayFloat()); } void getValue (Array& value) const { value.reference(asArrayDouble()); } void getValue (Array& value) const { value.reference(asArrayComplex()); } void getValue (Array& value) const { value.reference(asArrayDComplex()); } void getValue (Array& value) const { value.reference(asArrayString()); } // // Put the value as a field in a record. void toRecord (Record&, const RecordFieldId&) const; // Construct the object from the value in a record. static ValueHolder fromRecord (const Record&, const RecordFieldId&); // Compare two ValueHolder objects. // They must have the same data type. bool operator< (const ValueHolder& right) const { return itsRep->operator< (*right.itsRep); } // Write the ValueHolder to an output stream. // Arrays are written as normal arrays using ArrayIO.h. friend std::ostream& operator<< (std::ostream& os, const ValueHolder& vh) { return vh.itsRep->write (os); } private: CountedPtr itsRep; }; inline DataType ValueHolder::dataType() const { return itsRep->dataType(); } inline void ValueHolder::toRecord (Record& rec, const RecordFieldId& id) const { return itsRep->toRecord (rec, id); } inline ValueHolder ValueHolder::fromRecord (const Record& rec, const RecordFieldId& id) { return ValueHolder (ValueHolderRep::fromRecord (rec, id)); } inline Bool ValueHolder::asBool() const { return itsRep->asBool(); } inline uChar ValueHolder::asuChar() const { return itsRep->asuChar(); } inline Short ValueHolder::asShort() const { return itsRep->asShort(); } inline uShort ValueHolder::asuShort() const { return itsRep->asuShort(); } inline Int ValueHolder::asInt() const { return itsRep->asInt(); } inline uInt ValueHolder::asuInt() const { return itsRep->asuInt(); } inline Int64 ValueHolder::asInt64() const { return itsRep->asInt64(); } inline Float ValueHolder::asFloat() const { return itsRep->asFloat(); } inline Double ValueHolder::asDouble() const { return itsRep->asDouble(); } inline Complex ValueHolder::asComplex() const { return itsRep->asComplex(); } inline DComplex ValueHolder::asDComplex() const { return itsRep->asDComplex(); } inline const String& ValueHolder::asString() const { return itsRep->asString(); } inline const Array ValueHolder::asArrayBool() const { return itsRep->asArrayBool(); } inline const Array ValueHolder::asArrayuChar() const { return itsRep->asArrayuChar(); } inline const Array ValueHolder::asArrayShort() const { return itsRep->asArrayShort(); } inline const Array ValueHolder::asArrayuShort() const { return itsRep->asArrayuShort(); } inline const Array ValueHolder::asArrayInt() const { return itsRep->asArrayInt(); } inline const Array ValueHolder::asArrayuInt() const { return itsRep->asArrayuInt(); } inline const Array ValueHolder::asArrayInt64() const { return itsRep->asArrayInt64(); } inline const Array ValueHolder::asArrayFloat() const { return itsRep->asArrayFloat(); } inline const Array ValueHolder::asArrayDouble() const { return itsRep->asArrayDouble(); } inline const Array ValueHolder::asArrayComplex() const { return itsRep->asArrayComplex(); } inline const Array ValueHolder::asArrayDComplex() const { return itsRep->asArrayDComplex(); } inline const Array ValueHolder::asArrayString() const { return itsRep->asArrayString(); } inline const Record& ValueHolder::asRecord() const { return itsRep->asRecord(); } } //# NAMESPACE CASACORE - END #endif