Share std::fstream or std::stringstream trough std::iostream

只愿长相守 提交于 2019-12-06 09:44:17

You can pass around a pointer (raw or smart) to dynamically allocated instance of either std::fstream or std::stringstream typed as pointer to std::iostream. The client of the stream then just needs to do something like std::iostream & s = *iostreamPtr; s << "yay!"; to use it.

You could just make this into a templated function

template<class T>
void yourFunc(T& object) 
{
  if (typeid(T) == typeid(std::stringstream))
  {
    stringStream_.str(std::string());
    obj->Stream().rdbuf(stringStream.rdbuf());
  }
  else
  {
    boost::filesystem::path temp = boost::filesystem::unique_path();
    fileStream_.open(temp.native().c_str(), std::ios_base::trunc | std::ios_base::in | std::ios_base::out);
    obj->Stream().rdbuf(fileStream_.rdbuf());
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!