Re: global singleton object ? [message #26874] |
Wed, 03 October 2001 09:59  |
R.G.S.
Messages: 46 Registered: September 2000
|
Member |
|
|
Pavel A. Romashkin <pavel.romashkin@noaa.gov> wrote in message
news:3BBB2D52.346B882B@noaa.gov...
> Bob,
>
> The following URL points to a tiny file that does what you want.
> http://spot.colorado.edu/~romashki/idl/sobj_new.sav
> The SOBJ_NEW does not call OBJ_NEW is a singleton object already exists,
> thus avoiding OBJ_NEW overhead.
> I also recommend checking out
> http://spot.colorado.edu/~romashki/idl/single_set.sav
> which provides essentially the same services except the contents can be
> of any type, not just object. SINGLE_SET does not loop through heap
> variables, fetching the only one (the named one) of interest.
> The actual code is too embarassing to share, JD would not let me on the
> newsgroup again :-(
>
> s = sobj_new(/help) ; How to use it
> s = sobj_new() ; Make new object
> d = sobj_new() ; Get a reference to it again
> help, s, d ; Make sure they are the same
> ; Set NO_KILL to see what it does
> s -> store, obj_new('IDLgrModel'), /no_kill
> s -> store ; Take a look at contents...
> d -> store ; They are the same
> obj_destroy, s ; And kill it (by accident).
> d = sobj_new() ; NO_KILL was set, so it did not die.
> ; But NO_KILL is NOT set in D now, so that heap-gc, if
> ; ever used, does not go into an infinite loop.
> d -> store ; Make sure contents are the same
> ; except for NO_KILL setting. Set it again if you want.
>
> Cheers,
> Pavel
>
> P.S. The only use for a Singleton (or SINGLE_SET) that I can see is
> avoiding Common blocks while
> still allowing sharing some information between widget programs that may
or
> may not be launched independently, without using linking arguments. Or, as
> people asked before, keeping a Preferences structure for the duration of a
> session. When I wrote Display routines, I had to use Common block to keep
> track of linked displays and the topmost one because they were intended to
> be used interactively, and there is no way to define how many of them are
> present and in which order. A singleton would allow this without a common
> block.
Thanks for the reply!
As for why a singleton is useful, just wait til IDL is multithreaded. When
you
have multiple processes that need acces to information (info that can be
changed by the user) then a singleton object is very nice! Very Very Nice.
Cheers,
bob stockwell
PS BUFFY LIVES!
ack, geek cover blown, get out of that newsgroup now!
|
|
|
Re: global singleton object ? [message #26887 is a reply to message #26874] |
Wed, 03 October 2001 08:22   |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
Bob,
The following URL points to a tiny file that does what you want.
http://spot.colorado.edu/~romashki/idl/sobj_new.sav
The SOBJ_NEW does not call OBJ_NEW is a singleton object already exists,
thus avoiding OBJ_NEW overhead.
I also recommend checking out
http://spot.colorado.edu/~romashki/idl/single_set.sav
which provides essentially the same services except the contents can be
of any type, not just object. SINGLE_SET does not loop through heap
variables, fetching the only one (the named one) of interest.
The actual code is too embarassing to share, JD would not let me on the
newsgroup again :-(
s = sobj_new(/help) ; How to use it
s = sobj_new() ; Make new object
d = sobj_new() ; Get a reference to it again
help, s, d ; Make sure they are the same
; Set NO_KILL to see what it does
s -> store, obj_new('IDLgrModel'), /no_kill
s -> store ; Take a look at contents...
d -> store ; They are the same
obj_destroy, s ; And kill it (by accident).
d = sobj_new() ; NO_KILL was set, so it did not die.
; But NO_KILL is NOT set in D now, so that heap-gc, if
; ever used, does not go into an infinite loop.
d -> store ; Make sure contents are the same
; except for NO_KILL setting. Set it again if you want.
Cheers,
Pavel
P.S. The only use for a Singleton (or SINGLE_SET) that I can see is
avoiding Common blocks while
still allowing sharing some information between widget programs that may or
may not be launched independently, without using linking arguments. Or, as
people asked before, keeping a Preferences structure for the duration of a
session. When I wrote Display routines, I had to use Common block to keep
track of linked displays and the topmost one because they were intended to
be used interactively, and there is no way to define how many of them are
present and in which order. A singleton would allow this without a common
block.
|
|
|
Re: global singleton object ? [message #26891 is a reply to message #26887] |
Wed, 03 October 2001 06:11   |
R.G.S.
Messages: 46 Registered: September 2000
|
Member |
|
|
Mark Hadfield <m.hadfield@niwa.cri.nz> wrote in message
news:004301c14b8b$e33233e0$d938a8c0@Hadfield...
>> So, is there a way to easily define the object, or do I simply create it
>> upon startup of my IDL and define it to a system variable?
>
> Remember that functions are global in IDL.
>
> Write a function that creates your settings object and assigns it to a
> system variable. On every entry to the function, check whether the system
> variable exists and points to a valid object. If it does, use that object.
>
> I'm sure there are other ways to do this, and some of them may even be
> better than this one!
>
> ---
> Mark Hadfield
> m.hadfield@niwa.cri.nz http://katipo.niwa.cri.nz/~hadfield
> National Institute for Water and Atmospheric Research
Yes, that is what I have done (a procedure actually).
In every routine that I use the object, I first call a "object_initialize"
procedure that starts the object if it does not already exist.
That seems to be the way to go with using a "fake" singleton object.
(of course, the main code destroys the object when it cleans up)
Cheers,
bob
|
|
|
|
Re: global singleton object ? [message #26916 is a reply to message #26902] |
Tue, 02 October 2001 08:55   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
R.G.S. (rgs1967@hotmail.com) writes:
> partly inspired by the recent threads on global variables,
> I have a question regarding a global object.
>
>
> I have an IDL process that will run independently on several
> different computers. So, I need a method of deciding what
> computer and operating system I'm on, and defining various
> filepaths etc. So I have written an object with methods such
> as "->getfilepath". This method determines the computer name, and
> returns the appropriate info. There are several methods, for different
> read and write paths, and different data sets. The actual information is
> hardcoded into the object definition.
>
> So, I currently create the object and kill the object deep in each
> basic read/write function, but I would much rather have one instance
> of the object (i.e. the singleton object) that can be seen everywhere.
> (I've used this approach many times in labview, and it is really handy).
> I actually thought that objects were supposed to be of that scope, but
> I guess I got confused with the Labview nonreentrant vis.
>
> So, is there a way to easily define the object, or do I simply create it
> upon startup of my IDL and define it to a system variable?
Oh, oh. Pavel, where are you?
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
|
Re: global singleton object ? [message #26972 is a reply to message #26874] |
Wed, 03 October 2001 10:36  |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
> As for why a singleton is useful, just wait til IDL is multithreaded. When
> you
> have multiple processes that need acces to information (info that can be
> changed by the user) then a singleton object is very nice! Very Very Nice.
That probably will depend on what is allowed to be global between the
threads. My singleton relies on a function being global, and heap memory
addresses being global, too. Will each thread use separate heap
addressing? Now, that and the synchronization of the threads is a
separate can of worms that I have no knowledge about. It is almost the
same as distributed computing, isn't it?
Pavel
|
|
|