2023-02-03 12:31:39 -06:00
|
|
|
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE. */
|
|
|
|
|
|
|
|
|
|
#ifndef UTILS_HANDLE_H_
|
|
|
|
|
#define UTILS_HANDLE_H_
|
|
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
2023-05-30 19:21:14 +00:00
|
|
|
namespace rocprofiler {
|
2023-02-03 12:31:39 -06:00
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
template <typename T, typename D> class handle_t {
|
2023-02-03 12:31:39 -06:00
|
|
|
T value_;
|
|
|
|
|
bool will_delete_;
|
2023-07-13 19:48:38 +00:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
handle_t() noexcept : will_delete_(false) {}
|
|
|
|
|
|
|
|
|
|
template <typename U, std::enable_if_t<std::is_same<T, U>::value, bool> = true>
|
|
|
|
|
handle_t(U&& v, bool const will_delete = true) noexcept
|
|
|
|
|
: value_(std::move(v)), will_delete_(will_delete) {}
|
2023-02-03 12:31:39 -06:00
|
|
|
|
|
|
|
|
// No copy construction or copy assignment of handles
|
2023-07-13 19:48:38 +00:00
|
|
|
handle_t(handle_t const&) = delete;
|
|
|
|
|
handle_t& operator=(handle_t const&) = delete;
|
2023-02-03 12:31:39 -06:00
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
handle_t& operator=(handle_t&& h) noexcept {
|
2023-02-03 12:31:39 -06:00
|
|
|
reset();
|
|
|
|
|
value_ = std::move(h.value_);
|
|
|
|
|
will_delete_ = h.will_delete_;
|
|
|
|
|
h.release();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
handle_t(handle_t&& h) noexcept : value_(std::move(h.value_)) { h.release(); }
|
2023-02-03 12:31:39 -06:00
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
~handle_t() noexcept { reset(); }
|
2023-02-03 12:31:39 -06:00
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
T const& get() const noexcept { return value_; }
|
2023-02-03 12:31:39 -06:00
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
typename std::add_lvalue_reference<typename std::remove_pointer<T>::type>::type operator*()
|
|
|
|
|
const noexcept {
|
2023-02-03 12:31:39 -06:00
|
|
|
return *value_;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
T operator->() const noexcept { return value_; }
|
2023-02-03 12:31:39 -06:00
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
T const& release() noexcept {
|
2023-02-03 12:31:39 -06:00
|
|
|
will_delete_ = false;
|
|
|
|
|
return get();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
void reset() noexcept {
|
2023-02-03 12:31:39 -06:00
|
|
|
if (will_delete_) {
|
|
|
|
|
will_delete_ = false;
|
|
|
|
|
D{}(value_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
void reset(T&& v) noexcept {
|
2023-02-03 12:31:39 -06:00
|
|
|
reset();
|
|
|
|
|
value_ = std::move(v);
|
|
|
|
|
will_delete_ = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
} // namespace rocprofiler
|
2023-02-03 12:31:39 -06:00
|
|
|
|
2023-07-13 19:48:38 +00:00
|
|
|
#endif // UTILS_HANDLE_H_
|