diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:13:13 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:13:13 +0200 |
commit | 7f23ee90fd545995a29e2175f15e8b97e59ca67a (patch) | |
tree | f8d0afac51995032e58b9a475ccbbc73ba207baf /shared/check_exist.h | |
parent | 3.19 (diff) | |
download | FreeFileSync-7f23ee90fd545995a29e2175f15e8b97e59ca67a.tar.gz FreeFileSync-7f23ee90fd545995a29e2175f15e8b97e59ca67a.tar.bz2 FreeFileSync-7f23ee90fd545995a29e2175f15e8b97e59ca67a.zip |
3.20
Diffstat (limited to 'shared/check_exist.h')
-rw-r--r-- | shared/check_exist.h | 71 |
1 files changed, 67 insertions, 4 deletions
diff --git a/shared/check_exist.h b/shared/check_exist.h index 6ec5534c..14a8a3f8 100644 --- a/shared/check_exist.h +++ b/shared/check_exist.h @@ -3,23 +3,86 @@ // * 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_TIMEOUT + EXISTING_NOT_READY }; -ResultExist fileExists(const Zstring& filename, size_t timeout); //timeout in ms -ResultExist dirExists( const Zstring& dirname, size_t timeout); //timeout in ms +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 |