Bug fix: check if vm exists
[avr-sim.git] / Clock.inc
blob2167d9a4040e1eba28dfd14a836d2c9d5cc319b0
1 #ifndef AVR_CLOCK_INC
2 #define AVR_CLOCK_INC
4 namespace avr {
6         template <class Type>
7         void Clock<Type>::setBreak(ClockOffset cycle, Type *obj) {
8                 ClockListIt it = objects.begin();
9                 while( it != objects.end() ) {
10                         if( it->first >= cycle )
11                                 break;
13                         it++;
14                 }
16                 objects.insert( it, std::make_pair(cycle, obj) );
17         }
19         template <class Type>
20         void Clock<Type>::clearBreak(Type *obj) {
21                 ClockListIt it = objects.begin();
22                 while( it != objects.end() ) {
23                         if( it->second == obj )
24                                 break;
26                         it++;
27                 }
29                 if( it != objects.end() )
30                         objects.erase( it );
31         }
33         template <class Type>
34         void Clock<Type>::clearBreak(ClockOffset cycle) {
35                 ClockListIt it = objects.begin();
36                 while( it != objects.end() ) {
37                         if( it->first >= cycle )
38                                 break;
40                         it++;
41                 }
43                 if( (it != objects.end()) && (it->first == cycle) )
44                         objects.erase( it );
45         }
47         template <class Type>
48         void Clock<Type>::reassignBreak(ClockOffset oldCycle, ClockOffset newCycle,
49                                                                         Type *obj) {
51                 ClockListIt it = objects.begin();
52                 while( it != objects.end() ) {
53                         if( it->first >= oldCycle )
54                                 break;
55                         else
56                                 it++;
57                 }
59                 if( (it != objects.end()) && (it->first == oldCycle) ) {
60                         objects.erase( it );
61                         setBreak( newCycle, obj );
62                 }
63         }
65         template <class Type>
66         void Clock<Type>::reassignBreak(Type *obj, ClockOffset newCycle) {
67                 clearBreak(obj);
68                 setBreak( newCycle, obj );
69         }
71         template <class Type>
72         void Clock<Type>::step() {
73                 value++;
75                 ClockListIt it = objects.begin();
76                 while( it != objects.end() ) {
77                         if( it->first >= value )
78                                 break;
80                         Type *obj = it->second;
81                         obj->step();
82                         it++;
83                 }
85                 objects.erase( objects.begin(), it );
86         }
88         template <class Type>
89         void Clock<Type>::clearAll() {
90                 objects.clear();
91         }
93         template <class Type>
94         void Clock<Type>::reassignBreakDelta(ClockOffset delta, Type *obj) {
95                 clearBreak(obj);
96                 setBreakDelta(delta, obj);
97         }
100 #endif