Skip to content

Commit

Permalink
range_formatter : メンバ関数を追加 #1085
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Jan 25, 2023
1 parent 373f49d commit 74662b4
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 10 deletions.
2 changes: 1 addition & 1 deletion reference/format/format.md
Expand Up @@ -172,7 +172,7 @@ Range・シーケンスコンテナに対して使用できる標準のオプシ
- 要素型の書式を指定して、デバッグ出力指定をしなければ、デフォルトで有効になっているデバッグ出力を解除できる


#### 連想コンテナの書式 (C++23)
#### <a id="assoc-format-options" href="#assoc-format-options">連想コンテナの書式 (C++23)</a>

Range・シーケンスコンテナと同じだが、デフォルトで`m`書式を適用する。

Expand Down
2 changes: 1 addition & 1 deletion reference/format/formatter/set_separator.md
Expand Up @@ -16,7 +16,7 @@ constexpr void
このメンバ関数は、[`std::format()`](/reference/format/format.md)関数の「[pair、tupleの書式](/reference/format/format.md#tuple-format-options)」において区切り文字を変更するオプションを指定することで自動的に呼び出される。
デフォルトの開きカッコは`", "`である。
デフォルトの区切り文字は`", "`である。
## 効果
Expand Down
19 changes: 11 additions & 8 deletions reference/format/range_formatter.md
Expand Up @@ -19,18 +19,18 @@ namespace std {
ユーザー定義のコンテナ・RangeをRange書式に対応する場合は、以下のようにする:
- オリジナル書式を定義しないのであれば、このクラスではなく、[`format_kind`](format_kind.md)を特殊化する
- オリジナル書式を定義するのであれば、このクラスおよび[`format_kind`](format_kind.md)を特殊化して[`parse()`](range_formatter/parse.md.nolink)メンバ関数と[`format()`](range_formatter/format.md.nolink)メンバ関数を実装する
- オリジナル書式を定義するのであれば、このクラスおよび[`format_kind`](format_kind.md)を特殊化して[`parse()`](range_formatter/parse.md)メンバ関数と[`format()`](range_formatter/format.md)メンバ関数を実装する
## メンバ関数
| メンバ関数 | 説明 | 対応バージョン |
|------------|------|----------------|
| [`set_separator`](range_formatter/set_separator.md.nolink) | 要素の区切り文字を設定する | C++23 |
| [`set_brackets`](range_formatter/set_brackets.md.nolink) | 全体の囲み文字を設定する | C++23 |
| [`underlying`](range_formatter/underlying.md.nolink) | 要素型の`formatter`を取得する | C++23 |
| [`parse`](range_formatter/parse.md.nolink) | 書式の解析を行う | C++23 |
| [`format`](range_formatter/format.md.nolink) | 書式化を行う | C++23 |
| [`set_separator`](range_formatter/set_separator.md) | 要素の区切り文字を設定する | C++23 |
| [`set_brackets`](range_formatter/set_brackets.md) | 全体の囲み文字を設定する | C++23 |
| [`underlying`](range_formatter/underlying.md) | 要素型の`formatter`を取得する | C++23 |
| [`parse`](range_formatter/parse.md) | 書式の解析を行う | C++23 |
| [`format`](range_formatter/format.md) | 書式化を行う | C++23 |
## 例
Expand Down Expand Up @@ -64,6 +64,9 @@ public:
const std::vector<T>& base() const { return v_; }
};
template <class T>
constexpr std::range_format std::format_kind<MyVector<T>> = std::range_format::sequence;
template <class T>
class std::range_formatter<MyVector<T>> : public std::range_formatter<std::vector<T>> {
bool is_colon = false;
Expand All @@ -87,7 +90,7 @@ public:
// format()関数は書式の情報をもたない。
// parse()関数で解析した書式をメンバ変数で保持しておいて、
// それをもとに書式化する
auto format(const MyVector<T>& v, std::format_context& fctx) const {
auto format(MyVector<T>&& v, std::format_context& fctx) const {
if (is_colon) {
auto out = fctx.out();
bool is_first = true;
Expand Down Expand Up @@ -124,7 +127,7 @@ int main()
* fctx.advance_to[link basic_format_context/advance_to.md]
* std::format_to[link format_to.md]
* std::format[link format.md]
* underlying()[link range_formatter/underlying.md.nolink]
* underlying()[link range_formatter/underlying.md]


#### 出力
Expand Down
47 changes: 47 additions & 0 deletions reference/format/range_formatter/format.md
@@ -0,0 +1,47 @@
# format
* format[meta header]
* function[meta id-type]
* std[meta namespace]
* range_formatter[meta class]
* cpp23[meta cpp]

```cpp
template <ranges::input_range R, class FormatContext>
requires formattable<ranges::range_reference_t<R>, charT> &&
same_as<remove_cvref_t<ranges::range_reference_t<R>>, T>
typename FormatContext::iterator
format(R&& r, FormatContext& ctx) const; // (1)
```
* FormatContext[link /reference/format/basic_format_context.md]
* ranges::input_range[link /reference/ranges/input_range.md]
* ranges::range_reference_t[link /reference/ranges/range_reference_t.md]
## 概要
受け取ったRangeオブジェクト`R`を書式化する。
## 効果
- [`parse()`](parse.md)メンバ関数で解析した書式文字列の指定に基づいて、`r`の値を文字列に変換し、以下を[`ctx.out()`](/reference/format/basic_format_context/out.md)に出力する:
- Range書式として[`s` (文字列として出力)](/reference/format/format.md#range-format-options)が指定された場合、[`basic_string`](/reference/string/basic_string.md)`<charT>(`[`from_range`](/reference/ranges/from_range_t.md.nolink)`, r)`を出力する
- そうでなく、Range書式として[`?s` (デバッグ文字列として出力)](/reference/format/format.md#range-format-options)が指定された場合、[`basic_string`](/reference/string/basic_string.md)`<charT>(`[`from_range`](/reference/ranges/from_range_t.md.nolink)`, r)`に引用符を付け、エスケープシーケンスをエスケープして出力する
- そうでなければ、
- 開きカッコを出力する
- Range `r`の各要素`e`について、
- [`underlying()`](underlying.md)の[`formatter`](/reference/format/formatter.md)で`e`を書式化して出力する
- 最後の要素でなければ、区切り文字を出力する
- 閉じカッコを出力する
## 戻り値
出力がおわった位置の出力イテレータを返す。
## バージョン
### 言語
- C++23
### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [Visual C++](/implementation.md#visual_cpp): ??
39 changes: 39 additions & 0 deletions reference/format/range_formatter/parse.md
@@ -0,0 +1,39 @@
# parse
* format[meta header]
* function[meta id-type]
* std[meta namespace]
* range_formatter[meta class]
* cpp23[meta cpp]

```cpp
template <class ParseContext>
constexpr typename ParseContext::iterator
parse(ParseContext& ctx);
```
* ParseContext[link /reference/format/basic_format_parse_context.md]
## 概要
書式文字列の範囲`[ctx.begin(), ctx.end())`を解析する。
## 効果
- [`std::format()`](/reference/format/format.md)関数ページに記載されている「[Range・シーケンスコンテナの書式](/reference/format/format.md#range-format-options)」および「[連想コンテナの書式](/reference/format/format.md#assoc-format-options)」を解析する
- このとき、書式によって以下を設定する
- `n`オプション (囲み文字をなくす) が指定されなければ、全体の囲み文字を、[`set_brackets()`](set_brackets.md)メンバ関数で設定する
- 要素の区切り文字を、[`set_separator()`](set_separator.md)メンバ関数で設定する
- 要素に対する書式が空でなければ、各要素の[`formatter`](/reference/format/formatter.md)を`e`として、[`e.set_debug_format()`](/reference/format/formatter/set_debug_format.md)が有効な式であれば、それを呼び出してデバッグ書式を有効にする
## 戻り値
`ctx`のうち、解析がおわった位置を指すイテレータを返す。
## バージョン
### 言語
- C++23
### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [Visual C++](/implementation.md#visual_cpp): ??
34 changes: 34 additions & 0 deletions reference/format/range_formatter/set_brackets.md
@@ -0,0 +1,34 @@
# set_brackets
* format[meta header]
* function[meta id-type]
* std[meta namespace]
* range_formatter[meta class]
* cpp23[meta cpp]

```cpp
constexpr void
set_brackets(basic_string_view<charT> opening,
basic_string_view<charT> closing); // (1)
```
## 概要
全体の囲み文字を設定する。
このメンバ関数は、[`std::format()`](/reference/format/format.md)関数の「[Range・シーケンスコンテナの書式](/reference/format/format.md#range-format-options)」および「[連想コンテナの書式](/reference/format/format.md#assoc-format-options)」において囲み文字を変更するオプションを指定することで自動的に呼び出される。
- Range・シーケンスコンテナのデフォルトの開きカッコは`"["`、閉じカッコは`"]"`
- 連想コンテナのデフォルトの開きカッコは`"{"`、閉じカッコは`"}"`
## 効果
`opening`を開きカッコ、`closing`を閉じカッコとして設定する。
## バージョン
### 言語
- C++23
### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [Visual C++](/implementation.md#visual_cpp): ??
32 changes: 32 additions & 0 deletions reference/format/range_formatter/set_separator.md
@@ -0,0 +1,32 @@
# set_separator
* format[meta header]
* function[meta id-type]
* std[meta namespace]
* range_formatter[meta class]
* cpp23[meta cpp]

```cpp
constexpr void
set_separator(basic_string_view<charT> sep); // (1)
```
## 概要
要素の区切り文字を設定する。
このメンバ関数は、[`std::format()`](/reference/format/format.md)関数の「[Range・シーケンスコンテナの書式](/reference/format/format.md#range-format-options)」および「[連想コンテナの書式](/reference/format/format.md#assoc-format-options)」において区切り文字を変更するオプションを指定することで自動的に呼び出される。
デフォルトの区切り文字は`", "`である。
## 効果
`sep`を区切り文字として設定する。
## バージョン
### 言語
- C++23
### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [Visual C++](/implementation.md#visual_cpp): ??
33 changes: 33 additions & 0 deletions reference/format/range_formatter/underlying.md
@@ -0,0 +1,33 @@
# underlying
* format[meta header]
* function[meta id-type]
* std[meta namespace]
* range_formatter[meta class]
* cpp23[meta cpp]

```cpp
constexpr formatter<T, charT>& underlying(); // (1) C++23
constexpr const formatter<T, charT>& underlying() const; // (2) C++23
```
* formatter[link /reference/format/formatter.md]

## 概要
Rangeの要素型に対する[`formatter`](/reference/format/formatter.md)を取得する。

- (1) : 非const版
- (2) : const版


## 戻り値
メンバ変数として保持している、Rangeの要素型に対する[`formatter`](/reference/format/formatter.md)を返す。


## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [Visual C++](/implementation.md#visual_cpp): ??

0 comments on commit 74662b4

Please sign in to comment.