Guest User

asdasd

a guest
Feb 28th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. template < class Type, uint8_t Len > class my_array
  2. {
  3.     public:
  4.    
  5.     Type* data;
  6.     uint8_t length;
  7.     uint8_t pos;
  8.    
  9.     my_array(){
  10.         data = (Type*) malloc ( sizeof ( Type ) * Len );
  11.         for(uint8_t i=0; i<Len; i++){
  12.             data[i] = 0; //(*(Type*)0);
  13.         };
  14.         length = Len - 1;
  15.         pos = 0;
  16.     };
  17.    
  18.     ~my_array(){
  19.         pos = 0;
  20.         length = 0;
  21.         free(data);
  22.     };
  23.    
  24.     uint8_t insert(Type t){
  25.         if( pos == length ){ return 1; };
  26.         data[pos] = t;
  27.         pos++;
  28.     return 0;
  29.     };
  30.    
  31.     uint8_t remove_last(void){
  32.         if( pos == 0 )return 1;
  33.         pos--;
  34.         data[pos] = 0;
  35.     return 0;
  36.     };
  37.    
  38.     uint8_t size(void){
  39.         return (pos-1);
  40.     }
  41.    
  42.     Type& operator[] (uint8_t i)
  43.     {
  44.         if( (i > 0) && (i < length) ){
  45.             return data[i];
  46.         };
  47.     return (*(Type*)0);
  48.     };
  49.    
  50. };
Advertisement
Add Comment
Please, Sign In to add comment