Advertisement
Guest User

shared_array2

a guest
Oct 11th, 2011
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <boost/checked_delete.hpp>
  4. #include <boost/range/iterator_range.hpp>
  5. #include <boost/smart_ptr/shared_ptr.hpp>
  6. #include <boost/type_traits/is_class.hpp>
  7. #include <boost/utility/enable_if.hpp>
  8.  
  9. namespace boost {
  10.  
  11. template<class T>
  12. class shared_array2 : public boost::iterator_range<T*>
  13. {
  14. public:
  15.     shared_array2()
  16.     {
  17.     }
  18.  
  19.     explicit shared_array2(size_t sz)
  20.     {
  21.         shared_ptr<T> n(new T[sz], checked_array_deleter<T>());
  22.         static_cast<base_t&>(*this) = base_t(n.get(), n.get() + sz);
  23.         n_ = n;
  24.     }
  25.  
  26.     template<class V>
  27.     shared_array2(const V& v, typename enable_if<typename is_class<V>>::type* = 0) :
  28.         base_t(v.data(), v.data() + v.size()),
  29.         n_(v.n())
  30.     {
  31.     }
  32.  
  33.     shared_array2(T* b, T* e, shared_ptr<void> const& n) :
  34.         base_t(b, e),
  35.         n_(n)
  36.     {
  37.     }
  38.  
  39.     shared_array2(T* b, size_t sz, shared_ptr<void> const& n) :
  40.     base_t(b, b + sz),
  41.         n_(n)
  42.     {
  43.     }
  44.  
  45.     T* data() const
  46.     {
  47.         return base_t::begin();
  48.     }
  49.  
  50.     shared_ptr<void> const& n() const
  51.     {
  52.         return n_;
  53.     }
  54.  
  55.     shared_array2 substr(size_t ofs, size_t sz) const
  56.     {
  57.         return shared_array2(data() + ofs, sz, n());
  58.     }
  59. private:
  60.     typedef boost::iterator_range<T*> base_t;
  61.  
  62.     shared_ptr<void> n_;
  63. };
  64.  
  65. typedef shared_array2<const unsigned char> shared_data;
  66. typedef shared_array2<unsigned char> shared_mutable_data;
  67. typedef shared_array2<const unsigned char> shared_str;
  68. typedef shared_array2<unsigned char> shared_mutable_str;
  69.  
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement