c# - 将 boost::archive 写入 C# 流

标签 c# c++ boost c++-cli archive

我想知道是否有人对此有任何见解。

我有一个本地 C++ 对象(在 DLL 中),它使用 boost::archive 写入它的数据。

该项目由 C# 应用程序(通过 C++/CLI 类)使用,我想将其中的几个存档存储在一个压缩文件中。我在 C# (DotNetZipFile) 中使用 Ionic.Zipfile。

目前,我正在写入一个临时文件,然后使用 ZipFile.UpdateFile 添加到存档中,但我想避免中间人。

所以,这就是我想要的:

C#:

ZipEntry ae = archive.UpdateEntry("Project", (f, stream) =>
{
   pManagedObjectWrapper.Save(stream);
});

C++/命令行界面:

void ManagedObjectWrapper::Save(System::IO::Stream ^ stream)
{
    std::ofstream *myofstream = MAGICALFunction(stream);
    pUnmanagedObject->Save(myofstream);
}

C++:

void UnmanagedObject::Save(std::ofstream *myofstream)
{
    boost::archive::binary_oarchive oa(*myofstream);
    oa << *this;
}

对于什么是 MAGICALFunction 有任何帮助吗?

最佳答案

确实,使用适当的下溢/溢出实现子类化 streambuf 是可行的方法。

我不记得这很难,但我现在也没有合适的 Windows 机器来做这件事,所以你可以四处寻找现成的实现。

资源:

  • 根据我的经验,在 SO 上找到好的工作、小样本并不难:

  • 埃德先生有 an excellent blog 文章¹ 向您展示了开始使用标准库实际需要的一切。它使用 3 个不同的例子来说明这一点。

  • 不要忘记 Boost IOStreams。正如埃德先生所说:

    If you were new to stream buffers before you read this post, I hope you feel a little more comfortable with them now. All the implementations were pretty basic, but a lot more is possible. However, I've found that once I start attempting more extravagant buffers, things can get fiddly pretty quickly. This is when I'll reach for the Boost IOStreams library, which provides a framework for implementing more involved buffers and streams.

    It also allows you to treat the sources, sinks, filters and other concepts independently of one another. In our final example, we hard coded the sink as another std::ostream. What if we wanted the data to go somewhere that doesn't have a stream interface? The Boost IOStreams library allows more flexibility in this area by isolating concepts that I've had to mash together in my example code.

  • cppreference 有 the best ever visual explanation of stream buffers :

    enter image description here

¹ http://www.mr-edd.co.uk/blog/beginners_guide_streambuf

关于c# - 将 boost::archive 写入 C# 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513911/

相关文章:

c++ - 静态断言类型 A 可以从类型 B 构造

c# - 禁用asp.net中div内的所有控件

c# - MySQL - 当类似的 CREATE TABLE 不存在时,CREATE TABLE 会引发错误

c# - SharePoint ItemAdded 事件延迟

c# - 如何使用额外按钮更改多个按钮的背景颜色

c++ - gluProject 和 glhProject 之间有什么区别?

c++ - boost::lexical_cast<std::string>(Int_Type) 可以抛出吗?

c++ - 如何在不丢失精度的情况下将 float 转换为 double(均以 IEEE-754 表示形式存储)?

c++ - 未定义的标识符 - 缺少哪些头文件? - DX12

c++ - 带有 Boost.Filesystem 的 InDesign 插件没有出现在程序中