Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
simple lock wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Oct 7, 2014
1 parent 6d466f3 commit 47e977f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/qtaround/os.hpp
Expand Up @@ -17,6 +17,10 @@
#include <QString>
#include <QVariant>
#include <QDateTime>
#include <QLockFile>

#include <memory>


namespace qtaround { namespace os {

Expand Down Expand Up @@ -235,6 +239,35 @@ static inline QString getTemp(A &&...args)
return os::path::join(getTemp(), std::forward<A>(args)...);
}

typedef std::unique_ptr<QLockFile> LockFileHandle;
class FileLock
{
public:
FileLock(LockFileHandle &&from) : lock_(std::move(from)) {}
//lock_ is movable only
private:
LockFileHandle lock_;
};

template <typename OnLocked>
LockFileHandle tryLock(LockFileHandle &&locker, OnLocked fn)
{
if (locker) {
if (locker->tryLock()) {
fn(FileLock(std::move(locker)));
return nullptr;
}
}
return std::move(locker);
}

template <typename OnLocked>
LockFileHandle tryLock(QString const &fileName, OnLocked fn)
{
LockFileHandle h{new QLockFile(fileName)};
return tryLock(std::move(h), fn);
}

}}

#endif // _CUTES_OS_HPP_
6 changes: 6 additions & 0 deletions src/os.cpp
Expand Up @@ -11,6 +11,11 @@
#include <qtaround/debug.hpp>
#include <QDebug>

#include <sys/stat.h>
#include <fcntl.h>
#include <cor/util.hpp>
#include <tuple>

namespace qtaround { namespace os {

namespace path {
Expand Down Expand Up @@ -384,4 +389,5 @@ QString mkTemp(QVariantMap &&options)
return res.trimmed();
}


}} // os

0 comments on commit 47e977f

Please sign in to comment.