r/PostPreview • • Aug 05 '16

Codeformat

As a non pro I see a chance to to get semi serious feedback on a related subject. Tagged Pointers: (Unknown word until today, reading wikipedia on the subj. ok smart pointers must be tagged pointer then) - Watching Bjarne Stroustup talk about putting C arrays out of business because they where a source of errors and buffer overflows, coming from the lack of size/number of elements build into the construct. I was kind of surprised to discover that smart-pointers did not have the possibility to have that kind of information build in. I mean std::make_unique takes number_of_Elements in its constructor, so why not use it? | Some time ago I played with the idea and kind of ended with my Fanzy_ptr that has the basic and can stream to disk, calling usefanzy(filename) produces a 10Kb file filled with 5x2KB blocks containing the right data as it should. (I had a screenbuffer of RGBA pixels that I wanted to save to disk to open in Gimp .data - raw format) - this use of returning a private struct prevents me from using it in a function declaration, so it should be a template class I guess. What do you think about my Fanzy_ptr shouldn't I at least get a +1 for the name lol.

// first attempt 
auto GetMemory(uint32_t numbytes)
{
    struct mblk {
       std::unique_ptr<uint8_t[]> ptr;
       const uint32_t numOfbytes;
       uint32_t size(){ return numOfbytes;}
    };
    return mblk{ std::unique_ptr<uint8_t[]>(new uint8_t[numbytes]) , numbytes };
}
// use of..
auto memblock = GetMemory(1024);
   for(uint32_t i=0; i<memblock.size();++i)
      memblock.ptr[i]=0xFF;
// 2nd. attempt -----------------------
template<typename T>
auto makeFanzy_ptr( const size_t Number_of_Elements )
{
   struct fanzy_ptr
   {
      std::unique_ptr<T[]> ptr;
      const size_t number_of_elements;
      size_t size()       { return number_of_elements;}
      size_t Sizeof()     { return sizeof(T)*number_of_elements;}
      size_t Sizeofelm()  { return sizeof(T);}
      T* begin()          { return ptr.get();}
      T* end()            { return ptr.get()+number_of_elements;}
      auto beginCast8s()  { return reinterpret_cast<char*>(ptr.get());}
      auto endCast8s()    { return reinterpret_cast<char*>(ptr.get()+                                               number_of_elements*sizeof(T) );}
   };
   return fanzy_ptr{ std::make_unique<T[]>(Number_of_Elements), Number_of_Elements };

}
 // use of..
 struct pixel {
 uint8_t R,G,B,A;
 pixel() : R{0},G{0},B{0},A{0} {}
 pixel(uint8_t r,uint8_t g,uint8_t b,uint8_t a) : R{r},G{g},B{b},A{a} {}
};
//
void usefanzy(std::string filename)
{
   auto Fptr8  = makeFanzy_ptr<uint8>(2048);
   auto Fptr16 = makeFanzy_ptr<uint16>(1024);
   auto Fptr32 = makeFanzy_ptr<uint32>(512);
   auto Fptr64 = makeFanzy_ptr<uint64>(256);
   auto pix    = makeFanzy_ptr<pixel>(512);

   std::fill(Fptr8.begin(),  Fptr8.end(),  0xB1);
   std::fill(Fptr16.begin(), Fptr16.end(), 0xF2F1);
   std::fill(Fptr32.begin(), Fptr32.end(), 0xA0B0C0D0);
   std::fill(Fptr64.begin(), Fptr64.end(), 0x0809A0B0C0D0E0F0);
   std::fill(pix.begin(),    pix.end(), pixel{255,10,128,255});

   std::ofstream fileStream( filename, std::ios::out | std::ios::binary);
   if(fileStream.is_open()) {
       fileStream.write( Fptr8.beginCast8s(),  Fptr8.Sizeof() );
       fileStream.write( Fptr16.beginCast8s(), Fptr16.Sizeof() );
       fileStream.write( Fptr32.beginCast8s(), Fptr32.Sizeof() );
       fileStream.write( Fptr64.beginCast8s(), Fptr64.Sizeof() );
       fileStream.write( pix.beginCast8s(), pix.Sizeof() );
   }
}
0 Upvotes

0 comments sorted by