C++ Sample : Simple Choice
In This Topic
Schema Summary
This sample shows an element containing a choice of simple elements. One and only one child element may be provided.
Schema Details
The element AccountAdminRequest contains 3 child elements, CreateAccount, DeleteAccount and ChangeAccountPassword. Only one of these elements may be specified at a time, and one MUST be specified. All 3 of the child elements are represented in the generated code as classes (as opposed to primitive), despite being based on primitive types. This is because they all contain child attributes of there own. Elements with child elements or attributes must be represented as classes in the generated code.
Sample Description
The sample demonstrates what happens when more than one item in a choice is set.
|
|
Sample Code
Write Sample |
|
#include "..\..\..\Samples\SimpleChoice\Generated\CPP\SourceCodeCPP\SimpleChoiceLib.h"
#include "..\..\..\Samples\SimpleChoice\Generated\CPP\SourceCodeCPP\SimpleChoiceLib\AccountAdminRequest.h"
...
try
{
SimpleChoiceLib::CAccountAdminRequestPtr req = SimpleChoiceLib::CAccountAdminRequest::CreateInstance();
SimpleChoiceLib::CCreateAccountPtr naReq = SimpleChoiceLib::CCreateAccount::CreateInstance();
naReq->SetUsername("Guest");
naReq->SetPassword("Password");
req->SetCreateAccount(naReq);
SimpleChoiceLib::CDeleteAccountPtr daReq = SimpleChoiceLib::CDeleteAccount::CreateInstance();
daReq->SetUsername("Guest");
req->SetDeleteAccount(daReq);
printf(req->ToXml().c_str());
}
catch (LtXmlLib20::CLtException& e)
{
printf("Error: %s\n", e.GetFullMessage().c_str());
}
Output
<?xml version="1.0"?>
<!--Created by Liquid XML Data Binding Libraries (www.liquid-technologies.com) for Liquid Technologies Ltd -->
<AccountAdminRequest xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<DeleteAccount Username="Guest"/>
</AccountAdminRequest>
|
|
XSD Source Files
SimpleChoice.xsd |
|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="AccountAdminRequest">
<xs:complexType>
<xs:choice>
<xs:element name="CreateAccount">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Username" type="xs:string" use="required"/>
<xs:attribute name="Password" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="DeleteAccount">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Username" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="ChangeAccountPassword">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:date">
<xs:attribute name="Username" type="xs:string" use="required"/>
<xs:attribute name="OldPassword" type="xs:string" use="required"/>
<xs:attribute name="NewPassword" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
|
Schema Diagrams |
|
|
Generated Files
AccountAdminRequest.h |
|
#ifndef _SimpleChoiceLib_SimpleChoiceLib_CAccountAdminRequest_h
#define _SimpleChoiceLib_SimpleChoiceLib_CAccountAdminRequest_h
#include "../SimpleChoiceLib/CreateAccount.h"
#include "../SimpleChoiceLib/DeleteAccount.h"
#include "../SimpleChoiceLib/ChangeAccountPassword.h"
namespace SimpleChoiceLib { class CClassFactory; };
namespace SimpleChoiceLib
{
class SimpleChoiceLib_DLL CAccountAdminRequest : public CInstanceMonitor
, public virtual SimpleChoiceLib::CXmlCommonBase
{
public:
static SimpleChoiceLib::CAccountAdminRequestPtr CreateInstance(LPCTSTR lpctElementName=_T("AccountAdminRequest"));
protected:
CAccountAdminRequest(LPCTSTR lpctElementName=_T("AccountAdminRequest"));
virtual ~CAccountAdminRequest();
friend class SimpleChoiceLib::CClassFactory;
virtual void Init();
virtual void AccessProperty(int iPropertyIndex, bool read, LtXmlLib20::LtVariant& rValue);
virtual LtXmlLib20Data::CParentElementInfo* GetClassInfo() const;
virtual LtXmlLib20Data::CElementInfo** GetClassElementInfo() const;
virtual LtXmlLib20Data::CAttributeInfo** GetClassAttributeInfo() const;
static void CleanMetaData();
void Cleanup();
virtual void OnEvent(LtXmlLib20::CXmlObjectBase* pMsgSource, LtXmlLib20::IEventSink::MsgType eMsgType, void* pData);
void ClearChoice(LPCTSTR selectedElement);
public:
SimpleChoiceLib::CCreateAccountPtr GetCreateAccount() const;
void SetCreateAccount(SimpleChoiceLib::CCreateAccount* value);
protected:
SimpleChoiceLib::CCreateAccountPtr m_CreateAccount;
public:
SimpleChoiceLib::CDeleteAccountPtr GetDeleteAccount() const;
void SetDeleteAccount(SimpleChoiceLib::CDeleteAccount* value);
protected:
SimpleChoiceLib::CDeleteAccountPtr m_DeleteAccount;
public:
SimpleChoiceLib::CChangeAccountPasswordPtr GetChangeAccountPassword() const;
void SetChangeAccountPassword(SimpleChoiceLib::CChangeAccountPassword* value);
protected:
SimpleChoiceLib::CChangeAccountPasswordPtr m_ChangeAccountPassword;
public:
std::tstring GetChoiceSelectedElement() const;
protected:
std::tstring m_validElement;
public:
virtual SimpleChoiceLib::CAccountAdminRequestPtr Clone() const;
virtual std::tstring GetTargetNamespace() const;
virtual std::tstring GetNamespace() const;
virtual LtXmlLib20::CXmlObjectBase* GetBase();
// Internal data for XML serialization
private:
static LtXmlLib20Data::CParentElementInfo* ms_pParentElementInfo;
static LtXmlLib20Data::CElementInfo** ms_ppElementInfo;
static LtXmlLib20Data::CAttributeInfo** ms_ppAttributeInfo;
};
};
#endif
|
AccountAdminRequest.cpp |
|
#include "StdAfx.h"
#pragma warning (push)
#pragma warning (disable:4251)
#pragma warning (disable:4786)
#include "../SimpleChoiceLib.h"
#include "../SimpleChoiceLib/AccountAdminRequest.h"
namespace SimpleChoiceLib
{
LtXmlLib20Data::CParentElementInfo* CAccountAdminRequest::ms_pParentElementInfo = NULL;
LtXmlLib20Data::CAttributeInfo** CAccountAdminRequest::ms_ppAttributeInfo = NULL;
LtXmlLib20Data::CElementInfo** CAccountAdminRequest::ms_ppElementInfo = NULL;
CAccountAdminRequestPtr CAccountAdminRequest::CreateInstance(LPCTSTR lpctElementName)
{
return new SimpleChoiceLib::CAccountAdminRequest(lpctElementName);
}
CAccountAdminRequest::CAccountAdminRequest(LPCTSTR lpctElementName)
: CInstanceMonitor(_T("CAccountAdminRequest"))
{
m_elementName = lpctElementName;
Init();
}
CAccountAdminRequest::~CAccountAdminRequest()
{
Cleanup();
}
void CAccountAdminRequest::Cleanup()
{
// unregister for any events we have asked for
// cos there'll be no one left to hear soon
}
void CAccountAdminRequest::OnEvent(LtXmlLib20::CXmlObjectBase* pMsgSource, LtXmlLib20::IEventSink::MsgType eMsgType, void* pData)
{
if (eMsgType == LtXmlLib20::IEventSink::MT_CollectionChangeEvent)
{
}
}
void CAccountAdminRequest::Init()
{
Cleanup();
this->m_CreateAccount = NULL;
this->m_DeleteAccount = NULL;
this->m_ChangeAccountPassword = NULL;
m_validElement = _T("");
}
void CAccountAdminRequest::ClearChoice(LPCTSTR lpctSelectedElement)
{
this->m_CreateAccount = NULL;
this->m_DeleteAccount = NULL;
this->m_ChangeAccountPassword = NULL;
m_validElement = lpctSelectedElement;
}
void CAccountAdminRequest::AccessProperty(int iPropertyIndex, bool bRead, LtXmlLib20::LtVariant& rValue)
{
if (bRead)
{
switch(iPropertyIndex)
{
case 1:
rValue.SetXmlObject(GetCreateAccount().Ptr());
break;
case 2:
rValue.SetXmlObject(GetDeleteAccount().Ptr());
break;
case 3:
rValue.SetXmlObject(GetChangeAccountPassword().Ptr());
break;
default:
throw LtXmlLib20::CLtException(_T("Unknown Property Index"));
};
}
else
{
switch(iPropertyIndex)
{
case 1:
SetCreateAccount(dynamic_cast<SimpleChoiceLib::CCreateAccount*>(rValue.GetXmlObject().Ptr()));
break;
case 2:
SetDeleteAccount(dynamic_cast<SimpleChoiceLib::CDeleteAccount*>(rValue.GetXmlObject().Ptr()));
break;
case 3:
SetChangeAccountPassword(dynamic_cast<SimpleChoiceLib::CChangeAccountPassword*>(rValue.GetXmlObject().Ptr()));
break;
default:
throw LtXmlLib20::CLtException(_T("Unknown Property Index"));
}
}
}
SimpleChoiceLib::CCreateAccountPtr CAccountAdminRequest::GetCreateAccount() const
{
return this->m_CreateAccount;
}
void CAccountAdminRequest::SetCreateAccount(SimpleChoiceLib::CCreateAccount* value)
{
// Ensure only on element is populated at a time
ClearChoice(value == NULL?_T(""):_T("CreateAccount"));
if (value == NULL)
this->m_CreateAccount = NULL;
else
{
SetElementName(value, _T("CreateAccount"));
this->m_CreateAccount = value;
}
}
SimpleChoiceLib::CDeleteAccountPtr CAccountAdminRequest::GetDeleteAccount() const
{
return this->m_DeleteAccount;
}
void CAccountAdminRequest::SetDeleteAccount(SimpleChoiceLib::CDeleteAccount* value)
{
// Ensure only on element is populated at a time
ClearChoice(value == NULL?_T(""):_T("DeleteAccount"));
if (value == NULL)
this->m_DeleteAccount = NULL;
else
{
SetElementName(value, _T("DeleteAccount"));
this->m_DeleteAccount = value;
}
}
SimpleChoiceLib::CChangeAccountPasswordPtr CAccountAdminRequest::GetChangeAccountPassword() const
{
return this->m_ChangeAccountPassword;
}
void CAccountAdminRequest::SetChangeAccountPassword(SimpleChoiceLib::CChangeAccountPassword* value)
{
// Ensure only on element is populated at a time
ClearChoice(value == NULL?_T(""):_T("ChangeAccountPassword"));
if (value == NULL)
this->m_ChangeAccountPassword = NULL;
else
{
SetElementName(value, _T("ChangeAccountPassword"));
this->m_ChangeAccountPassword = value;
}
}
std::tstring CAccountAdminRequest::GetChoiceSelectedElement() const
{
return m_validElement;
}
SimpleChoiceLib::CAccountAdminRequestPtr CAccountAdminRequest::Clone() const
{
SimpleChoiceLib::CAccountAdminRequestPtr newObject = CreateInstance(m_elementName.c_str());
int index = 0;
newObject->m_CreateAccount = NULL;
if (m_CreateAccount != NULL)
newObject->m_CreateAccount = dynamic_cast<SimpleChoiceLib::CCreateAccount*>(m_CreateAccount->Clone().Ptr());
newObject->m_DeleteAccount = NULL;
if (m_DeleteAccount != NULL)
newObject->m_DeleteAccount = dynamic_cast<SimpleChoiceLib::CDeleteAccount*>(m_DeleteAccount->Clone().Ptr());
newObject->m_ChangeAccountPassword = NULL;
if (m_ChangeAccountPassword != NULL)
newObject->m_ChangeAccountPassword = dynamic_cast<SimpleChoiceLib::CChangeAccountPassword*>(m_ChangeAccountPassword->Clone().Ptr());
newObject->m_validElement = m_validElement;
return newObject.Ptr();
}
std::tstring CAccountAdminRequest::GetTargetNamespace() const
{
return _T("");
}
std::tstring CAccountAdminRequest::GetNamespace() const
{
return _T("");
}
LtXmlLib20::CXmlObjectBase* CAccountAdminRequest::GetBase()
{
return this;
}
void CAccountAdminRequest::CleanMetaData()
{
LtXmlLib20::CXmlGeneratedClass::CleanMetaData(ms_pParentElementInfo, ms_ppElementInfo, ms_ppAttributeInfo);
}
LtXmlLib20Data::CParentElementInfo* CAccountAdminRequest::GetClassInfo() const
{
if (ms_pParentElementInfo == NULL)
{
m_csInit.Enter();
if (ms_pParentElementInfo == NULL)
{
ms_pParentElementInfo = new LtXmlLib20Data::CParentElementInfo(
LtXmlLib20Data::XmlElementGroupType_CHOICE,
LtXmlLib20Data::XmlElementType_ELEMENT,
_T("AccountAdminRequest"),
_T(""),
true,
false,
-1,
LtXmlLib20::ItemType_none,
LtXmlLib20::CWhitespaceUtils::WhitespaceRule_None,
NULL,
false);
}
m_csInit.Leave();
}
return ms_pParentElementInfo;
}
LtXmlLib20Data::CElementInfo** CAccountAdminRequest::GetClassElementInfo() const
{
if (ms_ppElementInfo == NULL)
{
m_csInit.Enter();
if (ms_ppElementInfo == NULL)
{
ms_ppElementInfo = new LtXmlLib20Data::CElementInfo*[4];
ms_ppElementInfo[0] = new LtXmlLib20Data::CElementInfoChoiceClsOpt(_T("CreateAccount"), _T(""), 1, LtXmlLib20Data::XmlElementType_ELEMENT, (LtXmlLib20Data::pfnCreateClassDef)SimpleChoiceLib::CClassFactory::CreateClass, SimpleChoiceLib::CClassFactory::ClsName_CCreateAccount);
ms_ppElementInfo[1] = new LtXmlLib20Data::CElementInfoChoiceClsOpt(_T("DeleteAccount"), _T(""), 2, LtXmlLib20Data::XmlElementType_ELEMENT, (LtXmlLib20Data::pfnCreateClassDef)SimpleChoiceLib::CClassFactory::CreateClass, SimpleChoiceLib::CClassFactory::ClsName_CDeleteAccount);
ms_ppElementInfo[2] = new LtXmlLib20Data::CElementInfoChoiceClsOpt(_T("ChangeAccountPassword"), _T(""), 3, LtXmlLib20Data::XmlElementType_ELEMENT, (LtXmlLib20Data::pfnCreateClassDef)SimpleChoiceLib::CClassFactory::CreateClass, SimpleChoiceLib::CClassFactory::ClsName_CChangeAccountPassword);
ms_ppElementInfo[3] = NULL;
}
m_csInit.Leave();
}
return ms_ppElementInfo;
}
LtXmlLib20Data::CAttributeInfo** CAccountAdminRequest::GetClassAttributeInfo() const
{
if (ms_ppAttributeInfo == NULL)
{
m_csInit.Enter();
if (ms_ppAttributeInfo == NULL)
{
ms_ppAttributeInfo = new LtXmlLib20Data::CAttributeInfo*[1];
ms_ppAttributeInfo[0] = NULL;
}
m_csInit.Leave();
}
return ms_ppAttributeInfo;
}
};
|