473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

globals again

Hello!

<Background only>
Globals cannot always be avoided - in OS's with a graphical user interface
functions in an application are called from the OS asynchronously, and if
one of these functions needs the result of another function there seems to
be no way to avoid storing the result of function 1 in some global and
access it later from function 2
</Background only>
My way to deal with this situation is, to put those and only those functions
which need the same bunch of globals together into one compilation unit and
declare the globals for them static and as late as possible like in:
(pseudocode example, not necessarily "real life")

-Is there any better way to do this?

#include <whatever necessary>

static FILE *fp;
static unsigned int stream_ready;
void OpenFile(char *file_name, char mode) /*called when the user requests
the action*/
{
<open the file, store the filepointer and a success indicator>
} /* OpenFile */

static char read_buffer[<SOME_SIZE>];
static size_t buffer_size = sizeof line_buffer;
static int line_complete;
static char *line_buffer;
void ReadLine( size_t buffer_size)
{
char *fgets_result = NULL;
if(stream_ready)
{
fgets_result = fgets(read_buffer, buffer_size, fp);
if(fgets_result)
{
<pre - process the read_buffer, strip '\n', cat with the next line
if no '\n',
allocate memory for the complete line etc>
}
else
{
<the usual eof / error handling>
}
}
else
{
<do the necessary recovery actions>
}
} /* ReadLine */

void ProcessLine(void)
{
<do whatever necessary with the content of the line_buffer>
} /* ProcessLine */

void CloseFile(void)
{
if(fp)
{
fclose(fp);
fp = NULL;
}
} /* CloseFile */
Thank you for taking the time
Robert
Jun 12 '06 #1
6 1934
Geometer wrote:
Globals cannot always be avoided - in OS's with a graphical user interface
functions in an application are called from the OS asynchronously, and if
one of these functions needs the result of another function there seems to
be no way to avoid storing the result of function 1 in some global and
access it later from function 2


If your callback function did the whole action (open file, read
contents, close it, do something based on contents), instead of just
opening the file, you wouldn't need any globals. At what point are you
going to call your ProcessLine() function?

Glenn

Jun 12 '06 #2
in 680917 20060612 073749 "Glenn Hutchings" <zo*****@googlemail.com> wrote:
Geometer wrote:
Globals cannot always be avoided - in OS's with a graphical user interface
functions in an application are called from the OS asynchronously, and if
one of these functions needs the result of another function there seems to
be no way to avoid storing the result of function 1 in some global and
access it later from function 2


If your callback function did the whole action (open file, read
contents, close it, do something based on contents), instead of just
opening the file, you wouldn't need any globals. At what point are you
going to call your ProcessLine() function?

Glenn


You still need to pass in the file name.
Jun 12 '06 #3
"Glenn Hutchings" <zo*****@googlemail.com> schrieb im Newsbeitrag
news:11*********************@c74g2000cwc.googlegro ups.com...
Geometer wrote:
Globals cannot always be avoided - in OS's with a graphical user
interface
functions in an application are called from the OS asynchronously, and if
one of these functions needs the result of another function there seems
to
be no way to avoid storing the result of function 1 in some global and
access it later from function 2
If your callback function did the whole action (open file, read
contents, close it, do something based on contents), instead of just
opening the file, you wouldn't need any globals. At what point are you
going to call your ProcessLine() function?


Hi, Glenn,
First my apologies if this message appears twice - I resend it because
it did not show up for quite a time...

This pseudo-code was just for illustration and my question is more general.
In real life various functions are called either because the user clicks
some control, a timer expires, an i/o operation completes, another
application sends a message or requests data etc. iow everything runs
asynchronously.
Usually the problem comes up, when a user starts an action and then has a
tree of choices
how to proceed, and I am only notified about the kind of the user's choice. Glenn

Glenn

Jun 12 '06 #4

Geometer wrote:
This pseudo-code was just for illustration and my question is more general.
In real life various functions are called either because the user clicks
some control, a timer expires, an i/o operation completes, another
application sends a message or requests data etc. iow everything runs
asynchronously.
Usually the problem comes up, when a user starts an action and then has a
tree of choices
how to proceed, and I am only notified about the kind of the user's choice.


Usually, there is a mean to have a "context" argument passed to a
callback function.
This parameter is used as pointer to a user-defined object.

There are also OO interfaces such as MFC or wxWidgets, where a method
of a class is directly called.

The Win32 API is not very intuitive for that : You must store and
retrieve the user-defined data via SetWindowLong/GetWindowLong using
the GWL_USERDATA index.
Under Windows, you can also register a class and specify extra bytes
for each window (cbWndExtra member of WINDOWCLASS or WINDOWCLASSEX).

Except if an UI system is really badly designed, there should always be
a mean to pass/retrieve a context parameter.
Just read the documentation of your UI system.

Jun 12 '06 #5
Bob Martin wrote:
in 680917 20060612 073749 "Glenn Hutchings" <zo*****@googlemail.com> wrote:
Geometer wrote:
Globals cannot always be avoided - in OS's with a graphical user interface
functions in an application are called from the OS asynchronously, and if
one of these functions needs the result of another function there seems to
be no way to avoid storing the result of function 1 in some global and
access it later from function 2


If your callback function did the whole action (open file, read
contents, close it, do something based on contents), instead of just
opening the file, you wouldn't need any globals. At what point are you
going to call your ProcessLine() function?

Glenn


You still need to pass in the file name.


True... but that can either come from prompting via a modal dialog box
or the text in a widget displayed on the UI somewhere. Still no need
for a global.

Jun 12 '06 #6
Geometer wrote:
This pseudo-code was just for illustration and my question is more general.
In real life various functions are called either because the user clicks
some control, a timer expires, an i/o operation completes, another
application sends a message or requests data etc. iow everything runs
asynchronously.
Usually the problem comes up, when a user starts an action and then has a
tree of choices
how to proceed, and I am only notified about the kind of the user's choice.


For actions where there could be a large time gap between events, yes,
you need to keep track of the current state of things. But sometimes
there are ways other than global variables to do it: for example,
updating a database, or setting the contents of a widget. It's hard to
be specific, because it depends on the situation at hand.

In my own case, I try to avoid getting UI programs to do anything
time-consuming -- the UI just fires up a command-line program and
watches to see when it's finished. So I don't usually run into these
sorts of complexity (and it makes testing a lot simpler :-)

Glenn

Jun 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
6
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
2
by: DFS | last post by:
Not sure whether it's bad programming practice or not, but I have a module of globals I declare in each system: Global ws As Workspace Global db As Database Global td As TableDef Global rs As...
0
by: Geert Jansen | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm experiencing a weird problem with eval(), providing it a copy of the globals() dictionary. The following code works:
17
by: meridian | last post by:
I thought I had 'got' globals but this one seems strange. I want my example function 'doIt' to use and optionally modify a module variable 'gname', so I declare 'global gname' in the function, but...
8
by: Michael Wild | last post by:
Hi all I'm not quite new to PHP, but also not very proficient in its use, so please excuse me if my question is a FAQ. Perhaps I didn't know the right terms, but google wasn't my friend this...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
3
by: r0g | last post by:
Hi There, I'm refactoring some old code that uses global variables and was originally written in one big flat file with a view to nicening it up and then extending it. The problem I have though...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.