c++ /CLI模板包装器
C++/CLI template wrapper round
我有一组具有相同接口的多个c++类(尽管不是彼此派生的)。我试图包装这些,使它们在。net中可用。
我目前有一个方法,定义包装类使用C/c++ #定义,然后我可以随后实例化类与一行简单的代码
但是我不能调试这个。理想情况下,我希望能够使用通用或模板。然而,我不能在泛型中使用c++类型,这将是解决这个问题的最终方法。
有没有人知道我如何在不使用可怕的宏的情况下做到这一点?
编辑 :
下面是我编写的模板化类的一个例子: template< typename CPPResamplerClass >
ref class TResampler
{
CPPResamplerClass* pResampler;
public:
TResampler( int inputSampleRate, int outputSampleRate, int bufferLen ) :
pResampler( new CPPResamplerClass( inputSampleRate, outputSampleRate, bufferLen ) )
{
}
~TResampler()
{
this->!ResamplerName();
}
!TResampler()
{
if (pResampler)
{
delete pResampler;
pResampler = nullptr;
}
}
property int HistorySize
{
int get()
{
return pResampler->HistorySize();
}
}
array< float >^ ResampleAudio(array< float >^ in)
{
pResampler->Get
array< float >^ out = gcnew array< float >(in->Length);
cli::pin_ptr< float > pIn = &in[0];
cli::pin_ptr< float > pOut = &out[0];
unsigned int inLen = in->Length;
unsigned int outLen = out->Length;
if (pResampler->ResampleAudio(pOut, outLen, pIn, inLen))
{
System::Array::Resize(out, outLen);
return out;
}
return nullptr;
}
};
typedef TResampler< ::Vec::SpeexResample > SpeexResample;
然后我想从c#访问它,但是SpeexResample
不存在。这很可能是因为我使用了一个类型定义…
模板在实例化之前是不存在的。虽然您可以显式地实例化一个:
template ref class TResampler<SomeNativeClass>;
从c#中使用它并不完全是用户友好的。导出的类型将字面上在其名称中有尖括号。祝你好运使用。在c#中,它只能通过反射来实现。
其次是使用派生类型。下面是一个简单的例子:
#include "stdafx.h"
#include <iostream>
namespace CppCli {
class NativeClassA
{
int foo;
public:
NativeClassA(int foo) : foo(foo) { std::cout << "Built native class A" << std::endl; }
int getFoo() const { return foo; }
};
class NativeClassB
{
int foo;
public:
NativeClassB(int foo) : foo(foo) { std::cout << "Built native class B" << std::endl; }
int getFoo() const { return foo; }
};
template<typename NativeClass>
public ref class ManagedWrapper
{
NativeClass* ptr;
public:
ManagedWrapper(int foo)
: ptr(new NativeClass(foo))
{}
~ManagedWrapper()
{
this->!ManagedWrapper();
}
!ManagedWrapper()
{
if (ptr)
{
delete ptr;
ptr = nullptr;
}
}
property int Foo { int get() { return ptr->getFoo(); } }
};
public ref class ManagedWrapperA : ManagedWrapper<NativeClassA>
{
public:
ManagedWrapperA(int foo) : ManagedWrapper(foo) {}
};
public ref class ManagedWrapperB : ManagedWrapper<NativeClassB>
{
public:
ManagedWrapperB(int foo) : ManagedWrapper(foo) {}
};
};
可以肯定的是,ManagedWrapperA
和ManagedWrapperB
在c#中是可见的。也许您可以宏化这些定义,并且仍然获得不错的调试体验。
相关文章:
- 如何为要在 C# 中使用的静态库创建 C++ 或 C++/CLI 包装器
- 用于LabView中使用的本机c++的c++/cli dll包装器
- c++\CLI dll包装器,用于调用c++类中的虚拟成员
- 无法使用c++/cli包装从c#dll加载函数
- 使用 C++/CLI 包装器将 2D 数组从 C# 传递到非托管C++
- Visual Studio:C++\CLI 包装程序集路径依赖性问题
- C++/CLI 包装器不能使用来自本机C++ dll 的矢量
- 使用C /CLI包装器DLL时缺少功能
- C# 中与 CLI 包装器通信C++未处理的异常
- 将互操作 HwndSource 包装在 c++/cli 中,以用于本机 c++/MFC 应用程序
- 访问 C++/CLI 包装器中的私有C++方法
- 如何编写包装类以将 Qt 信号映射到 C# 事件(通过 C++/CLI)
- 将 C# 委托传递给 C++/CLI 包装器
- 为 GFLW 编写 C++/CLI 包装器
- Visual C#在调用包装的C++CLI dll中的Lapack时引发System.AccessViolation
- C++/CLI 包装器LNK2028和LNK2019错误
- 指针成员的 C++/CLI 包装器 - C++ 单例实例
- 指向指针 C++/CLI 包装器的指针
- 向 C# 公开 C++/CLI 模板化包装器
- 从 CLI 包装器检索本机对象