Re: [gtk-list] Re: Need help in GTK-project
- From: Derek Simkowiak <dereks kd-dev com>
- To: gtk-list redhat com
- Subject: Re: [gtk-list] Re: Need help in GTK-project
- Date: Sat, 11 Dec 1999 15:42:41 -0800 (PST)
> changed. So what I'm trying to figure out if there's another way to set
> the portstatus then declaring portstatus global or using functions that
> take pointers as their argument...
You *want* to have function that take a pointer as their argument.
You'll also want your API functions to return a pointer. Examine glib for
excellent examples of this methodology. You'll want to do something like
this:
In portstatus.h:
----------------
#include <glib.h>
typedef guint PortStatus;
/* port_status_set()
Description: This function sets the value of a "PortStatus" item, where
the value is a guint. Currently, a PortStatus is simply implemented as a
glib guint.
Preconditions: none.
Postconditions: none.
Inputs: *port_stat A pointer to the port_stat you want to set
value The value you want to set the port_stat to
Outputs: A pointer to the PortStatus with the correct value set, or NULL
if there was an error.
*/
PortStatus* port_status_set_value(PortStatus* port_stat, guint value);
/* port_status_get_value()
Description: Gives the values of the portstatus as a guint.
Preconditions: port_stat must be non-null.
Postconditions: none.
Inputs: *port_stat The port_stat whose value you wish to get
Outputs: The value of the PortStatus as a guint, or -1 if there's an
error.
*/
guint port_status_get_value(PortStatus* port_stat);
(and then,)
In portstatus.c:
----------------
#include portstatus.h
PortStatus* port_status_set(PortStatus* port_stat, guint value) {
/* Were we passed an invalid port_stat pointer? */
if (port_stat == NULL) return NULL;
*port_stat = value;
return port_stat;
}
[...similarly trivial function definition for port_status_get()...]
This may seem like a lot of work just to set an int, but if you
use the above example you will have successfully abstracted a "PortStatus"
into an object with an interface.
In the future, you may decide that a PortStatus should be a long
instead of an unsigned int. Or even more likely, that PortStatus should
be a struct containing all sorts of interesting state information,
including speed, average data rate, total number of packets, or maybe even
a Gtk widget. If that happens, the ONLY code you'll need to change is in
portstatus.h and portstatus.c. All of your other code will remain fully
functional, as long as they use the PortStatus API (which, so far, only
has a couple of functions.) This is called re-usable, modular code, and
it's the goal of every software developer.
Again, I have not looked at your app (I'm not really that
interested:) but this may not be the best abstraction for your program.
For one, instead of having the get/set functions take (and return) a gint,
I'd abstract the value of a "PortStatus" into "PortStatusValue", which
would be an enumerated list of items.
What you should probably do is abstract a "Port" item, which could
then have the functions "port_get_status()" and "port_set_status", along
with "port_set_speed", "port_get_speed", etc. But that's just my
assumption based on my past work with ports. You'll need to design your
own data abstractions :).
Download, and look at, the Glib source code. It's the best
C-based example of modular programming I've ever seen (with the minor
exception that there is one large header file, instead of one for each .c
file).
Good Luck,
Derek Simkowiak
dereks@kd-dev.com
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]