Boost logo

Boost :

Subject: Re: [boost] Abstract STL Container Adaptors
From: Andy Jost (Andrew.Jost_at_[hidden])
Date: 2013-03-14 19:07:13


> I really cannot follow this.
>
> If the container is type erased, how do you get that information you are alluding to back out?

Here's a very rough sketch demonstrating just one member (set::count). Many details are ignored, obviously.

template<typename ValueType> struct abstract_set
{
  typedef ValueType key_type;
  typedef ValueType value_type;
  typedef std::size_t size_type;
  // etc.

  template<typename GenericSet>
  abstract_set(GenericSet const & set)
    : m_holder(new holder<GenericSet>(set))
  {}

  size_type count(value_type const & value) const
    { return m_holder->count(value); }

private:

  struct holder_base
  {
    virtual size_t count(value_type const &) const = 0;
  };

  template<typename GenericSet>
  struct holder : holder_base
  {
    holder(GenericSet const & set) : m_container(set) {}

    virtual size_t count(value_type const & value) const
      { return m_container.count(value); }

  private:

    GenericSet const & m_container;
  };

  boost::shared_ptr<holder_base> m_holder;
};

void my_algorithm(abstract_set<int> const & set)
{
  std::cout << "7 in set? " << bool(set.count(7)) << std::endl;
}

int main()
{
  std::set<int> set0;
  set0.insert(4);
  my_algorithm(set0);

  std::tr1::unordered_set<int> set1;
  set1.insert(7);
  my_algorithm(set1);

  return 0;
}

>There are plenty of papers and implementations of any_iterator out there.
>Why is that not sufficient for your needs?

I want the container, not just an iterator. I might want to test for membership or add elements to the container.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk