/* Copyright (c) 1997-2010
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.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.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: list 9432 2010-02-01 23:30:24Z gawrilow $
*/

#ifndef _POLYMAKE_STD_LIST
#define _POLYMAKE_STD_LIST

#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
# include <iterators.h>
#else
# include <type_manip.h>
#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
   (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__)
   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);
   }
#endif
};

#if _POLYMAKE_GCC_NEW_STD_LIST

template <typename Value>
struct iterator_cross_const_helper<_GLIBCXX_STD::_List_iterator<Value>, 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>, 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:
