4

When using different libraries, I always found that each have a custom type for the same "real word entity".

Say I have a project using points in 3 dimensions, I just use algorithms from OpenCv and PCL(Point Cloud Library). I found myself with these types for a point :

  • Point3_ for OpenCv
  • PointXYZ for PCL
  • Point3d my custom type

Now I have algorithms I wrote for my Point3d, but I also want to use algorithms from these libraries. Converting each point in a big set from one type to another, back and forth, takes memory and time.

What would be the best way to have some kind of abstraction around this ?

Pluc
  • 901
  • 8
  • 21
  • are they binary compatible? –  Nov 29 '12 at 14:56
  • I'm not sure what you mean by binary compatible, but I guess the answer is no : different sizeof, members, padding... I probably need some sort of casting, but I was expecting some black magic. – Pluc Nov 29 '12 at 15:03
  • post the declaration of all three structs –  Nov 29 '12 at 15:16
  • @ThePluc Well, if it's just about black magic, you can probably *"assume"* that they are binary compatible. As long as they don't use virtual functions or unusual alignment (e.g. for use with SSE) they should just amount to 3 `float`s/`double`s one after the other. So an evil `reinterpret_cast` might do. Otherwise, if they all have a `[]`-operator, just make your algorithms templates (and probably provide some fundamental things yourself, like `normalize`...). – Christian Rau Nov 29 '12 at 15:40

1 Answers1

1

You could do something like this

template<class T>
    struct Getter{

};

template<class T>
struct Getter<Point3_<T>>{
     typedef T& type;
     static type getx(Point3_<T>& p){
         return p.x;
     }
};
template<>
struct Getter<PointXYZ>{
     typedef float& type;
     static type getx(PointXYZ& p){
         return p.x;
     }
};

template <class T>
point_x(T& p) -> Getter<T>::type{
      return Getter<T>::getx(p);

}

Do the same for y and z Then modify your algorithms to take a template and instead of using p.x = ... use

getx(p) = ..
auto x = getx(p)
John Bandela
  • 2,416
  • 12
  • 19