You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

112 lines
2.5 KiB

  1. #pragma once
  2. #include <vector>
  3. #include <cppfs/directory.h>
  4. #include "directory.h"
  5. namespace asyncpp {
  6. namespace fs {
  7. namespace __directory {
  8. struct read_all_stream
  9. : public base_stream<
  10. cppfs::directory::entry,
  11. read_all_stream>
  12. {
  13. public:
  14. using value_type = cppfs::directory::entry;
  15. using this_type = read_all_stream;
  16. using base_future_type = base_stream<value_type, this_type>;
  17. using result_type = typename base_future_type::result_type;
  18. private:
  19. struct entry
  20. {
  21. cppfs::directory::iterator it;
  22. cppfs::directory::iterator end;
  23. };
  24. using entry_stack = std::vector<entry>;
  25. entry_stack _stack;
  26. public:
  27. inline read_all_stream(
  28. const fs::directory& p_directory)
  29. {
  30. auto& d = p_directory.handle;
  31. if (d.exists())
  32. {
  33. _stack.emplace_back(entry {
  34. d.begin(),
  35. d.end()
  36. });
  37. }
  38. }
  39. public:
  40. inline result_type poll()
  41. {
  42. while (true)
  43. {
  44. if (_stack.empty())
  45. return result_type::done();
  46. auto& e = _stack.back();
  47. if (e.it == e.end)
  48. {
  49. _stack.pop_back();
  50. continue;
  51. }
  52. auto ret = result_type::ready(std::move(*e.it));
  53. cppfs::directory d(e.it->path);
  54. ++e.it;
  55. if (d.exists())
  56. {
  57. _stack.emplace_back(entry {
  58. d.begin(),
  59. d.end(),
  60. });
  61. }
  62. return ret;
  63. }
  64. }
  65. };
  66. template<
  67. chaining_mode X_mode,
  68. typename X_self>
  69. inline auto read_all_impl(X_self&& p_self)
  70. { return read_all_stream(std::forward<X_self>(p_self)); }
  71. } } }
  72. namespace asyncpp {
  73. namespace fs {
  74. template<chaining_mode X_mode>
  75. auto directory
  76. ::read_all() &
  77. {
  78. return __directory::read_all_impl<X_mode>(*this);
  79. }
  80. template<chaining_mode X_mode>
  81. auto directory
  82. ::read_all() &&
  83. {
  84. static_assert(
  85. X_mode != ref,
  86. "Can not store rvalue reference as lvalue reference!");
  87. return __directory::read_all_impl<X_mode>(std::move(*this));
  88. }
  89. } }