Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
OrbWorks Community Forum • View topic - PointerList

PointerList

Discuss and publish sample code and applications written in OrbC (OrbForms Designer or PocketC Architect)

Postby CloneX on Mon Jul 29, 2002 1:49 am

The following code illustrates how to wrap the built in StringList into an object that can provide a list for another type, in this case a void pointer. The interface is the same as StringList (with the exception of token() which has no obvious analogue for pointers). There is a small performance hit since the entries must be converted to and from strings but in most cases this is negligible.

Note that this list does not own the objects that it points to. If you want the list to own the objects be sure to provide a _destroy() function to loop through the list destroying the objects to avoid memory leaks.

This code can easily be modified to create versions to handle any of the fundamantal types (int, float, char, bool) as well as pointers to specific types of objects.

<pre>
// PointerList - void pointer list with interface matching StringList

object PointerList
{
void clear();
int add(void *pt);
int insert(int index, void *pt);
int del(int index);
int find(void *pt);
void *item(int index);

StringList list;
int count;
};

void PointerList.clear()
{
list.clear(); // clear list
count=0;
}

int PointerList.add(void *pt)
{
int temp=(int)pt; // cast pointer to int
count=list.add(temp); // add to list (autoconverts to string)
return count; // return number of items in list
}

int PointerList.insert(int index, void *pt)
{
int temp=(int)pt; // cast pointer to int
count=list.insert(index, temp); // insert in list (autoconverts to string)
return count; // return number of items in list
}

int PointerList.del(int index)
{
count=list.del(index); // delete entry
return count; // return number of items in list
}

int PointerList.find(void *pt)
{
int temp=(int)pt; // cast pointer to int
return list.find(temp); // return index in list
}

void *PointerList.item(int index)
{
int temp=list.item(index); // get item from list
return (void *)temp; // return pointer (NULL if not found)
}
</pre>
CloneX
 
Posts: 2
Joined: Fri Jul 12, 2002 12:01 pm

Postby design4use on Mon Jul 29, 2002 5:52 am

clone X:
this looks interesting. However as a beginner I have no idea what this can be used for and what the advantage of this technique is over others. Would you care to provide a bit of context? Thx.
design4use
 
Posts: 128
Joined: Fri May 10, 2002 3:31 pm

Postby CloneX on Mon Jul 29, 2002 12:47 pm

There are a number of cases where a list is more convenient than an array:
1) the number of items is not known at compile time
2) the number of items changes over time - items are added or removed as a result of user interaction

It is also more convenient than an array if you are passing the collection as an argument to a function - if you pass an array of values you must either have a second argument that says how many items there are or you must use some other convention (such as a NULL terminator) to signal the end of the array so that the function can determine the number of items. By using a list the function knows how many items there are (by checking the count member variable).

Here's an example where I use the PointerList: I have a Gadget that displays a single numeric digit (it uses bitmaps to give the digits an LED/LCD segmented look). I defined a MultiLCDDigit object to handle multiple digit numbers. The object uses a PointerList to hold pointers to the instances of the LCD digit gadgets. I simply add the digit pointers to the MultiLCDDigit object - I can add as many or as few digits as I need based on where I'm using the display & the code automatically handles it.

In short, the list is a more convenient way to hold a collection of items than an array. You pay a small price in performance & memory use but generally the flexibility offered outweighs the cost.

Hope this helps...
CloneX
 
Posts: 2
Joined: Fri Jul 12, 2002 12:01 pm

Postby design4use on Mon Jul 29, 2002 4:12 pm

It does help. Thanks. I was always wondering how to dynamically add or remove items from arrays, this is a good way of accomplishing that. Thanks for taking the time to explain!
design4use
 
Posts: 128
Joined: Fri May 10, 2002 3:31 pm


Return to OrbC User Samples

Who is online

Users browsing this forum: No registered users and 0 guests

cron