Le mardi 29 mars 2016 17:35:26, Ari Jolma a écrit : > I want to write template functions like this: > > template<typename cell_type> > void map(GDALRasterBand *b, mapper<cell_type> *mapper) { > ... > > This function would map the cell values of a band to new ones using some > logic that is coded within the mapper object (and the logic depends on > the cell_type). > > I would like to catch errors which are due to mismatch between the band > data type and the template cell_type. So far I've discovered this: > > band_wrapper band = band_initialize(b); > if (band.info.size_of_data_type != sizeof(cell_type)) > goto fail_because_of_data_type_mismatch; > > but it is not bullet proof since data types can have an equal size but > be different. The band_initialize contains a switch statement, which > sets the info element of the band wrapper. > > Are there other ways to solve this problem? I could build long switch > statements but it is not very appealing. And it gets messy when there is > more than one band.
Perhaps by using C++ traits like this : #include <gdal.h> template <typename T> struct GetGDALDataTypeTraits { static const GDALDataType datatype; }; template<> struct GetGDALDataTypeTraits<unsigned char> { static const GDALDataType datatype = GDT_Byte; }; template<> struct GetGDALDataTypeTraits<unsigned short> { static const GDALDataType datatype = GDT_UInt16; }; template <typename T> static GDALDataType GetGDALDataType() { return GetGDALDataTypeTraits<T>::datatype; } #include <iostream> int main(int argc, char* argv[]) { std::cout << "type unsigned char: " << GetGDALDataType<unsigned char>() << std::endl; std::cout << "type unsigned short: " << GetGDALDataType<unsigned short>() << std::endl; // Link time error //std::cout << "type double: " << GetGDALDataType<double>() << std::endl; return 0; } > > Ari > > _______________________________________________ > gdal-dev mailing list > gdal-dev@lists.osgeo.org > http://lists.osgeo.org/mailman/listinfo/gdal-dev -- Spatialys - Geospatial professional services http://www.spatialys.com _______________________________________________ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev