source: rtems/c/src/tests/samples/cdtest/main.cc @ 5a23ca84

4.104.114.84.95
Last change on this file since 5a23ca84 was ad502d18, checked in by Joel Sherrill <joel.sherrill@…>, on 11/09/99 at 22:14:21

Renamed classes to avoid single letter class names.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[e71ce071]1/*
[ac7d5ef0]2 *  This routine is the initialization task for this test program.
3 *  It is called from init_exec and has the responsibility for creating
4 *  and starting the tasks that make up the test.  If the time of day
5 *  clock is required for the test, it should also be set to a known
6 *  value by this function.
7 *
8 *  Input parameters:  NONE
9 *
10 *  Output parameters:  NONE
11 *
12 *  COPYRIGHT (c) 1994 by Division Incorporated
13 *  Based in part on OAR works.
[e71ce071]14 *
[98e4ebf5]15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
[e71ce071]17 *  http://www.OARcorp.com/rtems/license.html.
[ac7d5ef0]18 *
[fcf17a4]19 *
20 *  by Rosimildo da Silva:
21 *  Modified the test a bit to indicate when an instance is
22 *  global or not, and added code to test C++ exception.
23 *
24 *
[ac7d5ef0]25 *  $Id$
26 */
[fcf17a4]27// #define RTEMS_TEST_IO_STREAM
[ac7d5ef0]28
29#include <rtems.h>
30#include <stdio.h>
[2936b425]31#include <stdlib.h>
[91333c2]32#ifdef RTEMS_TEST_IO_STREAM
[ac7d5ef0]33#include <iostream.h>
[91333c2]34#endif
[ac7d5ef0]35
[fcf17a4]36extern "C"
37{
38#include <tmacros.h>
[ac7d5ef0]39extern rtems_task main_task(rtems_task_argument);
40}
41
42static int num_inst = 0;
43
[ad502d18]44class AClass {
[ac7d5ef0]45public:
[ad502d18]46  AClass(const char *p = "LOCAL" ) : ptr( p )
[ac7d5ef0]47    {
48        num_inst++;
49        printf(
[fcf17a4]50          "%s: Hey I'm in base class constructor number %d for %p.\n",
51          p, num_inst, this
[ac7d5ef0]52        );
53
54        /*
55         * Make sure we use some space
56         */
57
58        string = new char[50];
59        sprintf(string, "Instantiation order %d", num_inst);
60    };
61
[ad502d18]62    virtual ~AClass()
[ac7d5ef0]63    {
64        printf(
[fcf17a4]65          "%s: Hey I'm in base class destructor number %d for %p.\n",
66          ptr, num_inst, this
[ac7d5ef0]67        );
68        print();
69        num_inst--;
70    };
71
72    virtual void print()  { printf("%s\n", string); };
73
74protected:
75    char  *string;
[fcf17a4]76    const char *ptr;
[ac7d5ef0]77};
78
[ad502d18]79class BClass : public AClass {
[ac7d5ef0]80public:
[ad502d18]81  BClass(const char *p = "LOCAL" ) : AClass( p )
[ac7d5ef0]82    {
83        num_inst++;
84        printf(
[fcf17a4]85          "%s: Hey I'm in derived class constructor number %d for %p.\n",
86          p, num_inst,  this
[ac7d5ef0]87        );
88
89        /*
90         * Make sure we use some space
91         */
92
93        string = new char[50];
94        sprintf(string, "Instantiation order %d", num_inst);
95    };
96
[ad502d18]97    ~BClass()
[ac7d5ef0]98    {
99        printf(
[fcf17a4]100          "%s: Hey I'm in derived class destructor number %d for %p.\n",
101          ptr, num_inst,
[ac7d5ef0]102          this
103        );
104              print();
105        num_inst--;
106    };
107
108    void print()  { printf("Derived class - %s\n", string); }
109};
110
111
[fcf17a4]112class RtemsException
113{
114public:
115   
116    RtemsException( char *module, int ln, int err = 0 )
117    : error( err ), line( ln ), file( module )
118    {
119      printf( "RtemsException raised=File:%s, Line:%d, Error=%X\n",
120               file, line, error );
121    }
122
123    void show()
124    {
125      printf( "RtemsException ---> File:%s, Line:%d, Error=%X\n",
126               file, line, error );
127    }
128
129private:
130   int  error;
131   int  line;
132   char *file;
133
134};
135
136
137
[ad502d18]138AClass foo( "GLOBAL" );
139BClass foobar( "GLOBAL" );
[ac7d5ef0]140
141void
142cdtest(void)
143{
[ad502d18]144    AClass bar, blech, blah;
145    BClass bleak;
[ac7d5ef0]146
[91333c2]147#ifdef RTEMS_TEST_IO_STREAM
[ac7d5ef0]148    cout << "Testing a C++ I/O stream" << endl;
[91333c2]149#else
150    printf("IO Stream not tested\n");
151#endif
[ac7d5ef0]152    bar = blech;
[fcf17a4]153    rtems_task_wake_after( 5 * get_ticks_per_second() );
[ac7d5ef0]154}
155
156//
157// main equivalent
158//      It can not be called 'main' since the bsp owns that name
159//      in many implementations in order to get global constructors
160//      run.
161//
162
[fcf17a4]163static void foo_function()
164{
165    try
166    {
167      throw "foo_function() throw this exception"; 
168    }
169    catch( const char *e )
170    {
171     printf( "foo_function() catch block called:\n   < %s  >\n", e );
172     throw "foo_function() re-throwing execption..."; 
173    }
174}
[ac7d5ef0]175
176rtems_task main_task(
[61fc72c]177  rtems_task_argument
[ac7d5ef0]178)
179{
180    printf( "\n\n*** CONSTRUCTOR/DESTRUCTOR TEST ***\n" );
181
182    cdtest();
183
184    printf( "*** END OF CONSTRUCTOR/DESTRUCTOR TEST ***\n\n\n" );
185
[fcf17a4]186
187    printf( "*** TESTING C++ EXCEPTIONS ***\n\n" );
188
189    try
190    {
191      foo_function();
192    }
193    catch( const char *e )
194    {
195       printf( "Success catching a char * exception\n%s\n", e );
196    }
197    try
198    {
199      printf( "throw an instance based exception\n" );
200                throw RtemsException( __FILE__, __LINE__, 0x55 );
201    }
202    catch( RtemsException & ex )
203    {
204       printf( "Success catching RtemsException...\n" );
205       ex.show();
206    }
207    catch(...)
208    {
209      printf( "Caught another exception.\n" );
210    }
211    printf( "Exceptions are working properly.\n" );
212    rtems_task_wake_after( 5 * get_ticks_per_second() );
213    printf( "Global Dtors should be called after this line....\n" );
[ac7d5ef0]214    exit(0);
215}
Note: See TracBrowser for help on using the repository browser.