1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <GeomAPI_DataMapOfShapeMapOfShapes.h>
23 #include <NCollection_DataMap.hxx>
24 #include <NCollection_Map.hxx>
25 #include <TopoDS_Shape.hxx>
27 typedef NCollection_DataMap<TopoDS_Shape, NCollection_Map<TopoDS_Shape> > MAP;
28 #define MY_MAP implPtr<MAP>()
30 //=================================================================================================
31 GeomAPI_DataMapOfShapeMapOfShapes::GeomAPI_DataMapOfShapeMapOfShapes()
32 : GeomAPI_Interface(new NCollection_DataMap<TopoDS_Shape, NCollection_Map<TopoDS_Shape> >)
35 //=================================================================================================
36 bool GeomAPI_DataMapOfShapeMapOfShapes::bind(const std::shared_ptr<GeomAPI_Shape> theKey,
37 const ListOfShape& theItems)
39 const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
40 if(MY_MAP->IsBound(aKey)) {
41 MY_MAP->ChangeFind(aKey).Clear();
43 for(ListOfShape::const_iterator anIt = theItems.cbegin(); anIt != theItems.cend(); anIt++) {
44 const TopoDS_Shape& anItem = (*anIt)->impl<TopoDS_Shape>();
45 if(MY_MAP->IsBound(aKey)) {
46 MY_MAP->ChangeFind(aKey).Add(anItem);
48 NCollection_Map<TopoDS_Shape> anItems;
50 MY_MAP->Bind(aKey, anItems);
57 //=================================================================================================
58 bool GeomAPI_DataMapOfShapeMapOfShapes::add(const std::shared_ptr<GeomAPI_Shape> theKey,
59 const std::shared_ptr<GeomAPI_Shape> theItem)
61 const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
62 const TopoDS_Shape& anItem = theItem->impl<TopoDS_Shape>();
63 if(MY_MAP->IsBound(aKey)) {
64 return MY_MAP->ChangeFind(aKey).Add(anItem) == Standard_True;
66 NCollection_Map<TopoDS_Shape> anItems;
68 return MY_MAP->Bind(aKey, anItems) == Standard_True;
72 //=================================================================================================
73 bool GeomAPI_DataMapOfShapeMapOfShapes::isBound(const std::shared_ptr<GeomAPI_Shape> theKey) const
75 const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
76 return MY_MAP->IsBound(aKey) == Standard_True;
79 //=================================================================================================
80 bool GeomAPI_DataMapOfShapeMapOfShapes::find(const std::shared_ptr<GeomAPI_Shape> theKey,
81 ListOfShape& theItems) const
83 const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
85 if(MY_MAP->IsBound(aKey) == Standard_False) {
89 const NCollection_Map<TopoDS_Shape>& aMap = MY_MAP->Find(aKey);
90 for(NCollection_Map<TopoDS_Shape>::Iterator anIt(aMap); anIt.More(); anIt.Next()) {
91 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
92 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
93 theItems.push_back(aShape);
99 //=================================================================================================
100 bool GeomAPI_DataMapOfShapeMapOfShapes::unBind(const std::shared_ptr<GeomAPI_Shape> theKey)
102 const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
103 return MY_MAP->UnBind(aKey) == Standard_True;
106 //=================================================================================================
107 void GeomAPI_DataMapOfShapeMapOfShapes::clear()
109 return MY_MAP->Clear();
112 //=================================================================================================
113 int GeomAPI_DataMapOfShapeMapOfShapes::size() const
115 return MY_MAP->Size();
119 //=================================================================================================
120 // iterator implementation
121 //=================================================================================================
123 class IteratorImpl : public GeomAPI_DataMapOfShapeMapOfShapes::iterator
128 IteratorImpl(const MAP::Iterator& theIterator)
129 : myIterator(theIterator)
132 IteratorImpl(const std::shared_ptr<IteratorImpl>& theIterator)
134 mySelf = theIterator;
137 bool operator==(const std::shared_ptr<IteratorImpl>& theOther) const
140 return theOther->myIterator.IsEqual(myIterator);
142 // theOther is end iterator => check the current iterator is not at the end
143 return !myIterator.More();
146 virtual iterator& operator++()
151 virtual iterator operator++(int)
153 std::shared_ptr<IteratorImpl> aSelf = std::dynamic_pointer_cast<IteratorImpl>(mySelf);
154 std::shared_ptr<IteratorImpl> aCopy(new IteratorImpl(aSelf->myIterator));
156 return IteratorImpl(aCopy);
159 virtual key_type first() const
162 return iterator::first();
164 GeomShapePtr aShape(new GeomAPI_Shape);
165 aShape->setImpl(new TopoDS_Shape(myIterator.Key()));
169 virtual mapped_type second() const
172 return iterator::second();
175 const NCollection_Map<TopoDS_Shape>& aMap = myIterator.Value();
176 for(NCollection_Map<TopoDS_Shape>::Iterator anIt(aMap); anIt.More(); anIt.Next()) {
177 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
178 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
179 anItems.push_back(aShape);
185 MAP::Iterator myIterator;
189 GeomAPI_DataMapOfShapeMapOfShapes::iterator::iterator()
193 GeomAPI_DataMapOfShapeMapOfShapes::iterator::iterator(
194 const GeomAPI_DataMapOfShapeMapOfShapes::iterator& theOther)
195 : mySelf(theOther.mySelf)
199 const GeomAPI_DataMapOfShapeMapOfShapes::iterator&
200 GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator=(
201 const GeomAPI_DataMapOfShapeMapOfShapes::iterator& theOther)
203 mySelf = theOther.mySelf;
207 bool GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator==(const iterator& theOther) const
209 std::shared_ptr<IteratorImpl> aSelf = std::dynamic_pointer_cast<IteratorImpl>(mySelf);
210 std::shared_ptr<IteratorImpl> aSelfOther =
211 std::dynamic_pointer_cast<IteratorImpl>(theOther.mySelf);
213 return aSelf ? aSelf->operator==(aSelfOther)
214 : (aSelfOther ? aSelfOther->operator==(aSelf) : true);
217 bool GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator!=(const iterator& theOther) const
219 return !operator==(theOther);
222 GeomAPI_DataMapOfShapeMapOfShapes::iterator&
223 GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator++()
225 mySelf->operator++();
229 GeomAPI_DataMapOfShapeMapOfShapes::iterator
230 GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator++(int)
235 GeomAPI_DataMapOfShapeMapOfShapes::iterator::key_type
236 GeomAPI_DataMapOfShapeMapOfShapes::iterator::first() const
238 return mySelf->first();
241 GeomAPI_DataMapOfShapeMapOfShapes::iterator::mapped_type
242 GeomAPI_DataMapOfShapeMapOfShapes::iterator::second() const
244 return mySelf->second();
249 GeomAPI_DataMapOfShapeMapOfShapes::iterator GeomAPI_DataMapOfShapeMapOfShapes::begin()
251 MAP::Iterator anIt(*MY_MAP);
252 std::shared_ptr<IteratorImpl> anIter(new IteratorImpl(anIt));
253 return IteratorImpl(anIter);
256 GeomAPI_DataMapOfShapeMapOfShapes::const_iterator GeomAPI_DataMapOfShapeMapOfShapes::begin() const
258 MAP::Iterator anIt(*MY_MAP);
259 std::shared_ptr<IteratorImpl> anIter(new IteratorImpl(anIt));
260 return IteratorImpl(anIter);
263 GeomAPI_DataMapOfShapeMapOfShapes::iterator GeomAPI_DataMapOfShapeMapOfShapes::end()
265 return IteratorImpl(std::shared_ptr<IteratorImpl>());
268 GeomAPI_DataMapOfShapeMapOfShapes::const_iterator GeomAPI_DataMapOfShapeMapOfShapes::end() const
270 return IteratorImpl(std::shared_ptr<IteratorImpl>());