/* Copyright (c) 1997-2006
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program 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 General Public License for more details.
*/

#ifndef _POLYMAKE_STD_LIST
#define _POLYMAKE_STD_LIST "$Project: polymake $$Id: list 7315 2006-04-02 21:37:53Z gawrilow $"

#include_next <list>
#include <cstring>
#include <new>

#define _POLYMAKE_GCC_NEW_STD_LIST defined(__GNUC__) && (__GNUC__>3 || __GNUC_MINOR__>=4)

#if (defined(__INTEL_COMPILER) && !defined(__GNUC__)) || _POLYMAKE_GCC_NEW_STD_LIST
# ifndef _POLYMAKE_ITERATORS_H
#  include <iterators.h>
# endif
#else
# ifndef _POLYMAKE_TYPE_MANIP_H
#  include <type_manip.h>
# endif
#endif

namespace pm {

template <typename Value, typename Alloc>
struct spec_object_traits< std::list<Value,Alloc> >
   : spec_object_traits<is_container> {
   static const IO_separator_kind IO_separator=IO_sep_inherit;
};

template <typename Value, typename Alloc> inline
void relocate(std::list<Value,Alloc>* from, std::list<Value,Alloc>* to)
{
#if _POLYMAKE_GCC_NEW_STD_LIST || _POLYMAKE_ICC_ONLY
   (new(to) std::list<Value,Alloc>())->swap(*from);
   _Destroy(from);
#else
   std::memcpy(to, from, sizeof(*from));
#endif
}

template <typename Value, typename Alloc=std::allocator<Value> >
class std_list_traits : public std::list<Value,Alloc> {
protected:
   std_list_traits();
   ~std_list_traits();
public:
   typedef typename std::list<Value,Alloc>::_Node Node;

#if defined(__GNUC__) || defined(__COMO__)
   static Node* to_node(Value* data)
   {
      return reverse_cast(data, &Node::_M_data);
   }
   static const Node* to_node(const Value* data)
   {
      return reverse_cast(data, &Node::_M_data);
   }
#elif _POLYMAKE_ICC_ONLY
   static Node* to_node(Value* data)
   {
      return reverse_cast(data, &Node::_Myval);
   }
   static Node* to_node(const Value* data)
   {
      return reverse_cast(const_cast<Value*>(data), &Node::_Myval);
   }
#endif
};

#if _POLYMAKE_ICC_ONLY

namespace std_nested_iterators {
   enum { list=2 };

   namespace _impl {
      template <typename Iterator, typename Value>
      typename size_discriminant< derived_from<typename std::list<Value>::iterator, Iterator>::value ? list : unmatched >::type
      analyzer_f(Iterator*, Value*, typename std::list<Value>::const_iterator*, bait*);
   }

   template <typename Iterator, typename Value>
   struct extractor<Iterator, Value, list> {
      typedef typename std::list<Value>::iterator iterator;
      typedef typename std::list<Value>::const_iterator const_iterator;
   };
}

#elif _POLYMAKE_GCC_NEW_STD_LIST

template <typename Value>
struct iterator_cross_const_helper<_GLIBCXX_STD::_List_iterator<Value>, false, true> {
   typedef _GLIBCXX_STD::_List_iterator<Value> iterator;
   typedef _GLIBCXX_STD::_List_const_iterator<Value> const_iterator;
};

template <typename Value>
struct iterator_cross_const_helper<_GLIBCXX_STD::_List_const_iterator<Value>, false, true> {
   typedef _GLIBCXX_STD::_List_iterator<Value> iterator;
   typedef _GLIBCXX_STD::_List_const_iterator<Value> const_iterator;
};

#endif

} // end namespace pm

#endif // _POLYMAKE_STD_LIST

// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
