core/vil/vil_memory_chunk.h
Go to the documentation of this file.
00001 // This is core/vil/vil_memory_chunk.h
00002 #ifndef vil_memory_chunk_h_
00003 #define vil_memory_chunk_h_
00004 //:
00005 //  \file
00006 //  \brief Ref. counted block of data on the heap
00007 //  \author Tim Cootes
00008 
00009 #include <vcl_atomic_count.h>
00010 #include <vcl_cstddef.h>
00011 #include <vil/vil_smart_ptr.h>
00012 #include <vil/vil_pixel_format.h>
00013 
00014 //: Ref. counted block of data on the heap.
00015 //  Image data block used by vil_image_view<T>.
00016 class vil_memory_chunk
00017 {
00018   protected:
00019     //: Data
00020     void *data_;
00021 
00022     //: Number of elements (bytes)
00023     vcl_size_t size_;
00024 
00025     //: Indicate what format data is (used for binary IO)
00026     // Should always be a scalar type.
00027     vil_pixel_format pixel_format_;
00028 
00029     //: Reference count
00030     vcl_atomic_count ref_count_;
00031 
00032  public:
00033     //: Dflt ctor
00034     vil_memory_chunk();
00035 
00036     //: Allocate n bytes of memory
00037     // \param pixel_format indicates what format to be used for binary IO,
00038     // and should always be a scalar type.
00039     vil_memory_chunk(vcl_size_t n, vil_pixel_format pixel_format);
00040 
00041     //: Copy ctor
00042     vil_memory_chunk(const vil_memory_chunk&);
00043 
00044     //: Copy operator
00045     vil_memory_chunk& operator=(const vil_memory_chunk&);
00046 
00047     //: Destructor
00048     virtual ~vil_memory_chunk();
00049 
00050     //: Increment reference count
00051     void ref() { ++ref_count_; }
00052 
00053     //: Decrement reference count
00054     void unref();
00055 
00056     //: Number of objects referring to this data
00057     int ref_count() const { return ref_count_; }
00058 
00059     //: Pointer to first element of data
00060     virtual void* data();
00061 
00062     //: Pointer to first element of data
00063     virtual void* const_data() const;
00064 
00065     //: Indicate what format data is to be saved as in binary IO
00066     vil_pixel_format pixel_format() const { return pixel_format_; }
00067 
00068     //: Number of bytes allocated
00069     vcl_size_t size() const { return size_; }
00070 
00071     //: Create space for n bytes
00072     //  pixel_format indicates what format to be used for binary IO
00073     virtual void set_size(unsigned long n, vil_pixel_format pixel_format);
00074 };
00075 
00076 typedef vil_smart_ptr<vil_memory_chunk> vil_memory_chunk_sptr;
00077 
00078 #endif // vil_memory_chunk_h_