17454Snate@binkert.org/*
27454Snate@binkert.org * Copyright (c) 2010 The Hewlett-Packard Development Company
37454Snate@binkert.org * All rights reserved.
47454Snate@binkert.org *
57454Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67454Snate@binkert.org * modification, are permitted provided that the following conditions are
77454Snate@binkert.org * met: redistributions of source code must retain the above copyright
87454Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97454Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107454Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117454Snate@binkert.org * documentation and/or other materials provided with the distribution;
127454Snate@binkert.org * neither the name of the copyright holders nor the names of its
137454Snate@binkert.org * contributors may be used to endorse or promote products derived from
147454Snate@binkert.org * this software without specific prior written permission.
157454Snate@binkert.org *
167454Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177454Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187454Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197454Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207454Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217454Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227454Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237454Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247454Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257454Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267454Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277454Snate@binkert.org *
287454Snate@binkert.org * Authors: Nathan Binkert
297454Snate@binkert.org */
307454Snate@binkert.org
317454Snate@binkert.org#ifndef __BASE_STL_HELPERS_HH__
327454Snate@binkert.org#define __BASE_STL_HELPERS_HH__
337454Snate@binkert.org
347454Snate@binkert.org#include <algorithm>
357454Snate@binkert.org#include <iostream>
367454Snate@binkert.org
377454Snate@binkert.orgnamespace m5 {
387454Snate@binkert.orgnamespace stl_helpers {
397454Snate@binkert.org
407454Snate@binkert.orgtemplate <typename T>
417454Snate@binkert.orgvoid
427454Snate@binkert.orgdeletePointer(T &ptr)
437454Snate@binkert.org{
447454Snate@binkert.org    delete ptr;
457454Snate@binkert.org    ptr = NULL;
467454Snate@binkert.org}
477454Snate@binkert.org
487454Snate@binkert.orgtemplate <class T>
497454Snate@binkert.orgclass ContainerPrint
507454Snate@binkert.org{
517454Snate@binkert.org  private:
527454Snate@binkert.org    std::ostream &out;
537454Snate@binkert.org    bool first;
547454Snate@binkert.org
557454Snate@binkert.org  public:
567454Snate@binkert.org    ContainerPrint(std::ostream &out)
577454Snate@binkert.org        : out(out), first(true)
587454Snate@binkert.org    {}
597454Snate@binkert.org
607454Snate@binkert.org    void
617454Snate@binkert.org    operator()(const T &elem)
627454Snate@binkert.org    {
637454Snate@binkert.org        // First one doesn't get a space before it.  The rest do.
647454Snate@binkert.org        if (first)
657454Snate@binkert.org            first = false;
667454Snate@binkert.org        else
677454Snate@binkert.org            out << " ";
687455Snate@binkert.org
697455Snate@binkert.org        out << elem;
707454Snate@binkert.org    }
717454Snate@binkert.org};
727454Snate@binkert.org
737454Snate@binkert.org// Treat all objects in an stl container as pointers to heap objects,
747454Snate@binkert.org// calling delete on each one and zeroing the pointers along the way
758737Skoansin.tan@gmail.comtemplate <template <typename T, typename A> class C, typename T, typename A>
767454Snate@binkert.orgvoid
777454Snate@binkert.orgdeletePointers(C<T,A> &container)
787454Snate@binkert.org{
797454Snate@binkert.org    std::for_each(container.begin(), container.end(), deletePointer<T>);
807454Snate@binkert.org}
817454Snate@binkert.org
827454Snate@binkert.org// Write out all elements in an stl container as a space separated
837454Snate@binkert.org// list enclosed in square brackets
848737Skoansin.tan@gmail.comtemplate <template <typename T, typename A> class C, typename T, typename A>
857454Snate@binkert.orgstd::ostream &
867454Snate@binkert.orgoperator<<(std::ostream& out, const C<T,A> &vec)
877454Snate@binkert.org{
887454Snate@binkert.org    out << "[ ";
897454Snate@binkert.org    std::for_each(vec.begin(), vec.end(), ContainerPrint<T>(out));
907454Snate@binkert.org    out << " ]";
917454Snate@binkert.org    out << std::flush;
927454Snate@binkert.org    return out;
937454Snate@binkert.org}
947454Snate@binkert.org
957811Ssteve.reinhardt@amd.com} // namespace stl_helpers
967811Ssteve.reinhardt@amd.com} // namespace m5
977454Snate@binkert.org
987454Snate@binkert.org#endif // __BASE_STL_HELPERS_HH__
99