| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

RawBuffer.cc

Go to the documentation of this file.
00001 #include "RawData/RawBuffer.h"
00002 #include <string.h>
00003 #include <cstdlib>
00004 
00005 DayaBay::RawBuffer::RawBuffer()
00006   : m_buffer(0),
00007     m_bufferSize(0),
00008     m_isOwner(false)
00009 {
00010   // Default constructor
00011 }
00012 
00013 DayaBay::RawBuffer::RawBuffer(unsigned int* buffer,
00014                               bool isOwner/*=false*/, 
00015                               unsigned int bufferSize/*=0*/)
00016   : m_buffer(buffer),
00017     m_bufferSize(bufferSize),
00018     m_isOwner(isOwner)
00019 {
00020   // Standard constructor
00021 }
00022 
00023 DayaBay::RawBuffer::RawBuffer(DayaBay::RawBuffer& buffer,
00024                               bool adopt/*=false*/)
00025   : m_buffer(0),
00026     m_bufferSize(0),
00027     m_isOwner(false)
00028 {
00029   // Construct from another RawBuffer
00030   // If adopt==true, then take over ownership of the 
00031   // memory buffer (if the input buffer is the current owner)
00032   m_buffer = buffer.buffer();
00033   m_isOwner = false;
00034   m_bufferSize = buffer.bufferSize();
00035   if(adopt && buffer.isOwner()){
00036     // Make new RawBuffer the owner of this memory
00037     m_isOwner = true;
00038     buffer.setOwner(false);
00039   }
00040 }
00041 
00042 
00043 DayaBay::RawBuffer::~RawBuffer()
00044 {
00045   // Destructor
00046   if(m_isOwner && m_buffer) free(m_buffer);
00047   m_buffer=0;
00048   m_bufferSize=0;
00049   m_isOwner=false;
00050 }
00051 
00052 unsigned int* DayaBay::RawBuffer::buffer() const
00053 {
00054   // Return the pointer to the internal memory buffer
00055   return m_buffer;
00056 }
00057 
00058 unsigned int DayaBay::RawBuffer::bufferSize() const
00059 {
00060   // Return the allocated size of the internal memory buffer, if known
00061   return m_bufferSize;
00062 }
00063 
00064 bool DayaBay::RawBuffer::isOwner() const
00065 {
00066   // Does this RawBuffer own this memory buffer?
00067   return m_isOwner;
00068 }
00069 
00070 void DayaBay::RawBuffer::setOwner(bool isOwner/*=true*/){
00071   m_isOwner = isOwner;
00072 }
00073 
00074 bool DayaBay::RawBuffer::isValid() const
00075 {
00076   // Does this RawBuffer contain data?
00077   return (m_buffer != 0);
00078 }
00079 
00080 unsigned int* DayaBay::RawBuffer::getPointer(const unsigned int& line) const
00081 {
00082   // Return a pointer to a specific line in the data block
00083   if(m_bufferSize>0 && line>=m_bufferSize) return 0;
00084   return (m_buffer + line);
00085 }
00086 
00087 unsigned int DayaBay::RawBuffer::getValue(const unsigned int& line,
00088                                           const unsigned int& firstBit/*=0*/,
00089                                           const unsigned int& bitLength/*=32*/)   const
00090 {
00091   // Read a value from the memory buffer
00092   // line = line number in the memory block (line = integer length)
00093   // firstBit = first bit to read (max=31)
00094   // bitLength =  length of bits to read (max=32)
00095   if(bitLength>32 || firstBit>31) return 0;
00096   unsigned int* pointer = this->getPointer(line);
00097   if(!pointer) return 0;
00098   // Catch request for full line
00099   if(firstBit==0 && bitLength==32) return *pointer;
00100   // Prepare bit mask
00101   unsigned int oneBit = 0x1;
00102   unsigned int mask = 0;
00103   for(unsigned int shift=0; shift<bitLength; shift++) 
00104     mask += (oneBit << shift);
00105   // Shift value, mask, and return
00106   return (*pointer >> firstBit) & mask;
00107 }
00108 
00109 char* DayaBay::RawBuffer::getString(const unsigned int& line,
00110                                     const unsigned int& byteOffset/*=0*/) const
00111 {
00112   // Return a char pointer to a specific byte in the data block
00113   // line = line number in the memory block (line = integer length)
00114   // byteOffset = offset if string starts at an uneven byte position (max=3)
00115   if(byteOffset>3) return 0;
00116   unsigned int* pointer = this->getPointer(line);
00117   if(!pointer) return 0;
00118   return (char*)(pointer + byteOffset);
00119 }
00120 
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:07:27 2011 for RawData by doxygen 1.4.7