Thread: More template problems

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

    More template problems

    Trying to make an SVGA class that will handle all color depths.
    Problem is I dont want to implement each depth in a new class.

    Code:
    template <class T> class SVGASystem
    {
      T *Buffer;
      T *Screen;
      T *FramePtr;
      T REDS[255];
      T GRNS[255];
      T BLUS[255];
      public:
        SVGASystem<T> (void) {};
        void SetMode(void);
        void PlotPixel(int x,int y,unsigned char color);
        void PlotPixel(int x,int y,RGB color);
    };
    
    class Polygon:public SVGASystem<void>
    {
      public:
        Polygon(void) {};
        void DrawPolygon(void);
    };
    SetMode() allocates the correct pointer types for the current screen mode. This allows me to use one class for all depths and it works fine until another class needs access to the SVGASystem members. I don't want to include a pointer to the SVGASystem in every class that needs access to it. Instead I want to derive off of SVGASystem so that I gain access to SVGASystem's members. But when I derive, I'm not sure how to tell the compiler that Polygon is derived from SVGASystem of type T, and not type void. If I explicitly declare that Polygon is derived from a certain type of SVGASystem, I again have to implement 3 different objects for each color depth which is worse than implementing 3 classes for each color depth.

    To illustrate:
    Code:
    typedef unsigned long COLOR16M;
    typedef unsigned int    COLOR64K;
    typedef unsigned char COLOR256;
    For 32-bit color modes
    Code:
    SVGASystem<COLOR16M> Video;
    Video.SetMode(0x112);
    Video.PlotPixel(50,50,255,255,255);
    For 16-bit color modes
    Code:
    SVGASystem<COLOR64K> Video;
    Video.SetMode(0x111);
    Video.PlotPixel(50,50,255,255,255);
    For 256 color modes
    Code:
    SVGASystem<COLOR256> Video;
    Video.SetMode(0x13);
    Video.PlotPixel(50,50,255);
    These all work perfect. But now I want to derive an object from the SVGASystem like polygons, 3D objects (composed of polygons), etc. etc. These obviously need access to the SVGASystem so they can draw lines or at least need access to the Buffer, Screen, and FramePtr pointers. How can I derive objects from SVGASystem so the compiler understands that at run-time the type will be decided. So polygon might be derived from a 256 color, 16-bit color, or 32-bit color SVGASystem.

    My only alternative right now is to do this:
    Code:
    class Polygon:public SVGASystem<COLOR256>
    {
    }
    But this requires designing 3 objects for each color depth. This won't do either.
    Code:
    template <class T> class Polygon:public SVGASystem<T>
    {
    }
    This says that type T is not known. So in my PlotPixel function:
    Code:
    template <class T>
    SVGASystem<T>::PlotPixel(int x,int y,int red,int grn,int blu)
    {
       T color=REDS[red]+GRNS[grn]+BLUS[blu];
       ...
    }
    It says that color's type is not known. Since REDS, GRNS, and BLUS are defined to be of type T, it will give these errors.
    • Cannot convert [unsigned long | unsigned int | unsigned char] to type T (or in the first example to type void).
    • Size of storage type color is not known.


    Please someone that understands templates could you help me with this or guide me in another direction? I've looked at Bjarne's book closely and I'm sure you can do what I am trying to do, I just don't know how.

    Otherwise you could not do this:

    List<int> MyList;

    or

    List<MyClass> MyList;

    So the heart of my question is how to you derive a non-template class off of a template base when the derived class's type is not known? I want the derived class to inherit the base template class's type.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    It sounds from the examples you've given that your compiler isn't supporting templates properly. If I understand what you're trying to do, this should work (along as RTTI is enabled) -

    Code:
    #include <iostream> 
    
    using namespace std; 
    typedef unsigned long COLOR16M;
    typedef unsigned int    COLOR64K;
    typedef unsigned char COLOR256;
    
    
    template <class T> class SVGASystem
    {
      T *Buffer;
      T *Screen;
      T *FramePtr;
      T REDS[255];
      T GRNS[255];
      T BLUS[255];
      public:
        SVGASystem<T> (void) {};
    	void SetMode(void){}
    	void PlotPixel(int x,int y,unsigned char color){}
      };
    
    template <class T>
    class Polygon:public SVGASystem<T>
    {
      public:
        Polygon(void) {}
    	void DrawPolygon(void){cout << "Polygon : ";cout << typeid(T).name() << '\n' ;}
    };
    
    
    int main() 
    {
    	Polygon<COLOR16M> p;
    	p.DrawPolygon();
    	Polygon<COLOR256> pp;
    	pp.DrawPolygon();
    	
    	return 0;
    
    }

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yes that does work. My earlier question was how to eliminate the polygon<class T> stuff so that the programmer could simply create the SVGA object and then create polygons w/o having to specify the type.

    However I read some more stuff on the web and in Bjarnes's book. I cannot use a template class as a base class from which to derive off of.

    I'm still changing my implementation and may switch more towards using an interface class that will access the implentation class via pointers. This would allow me to change the implementation w/o changing the interface.

    But just to get my project going I've hard-coded it for 32-bit color since this SVGA problem has little or nothing to do with my actual project. I'll come back to the SVGA structure later after my project is done.

    Thanks for your help. Seems not many people use templates here on this board.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Something like this:


    class SVGAImp
    {
    }

    class SVGAIFace
    {
    }

    class SVGA:virtual protected SVGAImp, virtual public SVGAIFace
    {
    }

    I use protected because the public members of SVGAImp will become protected members which means that an outside source could not access my actual implementation functions. The SVGAIFace would have to be public or there would be no way for the programmer to draw primitives on the screen or in the buffer since SVGA does not handle the interface to the implementation.

    Also want to make SVGAImp and SVGAIFace virtual because I don't want multiple copies of them created, just one.

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