lundi 19 juillet 2021

unique_ptr as general purpose dispose object

Suppose I have a situation, where I have an SDK which provides certain interface, which implies some manual resources management or state changes. It is supposed to be used like this:

// SDK has state B by default
SDK->setStateA();
something();
requiring();
stateA();
SDK->setStateB();

Is it a good idea to incapsulate state changes as custom allocator/deleter for unique_ptr object or, probably, it would be better to get this behaviour through some manual Dispose pattern implementation.

Since it's not a resource allocation, I have doubts. It might cause confusion and make code cryptic.

My other concern is that I need a return code from both init and clean up steps. I could use lambdas and get those through captures, but it looks even more cryptic.

Maybe someone tried it already and saw how it makes code look after a while?

Aucun commentaire:

Enregistrer un commentaire