Move catalog handling to parser API from config API.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLObjectChildrenList.h
index f897df0..837d83e 100644 (file)
@@ -1,23 +1,27 @@
-/*
- *  Copyright 2001-2007 Internet2
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
  * @file xmltooling/util/XMLObjectChildrenList.h
  * 
- * STL-compatible container wrapper
+ * STL-compatible container wrapper.
  */
 
 #ifndef __xmltooling_list_h__
@@ -81,28 +85,150 @@ 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:
+#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(_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
+    };
+
+#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 typename std::iterator_traits<typename _Ty::iterator>::iterator_category iterator_category;
-        typedef typename std::iterator_traits<typename _Ty::iterator>::value_type value_type;
-        typedef typename std::iterator_traits<typename _Ty::iterator>::difference_type difference_type;
-        typedef typename std::iterator_traits<typename _Ty::iterator>::pointer pointer;
-        typedef typename std::iterator_traits<typename _Ty::iterator>::reference reference;
-        typedef typename _Ty::const_reference const_reference;
-        typedef typename _Ty::const_pointer const_pointer;
+        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(typename _Ty::iterator iter) {
+        XMLObjectChildrenIterator(_Ty* iter) {
             m_iter=iter;
         }
 
@@ -189,6 +315,118 @@ namespace xmltooling {
     };
 
     /**
+     * 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
      * @param _Ty   the base type in the underlying list (defaults to XMLObject)
@@ -210,8 +448,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
 
         /**
@@ -254,12 +492,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 {
@@ -324,7 +562,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);
         }
 
@@ -364,8 +602,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
 
         /**
@@ -408,12 +646,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 {
@@ -486,9 +724,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);
         }