1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-21 17:17:06 +02:00
vcmi/lib/UnlockGuard.h
Johannes Schauer Marin Rodrigues a1a5bc28c2
convert line endings from CRLF (Windows) to LF (Linux/Unix)
Mixed line endings cause problems when exporting patches with
git-format-patch and then trying to "git am" a patch with mixed and
non-matching line endings. In such a situation git will fail to apply
the patch.

This commit runs the dos2unix tools on the remaining files with CRLF
(\r\n) line endings to convert them to line-feeds (\n) only.

Files that are Windows specific like *.vcxproj and *.props files were
not converted.

Closes: #3073
2023-10-19 16:23:21 +02:00

121 lines
2.4 KiB
C++

/*
* UnlockGuard.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
namespace vstd
{
namespace detail
{
template<typename Mutex>
class unlock_policy
{
protected:
void unlock(Mutex &m)
{
m.unlock();
}
void lock(Mutex &m)
{
m.lock();
}
};
template<typename Mutex>
class unlock_shared_policy
{
protected:
void unlock(Mutex &m)
{
m.unlock_shared();
}
void lock(Mutex &m)
{
m.lock_shared();
}
};
}
//similar to boost::lock_guard but UNlocks for the scope + assertions
template<typename Mutex, typename LockingPolicy = detail::unlock_policy<Mutex> >
class unlock_guard : LockingPolicy
{
private:
Mutex* m;
explicit unlock_guard(unlock_guard&);
unlock_guard& operator=(unlock_guard&);
public:
explicit unlock_guard(Mutex& m_):
m(&m_)
{
this->unlock(*m);
}
unlock_guard()
{
m = nullptr;
}
unlock_guard(unlock_guard &&other)
: m(other.m)
{
other.m = nullptr;
}
void release()
{
m = nullptr;
}
~unlock_guard()
{
if(m)
this->lock(*m);
}
};
template<typename Mutex>
unlock_guard<Mutex, detail::unlock_policy<Mutex> > makeUnlockGuard(Mutex &m_)
{
return unlock_guard<Mutex, detail::unlock_policy<Mutex> >(m_);
}
template<typename Mutex>
unlock_guard<Mutex, detail::unlock_policy<Mutex> > makeEmptyGuard(Mutex &)
{
return unlock_guard<Mutex, detail::unlock_policy<Mutex> >();
}
template<typename Mutex>
unlock_guard<Mutex, detail::unlock_policy<Mutex> > makeUnlockGuardIf(Mutex &m_, bool shallUnlock)
{
return shallUnlock
? makeUnlockGuard(m_)
: unlock_guard<Mutex, detail::unlock_policy<Mutex> >();
}
template<typename Mutex>
unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> > makeUnlockSharedGuard(Mutex &m_)
{
return unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> >(m_);
}
template<typename Mutex>
unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> > makeUnlockSharedGuardIf(Mutex &m_, bool shallUnlock)
{
return shallUnlock
? makeUnlockSharedGuard(m_)
: unlock_guard<Mutex, detail::unlock_shared_policy<Mutex> >();
}
using unlock_shared_guard = unlock_guard<boost::shared_mutex, detail::unlock_shared_policy<boost::shared_mutex>>;
}
VCMI_LIB_NAMESPACE_END