Thread: Template problems

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Template problems

    Lets say I have a template class that encapsulates all video functions. I need the template because there are different color depths for certain modes. Rather than store 3 pointers to 3 types of buffers and screen pointers or creating 3 seperate classes, I use a template.

    template <class T> class SVGA
    {

    T* Buffer;
    T* Screen;
    T* ScreenPtr;
    ...
    ...

    };


    My problem is this. I also have some classes that need to gain access to the SVGA class so that it can draw lines, points, etc. For instance, let's say that a class called poly2D which encapsulates a 2D polygon object needs access to the SVGA::Line() function in its virtual draw function.

    I cannot include a template pointer to the SVGA class inside of the poly class and I really don't want to since this would require you to pass the SVGA object to every class that needed to use it.
    Instead I'd like to create a global pointer to the current SVGA object.

    SVGA<class T> *SVGAPtr;

    Problem is this. The compiler does not know at compile time what class T is. If I try to call SVGA::Line with that pointer the compiler pukes up about 25 errors saying that it cannot cast unsigned char, unsigned short, or unsigned long (depending on the instance of class SVGA) to class T. What I really need is run time type information to determine what class T is so that I can make reference to the SVGA object in the objects that need to use its functions.

    Also, I'm not sure that I can derive every object off of template class SVGA since all of its derived classes would not be templates themselves.
    Can you use a template class as a base class to derive non-template classes from? Does not sound right to me.

    Anyone have any ideas? Salem perhaps?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    What about making those other classes/functions friends to your svga template? If you don't want tp do that what about encapsulating your template in a singleton class? Just ideas if you would like more details just ask.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What I really need is run time type information to determine what class T is so that I can make reference to the SVGA object in the objects that need to use its functions.
    You would need to instantiate all the SVGA<T>, for al T you could possibly need while the program is running, at compile time. That is, you would need a few different instances of SVGA<T>, SVGA<char>, SVGA<int>, etc, despite probably only using one (although, at the cost of a word per pointer.. it's like no cost really).

    You can use a templated class as a base class for a non-derived class, but that non-derived class must specify exactly which templated class it is (IE SVGA<char> is fine, SVGA<T> is not, as it does not know what T is).

    It seems, however, that you want the opposite, which is fine as well. You want a templated class (that will implement all the SVGA functions) to be derived from some SVGA base, which would provide the interface. That way, you could make a single SVGA_base* globally, and use polymorphism in the function calls. Or then again, I could be totally missing your point .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Actually you hit the nail right on the head. But in my current design it is the programmer who is using my library who instantiates the class that he needs.


    int main(void)
    {
    SVGASys<COLOR16> Video;
    Video.SetMode(desiredVESAmode);

    ....

    return 0;
    }

    The constructor for the class:

    template <class T>
    SVGASys<T>::SVGASys(void)
    {
    }

    Most of the work is done in set mode which is where the current frame pointer, screen pointer, and buffer pointer are initialized.

    template <class T>
    SVGASys<T>::SetMode(unsigned short mode)
    {
    //Set the mode
    //Use FAT DS and near pointers in protected mode
    //yada yada yada
    ...
    ...
    Screen=(T*)(ADDRESS+__djgpp_conventional_base);
    Buffer=new T[ScreenSize];
    ScreenPtr=Screen;
    }

    So you see when the programmer or user instantiates the class, the correct buffer, screen, and frame pointer are allocated. This is very efficient since it only requires 3 pointers instead of 3*3 or 9 pointers (3 per bit depth), 6 of which will never be used. The other alternative is to dump the template and define 3 classes, 1 for the 256 color, 1 for 16-bit, and 1 for the 32-bit - but this is rather clunky.

    But I do like what you said about deriving off of the base SVGA. Currently I'm just overriding the primitive drawing functions which works because in 256 color modes you are only passing color and its an unsigned char. In hi color modes you will be passing 3 unsigned char's representing the desired RGB for the pixel, line, box, etc.

    Guess I'm gonna have to diagram the system in great detail on paper since there are so many design possibilities. Perhaps that will help me with the design. I have a small diagram but it needs to be revised.


    Also is there anyway for me to check that the programmer is in fact using the right data type as it relates to the selected video mode?

    Take this for example:

    SVGASys<COLOR16> Video;
    Video.SetMode(0x13);

    This will fail because mode 13h is an unsigned char mode (each pixel is one byte) not an unsigned short. How would I find out at compile time which data type the user has chosen? In other words, how would I check in SetMode() if the data type for Video (class SVGASys) is correct for the current mode? I would need to check to make sure that T was correct for the current mode.

    By the way, thx for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  2. class Template problems
    By Mr_roboto in forum C++ Programming
    Replies: 8
    Last Post: 02-16-2006, 10:21 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. templates with pointers
    By Cipher in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 11:45 AM