General Purpose library for Freestanding C++ and POSIX systems
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.

87 lines
2.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <stddef.h>
  3. #include "gp_config.hpp"
  4. #include "gp/variant.hpp"
  5. #include "gp/optional.hpp"
  6. #include "gp/buffer.hpp"
  7. #include "gp/vfs/fs_types.hpp"
  8. #include "gp/allocator/aggregator.hpp"
  9. namespace gp {
  10. class vfs {
  11. private:
  12. struct virtual_fs
  13. {
  14. virtual ~virtual_fs() = default;
  15. virtual fs::open_return open(buffer<char>, fs::file_flags, fs::file_permissions) = 0;
  16. virtual fs::read_return read(gp_config::file_descriptor_t, buffer<char>) = 0;
  17. virtual fs::write_return write(gp_config::file_descriptor_t, buffer<char>, size_t) = 0;
  18. virtual fs::close_return close(gp_config::file_descriptor_t) = 0;
  19. };
  20. template<typename concrete>
  21. class abstract_fs final : public virtual_fs{
  22. concrete internal_representation;
  23. public:
  24. abstract_fs(abstract_fs& v) = delete;
  25. abstract_fs(abstract_fs&& v)
  26. : internal_representation{v.internal_representation}
  27. {}
  28. abstract_fs(concrete& v) = delete;
  29. abstract_fs(concrete&& v)
  30. : internal_representation{v}
  31. {}
  32. virtual ~abstract_fs() override = default;
  33. virtual fs::open_return open(buffer<char> path, fs::file_flags flags, fs::file_permissions perms) override {
  34. return internal_representation.open(path, flags, perms);
  35. }
  36. virtual fs::read_return read(gp_config::file_descriptor_t fd, buffer<char> buff) override {
  37. return internal_representation.read(fd, buff);
  38. }
  39. virtual fs::write_return write(gp_config::file_descriptor_t fd, buffer<char> buff, size_t offset) override {
  40. return internal_representation.write(fd, buff, offset);
  41. }
  42. virtual fs::close_return close(gp_config::file_descriptor_t fd) override {
  43. return internal_representation.close(fd);
  44. }
  45. };
  46. reference_wrapper<aggregator> allocator;
  47. virtual_fs* file_system;
  48. public:
  49. template<typename T>
  50. vfs(T&& v, reference_wrapper<aggregator> _ref)
  51. : allocator{_ref}
  52. , file_system{new(allocator.get().allocate(sizeof(T))) T(gp::move(v))}
  53. {}
  54. fs::open_return open(buffer<char> path, fs::file_flags flags, fs::file_permissions perms) {
  55. return file_system->open(path, flags, perms);
  56. }
  57. fs::read_return read(gp_config::file_descriptor_t fd, buffer<char> buff) {
  58. return file_system->read(fd, buff);
  59. }
  60. fs::write_return write(gp_config::file_descriptor_t fd, buffer<char> buff, size_t offset) {
  61. return file_system->write(fd, buff, offset);
  62. }
  63. fs::close_return close(gp_config::file_descriptor_t fd) {
  64. return file_system->close(fd);
  65. }
  66. ~vfs() {
  67. file_system->~virtual_fs();
  68. allocator.get().deallocate(file_system);
  69. }
  70. };
  71. }