1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
7  * Copyright (C) 2006 Refractions Research Inc.
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  **********************************************************************/
15 
16 #ifndef GEOS_OP_UNION_GEOMETRYLISTHOLDER_H
17 #define GEOS_OP_UNION_GEOMETRYLISTHOLDER_H
18 
19 // Forward declarations
20 namespace geos {
21 namespace geom {
22 class Geometry;
23 }
24 }
25 
26 namespace geos {
27 namespace operation { // geos::operation
28 namespace geounion {  // geos::operation::geounion
29 
30 /**
31  * \brief Helper class holding Geometries, part of which are held by reference
32  *        others are held exclusively.
33  */
34 class GeometryListHolder : public std::vector<geom::Geometry*> {
35 private:
36     typedef std::vector<geom::Geometry*> base_type;
37 
38 public:
GeometryListHolder()39     GeometryListHolder() {}
~GeometryListHolder()40     ~GeometryListHolder()
41     {
42         std::for_each(ownedItems.begin(), ownedItems.end(),
43                       &GeometryListHolder::deleteItem);
44     }
45 
46     // items need to be deleted in the end
47     void
push_back_owned(geom::Geometry * item)48     push_back_owned(geom::Geometry* item)
49     {
50         this->base_type::push_back(item);
51         ownedItems.push_back(item);
52     }
53 
54     geom::Geometry*
getGeometry(std::size_t index)55     getGeometry(std::size_t index)
56     {
57         if(index >= this->base_type::size()) {
58             return nullptr;
59         }
60         return (*this)[index];
61     }
62 
63 private:
64     static void deleteItem(geom::Geometry* item);
65 
66 private:
67     std::vector<geom::Geometry*> ownedItems;
68 };
69 
70 } // namespace geos::operation::union
71 } // namespace geos::operation
72 } // namespace geos
73 
74 #endif
75