Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:13:50+00:00 2026-05-16T09:13:50+00:00

I have implemented a templated buffer class like this: template <class T, class Impl>

  • 0

I have implemented a templated buffer class like this:

template <class T, class Impl>
class base_hardware_buffer : Impl
{
public:
    typedef const T& const_reference;
    typedef T* pointer;

    void push_back(reference r)
    {
        // Do some generic operation, call impl when needed
        // ...
    }

    pointer map()
    {
        static_cast<Impl*> (this)->map();
    }

    // ... etc
};

Then I defined two implementations, one for OpenGL and one for DirectX each instantiated in their own dynamic library.
For OpenGL:

template <class T>
class glBuffer : base_hardware_buffer<T, glBuffer<T> >
{
public:
    typedef T* pointer;

    pointer map()
    {
        // Actual implementation of map
    }

    // Other implementation specific operation and members go here
};

This is all very nice, and works quite well.
Now my problem is that my team mate who is working on the layer above my subsystems wants to implement an algorithm that requires access to the buffers.

How can I give him a uniform access to the buffer without exposing / hard-coding the specific implementations into his code, nor compromising type safety ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:13:51+00:00Added an answer on May 16, 2026 at 9:13 am

    I can explain how I implemented a solution to a similar problem.

    Basically I have a “CreateBuffer” function that returns a pointer to a “base” buffer. Then when I call functions on that base buffer they move up to the relevant function.

    class BaseBuffer
    {
    public:
        BaseBuffer(void);
        virtual ~BaseBuffer(void);
    
        virtual bool Init( GFXHandler* pHandler, unsigned int usage, unsigned int sizeofData )  { return false; }
        virtual bool Shutdown()                                             = 0;
    
        virtual bool LockBuffer( unsigned int flags, void** ppData )        = 0;
        virtual bool UnlockBuffer()                                         = 0;
    };
    

    Now the trick I used, to make sure that the end user doesn’t care what the back end implementation of the buffer is, was to make “CreateBuffer” a function pointer. Then when I set up the graphics handler (e.g. GL2Handler) I assign the “GL2CreateBuffer” function to the “CreateBuffer” function pointer.

    This way the end-user can just do the following:

    BaseBuffer* pBuffer = CreateBuffer();
    pBuffer->Init( /*init params*/ );
    
    void* pData = NULL;
    pBuffer->LockBuffer( /*lock flags*/, &pData );
    
    memcpy( pData, pOriginalData, sizeOfOriginalData );
    
    pBuffer->UnlockBuffer();
    

    Job done. If a D3D9Handler was created it used the D3D9 implementation if a GL2Handler was created it uses the GL2 implementation etc.

    Edit: In answer to your comment wouldn’t you just be able to do the following:

    template< typename Type > bool LockBuffer( unsigned int flags, Type** pData );
    

    Or do you want to specifically create a class where the only returnable type is the type that it was created with? That would be a tad more complicated as you suddenly have tonnes of different types that would need assigning to the various function pointers as 2 seperate types passed into a template create 2 different functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a template class defined in a header file like this. Here I
I have implemented VirtualPathProvider class so I can keep all my views in the
I have a class Request.cs It has an abstract method: public abstract Response CreateResponse(XmlReader
I have implemented what I thought was a pretty decent representation of MVC in
I have implemented a simple file upload-download mechanism. When a user clicks a file
I have implemented a python webserver. Each http request spawns a new thread. I
I have implemented a SAX parser in Java by extending the default handler. The
I have implemented tracing based on System.Diagnostics. I am also using a System.Diagnostics.TextWriterTraceListener, and
We have implemented a popup window as a modal dialog using the IE method:
I have implemented authentication systems for webapps several times over the years, but before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.