[Boost-users] Please help me to choose a container

2 views
Skip to first unread message

Jayden Shui

unread,
Dec 20, 2011, 4:42:23 PM12/20/11
to boost...@lists.boost.org
Hi All,

I have a number of named objects of type B whose name can be accessed by B::Name(). B is polymorphic. D1, D2 and so on are derived from B. I want to put them in a set or map-like container ordered by their names. They are referenced by other objects, such as of type C. If the objects in the container are changed by its type (from type D1 to D2) or value, their users can know it automatically, i.e. those information is shared among owners and users. To erase an object in the container, first we need to ensure it is not be used by other objects.

Please help me on choose a proper container with smart pointers. I am struggling with it by trying multi-index-container, pointer container, standard container, and cannot find a good way.

Thanks a lot ! !

Best regards,

Jayden

Krzysztof Czainski

unread,
Dec 20, 2011, 6:40:52 PM12/20/11
to boost...@lists.boost.org
2011/12/20 Jayden Shui <jayde...@gmail.com>
Hi All,
Hi Jayden, 

I have a number of named objects of type B whose name can be accessed by B::Name(). B is polymorphic. D1, D2 and so on are derived from B. I want to put them in a set or map-like container ordered by their names. They are referenced by other objects, such as of type C. If the objects in the container are changed by its type (from type D1 to D2)
 
How can an object change it's type? Don't you need to erase an object and create a new one to achieve that?

or value, their users can know it automatically, i.e. those information is shared among owners and users. To erase an object in the container, first we need to ensure it is not be used by other objects.

Please help me on choose a proper container with smart pointers. I am struggling with it by trying multi-index-container, pointer container, standard container, and cannot find a good way.

My first guess would be write your own container adaptor, that uses std::set/map< shared_ptr<B>, your_compare >...

Regards
Kris

Jayden Shui

unread,
Dec 20, 2011, 6:53:36 PM12/20/11
to boost...@lists.boost.org
On Tue, Dec 20, 2011 at 6:40 PM, Krzysztof Czainski <1cza...@gmail.com> wrote:
2011/12/20 Jayden Shui <jayde...@gmail.com>
Hi All,
Hi Jayden, 

I have a number of named objects of type B whose name can be accessed by B::Name(). B is polymorphic. D1, D2 and so on are derived from B. I want to put them in a set or map-like container ordered by their names. They are referenced by other objects, such as of type C. If the objects in the container are changed by its type (from type D1 to D2)
 
How can an object change it's type? Don't you need to erase an object and create a new one to achieve that?

Yes it is.
 
or value, their users can know it automatically, i.e. those information is shared among owners and users. To erase an object in the container, first we need to ensure it is not be used by other objects.

Please help me on choose a proper container with smart pointers. I am struggling with it by trying multi-index-container, pointer container, standard container, and cannot find a good way.

My first guess would be write your own container adaptor, that uses std::set/map< shared_ptr<B>, your_compare >...

Do I need  std::set/map< shared_ptr<scoped_ptr<B>>, your_compare > to do it, since it is the B* shared? Thanks a lot.

 
Regards
Kris


 

Nevin Liber

unread,
Dec 20, 2011, 7:07:11 PM12/20/11
to boost...@lists.boost.org
On 20 December 2011 17:53, Jayden Shui <jayde...@gmail.com> wrote:

> Do I need  std::set/map< shared_ptr<scoped_ptr<B>>, your_compare > to do it,
> since it is the B* shared? Thanks a lot.

Yes. In addition, you'll have to erase then insert whenever you
change the object which scoped_ptr<B> holds, unless you have some
other way of guaranteeing that B::Name() won't change when the object
changes. Multiindex might be easier to use in this respect.

--
 Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com(847) 691-1404
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Jayden Shui

unread,
Dec 20, 2011, 7:13:21 PM12/20/11
to boost...@lists.boost.org
On Tue, Dec 20, 2011 at 7:07 PM, Nevin Liber <ne...@eviloverlord.com> wrote:
On 20 December 2011 17:53, Jayden Shui <jayde...@gmail.com> wrote:

> Do I need  std::set/map< shared_ptr<scoped_ptr<B>>, your_compare > to do it,
> since it is the B* shared? Thanks a lot.

Yes.  In addition, you'll have to erase then insert whenever you
change the object which scoped_ptr<B> holds, unless you have some
other way of guaranteeing that B::Name() won't change when the object
changes.  Multiindex might be easier to use in this respect.


Thanks a lot! Elements in container are on heap. I just feel the 
      
         somehow_container_ptr<shared_ptr<scoped_ptr<B>>>

so unfriendly to developers.

Mateusz Łoskot

unread,
Dec 21, 2011, 8:29:06 AM12/21/11
to boost...@lists.boost.org
On 21 December 2011 00:13, Jayden Shui <jayde...@gmail.com> wrote:
> Elements in container are on heap. I just feel the
>
>          somehow_container_ptr<shared_ptr<scoped_ptr<B>>>
>
> so unfriendly to developers.

Use typedef

Best regards
--
Mateusz Loskot, http://mateusz.loskot.net

Reply all
Reply to author
Forward
0 new messages