/**********************************************************************************************
 * Copyright (c) 2001-2017 Liquid Technologies Limited. All rights reserved.
 * See www.liquid-technologies.com for product details.
 *
 * Please see products End User License Agreement for distribution permissions.
 *
 * WARNING: THIS FILE IS GENERATED
 * Changes made outside of ##HAND_CODED_BLOCK_START blocks will be overwritten
 *
 * Generation  :  by Liquid XML Data Binder 15.0.1.7161
 * Using Schema: D:\Development2017\Liquid\Tools\DataBindingSamples\Samples\SimpleHierarchy\Schema\SimpleHierarchy.xsd
 **********************************************************************************************/
#include "StdAfx.h" 
#pragma warning( disable : 4786 ) 
#pragma warning( disable : 4251 ) 
#include "../SimpleHierarchyLib.h" 
#include "../SimpleHierarchyLib/ItemTypeCol.h" 
#include "../SimpleHierarchyLib/ItemType.h" 

// ##HAND_CODED_BLOCK_START ID="Additional Includes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS

// Add Additional Includes here...

// ##HAND_CODED_BLOCK_END ID="Additional Includes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS

namespace SimpleHierarchyLib
{

/*
 * we don't want the users just creating these. They need to 
 * be initialize with the name of the element that they are
 * going to represent in the XML document. This information
 * requires knowledge of the schema, we are trying to 
 * prevent the user from having to know anything about that.
 */
CItemTypeCol::CItemTypeCol(LPCTSTR lpctElementName, LPCTSTR lpctNamespaceUri, int minOccurs, int maxOccurs)
 : CXmlCollectionCommonBase(lpctElementName, lpctNamespaceUri, minOccurs, maxOccurs), CInstanceMonitor(_T("CItemTypeCol"))
{
}

/*
 * Adds a SimpleHierarchyLib::CItemType to the collection
 */
void CItemTypeCol::Add(SimpleHierarchyLib::CItemType* pCls)
{
    SetElementName(pCls, m_elementName.c_str());
    AppendItem(pCls);
}

void CItemTypeCol::AddAt(int index, SimpleHierarchyLib::CItemType* pCls)
{
    SetElementName(pCls, m_elementName.c_str());
    AddItemAt(index, pCls);
}

/*
 *  Create a new SimpleHierarchyLib::CItemType object and adds it the collection
 */
SimpleHierarchyLib::CItemTypePtr CItemTypeCol::Add()
{
    CSmartPtr<LtXmlLib15::CXmlObjectBase> spCls = SimpleHierarchyLib::CClassFactory::CreateClass(SimpleHierarchyLib::CClassFactory::ClsName_CItemType, m_elementName.c_str());
    AppendItem(spCls);
    return dynamic_cast<SimpleHierarchyLib::CItemType*>(spCls.Ptr());
}

/*
 * Gets a CItemType object from the collection (0 based)
 */
CSmartPtr<SimpleHierarchyLib::CItemType> CItemTypeCol::Item(int index) const
{   
    return dynamic_cast<SimpleHierarchyLib::CItemType*>(GetNodeAtIndex(index)->spObj.Ptr());
}

/*
 *  Removes the object from the collection
 */
void CItemTypeCol::Remove(SimpleHierarchyLib::CItemType* pCls)
{
    RemoveNode(GetNodeForItem(pCls));
}

/*
 * Gets a representation of the class as XML - Marshalls Objects to XML
 */
void CItemTypeCol::ToXml_Int(LtXmlLib15::CXmlWriter* xmlOut, bool bRegisterNamespaces, LPCTSTR lpctNamespaceUri, const LtXmlLib15::CXmlSerializationContext& context, bool isOptionalChoice) const
{
    ValidateCount(context);
    LtXmlLib15::CNode* pNode = m_pHead;
    while(pNode != NULL)
    {
        LtXmlLib15::CXmlObjectBase::ToXml_Int((pNode->spObj)->GetBase(), xmlOut, false, lpctNamespaceUri, context, isOptionalChoice);
        pNode = pNode->pNext;
    }
}

/*
 * Creates the collection from XML data - Unmarshalls XML to Objects
 */
LtXmlLib15::CXmlElement* CItemTypeCol::FromXml_Int(LtXmlLib15::CXmlElement* pXmlParent, LtXmlLib15::CXmlElement* pXmlChild, const LtXmlLib15::CXmlSerializationContext& context, bool isOptionalChoice)
{
    // go through the nodes until we run out of ones that match
    while (pXmlChild != NULL)
    {
        // Stop reading when we hit an element we cant deal with
        if (!DoesElementNameMatch(context, pXmlChild, m_elementName.c_str(), GetNamespace().c_str()))
            break;
        CSmartPtr<LtXmlLib15::CXmlObjectBase> spObj = SimpleHierarchyLib::CClassFactory::CreateClass(SimpleHierarchyLib::CClassFactory::ClsName_CItemType, m_elementName.c_str());
        LtXmlLib15::CXmlObjectBase::FromXml_Int(spObj->GetBase(), pXmlChild, pXmlChild->GetFirstElement(), context);

        // Add new item to the collection
        AppendItem(spObj.Ptr());
    
        // Move to next node
        pXmlChild = pXmlChild->GetNextSiblingElement();
    }
    
    return pXmlChild;
}

CItemTypeCol::iterator CItemTypeCol::begin()
{
    return m_pHead;
}
CItemTypeCol::iterator CItemTypeCol::end()
{
    return NULL;
}

// ##HAND_CODED_BLOCK_START ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS

// Add Additional Methods here...

// ##HAND_CODED_BLOCK_END ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS

}; // namespace