AutoPtrExt.h

// AutoPtrExt.h
//
// Author David Barrett-Lennard  
// (C)opyright Cedanet Pty Ltd 2005

#pragma once
#ifndef Ceda_cxUtils_AutoPtrExt_H
#define Ceda_cxUtils_AutoPtrExt_H

namespace ceda
{

template <typename T> void DeletePtrs(T i1, T i2)
{
    while(i1 != i2)
    {
        delete *i1;
        ++i1;
    }
}

template <typename T> void DeletePtrs(const T& v)
{
    DeletePtrs(v.begin(), v.end());
}

template <typename Collection>
struct AutoCollectionOfPtr : public Collection
{
    void DeleteAndClear()
    {
        for (typename Collection::iterator i = Collection::begin() ; i != Collection::end() ; ++i)
        {
            delete *i;
        }
        Collection::clear();
    }

    ~AutoCollectionOfPtr()
    {
        DeleteAndClear();
    }
};

template <typename Map>
struct AutoMapOfPtr : public Map
{
    ~AutoMapOfPtr()
    {
        for (typename Map::iterator i = Map::begin() ; i != Map::end() ; ++i)
        {
            delete i->second;
        }
    }
};

} // namespace ceda

#endif // include guard