2
\$\begingroup\$

I’m writing a render, there is a class that shaders should store, something like a shader manager. These shaders are stored as

std :: map <std :: string, std :: shared_ptr <BaseShader>> cache;

BaseShader is an abstract class (there are virtual methods).

Other classes want to own shaders, and request them accordingly by name. That is, they each have a copy of the smart pointer. After the change, the shaders need to be rebooted from the disk. Accordingly, new is done and written by name in the cache, the problem is that clients do not see this change. How to make sure that the reset for the smart pointer is not made, and the updated object was copied to the same address that it stores?

\$\endgroup\$
1
  • \$\begingroup\$ This question is to vague, can you tell us something about your architecture? How are the resource taker connected to the provider? \$\endgroup\$
    – PSquall
    Aug 20, 2019 at 13:30

2 Answers 2

2
\$\begingroup\$

Update Object in place.

*cache["foo"] = Object(stuff);

Or add a method that will reload the entire object.

cache["foo"]->reload(stuff);
\$\endgroup\$
0
\$\begingroup\$

How to make sure that the reset for the smart pointer is not made, and the updated object was copied to the same address that it stores?

I have not done this before, but you could allocate a chunk of memory yourself with malloc, and call the constructor yourself with an address that you now own (in the chunk allocated with malloc).

That could work if your object is a pod an never change in size. It can get very messy and complicated if your object has references to other objects (string, vector, etc.).

I would suggest you explore other strategies:

One option is that your cache returns weak_ptrs instead of shared_ptrs. Once the object is reloaded, the weak_ptr will be 'invalidated'. When this happens, when you weak_ptr.lock() and the returned shared_ptr is null, you know you need to re-fetch the object from the cache.

Another option is to make an event system where the owners of the objects reference are notified that the model has been reloaded in the cache and they should request a new version of it.

You could also simply request the cache via a handle every time you need the model, so you always have the most up-to-date version. (You use a string for now, you could hash that and turn it into an int to make it faster.)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .