Need Explain on This Small Code

  • Thread starter Thread starter drjackool
  • Start date Start date
D

drjackool

Guest
Hi

Can you say me, Why the "do {} while(0);" block are used in the following code (in macros in header file)? What are the benefits? If we remove the "while" block what does happens?

Code is part of the XML file reading and writing.

Thanks

Part of header file:

"Cleanup" is a jump label in the source functions.


#pragma once

#include <objbase.h>
#include <msxml6.h>

#define CHK_HR(stmt) do { HRESULT hr = (stmt); if (FAILED(hr)) {TRACE0("Failed\n"); goto CleanUp;} } while(0);

#define CHK_ALLOC(p) do {if (!(p)) { hr = E_OUTOFMEMORY; goto CleanUp;} } while(0);

#define SAFE_RELEASE(p) do {if ((p)) { (p)->Release(); (p) = NULL; }} while(0);

HRESULT VariantFromString(PCWSTR wszValue, VARIANT& var);

HRESULT AppendChildToParent(IXMLDOMNode* pChild, IXMLDOMNode* pParent);





Part of source file:

#include "stdafx.h"
#include "xmlhelper.h"

#include <objbase.h>
#include <msxml6.h>


HRESULT VariantFromString(PCWSTR wszValue, VARIANT& var)
{
HRESULT hr = S_OK;
BSTR bstr = SysAllocString(wszValue);
CHK_ALLOC(bstr);

V_VT(&var) = VT_BSTR;
V_BSTR(&var) = bstr;

CleanUp:
return hr;
}

HRESULT AppendChildToParent(IXMLDOMNode* pChild, IXMLDOMNode* pParent)
{
HRESULT hr = S_OK;
IXMLDOMNode *pChildOut = NULL;
CHK_HR(pParent->appendChild(pChild, &pChildOut));

CleanUp:
SAFE_RELEASE(pChildOut);
return hr;
}

Continue reading...
 
Back
Top