可搜索输入过滤器定义
seekable input filter definition
使用 boost,我需要一个可搜索的输入过滤器,它将对从 ifstream 读取的字节进行操作。这是我目前拥有的:
struct my_filter : boost::iostreams::seekable_filter
{
template<typename Source>
int get(Source& src)
{
int byte = boost::iostreams::get(src);
if(byte != EOF && byte != boost::iostreams::WOULD_BLOCK)
{
// Do something with byte
}
return byte;
}
template<typename Sink>
bool put(Sink&, char)
{
// No need to actually implement put because this filter is only used with ifstream
return true;
}
template<typename T>
std::streampos seek(T& t, boost::iostreams::stream_offset off, std::ios_base::seekdir way, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
{
return boost::iostreams::seek(t, off, way, which);
}
};
它有效,但我想摆脱无用的 put 方法。这里的提升文档 http://www.boost.org/doc/libs/1_60_0/libs/iostreams/doc/concepts/filter.html 说 Filter 有一个 InputSeekableFilter 改进,但我似乎无法理解如何使用它(没有我可以使用的 input_seekable_filter_tag 或 input_seekable_filter 结构) .
llonesmiz 在 boost-users 上的回答:
"boost::iostreams::seekable_filter" seems to just be a typedef for
"boost::iostreams::filter<boost::iostreams::seekable
>". I don't know
for sure, but I think it would make sense that deriving your "struct
my_filter" from
"boost::iostreams::filter<boost::iostreams::input_seekable
>" would
accomplish what you want.
使用 boost,我需要一个可搜索的输入过滤器,它将对从 ifstream 读取的字节进行操作。这是我目前拥有的:
struct my_filter : boost::iostreams::seekable_filter
{
template<typename Source>
int get(Source& src)
{
int byte = boost::iostreams::get(src);
if(byte != EOF && byte != boost::iostreams::WOULD_BLOCK)
{
// Do something with byte
}
return byte;
}
template<typename Sink>
bool put(Sink&, char)
{
// No need to actually implement put because this filter is only used with ifstream
return true;
}
template<typename T>
std::streampos seek(T& t, boost::iostreams::stream_offset off, std::ios_base::seekdir way, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
{
return boost::iostreams::seek(t, off, way, which);
}
};
它有效,但我想摆脱无用的 put 方法。这里的提升文档 http://www.boost.org/doc/libs/1_60_0/libs/iostreams/doc/concepts/filter.html 说 Filter 有一个 InputSeekableFilter 改进,但我似乎无法理解如何使用它(没有我可以使用的 input_seekable_filter_tag 或 input_seekable_filter 结构) .
llonesmiz 在 boost-users 上的回答:
"boost::iostreams::seekable_filter" seems to just be a typedef for "boost::iostreams::filter
<boost::iostreams::seekable
>". I don't know for sure, but I think it would make sense that deriving your "struct my_filter" from "boost::iostreams::filter<boost::iostreams::input_seekable
>" would accomplish what you want.