devector.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2013 Vicente J. Botet Escriba
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // 2013/10 Vicente J. Botet Escriba
  7. // Creation.
  8. #ifndef BOOST_CSBL_DEVECTOR_HPP
  9. #define BOOST_CSBL_DEVECTOR_HPP
  10. #include <boost/config.hpp>
  11. #include <boost/thread/csbl/vector.hpp>
  12. #include <boost/move/detail/move_helpers.hpp>
  13. namespace boost
  14. {
  15. namespace csbl
  16. {
  17. template <class T>
  18. class devector
  19. {
  20. typedef csbl::vector<T> vector_type;
  21. vector_type data_;
  22. std::size_t front_index_;
  23. BOOST_COPYABLE_AND_MOVABLE(devector)
  24. template <class U>
  25. void priv_push_back(BOOST_FWD_REF(U) x)
  26. { data_.push_back(boost::forward<U>(x)); }
  27. public:
  28. typedef typename vector_type::size_type size_type;
  29. typedef typename vector_type::reference reference;
  30. typedef typename vector_type::const_reference const_reference;
  31. devector() : front_index_(0) {}
  32. devector(devector const& x) BOOST_NOEXCEPT
  33. : data_(x.data_),
  34. front_index_(x.front_index_)
  35. {}
  36. devector(BOOST_RV_REF(devector) x) BOOST_NOEXCEPT
  37. : data_(boost::move(x.data_)),
  38. front_index_(x.front_index_)
  39. {}
  40. devector& operator=(BOOST_COPY_ASSIGN_REF(devector) x)
  41. {
  42. if (&x != this)
  43. {
  44. data_ = x.data_;
  45. front_index_ = x.front_index_;
  46. }
  47. return *this;
  48. }
  49. devector& operator=(BOOST_RV_REF(devector) x)
  50. #if defined BOOST_THREAD_USES_BOOST_VECTOR
  51. BOOST_NOEXCEPT_IF(vector_type::allocator_traits_type::propagate_on_container_move_assignment::value)
  52. #endif
  53. {
  54. data_ = boost::move(x.data_);
  55. front_index_ = x.front_index_;
  56. return *this;
  57. }
  58. bool empty() const BOOST_NOEXCEPT
  59. { return data_.size() == front_index_; }
  60. size_type size() const BOOST_NOEXCEPT
  61. { return data_.size() - front_index_; }
  62. reference front() BOOST_NOEXCEPT
  63. { return data_[front_index_]; }
  64. const_reference front() const BOOST_NOEXCEPT
  65. { return data_[front_index_]; }
  66. reference back() BOOST_NOEXCEPT
  67. { return data_.back(); }
  68. const_reference back() const BOOST_NOEXCEPT
  69. { return data_.back(); }
  70. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  71. void pop_front()
  72. {
  73. ++front_index_;
  74. if (empty()) {
  75. data_.clear();
  76. front_index_=0;
  77. }
  78. }
  79. };
  80. }
  81. }
  82. #endif // header