3
Copyright (C) 1999 CoreLinux Consortium
5
The CoreLinux++ Library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public License as
7
published by the Free Software Foundation; either version 2 of the
8
License, or (at your option) any later version.
10
The CoreLinux++ Library Library is distributed in the hope that it will
11
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with the GNU C Library; see the file COPYING.LIB. If not,
17
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
21
/** \example examp2.cpp
22
This example is to show use of the STL that CoreLinux wraps in macros.
23
There was no purpose to wrapping them except to simplify the type
24
defines to be consistent with CoreLinux++ C++ Standards naming
25
conventions.It is assumed that the reader has some knowledge of
31
#include <Map.hpp> // Defines map and multi-map
34
using namespace corelinux;
40
// A map is like a set in regards to the keys, it
41
// does not allow multiple key values. For our example
42
// a dictionary does allow multiple keys so we use
49
less<AbstractStringPtr>,
59
// Static entries to feed the dictionaries
65
CharCptr theDefinition;
68
DECLARE_TYPE( struct _Entry, Entry );
71
// In module function prototypes
77
// Allow user to feed the dictionaries
80
void initializeDictionary( DictionaryRef, DictionaryType );
83
// Generic print facility
86
void dumpDictionary( DictionaryRef, AbstractStringCref );
89
// Allow merge into main dictionary
93
// Assertion and Exception handlers
96
void handleAssertion( AssertionCref aAssert );
97
void handleException( ExceptionCref );
103
// Practice gracefull exception management
108
// Initialize the starter
111
initializeDictionary( mainBook, MAIN );
112
dumpDictionary(mainBook,StringUtf8("Members of Main Dictionary"));
114
// Emulate new entries added
117
initializeDictionary( userBook, USER );
118
dumpDictionary(userBook,StringUtf8("Members of User Dictionary"));
121
// Create a union of entries. We use a simple method
122
// (and probable not as efficient) as the routines
123
// available in stl_algo.h.
125
// You can consider which you would use based on
126
// what behavior you want a merge to have.
129
Dictionary updatedBook(mainBook);
130
updatedBook.insert(userBook.begin(),userBook.end());
131
dumpDictionary(updatedBook,StringUtf8("Members of NEW Main Dictionary"));
134
// Now we do the cleanup as I don't think it will
135
// happen on it's own
138
DictionaryIterator begin( updatedBook.begin() );
139
DictionaryIterator end( updatedBook.begin() );
141
for( ; begin != end; ++begin )
143
delete (*begin).first;
144
delete (*begin).second;
152
catch( AssertionRef aAssert )
154
handleAssertion(aAssert);
156
catch( ExceptionRef aException )
158
handleException(aException);
160
catch( std::exception & e )
162
cerr << e.what() << endl;
166
cerr << "Unknown exception." << endl;
173
// Initialize dictionary feeds the provided collection
174
// with dictionary entries based on the enumerator.
175
// Various assertions are provided.
177
Entry mainEntries[] =
180
"A class whose primary purpose is to define an interface"},
182
"A class defines an object's interface and implementation"},
184
"A class having no abstract operations"}
187
Entry userEntries[] =
190
"A person who stays up late creating code examples"},
192
"Where the monkey has to sleep"},
194
"The class of material that a monkey gets hit with in the dog house"}
197
void initializeDictionary
199
DictionaryRef aDictionary,
200
DictionaryType aTypeDictionary
203
Count aCount(0); // Generic entry counter
204
EntryCptr aHead(NULLPTR); // Generic entry pointer
207
// Determine which dictionary to feed.
208
// Extension suggestion: Have input come from
209
// external source (keyboard, stream, etc)
212
if( aTypeDictionary == MAIN )
214
aCount = sizeof( mainEntries );
217
else if( aTypeDictionary == USER )
219
aCount = sizeof( userEntries );
224
// Where missing something!
229
aCount /= sizeof( Entry );
236
CHECK( aHead != NULLPTR );
238
// Feed the dictionary
240
for( Count x = 0; x < aCount; ++x )
244
Dictionary::value_type
246
new StringUtf8(aHead[x].theKey),
247
new StringUtf8(aHead[x].theDefinition)
255
// General routine for dumping a dictionary to cout
258
void dumpDictionary( DictionaryRef aRef , AbstractStringCref aHeader )
260
REQUIRE( aHeader.supportsStandardInterface() == true );
261
REQUIRE( aHeader.getElementByteCount() == sizeof(Char) );
263
const string & aStdImpl = dynamic_cast<const std::string &>(aHeader);
265
cout << endl << aStdImpl << endl << endl;
267
DictionaryIterator begin = aRef.begin();
270
// First we test to insure that we can handle the string
271
// implementation using StringUtf8
274
if( (*begin).first->supportsStandardInterface() == true &&
275
(*begin).first->isUtf8() == true )
277
DictionaryIterator end = aRef.end();
278
StringUtf8Ptr aKey(NULLPTR);
279
StringUtf8Ptr aValue(NULLPTR);
280
for( ; begin != end; ++begin )
282
aKey = dynamic_cast<StringUtf8Ptr>((*begin).first);
283
aValue = dynamic_cast<StringUtf8Ptr>((*begin).second);
284
cout << *aKey << " = " << *aValue << endl;
290
throw Exception("Unable to support string type",LOCATION);
296
// Peform default (just show it)
299
void handleAssertion( AssertionCref aAssert )
301
cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" <<
304
if( aAssert.getType() == Assertion::NEVERGETHERE )
306
cerr << "NEVER_GET_HERE";
310
if( aAssert.getType() == Assertion::REQUIRE )
314
else if( aAssert.getType() == Assertion::ENSURE )
318
else if( aAssert.getType() == Assertion::CHECK )
326
cerr << "( " << aAssert.getWhy() << " )";
332
void handleException( ExceptionCref aExcp )
334
cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
335
"Exception: " << aExcp.getWhy() << endl;
339
Common rcs information do not modify
342
$Date: 2000/08/31 22:49:01 $