/**
* @brief Given one of the HDF Types as a string, this will return the HDF Type
* as an hid_t value.
* @param value The HDF_Type as a string
* @return the hid_t value for the given type. -1 if the string does
not match a type.
*/
static hid_t HDFTypeFromString(const std::string &value)
{
if (value.compare("H5T_NATIVE_INT8") == 0) return H5T_NATIVE_INT8;
if (value.compare("H5T_NATIVE_UINT8") == 0) return H5T_NATIVE_UINT8;
if (value.compare("H5T_NATIVE_INT16") == 0) return H5T_NATIVE_INT16;
if (value.compare("H5T_NATIVE_UINT16") == 0) return H5T_NATIVE_UINT16;
if (value.compare("H5T_NATIVE_INT32") == 0) return H5T_NATIVE_INT32;
if (value.compare("H5T_NATIVE_UINT32") == 0) return H5T_NATIVE_UINT32;
if (value.compare("H5T_NATIVE_INT64") == 0) return H5T_NATIVE_INT64;
if (value.compare("H5T_NATIVE_UINT64") == 0) return H5T_NATIVE_UINT64;
if (value.compare("H5T_NATIVE_FLOAT") == 0) return H5T_NATIVE_FLOAT;
if (value.compare("H5T_NATIVE_DOUBLE") == 0) return H5T_NATIVE_DOUBLE;
if (value.compare("H5T_STRING") == 0) return H5T_STRING;
std::cout << DEBUG_OUT(logTime) << "Error: HDFTypeFromString -
Unknown Type: " << value << std::endl;
return -1;
}
/**
* @brief Returns a string version of the HDF Type
* @param type The HDF5 Type to query
* @return
*/
static std::string StringForHDFType(hid_t type)
{
if (H5Tequal(type ,H5T_NATIVE_INT8) ) return "H5T_NATIVE_INT8";
if (H5Tequal(type ,H5T_NATIVE_UINT8) ) return "H5T_NATIVE_UINT8";
if (H5Tequal(type ,H5T_NATIVE_INT16) ) return "H5T_NATIVE_INT16";
if (H5Tequal(type ,H5T_NATIVE_UINT16) ) return "H5T_NATIVE_UINT16";
if (H5Tequal(type ,H5T_NATIVE_INT32) ) return "H5T_NATIVE_INT32";
if (H5Tequal(type ,H5T_NATIVE_UINT32) ) return "H5T_NATIVE_UINT32";
if (H5Tequal(type ,H5T_NATIVE_INT64) ) return "H5T_NATIVE_INT64";
if (H5Tequal(type ,H5T_NATIVE_UINT64) ) return "H5T_NATIVE_UINT64";
if (H5Tequal(type ,H5T_NATIVE_FLOAT) ) return "H5T_NATIVE_FLOAT";
if (H5Tequal(type ,H5T_NATIVE_DOUBLE) ) return "H5T_NATIVE_DOUBLE";
if ( type == H5T_STRING) return "H5T_STRING";
std::cout << DEBUG_OUT(logTime) << "Error: HDFTypeForPrimitiveAsStr
- Unknown Type: " << std::endl;
return "Unknown";
}
/**
* @brief Returns the HDF Type for a given primitive value.
* @param value A value to use. Can be anything. Just used to get the type info
* from
* @return A std::string representing the HDF5 Type
*/
template<typename T>
static std::string HDFTypeForPrimitiveAsStr(T value)
{
if (typeid(value) == typeid(int8)) return "H5T_NATIVE_INT8";
if (typeid(value) == typeid(uint8)) return "H5T_NATIVE_UINT8";
if (typeid(value) == typeid(int16)) return "H5T_NATIVE_INT16";
if (typeid(value) == typeid(uint16)) return "H5T_NATIVE_UINT16";
if (typeid(value) == typeid(int32)) return "H5T_NATIVE_INT32";
if (typeid(value) == typeid(uint32)) return "H5T_NATIVE_UINT32";
if (typeid(value) == typeid(int64)) return "H5T_NATIVE_INT64";
if (typeid(value) == typeid(uint64)) return "H5T_NATIVE_UINT64";
if (typeid(value) == typeid(float32)) return "H5T_NATIVE_FLOAT";
if (typeid(value) == typeid(float64)) return "H5T_NATIVE_DOUBLE";
//if (typeid(value) == typeid(bool)) return "H5T_NATIVE_UINT8";
std::cout << DEBUG_OUT(logTime) << "Error: HDFTypeForPrimitiveAsStr
- Unknown Type: " << typeid(value).name() << std::endl;
return "";
}
/**
* @brief Returns the HDF Type for a given primitive value.
* @param value A value to use. Can be anything. Just used to get the type info
* from
* @return The HDF5 native type for the value
*/
template<typename T>
static hid_t HDFTypeForPrimitive(T value)
{
if (typeid(value) == typeid(float32)) return H5T_NATIVE_FLOAT;
if (typeid(value) == typeid(float64)) return H5T_NATIVE_DOUBLE;
if (typeid(value) == typeid(int8)) return H5T_NATIVE_INT8;
if (typeid(value) == typeid(uint8)) return H5T_NATIVE_UINT8;
if (typeid(value) == typeid(int16)) return H5T_NATIVE_INT16;
if (typeid(value) == typeid(uint16)) return H5T_NATIVE_UINT16;
if (typeid(value) == typeid(int32)) return H5T_NATIVE_INT32;
if (typeid(value) == typeid(uint32)) return H5T_NATIVE_UINT32;
if (typeid(value) == typeid(int64)) return H5T_NATIVE_INT64;
if (typeid(value) == typeid(uint64)) return H5T_NATIVE_UINT64;
if (typeid(value) == typeid(bool)) return H5T_NATIVE_UINT8;
std::cout << DEBUG_OUT(logTime) << "Error: HDFTypeForPrimitive -
Unknown Type: " << (typeid(value).name()) << std::endl;
const char* name = typeid(value).name();
if (NULL != name && name[0] == 'l' ) {
std::cout << "You are using 'long int' as a type which is not
32/64 bit safe. Suggest you use one of the MXATypes defined in
<Common/MXATypes.h> such as int32 or uint32." << std::endl;
}
return -1;
}
···
On Mon, May 18, 2009 at 5:43 PM, Werner Benger <werner@cct.lsu.edu> wrote:
Cool enough, you found pretty much the same solution as I'm using...
As one more small optimization - you don't need to go via strings, because
the
native C++ typeid already supports a comparision between typeid's via its
before()
member function. All what is required is a some proxy class, and you can
directly
map typeid's to arbitrary data types (I tend to call this a "typemap").
Cheers,
Werner
class TypeInfo
{
const type_info&TypeID;
public:
/// Constructor
TypeInfo(const type_info&Id)
: TypeID(Id)
{}
/// The comparision operator
bool operator<\(const TypeInfo&T\) const
\{
return TypeID\.before\( T\.TypeID \) \!= 0;
\}
};
template <class T>
class typemap : public std::map<TypeInfo, T>
{
public:
};
On Mon, 18 May 2009 16:34:55 -0500, Lewandowski, George > <george.lewandowski@boeing.com> wrote:
I'm not sure that this answers your need. I want to have templated code
that will handle storage of various types, so I don't have to have a
function for float, one for double, one for int, etc. This is my
solution.
I won't claim that it is elegant, but it works for me. The key is the
C++ typeid feature. It can give you a unique text name for any given
type.
std::map< std::string, H5::DataType> H5TypeMap;
H5TypeMap h5_type_map_;
h5_type_map_[ typeid(float).name() ] = PredType::NATIVE_FLOAT;
h5_type_map_[ typeid(double).name() ] = PredType::NATIVE_DOUBLE;
h5_type_map_[ typeid(int32_t).name() ] = PredType::NATIVE_INT32;
// ... etc.
template<typename T>
H5::DataType getH5Type( T parameter )const
{
const char * type_name = typeid( parameter ).name();
H5TypeMap::const_iterator i = h5_type_map_.find( type_name );
if ( i == h5_type_map_.end() )
{
// throw exception
}
else
{
return i->second;
}
}
George Lewandowski
(314)777-7890
Mail Code S270-2204
Building 270-E Level 2E Room 20E
P-8A
-----Original Message-----
From: Patrick [mailto:jazzcat81@hotmail.com]
Sent: Monday, May 18, 2009 4:18 PM
To: hdf-forum
Subject: [hdf-forum] Re: creating hdf5 datatypes at compile-time?
Hello all,
I am currently trying to implement a function in C++ that stores a
multi-dimensional array to hdf5 whose C++- type is given by a template
parameter at compile time. Ideally, I would determine the corresponding
hdf5 datatype during compilation using a template meta-program. However,
I haven't been able to get this to work using the predefined types (i.e.
H5T_STD_U32LE) (I get the error "expected constant expression"), and
after reading more about the hdf5 datatype concept, I am not sure if the
compile time approach is at all possible. Is there an elegant generic
solution to this problem?
Thanks!
Regards,
Patrick
----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to
hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.
----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to
hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.
--
___________________________________________________________________________
Dr. Werner Benger <werner@cct.lsu.edu> Visualization Research
Laboratory for Creative Arts and Technology (LCAT)
Center for Computation & Technology at Louisiana State University (CCT/LSU)
239 Johnston Hall, Baton Rouge, Louisiana 70803
Tel.: +1 225 578 4809 Fax.: +1 225 578-5362
----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.