1 #ifndef GINTENLIB_INCLUDED_LIST_FORMAT_HPP_
2 #define GINTENLIB_INCLUDED_LIST_FORMAT_HPP_
6 <gintenlib/list_format.hpp>
8 list_format : コンテナのお手軽ストリーム出力
11 template< typename Range >
12 unspecified list_format( const Range& src );
14 template< typename InIte >
15 unspecified list_format( const InIte& begin, const InIte& end );
17 template< typename Range, typename Pre, typename Delim, typename Post >
18 unspecified list_format( const Range& src, const Pre& pre, const Delim& delim, const Post& post );
20 template< typename InIte, typename Pre, typename Delim, typename Post >
21 unspecified list_format( const InIte& begin, const InIte& end, const Pre& pre, const Delim& delim, const Post& post );
24 任意のコンテナや配列( boost 使用版では任意の range )を、ストリームに流せるよう加工します。
25 デフォルトでは ( 1, 2, 3, 4 ) のように出力されますが、引数を指定することでスタイルは変更できます。
28 // v は任意のコンテナ。( 1, 2, 3 ) のように表示されます
29 cout << gintenlib::list_format(v) << endl;
30 // 引数を指定すれば、[ 1 | 2 | 3 ] のような表示にもできます
31 cout << gintenlib::list_format( v, "[ ", " | ", " ]" ) << endl;
32 // また、イテレータを使って出力対象を指定することも出来ます。
34 cout << gintenlib::list_format( v.rbegin(), v.rend() ) << endl;
37 ・基本的に list_format 関数は、「戻り値をストリームに流す」という使い方のみを想定しています。
38 ・ただし、list_format 関数の戻り値に boost::lexical_cast を適応することは可能です。
39 ・また、戻り値を gintenlib::output_element オブジェクトに格納することも出来ます。
40 ・上記以外の目的で使った場合、list_format 関数の動作は不定となります。
44 // タイプ数削減のための using 宣言
45 #include "call_traits.hpp"
47 #include <boost/range.hpp>
52 template< typename InIte, typename Pre = const char*,
53 typename Delim = const char*, typename Post = const char* >
54 struct list_format_impl_
56 typedef typename call_traits<InIte>::param_type inite_param_t;
57 typedef typename call_traits<Pre>::param_type pre_param_t;
58 typedef typename call_traits<Delim>::param_type delim_param_t;
59 typedef typename call_traits<Post>::param_type post_param_t;
62 list_format_impl_( inite_param_t begin, inite_param_t end,
63 pre_param_t pre, delim_param_t delim, post_param_t post )
64 : begin_(begin), end_(end), pre_(pre), delim_(delim), post_(post) {}
66 typename call_traits<InIte>::value_type begin_, end_;
67 typename call_traits<Pre>::value_type pre_;
68 typename call_traits<Delim>::value_type delim_;
69 typename call_traits<Post>::value_type post_;
72 // テンプレートにより std::ostream に限らず出力できます
73 template<typename Stream>
74 friend Stream& operator<< ( Stream& os, const list_format_impl_& src )
76 // 空のリストを表示させようとした場合、何も表示されない
77 if( src.begin_ == src.end_ ){ return os; }
82 InIte ite = src.begin_;
84 for( ++ite; ite != src.end_; ++ite )
86 os << src.delim_ << *ite;
93 } // operator<< ( Stream&, const list_format_impl_& )
95 }; // struct list_format_impl_
102 // 一般形。任意の pre, delim, post に関して
103 // os << pre << 要素1 << delim << 要素2 << ... << delim << 要素n << end;
105 template< typename Range, typename Pre, typename Delim, typename Post >
106 inline const list_format_impl_< typename boost::range_iterator<const Range>::type, Pre, Delim, Post >
108 ( const Range& src, const Pre& pre, const Delim& delim, const Post& post )
110 using namespace boost;
112 return list_format_impl_< typename range_iterator<const Range>::type, Pre, Delim, Post >
113 ( begin(src), end(src), pre, delim, post );
116 // 省略版。( 要素1, 要素2, ... , 要素n ) という感じに出力
117 template< typename Range >
118 inline const list_format_impl_< typename boost::range_iterator<const Range>::type >
119 list_format( const Range& src )
121 using namespace boost;
123 return list_format_impl_< typename range_iterator<const Range>::type >
124 ( begin(src), end(src), "( ", ", ", " )" );
131 template< typename InIte, typename Pre, typename Delim, typename Post >
132 inline const list_format_impl_<InIte, Pre, Delim, Post>
134 ( const InIte& begin, const InIte& end, const Pre& pre, const Delim& delim, const Post& post )
136 return list_format_impl_<InIte, Pre, Delim, Post>( begin, end, pre, delim, post );
140 template< typename InIte >
141 inline const list_format_impl_<InIte>
142 list_format( const InIte& begin, const InIte& end )
144 return list_format_impl_<InIte>( begin, end, "( ", ", ", " )" );
147 } // namespace gintenlib
150 #endif // #ifndef GINTENLIB_INCLUDED_LIST_FORMAT_HPP_