将 boost::archive 写入 C# 流

writing boost::archive to C# stream

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

我有一个本地 C++ 对象(在 DLL 中),它使用 boost::archive.

写入它的数据

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

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

所以,这就是我想要的:

C#:

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

C++/CLI:

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 有什么帮助吗?

确实,使用适当的 underflow/overflow 实现对 streambuf 进行子类化是可行的方法。

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

资源:

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

    • How to boost::serialize into a sqlite::blob?; as I remember I used one of Dietmar Kühl这里的答案(关于加密或压缩流的内容?)
  • Ed 先生有 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:

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