Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: JaminGrey on Nov 7th, 2012  |  syntax: C++  |  size: 1.46 KB  |  hits: 3  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Copy constructor.
  2.     Grid(const Grid &other) : memory(nullptr)
  3.     {
  4.         //Note: Both Reserve() and copyElements() can throw exceptions. If they succeed properly, they will save the new state
  5.         //to member-variables - but no member-variables should be manually altered within this function before their call.
  6.        
  7.         //Reserve enough memory.
  8.         this->Reserve(other.capacity);
  9.        
  10.         //Copy-construct the elements.
  11.         this->copyElements(other.memory, other.capacity, this->memory, this->capacity, other.bounds, CopyMode::CopyConstructor);
  12.        
  13.         //Save the new bounds (once we are sure copyElements() didn't throw).
  14.         this->bounds = other.bounds;
  15.     }
  16.    
  17.     //Move constructor.
  18.     Grid(Grid &&other) : memory(nullptr)
  19.     {
  20.         this->operator=(std::forward<Grid>(other));
  21.     }
  22.    
  23.     //Assignment operator
  24.     Grid &operator=(const Grid &other)
  25.     {
  26.         Grid<Type> temp(other);
  27.         this->Swap(temp);
  28.         return *this;
  29.     }
  30.    
  31.     //Move-assignment.
  32.     Grid &operator=(Grid &&other)
  33.     {
  34.         this->Swap(other);
  35.         return *this;
  36.     }
  37.    
  38.     //Swaps the contents of this grid with 'other'.
  39.     void Swap(Grid &other) throw()
  40.     {
  41.         //Swap pointers.
  42.         std::swap(this->memory, other.memory);
  43.        
  44.         //Swap bounds and capacity.
  45.         std::swap(this->bounds, other.bounds);
  46.         std::swap(this->capacity, other.capacity);
  47.     }