multi_index and shared_ptrs

View: New views
2 Messages — Rating Filter:   Alert me  

multi_index and shared_ptrs

by Daniel Burrows-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

  I'm having trouble getting multi_index's key extractors to play nicely
with shared_ptr objects.  I'm trying to eliminate my custom hash and
equality objects in this code:

class screenshot_cache_entry : /* ... */
{
  /* ... */

public:
  const screenshot_key &get_key() const;

  /* ... */
};

class hash_cache_entry
{
public:
  size_t operator()(const boost::shared_ptr<screenshot_c
ntry> &entry) const
  {
    return boost::hash<screenshot_key>()(entry->get_key());
  }
};

class cache_entry_equal
{
public:
  bool operator()(const boost::shared_ptr<screenshot_cache_entry> &entry1,
                  const boost::shared_ptr<screenshot_cache_entry> &entry2) const
  {
    return entry1->get_key() == entry2->get_key();
  }
};

typedef multi_index_container<
  boost::shared_ptr<screenshot_cache_entry>,
  indexed_by<
    hashed_unique<tag<by_screenshot_tag>,
                  identity<boost::shared_ptr<screenshot_cache_entry> >,
                  hash_cache_entry,
                  cache_entry_equal>,
    sequenced<tag<ordered_tag> > >
  > cache_map;


  Note that all my custom hash and equality comparisons do is invoke
get_key() and pass through to the corresponding operation on it.  I've
been back and forth over the documentation and past mailing list posts,
and it seems like this "should" work:

typedef multi_index_container<
  boost::shared_ptr<screenshot_cache_entry>,
    indexed_by<
      hashed_unique<tag<by_screenshot_tag>,
                    const_mem_fun<screenshot_cache_entry, const screenshot_key &,
                                  &screenshot_cache_entry::get_key> >,
      sequenced<tag<ordered_tag> > >
  > cache_map;


  However, when I compile this, I get an error indicating that the index
is attempting to apply hash() to the *shared pointer* rather than to
the key object (at least it didn't just hash the pointer silently!)

(...)
screenshot_cache.cc:526:   instantiated from here
/usr/include/boost/multi_index/hashed_index.hpp:439: error: no match for call to ‘(const boost::hash<const aptitude::screenshot_key>) (const boost::shared_ptr<gui::<unnamed>::screenshot_cache_entry>&)’

  With the ordered index, I get a similar problem (it tries to compare
a screenshot_key to a shared_ptr).

  Can anyone shed some light on this problem?

    Thanks,
  Daniel
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: multi_index and shared_ptrs

by Joaquin M Lopez Munoz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel Burrows <dburrows <at> algebraicthunk.net> writes:

>
>   I'm having trouble getting multi_index's key extractors to play nicely
> with shared_ptr objects.  I'm trying to eliminate my custom hash and
> equality objects in this code:
>
> class screenshot_cache_entry : /* ... */
> {
>   /* ... */
>
> public:
>   const screenshot_key &get_key() const;
>
>   /* ... */
> };
>
> [...]
>
> typedef multi_index_container<
>   boost::shared_ptr<screenshot_cache_entry>,
>   indexed_by<
>     hashed_unique<tag<by_screenshot_tag>,
>                   identity<boost::shared_ptr<screenshot_cache_entry> >,
>                   hash_cache_entry,
>                   cache_entry_equal>,
>     sequenced<tag<ordered_tag> > >
>   > cache_map;
>
> [...]
>
> typedef multi_index_container<
>   boost::shared_ptr<screenshot_cache_entry>,
>     indexed_by<
>       hashed_unique<tag<by_screenshot_tag>,
>                     const_mem_fun<
>                       screenshot_cache_entry, const screenshot_key &,
>                       &screenshot_cache_entry::get_key> >,
>       sequenced<tag<ordered_tag> > >
>   > cache_map;
>
> [...]
>
> screenshot_cache.cc:526:   instantiated from here
> /usr/include/boost/multi_index/hashed_index.hpp:439: error: no match
> for call to ‘(const boost::hash<const aptitude::screenshot_key>)
> (const boost::shared_ptr<gui::<unnamed>::screenshot_cache_entry>&)’

Probably, you have code similar to the following:

  cache_map m;
  ...
  m.find(shared_ptr<gui::<unnamed>::screenshot_cache_entry>(...));

Am I right? Now that you've passed from a hashed index where
the key was boost::shared_ptr<screenshot_cache_entry> to one
where the key is screenshot_cache_entry, lookup ops have to
be passed values convertible to screenshot_cache_entry, not
to boost::shared_ptr<screenshot_cache_entry> as was your
original situation.

Please report back whether this solves your problem.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users