Debian Codegraph @

common/include/physfs_list.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 #pragma once
9 #include <physfs.h>
10
11 typedef char file_extension_t[5];
12
13 #ifdef __cplusplus
14 #include <cstdint>
15 #include <memory>
16 #include "null_sentinel_iterator.h"
17
18 #include "dxxsconf.h"
19 #include "dsx-ns.h"
20 #include "fwd-partial_range.h"
21 #include <array>
22 #include <ranges>
23
24 namespace dcx {
25
26 class PHYSFS_list_deleter
27 {
28 public:
29 void operator()(char **list) const
30 {
31 PHYSFS_freeList(list);
32 }
33 };
34
35 template <typename Deleter>
36 class PHYSFSX_uncounted_list_template : protected std::unique_ptr<char *[], Deleter>
37 {
38 using base_type = std::unique_ptr<char *[], Deleter>;
39 public:
40 using base_type::base_type;
41 using base_type::get;
42 using base_type::operator bool;
43 using base_type::operator[];
44 using base_type::release;
45 using base_type::reset;
46 null_sentinel_iterator<char *> begin() const
47 {
48 return this->get();
49 }
50 null_sentinel_sentinel<char *> end() const
51 {
52 return {};
53 }
54 };
55
56 template <typename Deleter>
57 class PHYSFSX_counted_list_template : public PHYSFSX_uncounted_list_template<Deleter>
58 {
59 using base_type = PHYSFSX_uncounted_list_template<Deleter>;
60 std::size_t count = 0;
61 public:
62 /* Inherit the zero-argument form of reset(), for clearing a list.
63 * Override and delete the one-argument form, so that the list cannot be
64 * given new contents, with a potentially different length, using
65 * `reset(other_list);`
66 */
67 using base_type::reset;
68 void reset(auto) = delete;
69 constexpr PHYSFSX_counted_list_template() = default;
70 constexpr PHYSFSX_counted_list_template(base_type &&base, std::size_t count) :
71 base_type{std::move(base)}, count{count}
72 {
73 }
74 std::size_t get_count() const
75 {
76 return count;
77 }
78 };
79
80 typedef PHYSFSX_uncounted_list_template<PHYSFS_list_deleter> PHYSFSX_uncounted_list;
81 typedef PHYSFSX_counted_list_template<PHYSFS_list_deleter> PHYSFSX_counted_list;
82
83 [[nodiscard]]
84 dxx_compiler_attribute_nonnull()
85 PHYSFSX_uncounted_list PHYSFSX_findFiles(const char *path, std::ranges::subrange<const file_extension_t *> exts);
86
87 [[nodiscard]]
88 dxx_compiler_attribute_nonnull()
89 PHYSFSX_uncounted_list PHYSFSX_findabsoluteFiles(const char *path, const char *realpath, std::ranges::subrange<const file_extension_t *> exts);
90
91 }
92 #endif