简体   繁体   English

覆盖 istream get(char)

[英]Overriding istream get(char)

I'm implementing a few very specific I/O streams, one of them, an input stream that has data available only sometimes (think about some kind of socket that you poll for data while it is open).我正在实现一些非常具体的 I/O 流,其中之一是输入 stream,它有时只有可用数据(想想在打开时轮询数据的某种套接字)。 My streams are based on a class that inherits from basic_iostream and internally use a buffer that inherits from basic_filebuf.我的流基于从 basic_iostream 继承的 class,并在内部使用从 basic_filebuf 继承的缓冲区。 So my filebuf has a special way of signalling that there's no data available when underflow is called.所以我的 filebuf 有一种特殊的方式来表示在调用下溢时没有可用数据。 I thought it's a good idea, because technically it's not EOF, unless the channel really gets closed.我认为这是个好主意,因为从技术上讲它不是 EOF,除非通道真的关闭了。 This status basically says - "there's no data now, but the channel is open, so you can keep asking, it might arrive eventually".这个状态基本上是说——“现在没有数据,但通道是开放的,所以你可以继续问,它最终可能会到达”。

The problem is traits_type::eof() is checked inside get(char) function which does not exactly look like it was supposed to be overriden (maybe I'm wrong).问题是在get(char) function 内部检查了traits_type::eof() ,它看起来并不像应该被覆盖(也许我错了)。

But anyway - what would be the best way of signalling such state?但无论如何 - 发送此类 state 的最佳方式是什么? If such state occurs, I would know about it in my buffer's underflow method...如果这样的 state 发生,我会在我的缓冲区的下溢方法中知道它......

For this propose you can use a user defined char_traits , and put it as the template parameter to your basic_filebuf ie something like:对于这个建议,您可以使用用户定义的 char_traits ,并将其作为模板参数添加到您的basic_filebuf中,例如:

#include <fstream>
#include <limits>

struct my_char_traits : public std::char_traits<char> 
{
  typedef std::char_traits<char>::int_type int_type;
  static constexpr int_type eof() noexcept 
  {
    return std::numeric_limits<int_type>::min();
  }
};

class my_buff: std::basic_filebuf<char, my_char_traits>
{
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM