如果第三方类型在我的C暴露++库的API [英] Should third-party types be exposed in my C++ library's API

查看:162
本文介绍了如果第三方类型在我的C暴露++库的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C ++库,其中用户提供复杂的输入,如
矩阵和四元。我不想重新实现这些类型的话,
在内部,我将使用库。

I'm developing a C++ library where user's will provide complex inputs, such as matrices and quaternions. I don't want to have to reimplement these types so, internally, I'll be using the Eigen library.

我想的最佳方式决定公开这些类型的我的图书馆
客户纷纷拿出我的API几个选项。我使用的是四元
键入作为例子,但是这可能同样适用于矩阵和这样。也,
虽然我专门谈论露出本征的类型,我想这
问题可以在使用中也同样适用于其他外部库。

I'm trying to decide on the best way to expose these types to my libraries' clients and have come up with a few options for my API. I use a quaternion type as an example, but this could apply equally to matrices and such. Also, although I'm specifically talking about exposing Eigen's types, I guess this question can apply equally well to other external libraries in use.

1)只使用基本的C ++类型

这个选项会要求客户在通过基本类型的数据传递。对于
例如,对于传递一个四元(4元),一个可以这样做:

This option would require clients to pass data in via basic types. For example, for passing in a quaternion (4 elements), one could do:

void my_func(double my_quat[4])

2)暴露征的类型

本征提供了多种模板类型数组和四元数。对于
例如,如果一个函数需要一个四元数,我可以用艾根的 Quaterniond
类型(这是真正为四元数℃的类型定义;双>

Eigen provides several templated types for arrays and quaternions. For example, if a function requires a quaternion, I could use Eigen's Quaterniond type (which is really a typedef for Quaternion<double>):

void my_func(const Eigen::Quaterniond& my_quat)

3)创建的各类一个简单的包装,为客户

我可以创建一个非常简单的四元式(比如说,某种简单的结构组成)
客户必须创建(可能通过某种工厂函数),以
传递给我的API:

I could create a very simple quaternion type (say, some sort of simple struct) that clients would have to create (perhaps via some sort of factory function) to pass to my API:

void my_func(const quaternion_t& my_quat)

我的图书馆将转换 quaternion_t 键入我的内部艾根
再presentation。

My library would convert the quaternion_t type to my internal Eigen representation.

我不喜欢选项1太多,因为我希望这是感更强
键入我的API。方案2将需要我的客户使用本征为好,不
他们应该使用不同的提兼容性的潜在问题
本征的版本(顺便说一句,本征是,如果一个只有头库
事项)。这使得选择3。

I don't like option 1 too much since I want there to be a stronger sense of typing in my APIs. Option 2 would require my clients to use Eigen as well, not to mention potential problems with compatibility should they use a different version of Eigen (incidentally, Eigen is a header-only library if that matters). That leaves option 3.

你的乡亲有什么看法?有没有我基本上回答我的问题?任何的例子在那里?

What do folks think? Have I basically answered my own question? Any examples out there?

相关问题

一个相关的问题被问<一个href=\"http://stackoverflow.com/questions/6527945/using-third-party-classes-in-a-c-header-file\">here但并没有真正进入是否有一个的公开外部类型的详细信息。

A related question was asked here but didn't really go into details of whether one should expose external types.

推荐答案

裹/封装。假设你想添加一些额外的功能,如缓存结果的计算,如四元的标准,为实现变革。你可以这样做(一样容易)如果你暴露第三方类型不强迫客户code键更改它的呼叫。

Wrap / encapsulate. Say you want to add some additional feature, such as caching the result of a computation, like the norm of a quaternion, as an implementation change. You can't do that (as easily) if you expose the 3rd party types without forcing your client code to change its call.

这篇关于如果第三方类型在我的C暴露++库的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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