Skip to content

Instantly share code, notes, and snippets.

@PatrikValkovic
Created February 27, 2020 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatrikValkovic/14baa4064dd9e5979a6ab23756560f92 to your computer and use it in GitHub Desktop.
Save PatrikValkovic/14baa4064dd9e5979a6ab23756560f92 to your computer and use it in GitHub Desktop.
Simple RAII (resource acquisition is initialization) paradigm implementation. Only for scalar types!
template<typename T>
class RAII
{
static_assert(std::is_scalar<T>::value, "RAII can handle only scalars");
private:
const T _resource;
std::function<void(T)> _destroy;
public:
RAII(std::function<void(T)> destroy, T resource) noexcept : _resource(resource), _destroy(destroy)
{}
template<typename FUNC, typename ...PARAMS>
RAII(std::function<void(T)> destroy, FUNC create, PARAMS&& ...params)
: _resource(create(std::forward<PARAMS>(params)...)), _destroy(destroy)
{}
RAII(const RAII&) = delete;
RAII(RAII&&) = default;
RAII& operator=(const RAII&) = delete;
RAII& operator=(RAII&&) = default;
~RAII() {
if (_destroy) {
_destroy(_resource);
_destroy = nullptr;
}
}
inline const T& getResource() {
return _resource;
}
operator T() {
return _resource;
}
inline void free() {
this->~RAII();
}
};
template<>
class RAII<void> {
private:
std::function<void()> _destroy;
public:
RAII(std::function<void()> destroy) noexcept : _destroy(destroy)
{}
template<typename FUNC, typename ...PARAMS>
RAII(std::function<void()> destroy, FUNC create, PARAMS&& ...params): RAII(destroy) {
create(std::forward<PARAMS>(params)...);
}
RAII(const RAII&) = delete;
RAII(RAII&&) = default;
RAII& operator=(const RAII&) = delete;
RAII& operator=(RAII&&) = default;
~RAII() {
if (_destroy) {
_destroy();
_destroy = nullptr;
}
}
void free() {
this->~RAII();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment