in our project, we're primarily using our own vector/matrix/quaternion types instead of ogre. Whenever we're interfacing with ogre, we have to convert to Ogre's types. If possible, I'd like to have that eliminated and automatically convert from our own types to an ogre-type whenever that's what a function expects. For now, I'd only like to have some-non-ogre-type to ogre-type conversions, though I think that you can also use a similar approach to enable ogre-type to non-ogre-type conversions. As an example, I'll show how to do this with boost and Ogre's Vector3. Brace yourselves, for here's some template meta-programming to make this conversion stuff "safe":
Code: Select all
// This should be added globally
namespace Ogre {
template <class SourceType, class TargetType> struct ConversionTag : public boost::mpl::false_ {};
}
// Now add this constructor to Ogre::Vector3 - DO NOT make it explicit!
template <class SourceType>
Ogre::Vector3::Vector3( const SourceType& src, const typename boost::enable_if<Ogre::ConversionTag<SourceType, Vector3>, void*>::type hidden=0 )
{
*this = Convert(src);
}
Code: Select all
namespace Ogre { template <> struct ConversionTag<CMyvec3, Vector3> : public boost::mpl::true_ {}; }
Code: Select all
// Add this globally
namespace Ogre {
template <class SourceType, class TargetType> struct ConversionTag {};
}
// Add constructors like this for each math type
template <class SourceType>
Ogre::Vector3::Vector3( const SourceType& other, typename ConversionTag<SourceType,Vector3>::type& hidden=0 )
{
*this = Convert(other);
}
Code: Select all
namespace Ogre {
template <> struct ConversionTag<CMyvec3, Vector3> { typedef void* type; }; // Enable the conversion-constructor
Vector3 Convert(const CMyvec3& rhs) { /* Implement here ... */ }
}
Edit: Added code that actually works