【发布时间】:2016-09-16 13:20:58
【问题描述】:
我有一组具有相同接口的多个 C++ 类(虽然不是从彼此派生的)。我正在尝试包装这些以使它们在.NET 中可用。
我目前有一个使用 C/C++ #defines 定义包装类的方法,然后我可以随后用简单的代码行实例化类
但是我无法调试它。理想情况下,我希望能够使用通用或模板。但是我不能在泛型中使用 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 不存在。这很可能是因为我使用的是 typedef ...
【问题讨论】:
-
这个问题肯定需要一些示例代码。作为高级用户,您应该知道minimal reproducible example 是什么...
-
模板是 C++ 细节,对任何其他 .NET 语言都没有用处。请改用
generic关键字。 -
@HansPassant:但我不能使用 C++ 类型作为泛型类的参数……可以吗?如果可以的话..你能解释一下,因为那是我问题的症结所在! ;)
-
您可以使用工厂模式并通过符合 cls 的接口类调用您的 C++ 模板类。
-
Hmya,这里的核心问题是您的代码根本不是通用的。当 C# 代码请求
TResampler<int>或TResampler` 时,您无能为力。还有一百万种。您将不得不将其淘汰,并且仅公开可以有意义使用的那些。应该是一个合理的短名单。
标签: c++ templates generics c++-cli