Unicode Fix Proposal

Developers
2009-05-28
2013-05-02
  • Simulacrum111
    Simulacrum111
    2009-05-28

    Unfortunately, I am pressed for time so I can only propose these solutions instead of contributing directly to the project.  Full unicode support is desirable and can perhaps can be had without much pain.

    A switch is sort of hackish but will work. When DevIL is initiallized it can test the size of wchar_t with the sizeof operator. A switch statement can then select the correct internal functions to process unicode strings. I only offer this because I believe you are still transitioning to a c++ native implementation of DevIL?

    switch(sizeof(wchar_t))
    {
    case 2:
    //use method 1
    break;
    case 4:
    //use method 2
    break;
    .
    .
    .
    default:

    }

    The switch presents a fixed price to pay in order to correctly handle Unicode strings but it is minimal and can serve well as a stop gap solution.

    Alternatively you can implement a sort of traits class so that the price paid to select the correct string handling methods is only paid at compile time.

    Here is an example program to demonstrate the concept. All that is required are some lightweight templates. I would think this is the better solution but hey...its YOUR library :)

    #include <iostream>

    template <int>
    struct ImplSelector
    {
    //you could place a static asset here for debugging purposes or to report access violations
    //to this template being instantiated.
    };

    template <>
    struct ImplSelector<2>
    {
    static void StringHandler()
    {
    std::cout<<"Implementation 1 has been selected."<<std::endl;
    }
    };

    template <>
    struct ImplSelector<4>
    {
    static void StringHandler()
    {
    std::cout<<"Implementation 2 has been selected."<<std::endl;
    }
    };

    template <>
    struct ImplSelector<8>
    {
    static void StringHandler()
    {
    std::cout<<"Implementation 3 has been selected."<<std::endl;
    }
    };

    int main(void)
    {
    // Here you can see that the compiler selects whatever method you would
    // like based upon what the environment dictates the size of a wchar_t is.
    // This should allow you to specify internal library functions to handle
    // all possible Unicode specifications(you wish to support) and have the compiler
    // select the correct versions of those functions to use at compile time without
    // having to change the interfaces of your loading/saving functions for clients
    // at all.
    //////////////////////////////////////////////////////////////////////////
    ImplSelector<sizeof(wchar_t)>::StringHandler();
    }