Boost logo

Boost Users :

From: Michael Marcin (mmarcin_at_[hidden])
Date: 2007-10-11 13:10:15


Okay I think I've got a solution but I it is a bit ugly. Is there a
cleaner solution?

template< typename T >
struct wrap
{
};

template< typename ResourceCaches >
struct CacheClearer
{
  ResourceCaches& m_caches;
  CacheClearer( ResourceCaches& caches ) : m_caches( caches ) {}

  template< typename Resource >
  void operator()( wrap<Resource> )
  {
   static_cast<Cache<Resource>&>(m_caches).value.Clear();
  }
};

template< typename ResourceCaches >
CacheClearer<ResourceCaches> MakeCacheClearer( ResourceCaches& caches )
{
  return CacheClearer<ResourceCaches>( caches );
};

template< typename Resource >
struct Cache
{
   resource::Cache<Resource> value;
};

class Manager
{
public:
  /// Returns Cache<Resource>::Retrieve( id )
  template< typename Resource >
  Resource& Get( ResourceId id )
  {
   return static_cast<Cache<Resource>&>(m_caches).value.Retrieve(id);
  }

  /// Calls Cache::Clear for all resource types
  void Clear()
  {
    boost::mpl::for_each<resource_types,
                         wrap<boost::mpl::_1>
>( MakeCacheClearer(m_caches) );
  }

private:
  typedef boost::mpl::vector25<
   Sound,
   Script,
   // etc.
> resource_types;

  boost::mpl::inherit_linearly<
   resource_types, boost::mpl::inherit< Cache< boost::mpl::_2 >,
boost::mpl::_1 >
>::type m_caches;
};

Thanks,

Michael Marcin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net