Debian Codegraph @

src/utils/io/base_file.hpp

raw
1 #ifndef BASE_FILE_HPP
2 #define BASE_FILE_HPP
3
4 #include "utils/span.hpp"
5 #include "utils/utils.hpp"
6
7 #include <type_traits>
8
9 CPPTRACE_BEGIN_NAMESPACE
10 namespace detail {
11 class base_file {
12 public:
13 virtual ~base_file() = default;
14 virtual string_view path() const = 0;
15 virtual Result<monostate, internal_error> read_bytes(bspan buffer, off_t offset) const = 0;
16
17 template<
18 typename T,
19 typename std::enable_if<
20 std::is_standard_layout<T>::value && is_trivially_copyable<T>::value && !is_span<T>::value,
21 int
22 >::type = 0
23 >
24 Result<T, internal_error> read(off_t offset) {
25 T object{};
26 auto res = read_bytes(make_bspan(object), offset);
27 if(!res) {
28 return res.unwrap_error();
29 }
30 return object;
31 }
32
33 template<
34 typename T,
35 typename std::enable_if<
36 std::is_standard_layout<T>::value && is_trivially_copyable<T>::value,
37 int
38 >::type = 0
39 >
40 Result<monostate, internal_error> read_span(span<T> items, off_t offset) {
41 return read_bytes(
42 make_span(reinterpret_cast<char*>(items.data()), reinterpret_cast<char*>(items.data() + items.size())),
43 offset
44 );
45 }
46 };
47 }
48 CPPTRACE_END_NAMESPACE
49
50 #endif