// Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. #pragma once #include #include #include "wpi/util/Handle.h" namespace wpi::util { /** * A move-only C++ wrapper around a WPI handle. * Will free the handle if FreeFunction is provided */ template requires std::same_as || std::invocable class Handle { public: Handle() = default; /*implicit*/ Handle(CType val) : m_handle(val) {} // NOLINT Handle(const Handle&) = delete; Handle& operator=(const Handle&) = delete; Handle(Handle&& rhs) : m_handle(rhs.m_handle) { rhs.m_handle = static_cast(CInvalid); } Handle& operator=(Handle&& rhs) { if (this != &rhs) { if constexpr (std::invocable) { if (m_handle != static_cast(CInvalid)) { FreeFunction(m_handle); } } } m_handle = rhs.m_handle; rhs.m_handle = static_cast(CInvalid); return *this; } ~Handle() { if constexpr (std::invocable) { if (m_handle != static_cast(CInvalid)) { FreeFunction(m_handle); } } } operator CType() const { return m_handle; } // NOLINT private: CType m_handle = static_cast(CInvalid); }; } // namespace wpi::util