/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CXmlNode.h  -  description
                             -------------------
    begin                : Sun May 22 2003
    based on the code    : Mikko Lahteenmaki <Laza@Flashmail.com>
    copyright            : (C) 2003-2005 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#ifndef __CXML_NODELIST_H__
#define __CXML_NODELIST_H__

#include <vector>
#include <algorithm>

class CXmlNode;

/// @addtogroup XML
/// @{

/// @brief XML node list
///
/// The CXmlNodeList interface provides the an ordered collection of nodes, 
/// The items in the NodeList are accessible via an integral index, starting from 0.
class CXmlNodeList : public std::vector<CXmlNode *> {
   typedef std::vector<CXmlNode *> CXmlNodeVector;
public:
   /// Constructor
   CXmlNodeList() { }

   /// Item access
   /// @param index unsigned, item index
   /// @returns item pointer
   CXmlNode *item(unsigned index) const { return (*this)[index]; }

   /// Appends an item after the last item
   /// @param item CXmlNode *, item to append
   void append(CXmlNode *item) { push_back(item); }

   /// Inserts an item before the first item
   /// @param item CXmlNode *, item to insert
   void prepend(CXmlNode *item) { CXmlNodeVector::insert(begin(),item); }

   /// Inserts an item
   /// @param pos unsigned, insert position
   /// @param item CXmlNode *, item to insert
   void insert(unsigned pos, CXmlNode *item) { CXmlNodeVector::insert(begin()+pos, item); }

   /// Replaces the item
   /// @param pos unsigned, replace position
   /// @param item CXmlNode *, item to insert
   void replace(unsigned pos, CXmlNode *item) { (*this)[pos] = item; }

   /// Removes the item
   /// @param pos unsigned, remove item position
   void remove(unsigned pos) { erase(begin()+pos); }

   /// Removes the item
   /// @param item CXmlNode *, item to remove
   bool remove(CXmlNode *item) { 
      iterator itor = std::find(begin(),end(),item);
      if (itor == end()) return false;
      erase(itor);
      return true;
   }

   /// Returns the item index in the list
   /// @param item CXmlNode *, item to find
   /// @returns item index, or -1 if not found
   int indexOf(const CXmlNode *item) const { 
      const_iterator itor = std::find(begin(),end(),item);
      if (itor == end()) return -1;
      return std::distance(begin(),itor); 
   }

protected:
   /// Releases item's memory
   void free_item(CXmlNode *item);
};

/// @}

#endif
