结束语非托管C ++类库用C ++ / CLI - 问题2 - 集合 [英] Wrapping an Unmanaged C++ Class Library with C++/CLI - Question 2 - Collections

查看:183
本文介绍了结束语非托管C ++类库用C ++ / CLI - 问题2 - 集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这篇文章再presents问题#我的询问2。引入块(直到数量也达到了所有的文本)中被重复这两个问题,因为这是可能需要回答的问题的背景资料。


介绍到问题

我有++库,其中包含类和函数是共同的,并在几个更上一层楼库共享的托管C。我现在需要提供给C#/。NET应用程序访问的公用库。要做到这一点,我将不得不换用C ++ / CLI的包装类的公用库。

中包含的公共库类可以是包含嵌套类的定义和成员变量是其它类对象的集合复杂的类。集合变量是一个自定义列表类用于管理集合类型定义的实例。公共图书馆还包括重新present使用FLEX /野牛创建一个自定义的脚本文件语法的分析结构类。公共库和更高层次的库都写在一个时尚,允许跨平台(Linux和GCC)编译和使用。我所做的任何更改仍然必须考虑到这一点。

在C ++ / CLI的包装类起初只需要读取能力。但随着项目的推进,我最终需要能够创建和修改的对象也是如此。

我知道C ++ / CLI和创造一些包装对于其他非托管的C / C ++项目,以及提供抽象的功能,同样的公共库。所以我的基础知识(和一些先进的知识)了。

我有相关的执行这项任务两个问题,因为它们既能产卵自己的讨论和解决方案,我分裂我的问题到不同的岗位。我将包括链接到每个岗位的另一个问题。


实际问题

  1. <一个href="http://stackoverflow.com/questions/4819781/wrapping-an-unmanaged-c-class-library-with-c-cli-project-$c$c-organization">Wrapping非托管C ++类库用C ++ / CLI - 项目/ code组织

  2. 如何有效地包装在非托管班/手柄集变量?

    • 集合对象是一个自定义模板列表类( CObjectList&LT; T&GT; )的类型定义写入处理对象指针的集合管理。该集合类提供了所有的基本信息收集功能,以及指针管理和解构对象的清理/释放。因此,对于 CWidget 将有一个的typedef CObjectList&LT; CWidget&GT; CWidgetList; 在code

    • 大多数在code和集合类模板参数中使用的类是类本身。但在某些情况下,集合是基类。这发生在已分析的结构,为自定义脚本FLEX /野牛解析器。例如,有一个的CCommand 所有其他可用的命令,从继承类。所以会有 CSetCommand CPrintCommand CIfCommand

    • 我的身影,为了做到这一点,我不得不创建我的,它维护单独的列表的非托管C ++ / CLI类集合包装类。内集合对象会管理的非托管对象,以后还有必须是一个管理的集合/列表对象来存储包装类的项目。

    • 是否有任何人,任何例子/建议,如何做到这一点?或者如何编写一个通用类,可以采取两种非托管C ++ / CLI类的类型作为参数?

解决方案

<一个href="http://stackoverflow.com/questions/4819801/wrapping-an-unmanaged-c-class-library-with-c-cli-question-2-collections/4819887#4819887">Lou有一个很好的建议,我同意他的时候这种方法是良好的条件。

如果不是收藏是非常大的,或者来回传递频繁,你会更好实现.NET枚举接口,但没有使用.NET的集合。基本上,你有一个集合包装,拥有一个原生的STL集合,一个迭代器包装,拥有一个原生的STL迭代器。该集合包装将实施的IEnumerable 接口和的GetEnumerator 将创建一个迭代包装的一个实例,迭代器包装将实施在的IEnumerator 接口。

您会希望让自己的包装指针在本地收集和做引用计数,如一个辅助管理类(大概值类)的boost :: shared_ptr的。使用栈语义标记,以确保引用计数就会自动完成,当托管集合包装或迭代器包装得到处理。

Note: This post represents Question #2 of my inquiry. The introduction block (all text until the numbers are reached) is repeated in both questions as it is background information that may be needed to answer the question.


Introduction to Question

I have an unmanaged C++ library that contains classes and functions that are common to and shared among several "higher level" libraries. I now have need to provide access to the common library to C#/.Net applications. To do this, I will have to wrap the common library with C++/CLI wrapper classes.

The classes contained in the common library can be complex classes containing nested class definitions and member variables that are collections of other class objects. The collection variables are instances of typedefs of a custom list class for managing the collection. The common library also includes classes that represent the parsed structure of a custom script file syntax created using FLEX/BISON. The common library and the "higher level" libraries are all written in a fashion that allows for cross platform (Linux and GCC) compiling and usage. Any changes I make must still allow for this.

The C++/CLI wrapper classes at first need only read capability. But as the project advances, I'll eventually need to be able to create and modify the objects as well.

I know C++/CLI and have created several wrappers for other unmanaged C/C++ projects as well as providing abstracted functionality to this same common library. So I have the basics (and some advanced knowledge) already.

I have two questions related to performing this task and since they could both spawn their own discussions and solutions, I'm splitting my questions into separate posts. I'll include the link to the other question in each post.


Actual Questions

  1. Wrapping an Unmanaged C++ Class Library with C++/CLI - Project/Code Organization

  2. How do I efficiently wrap/handle collection variables in the unmanaged classes?

    • The collection objects are typedefs of a custom template list class (CObjectList<T>) written to handle management of a collection of object pointers. The collection class provides all the basic collection functionality as well as pointer management and cleanup/freeing of the objects on deconstruction. So for CWidget there would be a typedef CObjectList<CWidget> CWidgetList; in the code.

    • Most of the classes used in the code and collection class template parameter are the class itself. But in some cases the collection is of a base class. This occurs in the parsed structure for the custom script FLEX/BISON parser. For example, there is a CCommand class that all the other available commands inherit from. So there would be CSetCommand, CPrintCommand, CIfCommand, etc.

    • I figure in order to do this, I'll have to create my collection wrapper class that maintains separate lists for unmanaged and C++/CLI classes. The inner collection object will manage the unmanaged object, and there'll have to be a managed collection/list object to store the wrapper class for the item.

    • Is there anyone that any examples/suggestions of how to do this? Or how to write a generic class that can take both unmanaged and C++/CLI class types as parameters?

解决方案

Lou has an excellent suggestion and I agree with his conditions on when that approach is good.

If instead the collections are very large, or are passed back and forth frequently, you'd be better off implementing the .NET enumerable interface but not using .NET collections. Basically, you'd have a collection wrapper that owns a native STL collection, and an iterator wrapper that owns a native STL iterator. The collection wrapper would implement the IEnumerable interface and GetEnumerator would create an instance of the iterator wrapper, the iterator wrapper would implement the IEnumerator interface.

You'd want to make yourself a helper managed class (probably a value class) that wraps the pointer to the native collection and does reference counting, like boost::shared_ptr. Use stack semantics notation to make sure that the reference counting gets done automatically when the managed collection wrapper or iterator wrapper get disposed.

这篇关于结束语非托管C ++类库用C ++ / CLI - 问题2 - 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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