C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
virtual_fs_operator.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <concepts>
26#include <istream>
27#include <memory>
28#include <ostream>
29#include <string_view>
30#include <type_traits>
31#include <utility>
32
33namespace essence::io::abstract {
38 public:
39 template <typename T>
40 requires(!std::same_as<std::decay_t<T>, virtual_fs_operator>)
41 explicit virtual_fs_operator(T&& value) : wrapper_{std::make_shared<wrapper<T>>(std::forward<T>(value))} {}
42
48 [[nodiscard]] bool exists(std::string_view path) const {
49 return wrapper_->exists(path);
50 }
51
57 [[nodiscard]] bool is_file(std::string_view path) const {
58 return wrapper_->is_file(path);
59 }
60
66 [[nodiscard]] bool is_directory(std::string_view path) const {
67 return wrapper_->is_directory(path);
68 }
69
76 [[nodiscard]] std::unique_ptr<std::iostream> open(
77 std::string_view path, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) const {
78 return wrapper_->open(path, mode);
79 }
80
87 [[nodiscard]] std::unique_ptr<std::istream> open_read(
88 std::string_view path, std::ios_base::openmode mode = std::ios_base::in) const {
89 return wrapper_->open_read(path, mode);
90 }
91
98 [[nodiscard]] std::unique_ptr<std::ostream> open_write(
99 std::string_view path, std::ios_base::openmode mode = std::ios_base::out) const {
100 return wrapper_->open_write(path, mode);
101 }
102
103 private:
104 struct base {
105 virtual ~base() = default;
106 virtual bool exists(std::string_view path) = 0;
107 virtual bool is_file(std::string_view path) = 0;
108 virtual bool is_directory(std::string_view path) = 0;
109 virtual std::unique_ptr<std::iostream> open(std::string_view path, std::ios_base::openmode mode) = 0;
110 virtual std::unique_ptr<std::istream> open_read(std::string_view path, std::ios_base::openmode mode) = 0;
111 virtual std::unique_ptr<std::ostream> open_write(std::string_view path, std::ios_base::openmode mode) = 0;
112 };
113
114 template <typename T>
115 class wrapper final : public base {
116 public:
117 template <std::convertible_to<T> U>
118 explicit wrapper(U&& value) : value_{std::forward<U>(value)} {}
119
120 bool exists(std::string_view path) override {
121 return value_.exists(path);
122 }
123
124 bool is_file(std::string_view path) override {
125 return value_.is_file(path);
126 }
127
128 bool is_directory(std::string_view path) override {
129 return value_.is_directory(path);
130 }
131
132 std::unique_ptr<std::iostream> open(std::string_view path, std::ios_base::openmode mode) override {
133 return value_.open(path, mode);
134 }
135
136 std::unique_ptr<std::istream> open_read(std::string_view path, std::ios_base::openmode mode) override {
137 return value_.open_read(path, mode);
138 }
139
140 std::unique_ptr<std::ostream> open_write(std::string_view path, std::ios_base::openmode mode) override {
141 return value_.open_write(path, mode);
142 }
143
144 private:
145 T value_;
146 };
147
148 std::shared_ptr<base> wrapper_;
149 };
150} // namespace essence::io::abstract
Provides an ability to operator files on a virtual file system defined by the user.
Definition virtual_fs_operator.hpp:37
std::unique_ptr< std::iostream > open(std::string_view path, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) const
Opens a file in read-write mode.
Definition virtual_fs_operator.hpp:76
std::unique_ptr< std::ostream > open_write(std::string_view path, std::ios_base::openmode mode=std::ios_base::out) const
Opens a file in write-only mode.
Definition virtual_fs_operator.hpp:98
std::unique_ptr< std::istream > open_read(std::string_view path, std::ios_base::openmode mode=std::ios_base::in) const
Opens a file in read-only mode.
Definition virtual_fs_operator.hpp:87
bool exists(std::string_view path) const
Checks whether a file exists.
Definition virtual_fs_operator.hpp:48
bool is_directory(std::string_view path) const
Checks whether a path names a directory.
Definition virtual_fs_operator.hpp:66
bool is_file(std::string_view path) const
Checks whether a path names a regular file.
Definition virtual_fs_operator.hpp:57