c – 继承std :: istream或等价物

c – 继承std :: istream或等价物,第1张

概述我需要通过一个流来连接两个库. QDataStream which is a stream from Qt 和另一个类似这样的库的一些功能 void read_something(istream& i); 我无法控制如何创建QDataStream,并且我不允许更改read_somthing函数的接口. 我可以想到的第一件事是写一个继承istream并包装QDataStream的类.有人做过吗? 如 我需要通过一个流来连接两个库.
QDataStream which is a stream from Qt

和另一个类似这样的库的一些功能

voID read_something(istream& i);

我无法控制如何创建QDataStream,并且我不允许更改read_somthing函数的接口.

我可以想到的第一件事是写一个继承istream并包装QDataStream的类.有人做过吗?

如果我认为不是正确的方法,我想知道什么是最好的方式来实现.

解决方法 你应该做的是写一个streambuf,它使用QDataStream readBytes和writeBytes来实现它的功能.然后使用rdbuf将streambuf注册到一个istream中(您也可以在初始化时编写一个istream descendant).

Boost包含一个旨在促进streambuf写作的图书馆.使用它可能比理解streambuf界面更简单(个人我从来没有使用过,但我已经写了多个streambuf;我会看看我有一个例子,我可以发布).

编辑:这里是一些(用法语评论 – 来自法国的fr.comp.lang.c的常见问题解答,我没有时间翻译,认为最好离开他们,而不是删除它们)包装文件*调用streambuf.这也是使用私有继承的示例:确保在基类之前初始化什么可以是成员.在IOStream的情况下,基类也可以接收一个NulL指针,然后使用成员init()来设置streambuf.

#include <stdio.h>#include <assert.h>#include <iostream>#include <streambuf>// streambuf minimal encapsulant un file*//    - utilise les tampons de file donc n'a pas de tampon interne en//      sortIE et a un tampon interne de taille 1 en entree car l'interface//      de streambuf ne permet pas de faire moins;//    - ne permet pas la mise en place d'un tampon//    - une version plus complete devrait permettre d'acceder aux//      informations d'erreur plus precises de file* et interfacer aussi//      les autres possibilites de file* (entre autres synchroniser les//      sungetc/sputbackc avec la possibilite correspondante de file*)class filebuf: public std::streambuf{public:  explicit filebuf(file* cstream);  // cstream doit etre non NulL.protected:  std::streambuf* setbuf(char_type* s,std::streamsize n);  int_type overflow(int_type c);  int      sync();  int_type underflow();private:  file*    cstream_;  char     inputBuffer_[1];};filebuf::filebuf(file* cstream)  : cstream_(cstream){  // le constructeur de streambuf equivaut a  // setp(NulL,NulL);  // setg(NulL,NulL,NulL);  assert(cstream != NulL);}std::streambuf* filebuf::setbuf(char_type* s,std::streamsize n){  // ne fait rIEn,ce qui est autorise.  Une version plus complete  // devrait vraissemblablement utiliser setvbuf  return NulL;}filebuf::int_type filebuf::overflow(int_type c){  if (traits_type::eq_int_type(c,traits_type::eof())) {    // la norme ne le demande pas exactement,mais si on nous passe eof    // la coutume est de faire la meme chose que sync()    return (sync() == 0        ? traits_type::not_eof(c)        : traits_type::eof());  } else {    return ((fputc(c,cstream_) != EOF)        ? traits_type::not_eof(c)        : traits_type::eof());  }}int filebuf::sync(){  return (fflush(cstream_) == 0      ? 0      : -1);}filebuf::int_type filebuf::underflow(){  // Assurance contre des implementations pas strictement conformes a la  // norme qui guaranti que le test est vrai.  Cette guarantIE n'existait  // pas dans les IOStream classiques.  if (gptr() == NulL || gptr() >= egptr()) {    int gotted = fgetc(cstream_);    if (gotted == EOF) {      return traits_type::eof();    } else {      *inputBuffer_ = gotted;      setg(inputBuffer_,inputBuffer_,inputBuffer_+1);      return traits_type::to_int_type(*inputBuffer_);    }  } else {    return traits_type::to_int_type(*inputBuffer_);  }}// ostream minimal facilitant l'utilisation d'un filebuf// herite de manIEre privee de filebuf,ce qui permet de s'assurer// qu'il est bIEn initialise avant std::ostreamclass ofilestream: private filebuf,public std::ostream {public:  explicit ofilestream(file* cstream);};ofilestream::ofilestream(file* cstream)  : filebuf(cstream),std::ostream(this){}// istream minimal facilitant l'utilisation d'un filebuf// herite de manIEre privee de filebuf,ce qui permet de s'assurer// qu'il est bIEn initialise avant std::istreamclass ifilestream: private filebuf,public std::istream{public:  explicit ifilestream(file* cstream);};ifilestream::ifilestream(file* cstream)  : filebuf(cstream),std::istream(this){}// petit programme de test#include <assert.h>int main(int argc,char* argv[]){  file* ocstream = fopen("result","w");  assert (ocstream != NulL);  ofilestream ocppstream(ocstream);  ocppstream << "Du texte";  fprintf(ocstream," melange");  fclose(ocstream);  file* icstream = fopen("result","r");  assert (icstream != NulL);  ifilestream icppstream(icstream);  std::string word1;  std::string word2;  icppstream >> word1;  icppstream >> word2;  char buf[1024];  fgets(buf,1024,icstream);  std::cout << "Got :" << word1 << ':' << word2 << ':' << buf << '\n';}
总结

以上是内存溢出为你收集整理的c – 继承std :: istream或等价物全部内容,希望文章能够帮你解决c – 继承std :: istream或等价物所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1253698.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存