/search.css" rel="stylesheet" type="text/css"/> /search.js">
00001 /* 00002 * ByteBuffer.cc 00003 * DaqReadoutFormat 00004 * 00005 * Created by Simon Patton on 7/18/10. 00006 * Copyright 2010 DayaBay Collaboration. All rights reserved. 00007 * 00008 */ 00009 00010 #include "DaqReadoutFormat/ByteBuffer.h" 00011 00012 #include <string> 00013 #include <cstring> 00014 00015 using DybDaq::ByteBuffer; 00016 00017 const unsigned int ByteBuffer::BYTES_IN_INT = 4; 00018 00019 ByteBuffer::ByteBuffer(char* contents, 00020 unsigned int capacity) : 00021 m_buffer(contents), 00022 m_capacity(capacity), 00023 m_limit(capacity), 00024 m_position(0), 00025 m_count(1) { 00026 } 00027 00028 ByteBuffer::~ByteBuffer() { 00029 if (0 != m_buffer) { 00030 delete m_buffer; 00031 } 00032 } 00033 00034 const char* ByteBuffer::grab() const { 00035 ++m_count; 00036 return m_buffer + m_position; 00037 } 00038 00039 void ByteBuffer::release() const { 00040 --m_count; 00041 if (0 == m_count) { 00042 delete this; 00043 } 00044 } 00045 00046 const unsigned int ByteBuffer::capacity() const { 00047 return m_capacity; 00048 } 00049 00050 const char* ByteBuffer::cursor() const { 00051 return m_buffer + m_position; 00052 } 00053 00054 const unsigned int ByteBuffer::remaining() const { 00055 return m_limit - m_position; 00056 } 00057 00058 const bool ByteBuffer::hasRemaining() const { 00059 return 0 != remaining(); 00060 } 00061 00062 const unsigned int ByteBuffer::limit() const { 00063 return m_limit; 00064 } 00065 00066 const ByteBuffer* ByteBuffer::limit(unsigned int newLimit) const { 00067 m_limit = newLimit; 00068 return this; 00069 } 00070 00071 const unsigned int ByteBuffer::position() const { 00072 return m_position; 00073 } 00074 00075 const ByteBuffer* ByteBuffer::position(unsigned int newPosition) const { 00076 m_position = newPosition; 00077 return this; 00078 } 00079 00080 const char ByteBuffer::readChar(unsigned int index) const { 00081 return (m_buffer + index)[0]; 00082 } 00083 00084 const unsigned int ByteBuffer::readUnsignedInt(unsigned int index) const { 00085 return *((unsigned int*)(m_buffer + index)); 00086 } 00087 00088 char* ByteBuffer::cursor() { 00089 return m_buffer + m_position; 00090 } 00091 00092 const unsigned int ByteBuffer::put(const char* source, 00093 const unsigned int length) { 00094 const unsigned int spaceLeft = remaining(); 00095 if (length > spaceLeft) { 00096 memcpy(m_buffer + m_position, 00097 source, 00098 spaceLeft); 00099 m_position += spaceLeft; 00100 return length - spaceLeft; 00101 } 00102 memcpy(m_buffer + m_position, 00103 source, 00104 length); 00105 return 0; 00106 }