如何避免由于模板参数而导致C++中的大型switch语句

How to avoid large switch statements in C++ due to template parameters

本文关键字:C++ 大型 语句 switch 何避免 于模板 参数      更新时间:2023-10-16

场景:我想读取一个包含其标题中定义的数据类型的值的文件,将其内容复制到临时图像,修改临时图像的内容并再次保存。

问题是数据类型的大小需要不同的访问/修改内容的方式,并且由于数据类型不同而导致较大的switch语句(此处的列表已缩短)。此外,该类型仅在运行时已知,在编译时不知道。

#include <stdint.h>
#include <stdlib.h>
typedef enum {
    DT_UCHAR,                   /** @brief Datatype 'unsigned char'     */
    DT_CHAR,                    /** @brief Datatype 'signed char'       */
    DT_USHORT,                  /** @brief Datatype 'unsigned short'    */
    DT_SHORT,                   /** @brief Datatype 'signed short'      */
    DT_UINT,                    /** @brief Datatype 'unsigned int'      */
    DT_INT,                     /** @brief Datatype 'signed int'        */
    DT_FLOAT,                   /** @brief Datatype 'float'             */
    DT_DOUBLE,                  /** @brief Datatype 'double'            */
} e_datatype;
struct image {
    e_datatype      type;
    size_t          size;
    void*           data;
};
image image1;
image image2;
image image3;
template <typename T>
void create_mask(void *const dst, const unsigned i_dst, const void *const src, const unsigned i_src, const void* const threshold) {
    if (static_cast<const T*>(src) < static_cast<const T*>(threshold))
        *(static_cast<T*>(dst)+i_dst) = 1;
}

void create_mask(image *const out, const unsigned out_i, const image *const in, const unsigned in_i, const image* const threshold) {
    if (in->type != threshold->type || out->type != DT_UCHAR)
        return;
    switch (out->type) {
    case DT_UCHAR:
        create_mask<unsigned char>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    case DT_CHAR:
        create_mask<signed char>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    case DT_USHORT:
        create_mask<unsigned short>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    case DT_SHORT:
        create_mask<signed short>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    case DT_UINT:
        create_mask<unsigned int>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    case DT_INT:
        create_mask<signed int>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    case DT_FLOAT:
        create_mask<float>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    case DT_DOUBLE:
        create_mask<double>(out->data, out_i, in->data, in_i, threshold->data);
        break;
    default:
        //printf("ERROR. Unknown type.n");
        break;
    }
}
size_t sizeof_image(e_datatype type) { return 1 /* another switch with the size of each datatype */;  }
void read_image() {
    image *my_image1 = &image1;
    image *my_image2 = &image2;
    image *threshold = &image3;
    // read header and save it in my_image and then
    // read data and copy it to the data field of my_image
    read_image_header("mydata_uint.dat", my_image1);
    my_image1->data = calloc(my_image1->size, sizeof_image(my_image1->type));
    read_image_data("mydata_uint.dat", my_image1);
    // create output mask
    my_image2->size = my_image1->size;
    my_image2->type = DT_UCHAR;
    my_image2->data = calloc(my_image2->size, sizeof_image(DT_UCHAR));
    // read threshold value from another image
    read_image_header("mydata_thresh.dat", threshold);
    threshold->data = calloc(threshold->size, sizeof_image(threshold->type));
    read_image_data("mydata_thresh.dat", threshold);
    for (unsigned i = 0; i < my_image1->size; i++)
        create_mask(my_image1, i, my_image2, i, threshold);
}

是否可以重写图像类/结构,使其使用数据类型设置在内部的模板类read_image()?从而减少了开关语句的数量。

我的限制是我无法使用C++标准库功能,并且仅限于 C++03。

我找到了一个带有函数指针的解决方案,但这个解决方案似乎并不比那些大的 switch 语句短。

如果不了解有关copy1D()和其他功能的用例的更多信息,就很难提供完整的解决方案。在任何情况下,解决方案的可能替代方法是使用动态多态性。例如:

使用您需要的所有方法创建接口

// Interface
struct Image {
    virtual Image* copy() = 0;
    virtual void manipulate() = 0;
    virtual void writeToFile() = 0;
    virtual ~Image() { }
};

现在让模板负责界面的实现

// Templated implementation
template <typename T>
struct ImageImpl : Image {
    size_t size;
    T* data;
    ImageImpl( const ImageImpl<T>& other ) {
        size = other.size;
        data = new T[size];
        memcpy( data, other.data, size * sizeof(T) );
    }
    ~ImageImpl() {
        delete[] data;
    }
    virtual Image* copy() {
        return new ImageImpl( *this );
    }
    virtual void manipulate() {
        for ( int i = 0; i < size; i++ ) {
            data[i] = data[i] + 1; // Do something with the data
        }
    }
    virtual void writeToFile( const char* filename ) {
        for ( int i = 0; i < size; i++ ) {
            write( data[i] );
        }
    }
};

用法示例:

Image* newFromFile( const char* filename )
{
    Image* i = NULL;
    if ( isIntFile( filename ) ) {
        i = new ImageImpl<int>( ... );
    }
    else if ( isFloatFile( filename ) ) {
        i = new ImageImpl<float>( ... );
    }
    ...
    return i;
}
int main()
{
    Image* i = newFromFile( "example.img" );
    Image* iCopy = i->copy();
    iCopy->manipulate();
    iCopy->writeToFile( "new.img" );
    delete iCopy;
    delete i;
}

您的问题是"用于查找代码的数据"的经典问题。除了为每个有效路径(即每个有效数据类型加上一个错误路径)提供代码之外,没有办法绕过它。

进一步的模板魔术最终不会减少击键。宏可能。

基于类型的重载函数可能会有所帮助。

此外,数据类型名称(或 ID)和函数指针的映射也会有所帮助。 这适用于 C 语言。

由于您的枚举是连续的,因此您不需要花哨的映射,并且可以使用老式数组(如果您可以在没有错误检查的情况下生活)。

typedef void (*copyFunction)(void *const, const unsigned, const void *const, const unsigned);
void copy1D(image *const out, const unsigned out_i, const image *const in, const unsigned in_i)
{
    static const copyFunction functions[] =
    {
       copyValue<unsigned char>,
       copyValue<signed char>,
       copyValue<unsigned short>,
       copyValue<signed short>,
       copyValue<unsigned int>,
       copyValue<signed int>,
       copyValue<float>,
       copyValue<double>
    };
    functions[out->type](out->data, out_i, in->data, in_i);
}

您可以使用宏对此进行概括:

#define MAKE_TABLE(function)
   {
       function<unsigned char>,
       function<signed char>,
       function<unsigned short>,
       function<signed short>,
       function<unsigned int>,
       function<signed int>,
       function<float>,
       function<double>
   }
typedef void (*copyFunction)(void *const, const unsigned, const void *const, const unsigned);
void copy1D(image *const out, const unsigned out_i, const image *const in, const unsigned in_i) 
{
   static const copyFunction functions[] = MAKE_TABLE(copyValue);
   functions[out->type](out->data, out_i, in->data, in_i);
}

或者,您可以将函数指针作为 image 的成员(这类似于手动管理的虚拟函数)

typedef void (*copyFunction)(void *const, const unsigned, const void *const, const unsigned);
struct image {
   e_datatype      type;
   size_t          size;
   void*           data;
   copyFunction    copy;
};
void read_image() 
{
    image *my_image = 0;
    // Read image...
    // ...
    // Set up the "virtual function"
    static const copyFunction functions[] = MAKE_TABLE(copyValue);
    my_image->copy = functions[my_image->type];
}
void copy1D(image *const out, const unsigned out_i, const image *const in, const unsigned in_i) 
{
   out->copy(out->data, out_i, in->data, in_i);
}

从您的代码片段中不清楚处理不同数据类型的实际差异是什么(与 sizeof 相关)。

处理一系列"类型化"对象C++方法之一是结合通用对象模板定义特征(C++03 应该没问题),然后在具有给定"特征"的模板专用化中实现特定于类型的处理。从本质上讲,特质负责"枚举"。这确实可以压缩编写的代码,尤其是在处理不同数据类型非常相似时。

但是,在您的情况下,有关数据类型的决定似乎与外部数据(文件)有关。数据类型粒度非常详细(即定义短的无符号类型),因此简单的strtol/strtod()不会在此处直接生成模板化对象。

因此,您可能希望在外部数据中包含数据类型 ID,并使用它来创建一个特征对象,该对象反过来会生成实际使用数据的模板化数据对象。否则,在解析外部数据后,需要至少有一个 swtich 语句,以将外部(基于字符串)类型映射到内部(数据类型 ID)类型。

我发现关于特质的一个很好的介绍是亚历山德雷斯库:http://erdani.com/publications/traits.html。

通过将图像 IO 与图像本身分开,您已经进入了正确的方向。为了能够在编写器中使用图像的类型,您有多种选择。我的方法是:

#include <cstdint>
#include <vector>
using namespace std;
template <typename T> class Image
{
public:
    typedef T Pixel_t;
    vector<Pixel_t> data; // or any suitable data structure
// all other stuff that belongs to the Image Class
};
template <class T> ImageWriter
{
public:
    typedef T Image_t;
    void writeImage(const Image_t& image)
    {
        write(file, &image[0], image.size()*sizeof(Image_t::Pixel_t));
    }
// other stuff that belongs to the ImageWriter
private:
    void write(File& file, void* data, size_t size);
};

然后,您可以在应用程序中使用它

typedef Image<uint32_t> img_t;
void myStuff()
{
    img_t img;
    ImageWriter<img_t::Pixel_t> writer;
    // ...
    writer.writeImage(img);
}