Source

gb_emulator / gb_debugger_msvs / properties / register_property.cpp

/*  Copyright Š 2011 Chris Spencer <spencercw@gmail.com>

    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 3 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, see <http://www.gnu.org/licenses/>.  */

#include "stdafx.h"

#include "register_property.h"

#include <iomanip>
#include <sstream>

#include "register_group_property.h"

using std::hex;
using std::setfill;
using std::setw;
using std::wostringstream;
using std::wstring;

// IDebugProperty2 methods

HRESULT CRegisterProperty::GetPropertyInfo(
	DEBUGPROP_INFO_FLAGS dwFields,
	DWORD nRadix,
	DWORD dwTimeout,
	IDebugReference2 **rgpArgs,
	DWORD dwArgCount,
	DEBUG_PROPERTY_INFO *pPropertyInfo)
{
	(void) dwTimeout;
	(void) rgpArgs;
	(void) dwArgCount;

	if (!pPropertyInfo)
	{
		return E_POINTER;
	}

	PopulatePropertyInfo(pPropertyInfo, dwFields, nRadix);
	return S_OK;
}

HRESULT CRegisterProperty::SetValueAsString(
	const OLECHAR *pszValue,
	DWORD nRadix,
	DWORD dwTimeout)
{
	(void) pszValue;
	(void) nRadix;
	(void) dwTimeout;

	return E_NOTIMPL;
}

HRESULT CRegisterProperty::SetValueAsReference(
	IDebugReference2 **rgpArgs,
	DWORD dwArgCount,
	IDebugReference2 *pValue,
	DWORD dwTimeout)
{
	(void) rgpArgs;
	(void) dwArgCount;
	(void) pValue;
	(void) dwTimeout;

	return E_NOTIMPL;
}

HRESULT CRegisterProperty::EnumChildren(
	DEBUGPROP_INFO_FLAGS dwFields,
	DWORD dwRadix,
	const GUID &guidFilter,
	DBG_ATTRIB_FLAGS dwAttribFilter,
	const OLECHAR *pszNameFilter,
	DWORD dwTimeout,
	IEnumDebugPropertyInfo2 **ppEnum)
{
	(void) dwFields;
	(void) dwRadix;
	(void) guidFilter;
	(void) dwAttribFilter;
	(void) pszNameFilter;
	(void) dwTimeout;

	if (!ppEnum)
	{
		return E_POINTER;
	}

	*ppEnum = NULL;
	return S_FALSE;
}

HRESULT CRegisterProperty::GetParent(IDebugProperty2 **ppParent)
{
	if (!ppParent)
	{
		return E_POINTER;
	}

	*ppParent = registerGroup_;
	(*ppParent)->AddRef();
	return S_OK;
}

HRESULT CRegisterProperty::GetDerivedMostProperty(IDebugProperty2 **ppDerivedMost)
{
	(void) ppDerivedMost;

	return E_NOTIMPL;
}

HRESULT CRegisterProperty::GetMemoryBytes(IDebugMemoryBytes2 **ppMemoryBytes)
{
	(void) ppMemoryBytes;

	return E_NOTIMPL;
}

HRESULT CRegisterProperty::GetMemoryContext(IDebugMemoryContext2 **ppMemory)
{
	if (!ppMemory)
	{
		return E_POINTER;
	}

	*ppMemory = NULL;
	return S_GETMEMORYCONTEXT_NO_MEMORY_CONTEXT;
}

HRESULT CRegisterProperty::GetSize(DWORD *pdwSize)
{
	(void) pdwSize;

	return E_NOTIMPL;
}

HRESULT CRegisterProperty::GetReference(IDebugReference2 **ppReference)
{
	(void) ppReference;

	return E_NOTIMPL;
}

HRESULT CRegisterProperty::GetExtendedInfo(const GUID &guidExtendedInfo, VARIANT *pExtendedInfo)
{
	(void) guidExtendedInfo;
	(void) pExtendedInfo;

	return E_NOTIMPL;
}

// CRegisterProperty methods

void CRegisterProperty::Init(CRegisterGroupProperty *registerGroup, const wstring &name,
	uint16_t value, unsigned width)
{
	registerGroup_ = registerGroup;
	name_ = name;
	value_ = value;
	width_ = width;
}

void CRegisterProperty::PopulatePropertyInfo(DEBUG_PROPERTY_INFO *propertyInfo,
	DEBUGPROP_INFO_FLAGS fields, DWORD radix)
{
	(void) radix;

	memset(propertyInfo, 0, sizeof(*propertyInfo));
	
	if (fields & DEBUGPROP_INFO_FULLNAME)
	{
		propertyInfo->dwFields |= DEBUGPROP_INFO_FULLNAME;
		propertyInfo->bstrFullName = SysAllocString(name_.c_str());
	}
	if (fields & DEBUGPROP_INFO_NAME)
	{
		propertyInfo->dwFields |= DEBUGPROP_INFO_NAME;
		propertyInfo->bstrName = SysAllocString(name_.c_str());
	}
	if (fields & DEBUGPROP_INFO_VALUE)
	{
		propertyInfo->dwFields |= DEBUGPROP_INFO_VALUE;

		// Format the value
		wostringstream oss;
		oss << hex << setfill(L'0') << setw(width_) << static_cast<unsigned>(value_);
		wstring value = oss.str();
		propertyInfo->bstrValue = SysAllocString(value.c_str());
	}
	if (fields & DEBUGPROP_INFO_ATTRIB)
	{
		propertyInfo->dwFields |= DEBUGPROP_INFO_ATTRIB;
		propertyInfo->dwAttrib = DBG_ATTRIB_VALUE_READONLY | DBG_ATTRIB_ACCESS_NONE |
			DBG_ATTRIB_STORAGE_REGISTER | DBG_ATTRIB_TYPE_NONE | DBG_ATTRIB_DATA;
	}
	if (fields & DEBUGPROP_INFO_PROP)
	{
		propertyInfo->dwFields |= DEBUGPROP_INFO_PROP;
		propertyInfo->pProperty = this;
		propertyInfo->pProperty->AddRef();
	}
}