squashed commit of private hostlib-resource branch
[tangerine.git] / test / OOPDemos / timerclass.c
blob7abebc5146ae4aafe0e3cee2b5e0f7db82943333
1 /*
2 Copyright © 1997-98, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Demo of new OOP system
6 Lang: english
7 */
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include "oop.h"
13 #include "protos.h"
14 #include "timerclass.h"
15 #include "sysdep/sysdep.h"
17 #define SDEBUG 0
18 #define DEBUG 0
19 #include "debug.h"
21 struct TimerData
23 struct timeval start_time;
24 struct timeval elapsed_time;
27 /* Does dest - src => dest */
28 VOID SubTime(struct timeval *dest, struct timeval *src)
30 /* Normalize the terms */
31 while(src->tv_usec > 999999)
33 src->tv_sec++;
34 src->tv_usec -= 1000000;
36 while(dest->tv_usec > 999999)
38 dest->tv_sec++;
39 dest->tv_usec -= 1000000;
42 dest->tv_usec -= src->tv_usec;
43 dest->tv_sec -= src->tv_sec;
45 if(dest->tv_usec < 0)
47 dest->tv_usec += 1000000;
48 dest->tv_sec--;
51 return;
54 VOID _Timer_Start(Class *cl, Object *o, Msg msg)
56 struct TimerData *data;
57 EnterFunc(bug("Timer::Start(cl=%p, o=%p)\n", cl, o));
58 D(bug("data=%p\n", data));
60 data = INST_DATA(cl, o);
61 D(bug("data=%p\n", data));
63 gettimeofday(&(data->start_time), NULL);
65 ReturnVoid("Timer::Start");
68 VOID _Timer_Stop(Class *cl, Object *o, Msg msg)
70 struct TimerData *data = INST_DATA(cl, o);
71 gettimeofday(&(data->elapsed_time), NULL);
73 SubTime(&(data->elapsed_time), &(data->start_time));
75 return;
78 VOID _Timer_PrintElapsed(Class *cl, Object *o, Msg msg)
80 struct TimerData *data = INST_DATA(cl, o);
82 printf("%d secs and %d micros\n"
83 ,data->elapsed_time.tv_sec
84 ,data->elapsed_time.tv_usec);
88 IPTR _Timer_TestMethod(Class *cl, Msg msg)
90 return (12345678);
94 Class *MakeTimerClass()
97 #if (HASHED_STRINGS)
98 struct MethodDescr mdescr[] =
100 { (IPTR (*)())_Timer_Start, M_Timer_Start },
101 { (IPTR (*)())_Timer_Stop, M_Timer_Stop },
102 { (IPTR (*)())_Timer_PrintElapsed, M_Timer_PrintElapsed },
103 { (IPTR (*)())_Timer_TestMethod, M_Timer_TestMethod },
106 struct InterfaceDescr ifdescr[]=
108 { mdescr, "Timer", 4},
109 { NULL, 0UL, 0UL}
112 #endif
113 #if (HASHED_IFS || HASHED_METHODS)
114 IPTR (*methods[])() =
116 (IPTR (*)())_Timer_Start,
117 (IPTR (*)())_Timer_Stop,
118 (IPTR (*)())_Timer_PrintElapsed,
119 (IPTR (*)())_Timer_TestMethod,
122 struct InterfaceDescr ifdescr[] =
124 { methods, I_Timer, 4},
125 { NULL, 0UL, 0UL}
128 #endif
130 Class *cl;
132 cl = MakeClass(TIMERCLASS, ROOTCLASS, ifdescr, sizeof (struct TimerData));
133 if (cl)
135 AddClass(cl);
138 return (cl);
141 VOID FreeTimerClass(Class *cl)
143 RemoveClass(cl);
144 FreeClass(cl);