#ifndef BASE_MAC_SCOPED_AUTHORIZATIONREF_H_
#define BASE_MAC_SCOPED_AUTHORIZATIONREF_H_
#include <Security/Authorization.h>
#include <utility>
#include "base/base_export.h"
#include "base/check.h"
namespace base::mac {
class BASE_EXPORT ScopedAuthorizationRef {
public:
explicit ScopedAuthorizationRef(AuthorizationRef authorization = nullptr)
: authorization_(authorization) {}
ScopedAuthorizationRef(const ScopedAuthorizationRef&) = delete;
ScopedAuthorizationRef& operator=(const ScopedAuthorizationRef&) = delete;
ScopedAuthorizationRef(ScopedAuthorizationRef&& that)
: authorization_(std::exchange(that.authorization_, nullptr)) {}
ScopedAuthorizationRef& operator=(ScopedAuthorizationRef&& that) {
authorization_ = std::exchange(that.authorization_, nullptr);
return *this;
}
~ScopedAuthorizationRef() {
if (authorization_) {
FreeInternal();
}
}
void reset(AuthorizationRef authorization = nullptr) {
if (authorization_ != authorization) {
if (authorization_) {
FreeInternal();
}
authorization_ = authorization;
}
}
bool operator==(AuthorizationRef that) const {
return authorization_ == that;
}
operator AuthorizationRef() const { return authorization_; }
explicit operator bool() const { return authorization_ != nullptr; }
[[nodiscard]] AuthorizationRef* InitializeInto() {
DCHECK(!authorization_);
return &authorization_;
}
AuthorizationRef get() const { return authorization_; }
[[nodiscard]] AuthorizationRef release() {
AuthorizationRef temp = authorization_;
authorization_ = nullptr;
return temp;
}
private:
void FreeInternal();
AuthorizationRef authorization_;
};
}
#endif