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

  • SEARCH
  • Home
  • 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 7077349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:24:45+00:00 2026-05-28T06:24:45+00:00

Thinking of a way how to best approach the following design (pseudo C++ code):

  • 0

Thinking of a way how to best approach the following design (pseudo C++ code):

class Display {

  public:

   Bitmap GetBitmap(uint16 width, uint16 height);

  private:

   // A list of active bitmaps
   std::set<Bitmap> bitmaps;
}

… where Display is a factory (and sole owner) for Bitmap objects. Display also has to track all the allocated bitmaps within a std::set container and may choose to invalidate/delete them at any time (like in a fullscreen switch).

So essentially a Bitmap is a weak resource handle and whoever holds the handle will have to check if it is valid before using it.

My theoretical solution right now is to use C++ smart pointers:

  • GetBitmap() will return a weak_ptr<Bitmap> and thus can be checked for validity using weakBitmap.lock().
  • Internal bitmaps std::set container will hold shared_ptr<Bitmap> so as to be able to give away weak bitmap pointers.

What is the best way to implement given design? Is there a de facto design pattern for given functionality I am missing?

  • 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-28T06:24:45+00:00Added an answer on May 28, 2026 at 6:24 am

    I think you may want to consider turning it around : give away the ownership of objects you have created (e.g. returning boost::shared_ptr<Bitmap>) and only retain weak pointers to objects in your internal collection (e.g. std::list<boost::weak_ptr<Bitmap>>). Whenever user asks for a Bitmap, do a lock on your collection and add a new Bitmap there. If your collection is a list you can safely store iterator to just added element and remove it from the collection later, thus handling destruction of a Bitmap.

    Since boost::shared_ptr::lock is atomic, it will also work when multithreading. But you still need to guard access to collection with a lock.

    Here is sample code:

    #include <boost/shared_ptr.hpp>
    #include <boost/weak_ptr.hpp>
    #include <boost/noncopyable.hpp>
    #include <boost/thread.hpp>
    
    class Display;
    
    class Bitmap : boost::noncopyable
    {
      friend class Display;
      Bitmap(int, Display* d) : display(d)
      {}
    
      Display* display;
    public:
    
      ~Bitmap(); // see below for definition
    };
    
    typedef boost::shared_ptr<Bitmap> BitmapPtr;
    
    class Display
    {
      typedef std::list<boost::weak_ptr<Bitmap>> bitmaps_t;
      typedef std::map<Bitmap*, bitmaps_t::iterator> bitmap_map_t;
      bitmaps_t     bitmaps_;
      bitmap_map_t  bitmap_map_;
      boost::mutex  mutex_;
    
      friend class Bitmap;
      void Remove(Bitmap* p)
      {
        boost::lock_guard<boost::mutex> g(mutex_);
        bitmap_map_t::iterator i = bitmap_map_.find(p);
        if (i != bitmap_map_.end())
        {
          bitmaps_.erase(i->second);
          bitmap_map_.erase(i);
        }
      }
    
    public:
      ~Display()
      {
        boost::lock_guard<boost::mutex> g(mutex_);
        for (bitmaps_t::iterator i = bitmaps_.begin(); i != bitmaps_.end(); ++i)
        {
          BitmapPtr ptr = i->lock();
          if (ptr)
            ptr->display = NULL;
        }
      }
    
      BitmapPtr GetBitmap(int i)
      {
        BitmapPtr r(new Bitmap(i, this));
        boost::lock_guard<boost::mutex> g(mutex_);
        bitmaps_.push_back(boost::weak_ptr<Bitmap>(r));
        bitmap_map_[r.get()] = --bitmaps_.end();
        return r;
      }
    };
    
    Bitmap::~Bitmap()
    {
      if (display)
        display->Remove(this);
    }
    

    Please note there is two-way link between Bitmap and Display – they both keep a weak pointer to each other, meaning they can freely communicate. For example, if you want Display to be able to “destroy” Bitmaps, it can simply disable them by updating “display” to NULL, as demonstrated in Display destructor. Display has to lock a weak_ptr<Bitmap> every time it wants to access the Bitmap (example in destructor) but this shouldn’t be much of a problem if this communication does not happen often.

    For thread safety you may need to lock Bitmap everytime you want to “disable” it i.e. reset display member to NULL, and lock it everytime you want to access “display” from Bitmap. Since this has performance implications, you might want to replace Display* with weak_ptr<Display> in Bitmap. This also removes need for destructor in Display. Here is updated code:

    class Bitmap : boost::noncopyable
    {
      friend class Display;
      Bitmap(int, const boost::shared_ptr<Display>& d) : display(d)
      {}
    
      boost::weak_ptr<Display> display;
    
    public:
      ~Bitmap(); // see below for definition
    };
    
    typedef boost::shared_ptr<Bitmap> BitmapPtr;
    
    class Display : public boost::enable_shared_from_this<Display>
                  , boost::noncopyable
    {
      //... no change here
    
    public:
      BitmapPtr GetBitmap(int i)
      {
        BitmapPtr r(new Bitmap(i, shared_from_this()));
        boost::lock_guard<boost::mutex> g(mutex_);
        bitmaps_.push_back(boost::weak_ptr<Bitmap>(r));
        bitmap_map_[r.get()] = --bitmaps_.end();
        return r;
      }
    };
    
    Bitmap::~Bitmap()
    {
      boost::shared_ptr<Display> d(display);
      if (d)
        d->Remove(this);
    }
    

    In this case “disabling” display pointer in Bitmap is thread safe without explicit synchronisation, and looks like this:

    Display::DisableBitmap(bitmaps_t::iterator i)
    {
      BitmapPtr ptr = i->lock();
      if (ptr)
        ptr->display.reset();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted your advice for the best design approach at the following Python project.
I am thinking on the following approach but not sure if its the best
Just thinking about the best way to build an Order form that would (from
What is the best way of tracking responses for email campaigns? I was thinking
Maybe I'm thinking about this the wrong way but here's the idea: Class A
I'm trying to figure out the best way to approach this. I have a
I think the best way to approach this is jump straight in and explain
I'm wondering what the best approach is to verify that a class has all
Anyone help with best way to represent/query the following problem products can be sold
I am trying to figure out what the best way to approach Async object

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.