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 "Eigen/src/StlSupport/details.h"
     14 
     15 // Define the explicit instantiation (e.g. necessary for the Intel compiler)
     16 #if defined(__INTEL_COMPILER) || defined(__GNUC__)
     17   #define EIGEN_EXPLICIT_STL_LIST_INSTANTIATION(...) template class std::list<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> >;
     18 #else
     19   #define EIGEN_EXPLICIT_STL_LIST_INSTANTIATION(...)
     20 #endif
     21 
     22 /**
     23  * This section contains a convenience MACRO which allows an easy specialization of
     24  * std::list such that for data types with alignment issues the correct allocator
     25  * is used automatically.
     26  */
     27 #define EIGEN_DEFINE_STL_LIST_SPECIALIZATION(...) \
     28 EIGEN_EXPLICIT_STL_LIST_INSTANTIATION(__VA_ARGS__) \
     29 namespace std \
     30 { \
     31   template<typename _Ay> \
     32   class list<__VA_ARGS__, _Ay>  \
     33     : public list<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > \
     34   { \
     35     typedef list<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__> > list_base; \
     36   public: \
     37     typedef __VA_ARGS__ value_type; \
     38     typedef typename list_base::allocator_type allocator_type; \
     39     typedef typename list_base::size_type size_type;  \
     40     typedef typename list_base::iterator iterator;  \
     41     explicit list(const allocator_type& a = allocator_type()) : list_base(a) {}  \
     42     template<typename InputIterator> \
     43     list(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) : list_base(first, last, a) {} \
     44     list(const list& c) : list_base(c) {}  \
     45     explicit list(size_type num, const value_type& val = value_type()) : list_base(num, val) {} \
     46     list(iterator start, iterator end) : list_base(start, end) {}  \
     47     list& operator=(const list& x) {  \
     48       list_base::operator=(x);  \
     49       return *this;  \
     50     } \
     51   }; \
     52 }
     53 
     54 // check whether we really need the std::vector specialization
     55 #if !(defined(_GLIBCXX_VECTOR) && (!EIGEN_GNUC_AT_LEAST(4,1))) /* Note that before gcc-4.1 we already have: std::list::resize(size_type,const T&). */
     56 
     57 namespace std
     58 {
     59 
     60 #define EIGEN_STD_LIST_SPECIALIZATION_BODY \
     61   public:  \
     62     typedef T value_type; \
     63     typedef typename list_base::allocator_type allocator_type; \
     64     typedef typename list_base::size_type size_type;  \
     65     typedef typename list_base::iterator iterator;  \
     66     typedef typename list_base::const_iterator const_iterator;  \
     67     explicit list(const allocator_type& a = allocator_type()) : list_base(a) {}  \
     68     template<typename InputIterator> \
     69     list(InputIterator first, InputIterator last, const allocator_type& a = allocator_type()) \
     70     : list_base(first, last, a) {} \
     71     list(const list& c) : list_base(c) {}  \
     72     explicit list(size_type num, const value_type& val = value_type()) : list_base(num, val) {} \
     73     list(iterator start, iterator end) : list_base(start, end) {}  \
     74     list& operator=(const list& x) {  \
     75     list_base::operator=(x);  \
     76     return *this; \
     77   }
     78 
     79   template<typename T>
     80   class list<T,EIGEN_ALIGNED_ALLOCATOR<T> >
     81     : public list<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
     82                   Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> >
     83   {
     84     typedef list<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T),
     85                  Eigen::aligned_allocator_indirection<EIGEN_WORKAROUND_MSVC_STL_SUPPORT(T)> > list_base;
     86     EIGEN_STD_LIST_SPECIALIZATION_BODY
     87 
     88     void resize(size_type new_size)
     89     { resize(new_size, T()); }
     90 
     91     void resize(size_type new_size, const value_type& x)
     92     {
     93       if (list_base::size() < new_size)
     94         list_base::insert(list_base::end(), new_size - list_base::size(), x);
     95       else
     96         while (new_size < list_base::size()) list_base::pop_back();
     97     }
     98 
     99 #if defined(_LIST_)
    100     // workaround MSVC std::list implementation
    101     void push_back(const value_type& x)
    102     { list_base::push_back(x); }
    103     using list_base::insert;
    104     iterator insert(const_iterator position, const value_type& x)
    105     { return list_base::insert(position,x); }
    106     void insert(const_iterator position, size_type new_size, const value_type& x)
    107     { list_base::insert(position, new_size, x); }
    108 #endif
    109   };
    110 }
    111 
    112 #endif // check whether specialization is actually required
    113 
    114 #endif // EIGEN_STDLIST_H
    115