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.