ConnectionRelationSetting.cpp

#include "StdAfx.h"
#include "ConnectionRelationSetting.h"


#include "ConnectionRelation.h"
#include "Link.h"


using namespace Core;
using namespace System;
using namespace System::Diagnostics;


/// [ Constructor ]
ConnectionRelationSetting::ConnectionRelationSetting( Links^ links, System::Xml::XmlElement^ root ) :
    m_links( links ),
    m_element( root )
{
    Debug::WriteLine( "リンクの接続関係を設定する" );
}

/// 生成する
ConnectionRelationSetting^ ConnectionRelationSetting::Create( Links^ links, System::Xml::XmlElement^ root )
{
    // クラスのインスタンスを生成する
    ConnectionRelationSetting^ result
        = gcnew ConnectionRelationSetting( links, root );

    // リンクの最初の要素から、接続元を指定せずに設定する
    result->Set( links[ 0 ], nullptr );

#ifdef DETAIL
    // 接続関係を表示する
    Debug::WriteLine( result );
#endif

    return result;
}


/// 設定する
void ConnectionRelationSetting::Set( Link^ ownLink, Link^ upper )
{
    if( ownLink != nullptr )
    {
        // 自分を定義している 要素を取得する
        Xml::XmlElement^ ownElement = GetElement( ownLink->Name );

        // ... 接続関係を設定する
        ownLink->Connection = gcnew ConnectionRelation( upper, m_links, ownElement );

        // ... それを確認する
        ownLink->Connection->CheckData( ownLink );


        // 接続先へ 自分へのハンドルを与えて再帰する
        Set( ownLink->Connection->Lower, ownLink );

        // 対等な接続へ 自分の接続元へのハンドルを与えて再帰する
        Set( ownLink->Connection->Equal, upper );
    }
}


/// 指定された属性を有する要素を取得する
Xml::XmlElement^ ConnectionRelationSetting::GetElement( System::String^ linkName )
{
    for each( Xml::XmlElement^ node in m_element->ChildNodes )
    {
        if( node->GetAttribute( "name" ) == linkName )
        {
            // 属性が一致したならば、それを返す
            return node;
        }
    }
    Debug::Fail( "指定された属性を有する要素が存在しない" );
    return nullptr;
}


/// インスタンスの説明を文字列で返す
String^ ConnectionRelationSetting::ToString()
{
    String^ result;

    for each( Link^ link in m_links )
    {
        ConnectionRelation^ connection = link->Connection;  // 接続関係
        Debug::Assert( connection != nullptr, "接続関係は生成されている" );

        // 接続関係を文字列にして連結する
        result += connection->ToString() + Environment::NewLine;
    }
    return result;
}