summaryrefslogtreecommitdiff
path: root/shared/check_exist.h
blob: 14a8a3f8b83ada10a18940dd74bfdbe1c8a91ad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************

#ifndef CHECKEXIST_H_INCLUDED
#define CHECKEXIST_H_INCLUDED

#include "zstring.h"
#include "boost_thread_wrap.h" //include <boost/thread.hpp>
#include "file_handling.h"


namespace util
{
//check for file or folder existence asynchronously
boost::unique_future<bool> somethingExistsAsync(const Zstring& somename);
boost::unique_future<bool>      fileExistsAsync(const Zstring& filename);
boost::unique_future<bool>       dirExistsAsync(const Zstring&  dirname);

//some syntactic sugar:
enum ResultExist
{
    EXISTING_TRUE,
    EXISTING_FALSE,
    EXISTING_NOT_READY
};

ResultExist somethingExists(const Zstring& somename, size_t timeout);
ResultExist      fileExists(const Zstring& filename, size_t timeout);
ResultExist       dirExists(const Zstring&  dirname, size_t timeout);












//################## implementation ##########################
template <bool (*fun)(const Zstring&)>
boost::unique_future<bool> objExistsAsync(const Zstring& objname)
{
    //thread safety: make it a pure value type for use in the thread!
    const Zstring objnameVal = objname; //atomic ref-count => binary value-type semantics!
    boost::packaged_task<bool> pt([=] { return (*fun)(objnameVal); });
    auto fut = pt.get_future();
    boost::thread(std::move(pt));
    return std::move(fut);
}


inline
boost::unique_future<bool> somethingExistsAsync(const Zstring& somename) { return objExistsAsync<&zen::somethingExists>(somename); }

inline
boost::unique_future<bool> fileExistsAsync(const Zstring& filename) { return objExistsAsync<&zen::fileExists>(filename); }

inline
boost::unique_future<bool>  dirExistsAsync(const Zstring& dirname) { return objExistsAsync<&zen::dirExists>(dirname); }


template <bool (*fun)(const Zstring&)> inline
ResultExist objExists(const Zstring& objname, size_t timeout)
{
    auto ft = objExistsAsync<fun>(objname);
    if (!ft.timed_wait(boost::posix_time::milliseconds(timeout)))
        return EXISTING_NOT_READY;
    return ft.get() ? EXISTING_TRUE : EXISTING_FALSE;
}


inline
ResultExist somethingExists(const Zstring& somename, size_t timeout) { return objExists<&zen::somethingExists>(somename, timeout); }

inline
ResultExist fileExists(const Zstring& filename, size_t timeout) { return objExists<&zen::fileExists>(filename, timeout); }

inline
ResultExist dirExists(const Zstring& dirname, size_t timeout) { return objExists<&zen::dirExists>(dirname, timeout); }
}

#endif // CHECKEXIST_H_INCLUDED
bgstack15