Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. /*
  2.  * Block.h
  3.  *
  4.  *  Created on: 23/04/2015
  5.  *      Author: Matt
  6.  */
  7. #include "stdlib.h"
  8. #include <stdexcept>
  9. #include <assert.h>
  10. #include <iostream>
  11.  
  12. #ifndef BLOCK_H_
  13. #define BLOCK_H_
  14.  
  15. #define BLOCK_SIZE 8
  16.  
  17. template<class T>
  18. class Block {
  19. public:
  20.     typedef int row[BLOCK_SIZE];
  21.  
  22.     Block() {
  23.         CELL_VALUE = NULL;
  24.         allocate();
  25.     }
  26.  
  27.     ~Block() {
  28.         destroy();
  29.     }
  30.  
  31.     row& operator[](std::size_t i) {
  32.         assert(i < BLOCK_SIZE);
  33.         return m_data[i];
  34.     }
  35.  
  36. private:
  37.     void allocate() {
  38.         CELL_VALUE = new T*[BLOCK_SIZE];
  39.         for (int i = 0; i < BLOCK_SIZE; i++) {
  40.             CELL_VALUE[i] = new T[BLOCK_SIZE];
  41.         }
  42.     }
  43.  
  44.     void destroy() {
  45.         if (NULL == CELL_VALUE)
  46.             return;
  47.         for (int i = 0; i < BLOCK_SIZE; i++) {
  48.             delete[] CELL_VALUE[i];
  49.         }
  50.         delete[] CELL_VALUE;
  51.         CELL_VALUE = NULL;
  52.     }
  53.  
  54.     row m_data[BLOCK_SIZE];
  55.     T** CELL_VALUE;
  56. };
  57.  
  58. #endif /* BLOCK_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement