#include <Common.hpp>
#include <Map.hpp>
using namespace corelinux;
#include <iostream>
#include <exception>
CORELINUX_MULTIMAP(
AbstractStringPtr,
AbstractStringPtr,
less<AbstractStringPtr>,
Dictionary );
enum DictionaryType
{
MAIN,
USER
};
struct _Entry
{
CharCptr theKey;
CharCptr theDefinition;
};
DECLARE_TYPE( struct _Entry, Entry );
int main( void );
void initializeDictionary( DictionaryRef, DictionaryType );
void dumpDictionary( DictionaryRef, AbstractStringCref );
void handleAssertion( AssertionCref aAssert );
void handleException( ExceptionCref );
int main( void )
{
try
{
Dictionary mainBook;
initializeDictionary( mainBook, MAIN );
dumpDictionary(mainBook,StringUtf8("Members of Main Dictionary"));
Dictionary userBook;
initializeDictionary( userBook, USER );
dumpDictionary(userBook,StringUtf8("Members of User Dictionary"));
Dictionary updatedBook(mainBook);
updatedBook.insert(userBook.begin(),userBook.end());
dumpDictionary(updatedBook,StringUtf8("Members of NEW Main Dictionary"));
DictionaryIterator begin( updatedBook.begin() );
DictionaryIterator end( updatedBook.begin() );
for( ; begin != end; ++begin )
{
delete (*begin).first;
delete (*begin).second;
}
updatedBook.clear();
mainBook.clear();
userBook.clear();
}
catch( AssertionRef aAssert )
{
handleAssertion(aAssert);
}
catch( ExceptionRef aException )
{
handleException(aException);
}
catch( std::exception & e )
{
cerr << e.what() << endl;
}
catch( ... )
{
cerr << "Unknown exception." << endl;
}
return 0;
}
Entry mainEntries[] =
{
{"abstract class",
"A class whose primary purpose is to define an interface"},
{"class",
"A class defines an object's interface and implementation"},
{"concrete class",
"A class having no abstract operations"}
};
Entry userEntries[] =
{
{"monkey",
"A person who stays up late creating code examples"},
{"dog house",
"Where the monkey has to sleep"},
{"concrete class",
"The class of material that a monkey gets hit with in the dog house"}
};
void initializeDictionary
(
DictionaryRef aDictionary,
DictionaryType aTypeDictionary
)
{
Count aCount(0);
EntryCptr aHead(NULLPTR);
if( aTypeDictionary == MAIN )
{
aCount = sizeof( mainEntries );
aHead = mainEntries;
}
else if( aTypeDictionary == USER )
{
aCount = sizeof( userEntries );
aHead = userEntries;
}
else
{
NEVER_GET_HERE;
}
aCount /= sizeof( Entry );
CHECK( aCount > 1 );
CHECK( aHead != NULLPTR );
for( Count x = 0; x < aCount; ++x )
{
aDictionary.insert
(
Dictionary::value_type
(
new StringUtf8(aHead[x].theKey),
new StringUtf8(aHead[x].theDefinition)
)
);
}
}
void dumpDictionary( DictionaryRef aRef , AbstractStringCref aHeader )
{
REQUIRE( aHeader.supportsStandardInterface() == true );
REQUIRE( aHeader.getElementByteCount() == sizeof(Char) );
const string & aStdImpl = dynamic_cast<const std::string &>(aHeader);
cout << endl << aStdImpl << endl << endl;
DictionaryIterator begin = aRef.begin();
if( (*begin).first->supportsStandardInterface() == true &&
(*begin).first->isUtf8() == true )
{
DictionaryIterator end = aRef.end();
StringUtf8Ptr aKey(NULLPTR);
StringUtf8Ptr aValue(NULLPTR);
for( ; begin != end; ++begin )
{
aKey = dynamic_cast<StringUtf8Ptr>((*begin).first);
aValue = dynamic_cast<StringUtf8Ptr>((*begin).second);
cout << *aKey << " = " << *aValue << endl;
}
}
else
{
throw Exception("Unable to support string type",LOCATION);
}
}
void handleAssertion( AssertionCref aAssert )
{
cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" <<
"Assertion: ";
if( aAssert.getType() == Assertion::NEVERGETHERE )
{
cerr << "NEVER_GET_HERE";
}
else
{
if( aAssert.getType() == Assertion::REQUIRE )
{
cerr << "REQUIRE";
}
else if( aAssert.getType() == Assertion::ENSURE )
{
cerr << "ENSURE";
}
else if( aAssert.getType() == Assertion::CHECK )
{
cerr << "CHECK";
}
else
{
cerr << "ASSERT";
}
cerr << "( " << aAssert.getWhy() << " )";
}
cerr << endl;
}
void handleException( ExceptionCref aExcp )
{
cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
"Exception: " << aExcp.getWhy() << endl;
}