~ubuntu-branches/ubuntu/gutsy/libcorelinux/gutsy

« back to all changes in this revision

Viewing changes to src/testdrivers/ex2/examp2.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2001-12-29 17:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20011229174334-ejlsuilsiro5vmzr
Tags: 0.4.32-4
* fix config.{guess,sub} out of date on hppa and s390 (closes: #124296,#121830)
* try to be more descriptive in the description of the package  (closes: #115758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  CoreLinux++ 
 
3
  Copyright (C) 1999 CoreLinux Consortium
 
4
  
 
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.
 
9
 
 
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.
 
14
 
 
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.  
 
19
*/   
 
20
 
 
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
 
26
   the STL.
 
27
*/                   
 
28
 
 
29
 
 
30
#include <Common.hpp>
 
31
#include <Map.hpp>      // Defines map and multi-map
 
32
 
 
33
 
 
34
using namespace corelinux;
 
35
 
 
36
#include <iostream>
 
37
#include <exception>
 
38
 
 
39
//
 
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
 
43
// a multi-map
 
44
//
 
45
 
 
46
CORELINUX_MULTIMAP(
 
47
                   AbstractStringPtr, 
 
48
                   AbstractStringPtr, 
 
49
                   less<AbstractStringPtr>,
 
50
                   Dictionary );
 
51
 
 
52
enum  DictionaryType
 
53
{
 
54
   MAIN,
 
55
   USER
 
56
};
 
57
 
 
58
//
 
59
// Static entries to feed the dictionaries
 
60
//
 
61
 
 
62
struct   _Entry
 
63
{
 
64
   CharCptr theKey;
 
65
   CharCptr theDefinition;
 
66
};
 
67
 
 
68
DECLARE_TYPE( struct _Entry, Entry );
 
69
 
 
70
//
 
71
// In module function prototypes
 
72
//
 
73
 
 
74
int   main( void );                       
 
75
 
 
76
//
 
77
// Allow user to feed the dictionaries
 
78
//
 
79
 
 
80
void  initializeDictionary( DictionaryRef, DictionaryType );
 
81
 
 
82
//
 
83
// Generic print facility
 
84
//
 
85
 
 
86
void  dumpDictionary( DictionaryRef, AbstractStringCref );
 
87
 
 
88
//
 
89
// Allow merge into main dictionary
 
90
//
 
91
 
 
92
//
 
93
// Assertion and Exception handlers
 
94
//
 
95
 
 
96
void  handleAssertion( AssertionCref aAssert );
 
97
void  handleException( ExceptionCref );   
 
98
 
 
99
 
 
100
int main( void )
 
101
{
 
102
   //
 
103
   // Practice gracefull exception management
 
104
   //
 
105
 
 
106
   try
 
107
   {
 
108
      // Initialize the starter
 
109
 
 
110
      Dictionary  mainBook;
 
111
      initializeDictionary( mainBook, MAIN );
 
112
      dumpDictionary(mainBook,StringUtf8("Members of Main Dictionary"));
 
113
 
 
114
      // Emulate new entries added
 
115
 
 
116
      Dictionary  userBook;
 
117
      initializeDictionary( userBook, USER );
 
118
      dumpDictionary(userBook,StringUtf8("Members of User Dictionary"));
 
119
 
 
120
      //
 
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.
 
124
      //
 
125
      // You can consider which you would use based on
 
126
      // what behavior you want a merge to have. 
 
127
      //
 
128
 
 
129
      Dictionary  updatedBook(mainBook);
 
130
      updatedBook.insert(userBook.begin(),userBook.end());
 
131
      dumpDictionary(updatedBook,StringUtf8("Members of NEW Main Dictionary"));
 
132
 
 
133
      //
 
134
      // Now we do the cleanup as I don't think it will
 
135
      // happen on it's own
 
136
      //
 
137
 
 
138
      DictionaryIterator   begin( updatedBook.begin() );
 
139
      DictionaryIterator   end( updatedBook.begin() );
 
140
 
 
141
      for( ; begin != end; ++begin )
 
142
      {
 
143
         delete (*begin).first;
 
144
         delete (*begin).second;
 
145
      }
 
146
 
 
147
      updatedBook.clear();
 
148
      mainBook.clear();
 
149
      userBook.clear();
 
150
 
 
151
   }
 
152
   catch( AssertionRef aAssert )
 
153
   {
 
154
      handleAssertion(aAssert);
 
155
   }
 
156
   catch( ExceptionRef aException )
 
157
   {
 
158
      handleException(aException);
 
159
   }
 
160
   catch( std::exception & e )
 
161
   {
 
162
      cerr  << e.what() << endl;
 
163
   }
 
164
   catch( ... )
 
165
   {
 
166
      cerr  << "Unknown exception." << endl;
 
167
   }
 
168
 
 
169
   return 0;               
 
170
}
 
171
 
 
172
//
 
173
// Initialize dictionary feeds the provided collection
 
174
// with dictionary entries based on the enumerator.
 
175
// Various assertions are provided.
 
176
 
 
177
Entry mainEntries[] =
 
178
{
 
179
   {"abstract class",
 
180
      "A class whose primary purpose is to define an interface"},
 
181
   {"class",
 
182
      "A class defines an object's interface and implementation"},
 
183
   {"concrete class",
 
184
      "A class having no abstract operations"}
 
185
};
 
186
 
 
187
Entry userEntries[] =
 
188
{
 
189
   {"monkey",
 
190
      "A person who stays up late creating code examples"},
 
191
   {"dog house",
 
192
      "Where the monkey has to sleep"},
 
193
   {"concrete class",
 
194
      "The class of material that a monkey gets hit with in the dog house"}
 
195
};
 
196
 
 
197
void  initializeDictionary
 
198
   ( 
 
199
      DictionaryRef  aDictionary,
 
200
      DictionaryType aTypeDictionary
 
201
   )
 
202
{
 
203
   Count       aCount(0);        // Generic entry counter
 
204
   EntryCptr   aHead(NULLPTR);   // Generic entry pointer
 
205
 
 
206
   //
 
207
   // Determine which dictionary to feed.
 
208
   // Extension suggestion: Have input come from
 
209
   // external source (keyboard, stream, etc)
 
210
   //
 
211
 
 
212
   if( aTypeDictionary == MAIN )
 
213
   {
 
214
      aCount = sizeof( mainEntries );
 
215
      aHead = mainEntries;
 
216
   }
 
217
   else if( aTypeDictionary == USER )
 
218
   {
 
219
      aCount = sizeof( userEntries );
 
220
      aHead = userEntries;
 
221
   }
 
222
   else
 
223
   {
 
224
      // Where missing something!
 
225
 
 
226
      NEVER_GET_HERE;
 
227
   }
 
228
 
 
229
   aCount /= sizeof( Entry );
 
230
 
 
231
   //
 
232
   // Logic assertions
 
233
   //
 
234
 
 
235
   CHECK( aCount > 1 );
 
236
   CHECK( aHead != NULLPTR );
 
237
 
 
238
   // Feed the dictionary
 
239
 
 
240
   for( Count x = 0; x < aCount; ++x )
 
241
   {
 
242
      aDictionary.insert
 
243
         ( 
 
244
            Dictionary::value_type
 
245
            (
 
246
               new StringUtf8(aHead[x].theKey),
 
247
               new StringUtf8(aHead[x].theDefinition)
 
248
            )
 
249
         );
 
250
   }
 
251
 
 
252
}
 
253
 
 
254
//
 
255
// General routine for dumping a dictionary to cout
 
256
//
 
257
 
 
258
void  dumpDictionary( DictionaryRef aRef , AbstractStringCref aHeader )
 
259
{
 
260
   REQUIRE( aHeader.supportsStandardInterface() == true );
 
261
   REQUIRE( aHeader.getElementByteCount() == sizeof(Char) );
 
262
 
 
263
   const string & aStdImpl = dynamic_cast<const std::string &>(aHeader);
 
264
 
 
265
   cout  << endl << aStdImpl << endl << endl;
 
266
 
 
267
   DictionaryIterator   begin = aRef.begin();
 
268
 
 
269
   //
 
270
   // First we test to insure that we can handle the string
 
271
   // implementation using StringUtf8
 
272
   //
 
273
 
 
274
   if( (*begin).first->supportsStandardInterface() == true &&
 
275
       (*begin).first->isUtf8() == true )
 
276
   {
 
277
      DictionaryIterator   end = aRef.end();
 
278
      StringUtf8Ptr        aKey(NULLPTR);
 
279
      StringUtf8Ptr        aValue(NULLPTR);
 
280
      for( ; begin != end; ++begin )
 
281
      {
 
282
         aKey = dynamic_cast<StringUtf8Ptr>((*begin).first);
 
283
         aValue = dynamic_cast<StringUtf8Ptr>((*begin).second);
 
284
         cout  << *aKey << " = " << *aValue << endl;
 
285
      }
 
286
 
 
287
   }
 
288
   else
 
289
   {
 
290
      throw Exception("Unable to support string type",LOCATION);
 
291
   }
 
292
 
 
293
}
 
294
 
 
295
//
 
296
// Peform default (just show it)
 
297
//
 
298
 
 
299
void  handleAssertion( AssertionCref aAssert )
 
300
{
 
301
   cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" << 
 
302
      "Assertion: ";
 
303
 
 
304
   if( aAssert.getType() == Assertion::NEVERGETHERE )
 
305
   {
 
306
      cerr << "NEVER_GET_HERE";
 
307
   }
 
308
   else
 
309
   {
 
310
      if( aAssert.getType() == Assertion::REQUIRE )
 
311
      {
 
312
         cerr  << "REQUIRE";
 
313
      }
 
314
      else if( aAssert.getType() == Assertion::ENSURE )
 
315
      {
 
316
         cerr  << "ENSURE";
 
317
      }
 
318
      else if( aAssert.getType() == Assertion::CHECK )
 
319
      {
 
320
         cerr  << "CHECK";
 
321
      }
 
322
      else 
 
323
      {
 
324
         cerr  << "ASSERT";
 
325
      }
 
326
      cerr << "( " << aAssert.getWhy() << " )";
 
327
   }
 
328
 
 
329
   cerr << endl;
 
330
}
 
331
 
 
332
void  handleException( ExceptionCref aExcp )
 
333
{
 
334
   cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
 
335
      "Exception: " << aExcp.getWhy() << endl;
 
336
}
 
337
 
 
338
/*
 
339
   Common rcs information do not modify
 
340
   $Author: prudhomm $
 
341
   $Revision: 1.5 $
 
342
   $Date: 2000/08/31 22:49:01 $
 
343
   $Locker:  $
 
344
*/
 
345
 
 
346