[ create a new paste ] login | about

Project: boost
Link: http://boost.codepad.org/X4NzcJoS    [ raw code | fork ]

joel_f - C++, pasted on Aug 26:
struct some_exception {};

template<class T,size_t N> struct aligned_ptr
{
  public:
  typedef aligned_ptr<T,N>                                      this_type;
  typedef T                                                     element_type;
  typedef T                                                     value_type;
  typedef T*                                                    pointer;
  typedef typename details::aligned_ptr_reference<T>::type      reference;
  typedef typename meta::integer<sizeof(void*),unsigned>::type  address_type;
  typedef details::alignement_traits<T,N>                       align_traits;
  BOOST_STATIC_CONSTANT( address_type, alignement = align_traits::alignement );

            aligned_ptr()                   : mData(0)        {}
            aligned_ptr(this_type const& p) : mData(p.get())  {}
  explicit  aligned_ptr(T* p) { reset(p); }

  this_type& operator=(this_type const& p)
  {
    reset(p);
    return *this;
  }

  void swap(this_type& other) { std::swap(mData,other.mData); }
  void reset( this_type const& p ) { mData = p.get(); }
  void reset( T* p )
  {
    if( (address_type)(p)%alignement )
      throw some_exception;
    else
      mData=p;
  }

  reference operator*  () const { return *mData; }
  pointer   operator-> () const { return mData;  }
  pointer   get()         const { return mData;  }

  typedef T * (this_type::*unspecified_bool_type)() const;
  operator unspecified_bool_type() const // never throws
  {
    return mData == 0 ? 0: &this_type::get;
  }

  private:
  T* mData;
};


Create a new paste based on this one


Comments: