libostd/ostd/filesystem.hh

112 lines
2.9 KiB
C++
Raw Normal View History

2017-04-05 20:53:14 +02:00
/** @addtogroup Utilities
* @{
*/
/** @file filesystem.hh
*
* @brief Filesystem abstraction module.
*
* This module defines the namespace ostd::filesystem, which is either
* std::experimental::filesystem or std::filesystem depending on which
* is available. For portable applications, only use the subset of the
* module covered by both versions.
*
* It also provides range integration for directory iterators and
* ostd::format_traits specialization for std::filesystem::path.
2015-09-04 19:25:17 +02:00
*
2017-05-03 02:14:27 +02:00
* @include listdir.cc
*
2017-04-05 20:53:14 +02:00
* @copyright See COPYING.md in the project tree for further information.
2015-09-04 19:25:17 +02:00
*/
#ifndef OSTD_FILESYSTEM_HH
#define OSTD_FILESYSTEM_HH
#if __has_include(<filesystem>)
# include <filesystem>
namespace ostd {
namespace filesystem = std::filesystem;
}
#elif __has_include(<experimental/filesystem>)
# include <experimental/filesystem>
namespace ostd {
namespace filesystem = std::experimental::filesystem;
}
2016-07-06 19:31:21 +02:00
#else
# error "Unsupported platform"
#endif
2015-09-04 19:25:17 +02:00
#include "ostd/range.hh"
#include "ostd/format.hh"
2015-09-04 19:25:17 +02:00
namespace ostd {
2017-04-05 20:53:14 +02:00
/** @addtogroup Utilities
* @{
*/
/** @brief Range integration for std::filesystem::directory_iterator.
*
* Allows directory iterators to be made into ranges via ostd::iter().
*
* @see ostd::ranged_traits<filesystem::recursive_directory_iterator>
*/
template<>
struct ranged_traits<filesystem::directory_iterator> {
2017-04-05 20:53:14 +02:00
/** @brief The range type for the iterator. */
using range = iterator_range<filesystem::directory_iterator>;
2015-09-05 04:35:07 +02:00
2017-04-05 20:53:14 +02:00
/** @brief Creates a range for the iterator. */
static range iter(filesystem::directory_iterator const &r) {
return range{filesystem::begin(r), filesystem::end(r)};
2015-09-05 04:35:07 +02:00
}
2015-09-04 19:25:17 +02:00
};
2017-04-05 20:53:14 +02:00
/** @brief Range integration for std::filesystem::recursive_directory_iterator.
*
* Allows recursive directory iterators to be made into ranges via ostd::iter().
*
* @see ostd::ranged_traits<filesystem::directory_iterator>
*/
template<>
struct ranged_traits<filesystem::recursive_directory_iterator> {
2017-04-05 20:53:14 +02:00
/** @brief The range type for the iterator. */
using range = iterator_range<filesystem::recursive_directory_iterator>;
2017-04-05 20:53:14 +02:00
/** @brief Creates a range for the iterator. */
static range iter(filesystem::recursive_directory_iterator const &r) {
return range{filesystem::begin(r), filesystem::end(r)};
}
};
2015-09-04 19:25:17 +02:00
2017-04-05 20:53:14 +02:00
/** @brief ostd::format_traits specialization for std::filesystem::path.
*
* This allows paths to be formatted as strings. The value is formatted
* as if `path.string()` was formatted, using the exact ostd::format_spec.
*/
template<>
struct format_traits<filesystem::path> {
2017-04-05 20:53:14 +02:00
/** @brief Formats the path's string value.
*
* This calls exactly
*
* ~~~{.cc}
* fs.format_value(writer, p.string());
* ~~~
*/
template<typename R>
static void to_format(
filesystem::path const &p, R &writer, format_spec const &fs
) {
fs.format_value(writer, p.string());
2015-09-04 19:25:17 +02:00
}
};
2017-04-05 20:53:14 +02:00
/** @} */
2015-09-04 19:25:17 +02:00
} /* namespace ostd */
2016-02-07 22:17:15 +01:00
#endif
2017-04-05 20:53:14 +02:00
/** @} */