2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file xmltooling/util/XMLObjectChildrenList.h
20 * STL-compatible container wrapper
23 #ifndef __xmltooling_list_h__
24 #define __xmltooling_list_h__
26 #include <xmltooling/exceptions.h>
27 #include <xmltooling/XMLObject.h>
32 * Shorthand for an XMLObjectChildrenList wrapped around a vector
34 * @param type the type of object in the vector
36 #define VectorOf(type) xmltooling::XMLObjectChildrenList< std::vector<type*> >
39 * Shorthand for an XMLObjectChildrenList wrapped around a list
41 * @param type the type of object in the list
43 #define ListOf(type) xmltooling::XMLObjectChildrenList< std::list<type*> >
46 * Shorthand for an XMLObjectChildrenList wrapped around a deque
48 * @param type the type of object in the deque
50 #define DequeOf(type) xmltooling::XMLObjectChildrenList< std::deque<type*> >
53 * Shorthand for an XMLObjectPairList wrapped around a vector
55 * @param type1 the first type of object in the vector
56 * @param type2 the second type of object in the vector
58 #define VectorOfPairs(type1,type2) xmltooling::XMLObjectPairList< std::vector< std::pair<type1*,type2*> > >
61 * Shorthand for an XMLObjectPairList wrapped around a list
63 * @param type1 the first type of object in the vector
64 * @param type2 the second type of object in the vector
66 #define ListOfPairs(type1,type2) xmltooling::XMLObjectPairList< std::list< std::pair<type1*,type2*> > >
69 * Shorthand for an XMLObjectPairList wrapped around a deque
71 * @param type1 the first type of object in the vector
72 * @param type2 the second type of object in the vector
74 #define DequeOfPairs(type1,type2) xmltooling::XMLObjectPairList< std::deque< std::pair<type1*,type2*> > >
76 namespace xmltooling {
79 template <class _Tx, class _Ty=XMLObject> class XMLObjectChildrenList;
80 template <class _Tx, class _Ty=XMLObject> class XMLObjectPairList;
83 * STL iterator that mediates access to an iterator over typed XML children.
84 * @param _Ty a bidrectional sequence of the subtype to iterate over
87 class XMLObjectChildrenIterator
90 typename _Ty::iterator m_iter;
91 template <class _Tx, class _Tz> friend class XMLObjectChildrenList;
92 template <class _Tx, class _Tz> friend class XMLObjectPairList;
94 typedef typename _Ty::value_type value_type;
95 typedef typename _Ty::reference reference;
96 typedef typename _Ty::pointer pointer;
97 typedef typename _Ty::const_reference const_reference;
98 typedef typename _Ty::const_pointer const_pointer;
99 typedef typename _Ty::difference_type difference_type;
101 XMLObjectChildrenIterator() {
104 XMLObjectChildrenIterator(typename _Ty::iterator iter) {
108 const_reference operator*() const {
112 const_reference operator->() const {
116 XMLObjectChildrenIterator& operator++() {
122 XMLObjectChildrenIterator& operator--() {
128 XMLObjectChildrenIterator operator++(int) {
130 XMLObjectChildrenIterator _Tmp = *this;
135 XMLObjectChildrenIterator operator--(int) {
137 XMLObjectChildrenIterator _Tmp = *this;
142 XMLObjectChildrenIterator& operator+=(difference_type _Off) {
143 // increment by integer
148 XMLObjectChildrenIterator operator+(difference_type _Off) const {
149 // return this + integer
150 return m_iter + _Off;
153 XMLObjectChildrenIterator& operator-=(difference_type _Off) {
154 // decrement by integer
155 return (*this += -_Off);
158 XMLObjectChildrenIterator operator-(difference_type _Off) const {
159 // return this - integer
160 XMLObjectChildrenIterator _Tmp = *this;
161 return (_Tmp -= _Off);
164 difference_type operator-(const XMLObjectChildrenIterator& _Right) const {
165 // return difference of iterators
166 return m_iter - _Right.m_iter;
169 const_reference operator[](difference_type _Off) const {
171 return (*(*this + _Off));
174 bool operator==(const XMLObjectChildrenIterator &_Right) const {
175 // test for iterator equality
176 return (m_iter == _Right.m_iter);
179 bool operator!=(const XMLObjectChildrenIterator &_Right) const {
180 // test for iterator inequality
181 return (!(m_iter == _Right.m_iter));
187 * STL-compatible container that mediates access to underlying lists of typed XML children.
188 * @param _Tx the subtype container to encapsulate
189 * @param _Ty the base type in the underlying list (defaults to XMLObject)
191 template <class Container, class _Ty>
192 class XMLObjectChildrenList
194 Container& m_container;
195 typename std::list<_Ty*>* m_list;
196 typename std::list<_Ty*>::iterator m_fence;
201 typedef typename Container::value_type value_type;
202 typedef typename Container::reference reference;
203 typedef typename Container::const_reference const_reference;
204 typedef typename Container::difference_type difference_type;
205 typedef typename Container::size_type size_type;
207 // We override the iterator types with our constrained wrapper.
208 typedef XMLObjectChildrenIterator<Container> iterator;
209 typedef XMLObjectChildrenIterator<Container> const_iterator;
213 * Constructor to expose a typed collection of children backed by a list of a base type.
215 * @param parent parent object of the collection
216 * @param sublist underlying container to expose
217 * @param backing pointer to backing list for children, if any
218 * @param ins_fence a marker designating where new children of this type should be added
220 XMLObjectChildrenList(
223 typename std::list<_Ty*>* backing,
224 typename std::list<_Ty*>::iterator ins_fence
225 ) : m_container(sublist), m_list(backing), m_fence(ins_fence), m_parent(parent) {
230 size_type size() const {
231 // return length of sequence
232 return m_container.size();
236 // test if sequence is empty
237 return m_container.empty();
241 // return iterator for beginning of mutable sequence
242 return m_container.begin();
246 // return iterator for end of mutable sequence
247 return m_container.end();
250 const_iterator begin() const {
251 // return iterator for beginning of const sequence
252 return m_container.begin();
255 const_iterator end() const {
256 // return iterator for end of const sequence
257 return m_container.end();
260 const_reference at(size_type _Pos) const {
261 // subscript nonmutable sequence with checking
262 return m_container.at(_Pos);
265 const_reference operator[](size_type _Pos) const {
266 // subscript nonmutable sequence
267 return m_container[_Pos];
270 const_reference front() const {
271 // return first element of nonmutable sequence
272 return m_container.front();
275 const_reference back() const {
276 // return last element of nonmutable sequence
277 return m_container.back();
280 void push_back(const_reference _Val) {
283 m_list->insert(m_fence,_Val);
284 m_container.push_back(_Val);
287 iterator erase(iterator _Where) {
288 removeParent(*_Where);
290 removeChild(*_Where);
292 delete *_Where.m_iter;
293 return m_container.erase(_Where.m_iter);
296 iterator erase(iterator _First, iterator _Last) {
297 for (iterator i=_First; i!=_Last; i++) {
304 return m_container.erase(_First.m_iter,_Last.m_iter);
308 erase(begin(),end());
312 void setParent(const_reference _Val) {
313 if (_Val->getParent())
314 throw XMLObjectException("Child object already has a parent.");
315 _Val->setParent(m_parent);
316 _Val->releaseParentDOM(true);
319 void removeParent(const_reference _Val) {
320 if (_Val->getParent()!=m_parent)
321 throw XMLObjectException("Child object not owned by this parent.");
322 _Val->setParent(NULL);
323 m_parent->releaseParentDOM(true);
326 void removeChild(const_reference _Val) {
327 for (typename std::list<_Ty*>::iterator i=m_list->begin(); i!=m_list->end(); i++) {
339 * STL-compatible container that mediates access to underlying lists of typed XML children
340 * that come in pairs.
342 * @param _Tx the subtype container to encapsulate
343 * @param _Ty the base type in the underlying list (defaults to XMLObject)
345 template <class Container, class _Ty>
346 class XMLObjectPairList
348 Container& m_container;
349 typename std::list<_Ty*>* m_list;
350 typename std::list<_Ty*>::iterator m_fence;
355 typedef typename Container::value_type value_type;
356 typedef typename Container::reference reference;
357 typedef typename Container::const_reference const_reference;
358 typedef typename Container::difference_type difference_type;
359 typedef typename Container::size_type size_type;
361 // We override the iterator types with our constrained wrapper.
362 typedef XMLObjectChildrenIterator<Container> iterator;
363 typedef XMLObjectChildrenIterator<Container> const_iterator;
367 * Constructor to expose a typed collection of pairs backed by a list of a base type.
369 * @param parent parent object of the collection
370 * @param sublist underlying container to expose
371 * @param backing pointer to backing list for children, if any
372 * @param ins_fence a marker designating where new children of this type should be added
377 typename std::list<_Ty*>* backing,
378 typename std::list<_Ty*>::iterator ins_fence
379 ) : m_container(sublist), m_list(backing), m_fence(ins_fence), m_parent(parent) {
384 size_type size() const {
385 // return length of sequence
386 return m_container.size();
390 // test if sequence is empty
391 return m_container.empty();
395 // return iterator for beginning of mutable sequence
396 return m_container.begin();
400 // return iterator for end of mutable sequence
401 return m_container.end();
404 const_iterator begin() const {
405 // return iterator for beginning of const sequence
406 return m_container.begin();
409 const_iterator end() const {
410 // return iterator for end of const sequence
411 return m_container.end();
414 const_reference at(size_type _Pos) const {
415 // subscript nonmutable sequence with checking
416 return m_container.at(_Pos);
419 const_reference operator[](size_type _Pos) const {
420 // subscript nonmutable sequence
421 return m_container[_Pos];
424 const_reference front() const {
425 // return first element of nonmutable sequence
426 return m_container.front();
429 const_reference back() const {
430 // return last element of nonmutable sequence
431 return m_container.back();
434 void push_back(const_reference _Val) {
437 m_list->insert(m_fence,_Val.first);
438 m_list->insert(m_fence,_Val.second);
440 m_container.push_back(_Val);
443 iterator erase(iterator _Where) {
444 removeParent(*_Where);
446 removeChild(*_Where);
448 delete _Where.m_iter->first;
449 delete _Where.m_iter->second;
451 return m_container.erase(_Where.m_iter);
454 iterator erase(iterator _First, iterator _Last) {
455 for (iterator i=_First; i!=_Last; i++) {
460 delete i.m_iter->first;
461 delete i.m_iter->second;
464 return m_container.erase(_First,_Last);
468 erase(begin(),end());
472 void setParent(const_reference _Val) {
473 if (_Val.first->getParent() || (_Val.second && _Val.second->getParent()))
474 throw XMLObjectException("One of the child objects already has a parent.");
475 _Val.first->setParent(m_parent);
477 _Val.second->setParent(m_parent);
478 _Val.first->releaseParentDOM(true);
481 void removeParent(const_reference _Val) {
482 if (_Val.first->getParent()!=m_parent || (_Val.second && _Val.second->getParent()!=m_parent))
483 throw XMLObjectException("One of the child objects not owned by this parent.");
484 _Val.first->setParent(NULL);
486 _Val.second->setParent(NULL);
487 m_parent->releaseParentDOM(true);
490 void removeChild(const_reference _Val) {
491 for (typename std::list<_Ty*>::iterator i=m_list->begin(); i!=m_list->end(); i++) {
492 if ((*i)==_Val.first) {
493 typename std::list<_Ty*>::iterator j=i++;
508 #endif /* __xmltooling_list_h__ */