2 * Simulator of microcontrollers (pobj.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 /*#include "pobjt.h"*/
39 ==========================================================================*
41 ==========================================================================*
46 * Initializing the object
49 cl_base::cl_base(void)
58 * Destructing the object: calling hte virtual Done method
61 cl_base::~cl_base(void)
68 for (i= 0; i < children->count; i++)
71 children->disconn_all();
77 int cl_base::init(void) {return(0);}
80 cl_base::get_name(char *def)
88 cl_base::set_name(char *new_name)
95 name= strdup(new_name);
102 cl_base::set_name(char *new_name, char *def_name)
110 def= strdup(def_name);
116 name= strdup(new_name);
123 cl_base::is_named(char *the_name)
130 return(strcmp(name, the_name) == 0);
134 cl_base::is_inamed(char *the_name)
141 return(strcasecmp(name, the_name) == 0);
146 cl_base::nuof_children(void)
150 return(children->count);
154 cl_base::add_child(class cl_base *child)
159 s= (char*)malloc(strlen(get_name("?"))+100);
160 sprintf(s, "childs of %s", get_name("?"));
161 children= new cl_list(1, 1, s);
166 children->add(child);
172 cl_base::remove_child(class cl_base *child)
178 children->disconn(child);
183 cl_base::remove_from_chain(void)
186 parent->remove_child(this);
190 cl_base::unlink(void)
196 cl_base::first_child(void)
199 children->count == 0)
201 return(dynamic_cast<class cl_base *>(children->object_at(0)));
205 cl_base::next_child(class cl_base *child)
210 return((class cl_base *)(children->next(child)));
215 cl_base::handle_event(class cl_event &event)
217 return(pass_event_down(event));
221 cl_base::pass_event_down(class cl_event &event)
226 for (i= 0; i < children->count; i++)
228 class cl_base *child=
229 dynamic_cast<class cl_base *>(children->object_at(i));
232 child->handle_event(event);
233 if (event.is_handled())
245 cl_event::cl_event(enum event what_event):
252 cl_event::~cl_event(void)
258 ==========================================================================*
260 ==========================================================================*
265 * Initializing a collection
268 cl_list::cl_list(t_index alimit, t_index adelta, char *aname):
276 set_name(aname, "unnamed list");
281 * Disposing object's variables
284 cl_list::~cl_list(void)
292 * Get indexed item from the collection
296 cl_list::at(t_index index)
301 return(Items[index]);
305 cl_list::object_at(t_index index)
310 return((class cl_base *)(Items[index]));
314 cl_list::operator[](t_index index)
319 return(Items[index]);
324 * Deleting the indexed item from the collection
328 cl_list::disconn_at(t_index index)
336 sprintf(s, "disconn_at(%d) PC=0x%x", index,
339 ((application->sim->uc)?(application->sim->uc->PC):
344 memmove(&Items[index], &Items[index+1], (count-index)*sizeof(void *));
349 * Deleting an item from the collection but not disposing it
353 cl_list::disconn(void *item)
357 if (index_of(item, &i))
363 * Deleting all the items from the collection but not disposing them
367 cl_list::disconn_all(void)
374 * Deleting the indexed item from the collection and disposing it
378 cl_list::free_at(t_index index)
380 void *Item= at(index);
387 cl_list::free_all(void)
393 for (i= count-1; i; i--)
401 * Inserting a new item to the exact position
405 cl_list::add_at(t_index index, void *item)
410 set_limit(count + Delta);
414 sprintf(s, "%s add_at(%d,%p) PC=0x%x (count=%d)", get_name("?"), index, item,
417 ((application->sim->uc)?(application->sim->uc->PC):
422 memmove(&Items[index+1], &Items[index], (count-index)*sizeof(void *));
430 * Put a new item to the collection. This function replaces an existing
431 * item with a new one but it does not delete or dispose the old item!
435 cl_list::put_at(t_index index, void *item)
444 * Action taken when an error occure
448 cl_list::error(t_index code, t_index info)
451 "Collection index error. Code= %d, Info= %d.\n",
458 * Iterator method. This function calls 'Test' using every items as Test's
459 * argument until Test returns TRUE.
463 cl_list::first_that(match_func test, void *arg)
465 for (t_index i= 0; i < count; i++)
467 if (test(Items[i], arg)) return(Items[i]);
474 * Iterator method. This function calls 'Action' using every items as
479 cl_list::for_each(iterator_func action, void *arg)
481 for(t_index i= 0; i < count; i++)
482 action(Items[i], arg);
491 cl_list::free_item(void *item)
493 delete (class cl_base*)item;
498 * Get the number of collected items.
502 cl_list::get_count(void)
529 * Returning the index of an item.
533 cl_list::index_of(void *item)
535 for (t_index i= 0; i < count; i++)
536 if (item == Items[i])
539 return(0); /* Needed by Sun! */
543 cl_list::index_of(void *item, t_index *idx)
545 for (t_index i= 0; i < count; i++)
546 if (item == Items[i])
556 cl_list::next(void *item)
558 for (t_index i= 0; i < count; i++)
559 if (item == Items[i])
570 * Inserting a new item to the collection.
574 cl_list::add(void *item)
583 cl_list::add(class cl_base *item, class cl_base *parent)
586 parent->add_child(item);
591 cl_list::push(void *item)
601 * Iterator method. This function calls 'Test' using every items
602 * (in reversed order) as Test's argument until Test returns TRUE.
606 cl_list::last_that(match_func test, void *arg)
608 for(t_index i= count; i > 0; i--)
609 if (test(Items[i-1], arg))
622 void **CurDst= Items;
623 void **CurSrc= Items;
624 void **Last = Items + count;
626 while (CurSrc < Last)
636 * Setting up the maximum number of items. This function may expand
637 * the size of the collection.
641 cl_list::set_limit(t_index alimit)
647 if (alimit > (int)max_list_size)
648 alimit= max_list_size;
655 //AItems = new void *[alimit];
656 int i= alimit*(sizeof(void *));
657 AItems= (void **)malloc(i);
659 memcpy(AItems, Items, count*sizeof(void *));
670 ==========================================================================*
672 ==========================================================================*
677 * Initilizing the sorted collection
680 cl_sorted_list::cl_sorted_list(t_index alimit, t_index adelta, char *aname):
681 cl_list(alimit, adelta, aname)
683 Duplicates= DD_FALSE;
687 cl_sorted_list::~cl_sorted_list(void) {}
691 * Get the address of the key field in an item.
695 cl_sorted_list::key_of(void *item)
702 * Get index of an item.
706 cl_sorted_list::index_of(void *item)
710 if (search(key_of(item), i) == 0)
727 * Inserting a new item to the collection
731 cl_sorted_list::add(void *item)
735 if (search(key_of(item), i) == 0 ||
736 Duplicates) // order dependency!
737 add_at(i, item); // must do Search
745 * Searching an item using binary search.
749 cl_sorted_list::search(void *key, t_index &index)
752 t_index h = count - 1;
757 t_index i= (l + h) >> 1;
758 t_index c= compare(key_of(Items[i]), key);
777 ==========================================================================*
779 ==========================================================================*
784 * Initilizing the string collection
787 cl_strings::cl_strings(t_index alimit, t_index adelta, char *aname):
788 cl_sorted_list(alimit, adelta, aname)
794 cl_strings::~cl_strings(void) {}
798 * Comapare two string from the collection
802 cl_strings::compare(void *key1, void *key2)
804 return(strcmp((char *)key1, (char *)key2));
809 * Deallocate string item of the collection
813 cl_strings::free_item(void* item)
815 delete (class cl_base*)item;
820 ==========================================================================*
822 ==========================================================================*
827 * Initilizing the unsorted string collection
830 cl_ustrings::cl_ustrings(t_index alimit, t_index adelta, char *aname):
831 cl_strings(alimit, adelta, aname)
835 cl_ustrings::~cl_ustrings(void) {}
839 * Comapare two string from the collection
843 cl_ustrings::compare(void *key1, void *key2)
850 * Searching an item using linear search.
854 cl_ustrings::search(void *key, t_index& index)
857 bool found= DD_FALSE;
862 while (!found && (i < count))
864 Actual= key_of(at(i));
865 found = (Actual != 0) &&
866 (compare(key, Actual) == 0);