Skip to content

Commit f462880

Browse files
committed
fstream : C++26対応としてnative_handleを追加 (close #1196)
1 parent cdaf0f7 commit f462880

File tree

8 files changed

+372
-11
lines changed

8 files changed

+372
-11
lines changed

reference/fstream/basic_filebuf.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ Cの`FILE*`に対する入出力関数を使って実装される。
3030
| `wfilebuf` | `wchar_t`型。ワイド文字列として使用する。 | |
3131
3232
33-
### 型
34-
35-
| 名前 | 説明 | 対応バージョン |
36-
|---------------|----------------------------|----------------|
37-
| `char_type` | テンプレート仮引数`CharT` | |
38-
| `int_type` | `Traits::int_type` | |
39-
| `pos_type` | `Traits::pos_type` | |
40-
| `off_type` | `Traits::off_type` | |
41-
| `traits_type` | テンプレート仮引数`Traits` | |
42-
4333
### 構築・破棄
4434
4535
| 名前 | 説明 | 対応バージョン |
@@ -59,6 +49,13 @@ Cの`FILE*`に対する入出力関数を使って実装される。
5949
| [`open`](basic_filebuf/open.md) | ファイルを開く | |
6050
| [`close`](basic_filebuf/close.md) | ファイルを閉じる | |
6151
52+
### 環境固有の情報
53+
54+
| 名前 | 説明|対応バージョン |
55+
|-----|-----|-------------|
56+
| [`native_handle()`](basic_filebuf/native_handle.md) | ネイティブハンドルを取得する[処理系定義] | C++26 |
57+
58+
6259
### オーバーライドされている関数
6360
6461
すべて`protected`で定義されている。
@@ -76,5 +73,17 @@ Cの`FILE*`に対する入出力関数を使って実装される。
7673
| `pbackfail` | 1文字を入力列に戻す (protected virtual) | |
7774
| `overflow` | (protected virtual) |
7875
76+
## メンバ型
77+
78+
| 名前 | 説明 | 対応バージョン |
79+
|---------------|----------------------------|----------------|
80+
| `char_type` | テンプレート仮引数`CharT` | |
81+
| `int_type` | `Traits::int_type` | |
82+
| `pos_type` | `Traits::pos_type` | |
83+
| `off_type` | `Traits::off_type` | |
84+
| `traits_type` | テンプレート仮引数`Traits` | |
85+
| `native_handle_type` | ネイティブハンドルの型 [処理系定義] | C++26 |
86+
87+
7988
## 参照
8089
- [`basic_streambuf`](../streambuf/basic_streambuf.md)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# native_handle
2+
* fstream[meta header]
3+
* std[meta namespace]
4+
* basic_filebuf[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
native_handle_type native_handle() const noexcept;
10+
```
11+
12+
## 概要
13+
処理系定義のネイティブハンドルを取得する。
14+
15+
16+
## 事前条件
17+
- [`is_open()`](is_open.md)`true`を返すこと
18+
19+
20+
## 戻り値
21+
`*this`が指すファイルに対するネイティブハンドルを返す。
22+
23+
24+
## 備考
25+
- ネイティブハンドルは、ファイルが閉じられると無効になる
26+
27+
28+
##
29+
```cpp example
30+
// POSIX環境
31+
#include <fstream>
32+
#include <print>
33+
#include <chrono>
34+
35+
#include <sys/types.h>
36+
#include <sys/stat.h>
37+
38+
int main() {
39+
std::fstream fs("example.txt", std::ios_base::in);
40+
std::filebuf* buf = fs.rdbuf();
41+
42+
if (!buf->is_open()) {
43+
std::println("ファイルを開けなかった");
44+
return 1;
45+
}
46+
47+
// ネイティブハンドルから、POSIXのAPIでファイルの最終更新日時を取得する
48+
int fd = buf->native_handle();
49+
struct ::stat s{};
50+
::fstat(fd, &s);
51+
52+
std::chrono::sys_seconds last_modified {std::chrono::seconds(s.st_mtim.tv_sec)};
53+
std::chrono::zoned_time zoned{"Asia/Tokyo", last_modified};
54+
std::println("Last modified: {}", zoned);
55+
}
56+
```
57+
* native_handle[color ff0000]
58+
* std::fstream[link /reference/fstream/basic_fstream.md]
59+
* fs.rdbuf()[link /reference/fstream/basic_fstream/rdbuf.md]
60+
* buf->isopen()[link is_open.md]
61+
* std::chrono::sys_seconds[link /reference/chrono/sys_time.md]
62+
* std::chrono::zoned_time[link /reference/chrono/zoned_time.md]
63+
64+
### 出力例
65+
```
66+
Last modified: 2025-05-28 17:56:55 JST
67+
```
68+
69+
## バージョン
70+
### 言語
71+
- C++26
72+
73+
### 処理系
74+
- [Clang](/implementation.md#clang): 18 [mark verified]
75+
- [GCC](/implementation.md#gcc): 14 [mark verified]
76+
- [Visual C++](/implementation.md#visual_cpp): ??
77+
78+
## 参照
79+
- [P1759R6 Native handles and file streams](http://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p1759r6.html)

reference/fstream/basic_fstream.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ namespace std {
3232
| [`close`](basic_fstream/close.md) | ファイルを閉じる | |
3333
3434
35+
### ネイティブハンドルの取得
36+
37+
| 名前 | 説明|対応バージョン |
38+
|-----|-----|-------------|
39+
| [`native_handle()`](basic_fstream/native_handle.md) | ネイティブハンドルを取得する[処理系定義] | C++26 |
40+
41+
3542
## 非メンバ関数
3643
3744
| 名前 | 説明 | 対応バージョン |
@@ -48,6 +55,8 @@ namespace std {
4855
| `pos_type` | `Traits::pos_type` | |
4956
| `off_type` | `Traits::off_type` | |
5057
| `traits_type` | テンプレート仮引数`Traits` | |
58+
| `native_handle_type` | 処理系定義のネイティブハンドルの型`typename basic_filebuf<CharT, Traits>::native_handle_type` | C++26 |
59+
5160
5261
## 例
5362
```cpp example
@@ -83,4 +92,4 @@ int main() {
8392
}
8493
return 0;
8594
}
86-
```
95+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# native_handle
2+
* fstream[meta header]
3+
* std[meta namespace]
4+
* basic_fstream[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
native_handle_type native_handle() const noexcept;
10+
```
11+
12+
## 概要
13+
処理系定義のネイティブハンドルを取得する。
14+
15+
16+
## 事前条件
17+
- [`is_open()`](is_open.md)`true`を返すこと
18+
19+
20+
## 戻り値
21+
以下と等価:
22+
23+
```cpp
24+
return rdbuf()->native_handle();
25+
```
26+
* rdbuf()[link rdbuf.md]
27+
* native_handle()[link /reference/fstream/basic_filebuf/native_handle.md]
28+
29+
30+
## 備考
31+
- ネイティブハンドルは、ファイルが閉じられると無効になる
32+
33+
34+
##
35+
```cpp example
36+
// POSIX環境
37+
#include <fstream>
38+
#include <print>
39+
#include <chrono>
40+
41+
#include <sys/types.h>
42+
#include <sys/stat.h>
43+
44+
int main() {
45+
std::fstream fs("example.txt", std::ios_base::in);
46+
47+
if (!fs.is_open()) {
48+
std::println("ファイルを開けなかった");
49+
return 1;
50+
}
51+
52+
// ネイティブハンドルから、POSIXのAPIでファイルの最終更新日時を取得する
53+
int fd = fs.native_handle();
54+
struct ::stat s{};
55+
::fstat(fd, &s);
56+
57+
std::chrono::sys_seconds last_modified {std::chrono::seconds(s.st_mtim.tv_sec)};
58+
std::chrono::zoned_time zoned{"Asia/Tokyo", last_modified};
59+
std::println("Last modified: {}", zoned);
60+
}
61+
```
62+
* native_handle[color ff0000]
63+
* fs.is_open()[link is_open.md]
64+
* std::chrono::sys_seconds[link /reference/chrono/sys_time.md]
65+
* std::chrono::zoned_time[link /reference/chrono/zoned_time.md]
66+
67+
### 出力例
68+
```
69+
Last modified: 2025-05-28 17:56:55 JST
70+
```
71+
72+
## バージョン
73+
### 言語
74+
- C++26
75+
76+
### 処理系
77+
- [Clang](/implementation.md#clang): 18 [mark verified]
78+
- [GCC](/implementation.md#gcc): 14 [mark verified]
79+
- [Visual C++](/implementation.md#visual_cpp): ??
80+
81+
## 参照
82+
- [P1759R6 Native handles and file streams](http://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p1759r6.html)

reference/fstream/basic_ifstream.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ namespace std {
3333
| [`close`](basic_ifstream/close.md) | ファイルを閉じる | |
3434
3535
36+
### ネイティブハンドルの取得
37+
38+
| 名前 | 説明|対応バージョン |
39+
|-----|-----|-------------|
40+
| [`native_handle()`](basic_ifstream/native_handle.md) | ネイティブハンドルを取得する[処理系定義] | C++26 |
41+
42+
3643
## 非メンバ関数
3744
3845
| 名前 | 説明 | 対応バージョン |
@@ -49,6 +56,8 @@ namespace std {
4956
| `pos_type` | `Traits::pos_type` | |
5057
| `off_type` | `Traits::off_type` | |
5158
| `traits_type` | テンプレート仮引数`Traits` | |
59+
| `native_handle_type` | 処理系定義のネイティブハンドルの型`typename basic_filebuf<CharT, Traits>::native_handle_type` | C++26 |
60+
5261
5362
## 例
5463
```cpp example
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# native_handle
2+
* fstream[meta header]
3+
* std[meta namespace]
4+
* basic_ifstream[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
native_handle_type native_handle() const noexcept;
10+
```
11+
12+
## 概要
13+
処理系定義のネイティブハンドルを取得する。
14+
15+
16+
## 事前条件
17+
- [`is_open()`](is_open.md)`true`を返すこと
18+
19+
20+
## 戻り値
21+
以下と等価:
22+
23+
```cpp
24+
return rdbuf()->native_handle();
25+
```
26+
* rdbuf()[link rdbuf.md]
27+
* native_handle()[link /reference/fstream/basic_filebuf/native_handle.md]
28+
29+
30+
## 備考
31+
- ネイティブハンドルは、ファイルが閉じられると無効になる
32+
33+
34+
##
35+
```cpp example
36+
// POSIX環境
37+
#include <fstream>
38+
#include <print>
39+
#include <chrono>
40+
41+
#include <sys/types.h>
42+
#include <sys/stat.h>
43+
44+
int main() {
45+
std::ifstream ifs("example.txt");
46+
47+
if (!ifs.is_open()) {
48+
std::println("ファイルを開けなかった");
49+
return 1;
50+
}
51+
52+
// ネイティブハンドルから、POSIXのAPIでファイルの最終更新日時を取得する
53+
int fd = ifs.native_handle();
54+
struct ::stat s{};
55+
::fstat(fd, &s);
56+
57+
std::chrono::sys_seconds last_modified {std::chrono::seconds(s.st_mtim.tv_sec)};
58+
std::chrono::zoned_time zoned{"Asia/Tokyo", last_modified};
59+
std::println("Last modified: {}", zoned);
60+
}
61+
```
62+
* native_handle[color ff0000]
63+
* ifs.is_open()[link is_open.md]
64+
* std::chrono::sys_seconds[link /reference/chrono/sys_time.md]
65+
* std::chrono::zoned_time[link /reference/chrono/zoned_time.md]
66+
67+
### 出力例
68+
```
69+
Last modified: 2025-05-28 17:56:55 JST
70+
```
71+
72+
## バージョン
73+
### 言語
74+
- C++26
75+
76+
### 処理系
77+
- [Clang](/implementation.md#clang): 18 [mark verified]
78+
- [GCC](/implementation.md#gcc): 14 [mark verified]
79+
- [Visual C++](/implementation.md#visual_cpp): ??
80+
81+
## 参照
82+
- [P1759R6 Native handles and file streams](http://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p1759r6.html)

reference/fstream/basic_ofstream.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ namespace std {
3333
| [`close`](basic_ofstream/close.md) | ファイルを閉じる | |
3434
3535
36+
### ネイティブハンドルの取得
37+
38+
| 名前 | 説明|対応バージョン |
39+
|-----|-----|-------------|
40+
| [`native_handle()`](basic_ofstream/native_handle.md) | ネイティブハンドルを取得する[処理系定義] | C++26 |
41+
42+
3643
## 非メンバ関数
3744
3845
| 名前 | 説明 | 対応バージョン |
@@ -49,6 +56,8 @@ namespace std {
4956
| `pos_type` | `Traits::pos_type` | |
5057
| `off_type` | `Traits::off_type` | |
5158
| `traits_type` | テンプレート仮引数`Traits` | |
59+
| `native_handle_type` | 処理系定義のネイティブハンドルの型`typename basic_filebuf<CharT, Traits>::native_handle_type` | C++26 |
60+
5261
5362
## 例
5463
```cpp example

0 commit comments

Comments
 (0)