Beispiel #1
0
void main()
{

	try
	{

		Component *			HeadComponent;
		Component *			CurrComponent;
		Component *			SubComponent;
		Component *			SubSubComponent;

		ComponentIterator * CompIter;
		int x;
		int y;
		int z;
		char name[100] = {"Object level"};
		char actual_name[100];
		char add_string[100];

		// build it up.....

		HeadComponent = new Composite("Head");
		CurrComponent = HeadComponent;

		for (x = 0; x<10 ;x++)
		{
			strcpy(actual_name,name);
			sprintf(add_string,"1-%d",x);
			strcat(actual_name,add_string);

			if(x == 2 || x == 4)
			{
				strcpy(actual_name,name);
				sprintf(add_string,"1-%d-%d",x,y);
				strcat(actual_name,add_string);

				SubComponent = new Composite(actual_name);
				CurrComponent->Add(SubComponent);
				for (y = 0; y < 10; y++)
				{
					if (y == 7 || y == 9)
					{
						SubSubComponent = new Composite(actual_name);
						SubComponent->Add(SubSubComponent);


					}
					else
					{
						SubComponent->Add(new Component(actual_name));
					}
				}

			}
			else
			{
				CurrComponent->Add(new Component(actual_name));
    		}
		}

		HeadComponent->Operation();

		// test iterator operation!
		if (HeadComponent->GetChild())
		{
			CompIter = HeadComponent->CreateIterator();

			while (! CompIter->IsDone())
			{
				CompIter->CurrentItem()->PrintName();
				(*CompIter)++;
			}
		}

		delete CompIter;

		delete HeadComponent;
	}
	catch(EXCEPTION_CODE ec)
	{
		
		switch (ec)
		{
		case EXC_MEMORY_ALLOC_FAILURE_LISTCLASS :
			cout << "Exception:" << "mem alloc failure.. list class" << endl;
			break;

		case EXC_HEAP_ALLOC_FAILURE :
			cout << "Exception:" << "Heap allocation failure!" << endl;
			break;

		case EXC_BAD_ADD_COMPONENT :
			cout << "Exception:" << "Tried to add w/ component object" << endl;
			break;

		case EXC_BAD_REMOVE_COMPONENT :
			cout << "Exception:" << "Tried to remove w/ component object" << endl;
			break;

		case EXC_BAD_COUNT_CALL :
			cout << "Exception:" << "Tried to get count on a component object" << endl;
			break;

		case EXC_BAD_GET_CALL :
			cout << "Exception:" << "Tried to get a component object from a component object" << endl;
			break;

		case EXC_CANT_CREATE_ITERATOR :
			cout << "Exception:" << "Can't create component for object of this type!" << endl;
			break;


		default:
			cout << "Exception: " << " no msg for this exception" << endl;

		}
	}
}