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 500k
  • Answers 500k
  • 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 This would be very nice, but NDepend is limited to… May 16, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer Do you mean you dont want the table to scroll… May 16, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer If it's a TemplateField, you have to use the FindControl("control… May 16, 2026 at 1:58 pm

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 problem regarding 'static const' member initialization. In a templated class I
I have a class, A , in C++/CLI which derives from a templated base
Let's say I have 2 interfaces defined like so: public interface ISkuItem { public
I have that template class that uses a policy for it's output and another
How would you implement this ? I have the following model : class Something
This is possibly a candidate for a one-line answer. I would like know it
We have several legacy & 3'd-party systems in organization that use several RDBMS vendors
This is in C++. So, I'm starting from scratch writing a game engine for
I create many times I use class DataBuffer in many places in code, so
EDIT -- Answered below, missed the angled braces. Thanks all. I have been attempting

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.