legacy containers

Discussion in 'C++' started by bob@blah.com, Dec 13, 2005.

  1. Guest

    Hi,

    Given:

    1) Custom containers that have nothing to do with vector, deque, list,
    map etc,
    2) a custom version of new and delete defined for these containers,
    customNew and customDelete, say. When an item is to be inserted/removed
    into/from these custom containers, customNew and customDelete is
    called.
    3) Iterators are available for these custom containers which adhere to
    the requirements of stl.

    e.g.

    CustomContainer cc.
    cc.add(something); // will call customNew whenever memory needs to be
    allocated
    cc.remove(something) // will call customDelete whenever memory needs to
    be deleted

    for (CustomContainer::iterator i = cc.begin(), i!=cc.end(); ++i)
    {
    // do stuff to each element;
    }


    Now assuming I have an allocator defined such that customNew and
    customDelete will be called instead of the defaults/usual new/delete, I
    need to know how arrange things such that customNew and customDelete
    will be called for the container by the STL algorithms.

    If I look at how I would use custom allocators for say a std::vector,
    I would do something like this (given my_allocator is defined)


    std::vector<int, my_allocator<int> > intVect;

    std::sort(cc.begin(), cc.end()); // sort will use my_allocator for
    memory alloc/dealloc etc.



    however my custom containers know nothing about allocators. I can get
    std::vector to use my_allocator for (de)/allocations, but I can't see
    how I can get the stl algorithms to use my_allocator with my custom
    containers. I have all the parts but I can't seem to glue them all
    together, if that makes sense.

    So in order to have this code;


    myContainer<int> intCont;
    intCont.add(2)); // will call customNew if necessary
    intCont.remove(2); // will call customDelete if necessary

    // these iterators adhere to the stl requirements for randomaccess
    iterators.
    myContainer::iterator begin = intCont.begin();
    myContainer::iterator end = intCont.end();

    // now heres the rub.
    std::sort(begin, end);

    I want these alorithms to use customNew and customDelete to get memory
    if needed,.

    I thought I had to use allocators originally but know I'm thinking
    thats not the case. What exactly do I need to do to have std::sort
    (and other algorithms) use customNew and customDelete.


    thanks
    , Dec 13, 2005
    #1
    1. Advertising

  2. Axter Guest

    wrote:
    > Hi,
    >
    > Given:
    >
    > 1) Custom containers that have nothing to do with vector, deque, list,
    > map etc,
    > 2) a custom version of new and delete defined for these containers,
    > customNew and customDelete, say. When an item is to be inserted/removed
    > into/from these custom containers, customNew and customDelete is
    > called.
    > 3) Iterators are available for these custom containers which adhere to
    > the requirements of stl.
    >
    > e.g.
    >
    > CustomContainer cc.
    > cc.add(something); // will call customNew whenever memory needs to be
    > allocated
    > cc.remove(something) // will call customDelete whenever memory needs to
    > be deleted
    >
    > for (CustomContainer::iterator i = cc.begin(), i!=cc.end(); ++i)
    > {
    > // do stuff to each element;
    > }
    >
    >
    > Now assuming I have an allocator defined such that customNew and
    > customDelete will be called instead of the defaults/usual new/delete, I
    > need to know how arrange things such that customNew and customDelete
    > will be called for the container by the STL algorithms.
    >
    > If I look at how I would use custom allocators for say a std::vector,
    > I would do something like this (given my_allocator is defined)
    >
    >
    > std::vector<int, my_allocator<int> > intVect;
    >
    > std::sort(cc.begin(), cc.end()); // sort will use my_allocator for
    > memory alloc/dealloc etc.
    >
    >
    >
    > however my custom containers know nothing about allocators. I can get
    > std::vector to use my_allocator for (de)/allocations, but I can't see
    > how I can get the stl algorithms to use my_allocator with my custom
    > containers. I have all the parts but I can't seem to glue them all
    > together, if that makes sense.
    >
    > So in order to have this code;
    >
    >
    > myContainer<int> intCont;
    > intCont.add(2)); // will call customNew if necessary
    > intCont.remove(2); // will call customDelete if necessary
    >
    > // these iterators adhere to the stl requirements for randomaccess
    > iterators.
    > myContainer::iterator begin = intCont.begin();
    > myContainer::iterator end = intCont.end();
    >
    > // now heres the rub.
    > std::sort(begin, end);
    >
    > I want these alorithms to use customNew and customDelete to get memory
    > if needed,.
    >
    > I thought I had to use allocators originally but know I'm thinking
    > thats not the case. What exactly do I need to do to have std::sort
    > (and other algorithms) use customNew and customDelete.
    >

    Most STL algorithms are structured in a way so that they don't have to
    call any memory allocators at all.
    std::sort does not have to allocate or deallocate memory, and I can't
    think of any of the algorithms in <algorithm> or <functional> headers
    that would need to allocate or deallocate memory.
    Axter, Dec 13, 2005
    #2
    1. Advertising

  3. Neil Cerutti Guest

    On 2005-12-13, <> wrote:
    > Hi,
    >
    > Given:
    >
    > 1) Custom containers that have nothing to do with vector, deque, list,
    > map etc,
    > 2) a custom version of new and delete defined for these containers,
    > customNew and customDelete, say. When an item is to be inserted/removed
    > into/from these custom containers, customNew and customDelete is
    > called.
    > 3) Iterators are available for these custom containers which adhere to
    > the requirements of stl.
    >
    > // these iterators adhere to the stl requirements for randomaccess
    > iterators.
    > myContainer::iterator begin = intCont.begin();
    > myContainer::iterator end = intCont.end();
    >
    > // now heres the rub.
    > std::sort(begin, end);
    >
    > I want these alorithms to use customNew and customDelete to get
    > memory if needed.
    >
    > I thought I had to use allocators originally but know I'm
    > thinking thats not the case. What exactly do I need to do to
    > have std::sort (and other algorithms) use customNew and
    > customDelete.


    The standard algorithms don't need to allocate or release memory.
    They just assign and copy elements. You need to pass special
    inserter iterators to get the behavior you're thinking of.

    For example:

    MyContainer<int> unique_list;
    std::unique_copy(begin, end, std::back_inserter(unique_list));

    That will work for your container as long as it supports
    MyContainer::push_back.

    --
    Neil Cerutti
    Neil Cerutti, Dec 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Steele [MVP]
    Replies:
    1
    Views:
    496
    John Timney \(Microsoft MVP\)
    Jul 14, 2003
  2. Duncan Welch

    legacy String command

    Duncan Welch, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    365
    Ravikanth[MVP]
    Jul 22, 2003
  3. =?Utf-8?B?QnV0Y2g=?=

    Error messages for legacy ASP pages

    =?Utf-8?B?QnV0Y2g=?=, Nov 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    324
    =?Utf-8?B?SWFuIFN1dHRsZQ==?=
    Nov 15, 2004
  4. Replies:
    7
    Views:
    540
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    304
Loading...

Share This Page