Debian Codegraph @

libstdc++-v3/testsuite/20_util/smartptr.adapt/out_ptr/1.cc

raw
1 // { dg-do run { target c++23 } }
2 // { dg-require-fileio "" }
3 // { dg-additional-files "thirty_years_among_the_dead_preproc.txt" }
4
5 #include <memory>
6 #include <cstdio>
7 #include <testsuite_hooks.h>
8
9 // C++23 [out.ptr.t] Class template out_ptr_t
10
11 int fopen_s(std::FILE** f, const char* name, const char* mode);
12
13 struct fclose_deleter {
14 void operator()(std::FILE* f) const noexcept {
15 std::fclose(f);
16 }
17 };
18
19 // Example 1 from [out.ptr.t]
20 void
21 test_example_1()
22 {
23 constexpr const char* file_name = "thirty_years_among_the_dead_preproc.txt";
24 std::unique_ptr<std::FILE, fclose_deleter> file_ptr;
25 int err = fopen_s(std::out_ptr<std::FILE*>(file_ptr), file_name, "r+b");
26 if (err != 0)
27 VERIFY(false);
28 // *file_ptr is valid
29 VERIFY(file_ptr != nullptr);
30 }
31
32 // Same again without explicit template argument list.
33 void
34 test_example_1_2()
35 {
36 constexpr const char* file_name = "thirty_years_among_the_dead_preproc.txt";
37 std::unique_ptr<std::FILE, fclose_deleter> file_ptr;
38 int err = fopen_s(std::out_ptr(file_ptr), file_name, "r+b");
39 if (err != 0)
40 VERIFY(false);
41 // *file_ptr is valid
42 VERIFY(file_ptr != nullptr);
43 }
44
45 // And again with a deleter argument.
46 void
47 test_example_1_3()
48 {
49 constexpr const char* file_name = "thirty_years_among_the_dead_preproc.txt";
50 std::unique_ptr<std::FILE, fclose_deleter> file_ptr;
51 int err = fopen_s(std::out_ptr(file_ptr, fclose_deleter()), file_name, "r+b");
52 if (err != 0)
53 VERIFY(false);
54 // *file_ptr is valid
55 VERIFY(file_ptr != nullptr);
56 }
57
58 // Same again using std::shared_ptr
59 void
60 test_example_1_sp()
61 {
62 constexpr const char* file_name = "thirty_years_among_the_dead_preproc.txt";
63 std::shared_ptr<std::FILE> file_ptr;
64 int err = fopen_s(std::out_ptr<std::FILE*>(file_ptr, fclose_deleter()),
65 file_name, "r+b");
66 if (err != 0)
67 VERIFY(false);
68 // *file_ptr is valid
69 VERIFY(file_ptr != nullptr);
70 }
71
72 // And again without explicit template argument list.
73 void
74 test_example_1_sp_2()
75 {
76 constexpr const char* file_name = "thirty_years_among_the_dead_preproc.txt";
77 std::shared_ptr<std::FILE> file_ptr;
78 int err = fopen_s(std::out_ptr(file_ptr, fclose_deleter()), file_name, "r+b");
79 if (err != 0)
80 VERIFY(false);
81 // *file_ptr is valid
82 VERIFY(file_ptr != nullptr);
83 }
84
85 // And again using a raw pointer.
86 void
87 test_example_1_raw()
88 {
89 constexpr const char* file_name = "thirty_years_among_the_dead_preproc.txt";
90 std::FILE* file_ptr;
91 int err = fopen_s(std::out_ptr(file_ptr), file_name, "r+b");
92 if (err != 0)
93 VERIFY(false);
94 // *file_ptr is valid
95 VERIFY(file_ptr != nullptr);
96 std::fclose(file_ptr);
97 }
98
99 int main()
100 {
101 test_example_1();
102 test_example_1_2();
103 test_example_1_3();
104 test_example_1_sp();
105 test_example_1_sp_2();
106 test_example_1_raw();
107 }
108
109 #include <cerrno>
110
111 int fopen_s(std::FILE** f, const char* name, const char* mode)
112 {
113 if ((*f = std::fopen(name, mode)))
114 return 0;
115 return errno;
116 }