// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later

#ifndef OBJECTLOCK_H
#define OBJECTLOCK_H

#include "base/object.hpp"

namespace icinga
{

/**
 * A scoped lock for Objects.
 */
struct ObjectLock
{
public:
	ObjectLock(const Object::Ptr& object);
	ObjectLock(const Object::Ptr& object, std::defer_lock_t);
	ObjectLock(const Object *object);

	ObjectLock(const ObjectLock&) = delete;
	ObjectLock& operator=(const ObjectLock&) = delete;

	~ObjectLock();

	bool TryLock() noexcept;
	void Lock();
	void Unlock();

	operator bool() const;

private:
	const Object *m_Object{nullptr};
	bool m_Locked{false};
};

}

#endif /* OBJECTLOCK_H */
