![]() |
UniqueResource, a C++ template class as a universal resource manager
https://github.com/z16166/UniqueResource
UniqueResource, a C++ template class which works as a universal resource manager to own and free all kinds of resources. C++14 and above, header-only. Made by me, inspired by std::experimental::unique_resource and similar projects. |
You can also do something similar to this by using the built-in 'std::shared_ptr' type. It allows for initialization with a custom deleter function. With that, you can pass a type cleanup call for types that have one. (You can also just define your own callback to cleanup custom/more unique types as needed.)
For example, using a 'HANDLE' type, which is cleaned up using 'CloseHandle': PHP Code:
|
Yes. I have done that for Windows native RPC handles.
The problem is, we have to specify the custom deleter each time when we declare a new variable. It's tedious. Maybe a typedef or "using"? And, std::unique_ptr lacks abstraction for a default_value. Also, we can add custom conversion operator for UniqueResource to avoid explicitly using Get() method each time, just like "operator LPCSTR()", and "operator LPCWSTR()" of the ancient CString class from Microsoft. Code:
explicit operator ResourceType() const noexceptsamples of custom deleters for windows native RPC( "pointer" is defined for each functor): Code:
struct StringBindingDeleter {usage: Code:
RPC_WSTR stringBinding = nullptr;Quote:
|
Using templates can remove the need to manually define the delete function. It can also be used to further extend into a custom wrapper to deal with the other parts you mentioned as well such as:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
|
Your coding a wrapper/aggregator for std::unique_ptr is a good choice.
Some types are same, but with different default values and different cleanup APIs. For OpenProcess()/OpenThreadToken() etc., the type is HANDLE, the default value is NULL, the cleanup API is CloseHandle(). For CreateFile()/FindFirstFile()/CreateToolhelp32Snapshot() etc., the type is also HANDLE, but the default value is INVALID_HANDLE_VALUE, and the cleanup API for FindFirstFile() is FindClose(), instead of CloseHandle(). So that's why I use traits/adapter to describe the difference. And, I need the operator&() override to auto reset to the default value when reusing the same UniqueResource variable. Quote:
|
Is it hard to abstract the whole Win API I would think without having a way to define dependencies. Resources sometimes form a hierarchy and have to be closed in that order e.g. CloseServiceHandle on the service then the manager. But it's hard to do as eventually you are doing reference dependency checking similar to a garbage collector. M$FT wrapped the Win API using MFC for that reason as they could construct the hierarchy and encapsulate handles. Not to say this technique isn't usable in some common and simple everyday scenarios.
|
There is no silver bullet :D
Such unique/scope resource managers never consider dependencies. There is also std::experimental::scope_exit() for RAII. Quote:
|
| All times are GMT +8. The time now is 09:00. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX