/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "ByteStreamRegion.h"
#include "Debugger.h"
#include "DebuggerCoreInterface.h"

//------------------------------------------------------------------------------
// Name: ByteStreamRegion()
// Desc: default constructor
//------------------------------------------------------------------------------
ByteStreamRegion::ByteStreamRegion() {
}

//------------------------------------------------------------------------------
// Name: ByteStreamRegion(const MemoryRegions::Region &r)
// Desc: constructor
//------------------------------------------------------------------------------
ByteStreamRegion::ByteStreamRegion(const MemoryRegions::Region &r) : m_Region(r) {
}

//------------------------------------------------------------------------------
// Name: setRegion(const MemoryRegions::Region &r)
// Desc: 
//------------------------------------------------------------------------------
void ByteStreamRegion::setRegion(const MemoryRegions::Region &r) {
	m_Region = r;
}

//------------------------------------------------------------------------------
// Name: clear()
// Desc: 
//------------------------------------------------------------------------------
void ByteStreamRegion::clear() {
	setRegion(MemoryRegions::Region());
}

//------------------------------------------------------------------------------
// Name: size() const
// Desc: return s the size of this stream
//------------------------------------------------------------------------------
ByteStreamRegion::size_type ByteStreamRegion::size() const {
	return m_Region.size();
}

//------------------------------------------------------------------------------
// Name: max_size() const
// Desc: return s the max size
//------------------------------------------------------------------------------
ByteStreamRegion::size_type ByteStreamRegion::max_size() const {
	return static_cast<size_type>(-1);
}

//------------------------------------------------------------------------------
// Name: empty() const
// Desc: returns true if the container has no bytes
//------------------------------------------------------------------------------
bool ByteStreamRegion::empty() const {
	return size() != 0;
}

//------------------------------------------------------------------------------
// Name: read(ByteStreamRegion::size_type i) const
// Desc: reads a byte at a given index from the stream
//------------------------------------------------------------------------------
ByteStreamRegion::value_type ByteStreamRegion::read(ByteStreamRegion::size_type i) const {
	value_type ret;
	Q_CHECK_PTR(edb::v1::debuggerBase);
	edb::v1::debuggerBase->readBytes(m_Region.start + i, &ret, sizeof(ret));
	return ret;
}

//------------------------------------------------------------------------------
// Name: write(ByteStreamRegion::size_type i, const ByteStreamRegion::value_type &v)
// Desc: writes a byte to the stream at a given index
//------------------------------------------------------------------------------
void ByteStreamRegion::write(ByteStreamRegion::size_type i, const ByteStreamRegion::value_type &v) {
	Q_CHECK_PTR(edb::v1::debuggerBase);
	edb::v1::debuggerBase->writeBytes(m_Region.start + i, &v, sizeof(v));
}
