第三方类型是否应该在我的C ++库的API中公开 [英] Should third-party types be exposed in my C++ library's API

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

问题描述

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



我正在尝试最好的方法来将这些类型公开给我的图书馆的
客户端,并提出了几个选项我的API我使用四元数
类型作为例子,但这可以同样适用于矩阵等。另外,
虽然我特别在谈论暴露Eigen的类型,我猜这个
的问题可以同样适用于其他使用的外部库。



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



此选项将要求客户端通过基本类型传递数据。对于
示例,对于传递四元数(4个元素),可以执行以下操作:

  void my_func(double my_quat [4])

2)公开Eigen的类型



Eigen为数组和四元数提供了几种模板类型。对于
示例,如果一个函数需要四元数,我可以使用Eigen的 Quaterniond
类型(这真是一个typedef for 四元组< double> ):

  void my_func(const Eigen :: Quaterniond& my_quat)

3)为客户端的各种类型创建一个简单的包装器



我可以创建一个非常简单的四元数类型(比如,某种简单的结构),
客户端必须创建(可能通过某种工厂功能)到
传递给我的API:

  void my_func(const quaternion_t& my_quat)

我的库将把 quaternion_t 类型转换为内部的Eigen
表示形式。 p>

我不喜欢选项1太多,因为我希望在我的API中输入
更强的感觉。选项2将要求我的客户使用Eigen,而不是
提及兼容性的潜在问题,如果他们使用不同的
版本的Eigen(顺便说一下,Eigen是一个只有标题的库,如果
事项)。那会留下选项3。



人们认为什么?我基本上回答了我自己的问题吗?有什么例子吗?



相关问题



一个href =http://stackoverflow.com/questions/6527945/using-third-party-classes-in-ac-header-file>这里,但没有真正详细说明一个应该公开外部类型。

解决方案

包装/封装。假设您想添加一些附加功能,例如缓存计算结果,如四元数的规范,作为实现更改。如果您暴露了第三方类型,而不强制您的客户端代码更改其呼叫,则您无法轻松执行此操作。


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.

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) Use only basic C++ types

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) Expose Eigen's Types

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) Create a simple wrapper for the various types for clients

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)

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

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?

Related Questions

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

解决方案

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天全站免登陆