Convert from NULL macro to nullptr.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLObjectChildrenList.h
index 8a21e60..c9955fb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2007 Internet2
+ *  Copyright 2001-2010 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,9 +15,9 @@
  */
 
 /**
- * @file XMLObjectChildrenList.h
+ * @file xmltooling/util/XMLObjectChildrenList.h
  * 
- * STL-compatible container wrapper
+ * STL-compatible container wrapper.
  */
 
 #ifndef __xmltooling_list_h__
@@ -81,28 +81,38 @@ namespace xmltooling {
 
     /**
      * STL iterator that mediates access to an iterator over typed XML children.
-     * @param _Ty   a bidrectional sequence of the subtype to iterate over
+     *
+     * @param Container type of container
+     * @param _Ty       a bidrectional iterator to guard
      */
-    template <class _Ty>
+    template <class Container, typename _Ty>
     class XMLObjectChildrenIterator
     {
         /// @cond OFF
-        typename _Ty::iterator m_iter;
+        _Ty m_iter;
         template <class _Tx, class _Tz> friend class XMLObjectChildrenList;
         template <class _Tx, class _Tz> friend class XMLObjectPairList;
     public:
-        typedef typename _Ty::iterator::iterator_category iterator_category;
-        typedef typename _Ty::iterator::value_type value_type;
-        typedef typename _Ty::iterator::reference reference;
-        typedef typename _Ty::iterator::pointer pointer;
-        typedef typename _Ty::const_iterator::reference const_reference;
-        typedef typename _Ty::const_iterator::pointer const_pointer;
-        typedef typename _Ty::iterator::difference_type difference_type;
+#ifdef HAVE_ITERATOR_TRAITS
+        typedef typename std::iterator_traits<_Ty>::iterator_category iterator_category;
+        typedef typename std::iterator_traits<_Ty>::value_type value_type;
+        typedef typename std::iterator_traits<_Ty>::difference_type difference_type;
+        typedef typename std::iterator_traits<_Ty>::pointer pointer;
+        typedef typename std::iterator_traits<_Ty>::reference reference;
+#else
+        typedef typename _Ty::iterator_category iterator_category;
+        typedef typename _Ty::value_type value_type;
+        typedef typename _Ty::difference_type difference_type;
+        typedef typename _Ty::pointer pointer;
+        typedef typename _Ty::reference reference;
+#endif
+        typedef typename Container::const_reference const_reference;
+        typedef typename Container::const_pointer const_pointer;
 
         XMLObjectChildrenIterator() {
         }
 
-        XMLObjectChildrenIterator(typename _Ty::iterator iter) {
+        XMLObjectChildrenIterator(_Ty iter) {
             m_iter=iter;
         }
 
@@ -111,7 +121,7 @@ namespace xmltooling {
         }
 
         const_reference operator->() const {
-            return *(m_iter.operator->());
+            return *m_iter;
         }
 
         XMLObjectChildrenIterator& operator++() {
@@ -181,9 +191,237 @@ namespace xmltooling {
                    // test for iterator inequality
                    return (!(m_iter == _Right.m_iter));
            }
+           
+           bool operator<(const XMLObjectChildrenIterator &_Right) const {
+               return (m_iter < _Right.m_iter);
+           }
         /// @endcond
     };
 
+#ifndef HAVE_ITERATOR_TRAITS
+    /**
+     * STL iterator that mediates access to an iterator that's a pointer.
+     *
+     * @param Container type of container
+     * @param _Ty       the type of object being referenced
+     */
+    template <class Container, typename _Ty>
+    class XMLObjectChildrenIterator<Container, _Ty*>
+    {
+        /// @cond OFF
+        typename _Ty* m_iter;
+        template <class _Tx, class _Tz> friend class XMLObjectChildrenList;
+        template <class _Tx, class _Tz> friend class XMLObjectPairList;
+    public:
+        typedef std::random_access_iterator_tag iterator_category;
+        typedef _Ty value_type;
+        typedef ptrdiff_t difference_type;
+        typedef _Ty* pointer;
+        typedef _Ty& reference;
+        typedef const _Ty& const_reference;
+        typedef const _Ty* const_pointer;
+
+        XMLObjectChildrenIterator() {
+        }
+
+        XMLObjectChildrenIterator(_Ty* iter) {
+            m_iter=iter;
+        }
+
+        const_reference operator*() const {
+            return *m_iter;
+        }
+
+        const_reference operator->() const {
+            return *m_iter;
+        }
+
+        XMLObjectChildrenIterator& operator++() {
+            // preincrement
+            ++m_iter;
+            return (*this);
+        }
+
+        XMLObjectChildrenIterator& operator--() {
+            // predecrement
+            --m_iter;
+            return (*this);
+        }
+
+        XMLObjectChildrenIterator operator++(int) {
+            // postincrement
+            XMLObjectChildrenIterator _Tmp = *this;
+            ++*this;
+            return (_Tmp);
+        }
+
+        XMLObjectChildrenIterator operator--(int) {
+            // postdecrement
+            XMLObjectChildrenIterator _Tmp = *this;
+            --*this;
+            return (_Tmp);
+        }
+
+        XMLObjectChildrenIterator& operator+=(difference_type _Off) {
+            // increment by integer
+            m_iter += _Off;
+            return (*this);
+        }
+
+        XMLObjectChildrenIterator operator+(difference_type _Off) const {
+            // return this + integer
+            return m_iter + _Off;
+        }
+
+        XMLObjectChildrenIterator& operator-=(difference_type _Off) {
+            // decrement by integer
+            return (*this += -_Off);
+        }
+
+        XMLObjectChildrenIterator operator-(difference_type _Off) const {
+            // return this - integer
+            XMLObjectChildrenIterator _Tmp = *this;
+            return (_Tmp -= _Off);
+        }
+
+        difference_type operator-(const XMLObjectChildrenIterator& _Right) const {
+            // return difference of iterators
+            return m_iter - _Right.m_iter;
+        }
+
+        const_reference operator[](difference_type _Off) const {
+            // subscript
+            return (*(*this + _Off));
+        }
+
+        bool operator==(const XMLObjectChildrenIterator &_Right) const {
+                   // test for iterator equality
+                   return (m_iter == _Right.m_iter);
+           }
+
+           bool operator!=(const XMLObjectChildrenIterator &_Right) const {
+                   // test for iterator inequality
+                   return (!(m_iter == _Right.m_iter));
+           }
+           
+           bool operator<(const XMLObjectChildrenIterator &_Right) const {
+               return (m_iter < _Right.m_iter);
+           }
+        /// @endcond
+    };
+
+    /**
+     * STL iterator that mediates access to an iterator that's a const pointer.
+     *
+     * @param Container type of container
+     * @param _Ty       the type of object being referenced
+     */
+    template <class Container, typename _Ty>
+    class XMLObjectChildrenIterator<Container, const _Ty*>
+    {
+        /// @cond OFF
+        typename const _Ty* m_iter;
+        template <class _Tx, class _Tz> friend class XMLObjectChildrenList;
+        template <class _Tx, class _Tz> friend class XMLObjectPairList;
+    public:
+        typedef std::random_access_iterator_tag iterator_category;
+        typedef _Ty value_type;
+        typedef ptrdiff_t difference_type;
+        typedef const _Ty* pointer;
+        typedef const _Ty& reference;
+        typedef const _Ty& const_reference;
+        typedef const _Ty* const_pointer;
+
+        XMLObjectChildrenIterator() {
+        }
+
+        XMLObjectChildrenIterator(_Ty* iter) {
+            m_iter=iter;
+        }
+
+        const_reference operator*() const {
+            return *m_iter;
+        }
+
+        const_reference operator->() const {
+            return *m_iter;
+        }
+
+        XMLObjectChildrenIterator& operator++() {
+            // preincrement
+            ++m_iter;
+            return (*this);
+        }
+
+        XMLObjectChildrenIterator& operator--() {
+            // predecrement
+            --m_iter;
+            return (*this);
+        }
+
+        XMLObjectChildrenIterator operator++(int) {
+            // postincrement
+            XMLObjectChildrenIterator _Tmp = *this;
+            ++*this;
+            return (_Tmp);
+        }
+
+        XMLObjectChildrenIterator operator--(int) {
+            // postdecrement
+            XMLObjectChildrenIterator _Tmp = *this;
+            --*this;
+            return (_Tmp);
+        }
+
+        XMLObjectChildrenIterator& operator+=(difference_type _Off) {
+            // increment by integer
+            m_iter += _Off;
+            return (*this);
+        }
+
+        XMLObjectChildrenIterator operator+(difference_type _Off) const {
+            // return this + integer
+            return m_iter + _Off;
+        }
+
+        XMLObjectChildrenIterator& operator-=(difference_type _Off) {
+            // decrement by integer
+            return (*this += -_Off);
+        }
+
+        XMLObjectChildrenIterator operator-(difference_type _Off) const {
+            // return this - integer
+            XMLObjectChildrenIterator _Tmp = *this;
+            return (_Tmp -= _Off);
+        }
+
+        difference_type operator-(const XMLObjectChildrenIterator& _Right) const {
+            // return difference of iterators
+            return m_iter - _Right.m_iter;
+        }
+
+        const_reference operator[](difference_type _Off) const {
+            // subscript
+            return (*(*this + _Off));
+        }
+
+        bool operator==(const XMLObjectChildrenIterator &_Right) const {
+                   // test for iterator equality
+                   return (m_iter == _Right.m_iter);
+           }
+
+           bool operator!=(const XMLObjectChildrenIterator &_Right) const {
+                   // test for iterator inequality
+                   return (!(m_iter == _Right.m_iter));
+           }
+           
+           bool operator<(const XMLObjectChildrenIterator &_Right) const {
+               return (m_iter < _Right.m_iter);
+           }
+        /// @endcond
+    };
+#endif
+
     /**
      * STL-compatible container that mediates access to underlying lists of typed XML children.
      * @param _Tx   the subtype container to encapsulate
@@ -206,8 +444,8 @@ namespace xmltooling {
         typedef typename Container::size_type size_type;
 
         // We override the iterator types with our constrained wrapper.
-        typedef XMLObjectChildrenIterator<Container> iterator;
-        typedef XMLObjectChildrenIterator<Container> const_iterator;
+        typedef XMLObjectChildrenIterator<Container, typename Container::iterator> iterator;
+        typedef XMLObjectChildrenIterator<Container, typename Container::const_iterator> const_iterator;
         /// @endcond
 
         /**
@@ -250,12 +488,12 @@ namespace xmltooling {
 
         const_iterator begin() const {
             // return iterator for beginning of const sequence
-            return m_container.begin();
+            return const_cast<const Container&>(m_container).begin();
         }
 
         const_iterator end() const {
             // return iterator for end of const sequence
-            return m_container.end();
+            return const_cast<const Container&>(m_container).end();
         }
 
         const_reference at(size_type _Pos) const {
@@ -320,7 +558,7 @@ namespace xmltooling {
         void removeParent(const_reference _Val) {
             if (_Val->getParent()!=m_parent)
                 throw XMLObjectException("Child object not owned by this parent.");
-            _Val->setParent(NULL);
+            _Val->setParent(nullptr);
             m_parent->releaseParentDOM(true);
         }
 
@@ -360,8 +598,8 @@ namespace xmltooling {
         typedef typename Container::size_type size_type;
 
         // We override the iterator types with our constrained wrapper.
-        typedef XMLObjectChildrenIterator<Container> iterator;
-        typedef XMLObjectChildrenIterator<Container> const_iterator;
+        typedef XMLObjectChildrenIterator<Container, typename Container::iterator> iterator;
+        typedef XMLObjectChildrenIterator<Container, typename Container::const_iterator> const_iterator;
         /// @endcond
 
         /**
@@ -404,12 +642,12 @@ namespace xmltooling {
 
         const_iterator begin() const {
             // return iterator for beginning of const sequence
-            return m_container.begin();
+            return const_cast<const Container&>(m_container).begin();
         }
 
         const_iterator end() const {
             // return iterator for end of const sequence
-            return m_container.end();
+            return const_cast<const Container&>(m_container).end();
         }
 
         const_reference at(size_type _Pos) const {
@@ -482,9 +720,9 @@ namespace xmltooling {
         void removeParent(const_reference _Val) {
             if (_Val.first->getParent()!=m_parent || (_Val.second && _Val.second->getParent()!=m_parent))
                 throw XMLObjectException("One of the child objects not owned by this parent.");
-            _Val.first->setParent(NULL);
+            _Val.first->setParent(nullptr);
             if (_Val.second)
-                _Val.second->setParent(NULL);
+                _Val.second->setParent(nullptr);
             m_parent->releaseParentDOM(true);
         }