如何暴露STL列表DLL边界? [英] How to expose STL list over DLL boundary?

查看:115
本文介绍了如何暴露STL列表DLL边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DLL需要访问存储在主机应用程序的STL容器中的数据。因为C ++没有标准的ABI,我想支持不同的编译器,应用程序和DLL之间的接口基本上必须保持原有的数据。

I have a DLL which needs to access data stored in STL containers in the host application. Because C++ has no standard ABI, and I want to support different compilers, the interface between the application and DLL basically has to remain plain-old-data.

是相对直接的。您可以简单地返回向量的内存块,因为它保证是连续的:

For vectors this is relatively straightforward. You can simply return the memory block of the vector, because it is guaranteed to be contigious:

// To return vector<int> data
virtual void GetVectorData(const int*& ptr, size_t& count) const
{
    if (!vec.empty())
        ptr = &(vec.front());

    count = vec.size();
}

现在,DLL可以安全地只读访问向量的数据接口。 DLL也可以将这个内容复制到一个向量本身。

Now the DLL can have safe read-only access to the vector's data via that interface. The DLL can also wrap this to copy the contents in to a vector for itself as well.

但是STL列表(和deques)呢?有没有另一个直接的方式允许通过DLL边界访问?还是我必须诉诸某种GetFirst()/ GetNext()接口?我可能需要为很多列表做这个,所以有一个解决方案很简单的矢量的很好。

What about STL lists (and deques) though? Is there another straightforward way to allow access via a DLL boundary? Or will I have to resort to some kind of GetFirst()/GetNext() interface? I might need to do this for a lot of lists, so it'd be nice to have a solution as simple as vector's.

推荐答案

也许你可以传递像手柄到列表/ deque迭代器?这些句柄类型将是不透明的,并在您要发送给用户的头文件中声明。在内部,你需要将句柄值映射到list / deque迭代器。基本上,用户将编写如下代码:

Perhaps you can pass something like "handles" to list/deque iterators? These handle types would be opaque and declared in a header file you would ship to the users. Internally, you would need to map the handle values to list/deque iterators. Basically, the user would write code like:

ListHandle lhi = GetListDataBegin();
const ListHandle lhe = GetListDataEnd();

while (lhi != lhe)
{
  int value = GetListItem(lhi);
  ...
  lhi = GetNextListItem(lhi);
}

这篇关于如何暴露STL列表DLL边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆