Home | History | Annotate | Download | only in StlSupport
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2009 Hauke Heibel <hauke.heibel (at) googlemail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #ifndef EIGEN_STDLIST_H
     11 #define EIGEN_STDLIST_H
     12 
     13 #include "details.h"
     14 
     15 /**
     16  * This section contains a convenience MACRO which allows an easy specialization of
     17  * std::list such that for data types with alignment issues the correct allocator
     18  * is used automatically.
     19  */
     20 #define EIGEN_DEFINE_STL_LIST_SPECIALIZATION(...) \
     21 namespace std \
     22 { \
     23   template<> \
     24   class list<__VA_ARGS__, std::allocator<__VA_ARGS__> >           \
     25     : public list<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \
     26   { \
     27     typedef list<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > list_base; \
     28   public: \
     29     typedef __VA_ARGS__ value_type; \
     30     typedef list_base::allocator_type allocator_type; \
     31     typedef list_base::size_type size_type;  \
     32     typedef list_base::iterator iterator;  \
     33     explicit list(const allocator_type& a = allocator_type()) : list_base(a) {}  \
     34     template<typename InputIterator> \
     35     list(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : list_base(first, last, a) {} \
     36     list(const list& c) : list_base(c) {}  \
     37     explicit list(size_type num, const value_type& val = value_type()) : list_base(num, val) {} \
     38     list(iterator start, iterator end) : list_base(start, end) {}  \
     39     list& operator=(const list& x) {  \
     40       list_base::operator=(x);  \
     41       return *this;  \
     42     } \
     43   }; \
     44 }
     45 
     46 // check whether we really need the std::list specialization
     47 #if !EIGEN_HAS_CXX11_CONTAINERS && !(defined(_GLIBCXX_LIST) && (!EIGEN_GNUC_AT_LEAST(4,1))) /* Note that before gcc-4.1 we already have: std::list::resize(size_type,const T&). */
     48 
     49 namespace std
     50 {
     51 
     52 #define EIGEN_STD_LIST_SPECIALIZATION_BODY \
     53   public:  \
     54     typedef T value_type; \
     55     typedef typename list_base::allocator_type allocator_type; \
     56     typedef typename list_base::size_type size_type;  \
     57     typedef typename list_base::iterator iterator;  \
     58     typedef typename list_base::const_iterator const_iterator;  \
     59     explicit list(const allocator_type& a = allocator_type()) : list_base(a) {}  \
     60     template<typename InputIterator> \
     61     list(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \
     62     : list_base(first, last, a) {} \
     63     list(const list& c) : list_base(c) {}  \
     64     explicit list(size_type num, const value_type& val = value_type()) : list_base(num, val) {} \
     65     list(iterator start, iterator end) : list_base(start, end) {}  \
     66     list& operator=(const list& x) {  \
     67     list_base::operator=(x);  \
     68     return *this; \
     69   }
     70 
     71   template<typename T>
     72   class list<T,EIGEN_ALIGNED_ALLOCATOR<T> >
     73     : public list<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
     74                   Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> >
     75   {
     76     typedef list<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
     77                  Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > list_base;
     78     EIGEN_STD_LIST_SPECIALIZATION_BODY
     79 
     80     void resize(size_type new_size)
     81     { resize(new_size, T()); }
     82 
     83     void resize(size_type new_size, const value_type& x)
     84     {
     85       if (list_base::size() < new_size)
     86         list_base::insert(list_base::end(), new_size - list_base::size(), x);
     87       else
     88         while (new_size < list_base::size()) list_base::pop_back();
     89     }
     90 
     91 #if defined(_LIST_)
     92     // workaround MSVC std::list implementation
     93     void push_back(const value_type& x)
     94     { list_base::push_back(x); }
     95     using list_base::insert;
     96     iterator insert(const_iterator position, const value_type& x)
     97     { return list_base::insert(position,x); }
     98     void insert(const_iterator position, size_type new_size, const value_type& x)
     99     { list_base::insert(position, new_size, x); }
    100 #endif
    101   };
    102 }
    103 
    104 #endif // check whether specialization is actually required
    105 
    106 #endif // EIGEN_STDLIST_H
    107