Skip to content

Commit

Permalink
Use Boost.TypeIndex CTTI to implement type info when RTTI is not enab…
Browse files Browse the repository at this point in the history
…led.

This allows to solve the problem with comparing addresses of global id_provider
objects, which fail if the objects reside in different modules. Boost.TypeIndex
solves this problem by comparing specially crafted strings, which are equal
if the respective types are the same.

Unfortunately, this will likely result in a performance drop as string
comparison is likely more expensive than comparing pointers. Any performance
optimizations are better placed in Boost.TypeIndex.
  • Loading branch information
Lastique committed Oct 27, 2018
1 parent 4c4e3e2 commit 4bbcf67
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions include/boost/statechart/detail/rtti_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include <boost/assert.hpp>
#include <boost/config.hpp> // BOOST_MSVC
#include <boost/detail/workaround.hpp>
#if !defined(BOOST_STATECHART_USE_NATIVE_RTTI)
#include <boost/type_index/ctti_type_index.hpp>
#endif

#include <typeinfo> // std::type_info



namespace boost
{
namespace statechart
Expand All @@ -24,10 +26,12 @@ namespace detail
{


#if !defined(BOOST_STATECHART_USE_NATIVE_RTTI)

//////////////////////////////////////////////////////////////////////////////
struct id_provider
{
const boost::typeindex::ctti_type_index::type_info_t* const pTypeInfo;
const void * pCustomId_;
#if defined( BOOST_ENABLE_ASSERT_HANDLER ) || !defined( NDEBUG )
const std::type_info * pCustomIdType_;
Expand All @@ -41,8 +45,14 @@ struct id_holder
};

template< class MostDerived >
id_provider id_holder< MostDerived >::idProvider_;
id_provider id_holder< MostDerived >::idProvider_ =
{
&boost::typeindex::ctti_type_index::type_id< MostDerived >().type_info(),
0,
0
};

#endif // !defined(BOOST_STATECHART_USE_NATIVE_RTTI)


//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -76,8 +86,8 @@ struct rtti_policy

typedef bool id_provider_type; // dummy
#else
typedef const void * id_type;
typedef const id_provider * id_provider_type;
typedef boost::typeindex::ctti_type_index id_type;
typedef const id_provider* id_provider_type;
#endif

////////////////////////////////////////////////////////////////////////////
Expand All @@ -93,7 +103,7 @@ struct rtti_policy
#ifdef BOOST_STATECHART_USE_NATIVE_RTTI
return id_type( typeid( *this ) );
#else
return idProvider_;
return id_type( *idProvider_->pTypeInfo );
#endif
}

Expand All @@ -112,25 +122,13 @@ struct rtti_policy
#ifdef BOOST_STATECHART_USE_NATIVE_RTTI
rtti_base_type( id_provider_type ) {}

////////////////////////////////////////////////////////////////////////
#if BOOST_WORKAROUND( __GNUC__, BOOST_TESTED_AT( 4 ) )
// We make the destructor virtual for GCC because with this compiler
// there is currently no way to disable the "has virtual functions but
// non-virtual destructor" warning on a class by class basis. Although
// it can be done on the compiler command line with
// -Wno-non-virtual-dtor, this is undesirable as this would also
// suppress legitimate warnings for types that are not states.
virtual ~rtti_base_type() {}
#else
~rtti_base_type() {}
#endif

private:
////////////////////////////////////////////////////////////////////////
// For typeid( *this ) to return a value that corresponds to the most-
// derived type, we need to have a vptr. Since this type does not
// contain any virtual functions we need to artificially declare one so.
virtual void dummy() {}
// A virtual destructor will do.
virtual ~rtti_base_type() {}

#else
rtti_base_type(
id_provider_type idProvider
Expand Down Expand Up @@ -158,7 +156,7 @@ struct rtti_policy
#ifdef BOOST_STATECHART_USE_NATIVE_RTTI
return id_type( typeid( const MostDerived ) );
#else
return &id_holder< MostDerived >::idProvider_;
return boost::typeindex::ctti_type_index::type_id< MostDerived >();
#endif
}

Expand Down Expand Up @@ -204,5 +202,4 @@ struct rtti_policy
} // namespace boost



#endif

0 comments on commit 4bbcf67

Please sign in to comment.