PDA

View Full Version : Sharing Instantiated Object



PropNewbie
05-21-2008, 01:30 PM
If I define an object in the OBJ block is it possible to share the same instance of that object with other objects? The reason I ask is that I have written some debug code (in·a UTIL object) that tips·text out to a VGA display. I would like to be able to instantiate a UTIL object and make a reference to it available in·multiple other·objects. It is not possible to have two UTIL objects·started as they·would each take a cog·and collide on the VGA output pins.

The scenario I wish to achive (in·pseudo code - I know this code is not valid) is

### Main·Object

OBJ
·· util = "utils"
·· wObj = "worker"

PUB·Main
·· utils.Init
·· utils.DebugOut(string(Main Initialized),CR)·
·· wObj.Init(@util)

###·Worker·Object
·
OBJ
·· util = null
·
PUB·Init(utilObj)
·· utils := utilObj
·· utils.DebugOut(string(Worker Initialized),CR)··

The idea is to have a kind of base utility class I can reference in various spin objects and make a standardized intialization convention. Is this possible? If I just do a·util = "utils"·in the worker object OBJ block·I get an "illegal circular reference" message.

Is it possible to do what I have just described or is it simply too high level a concept?

mirror
05-21-2008, 05:35 PM
Have a look at SerialMirror in the object exchange. I think it's going to be about as conceptually close as you're going to get. http://obex.parallax.com/objects/189/

Paul Baker
05-21-2008, 11:40 PM
There is no inherent support for passing references to objects and methods, that said some people have found some work-around hacks. Unless you're dealing with some seriously complicated inheritance it's best to follow the precepts of the language and have your object hierarchy like this:



Main
└──Worker
└──Utils

Then write wrapper functions in Worker for those routines you need to use in Utils. Say there is a method in Utils call get_parameters(return_ptr) that you want to use in Main, you can write the following routine in Worker:



PUB get_parameters(return_ptr)
Utils.get_parameters(return_ptr)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

PropNewbie
05-22-2008, 04:07 AM
Thank you

As a suggestion, why not implement a GLOBALOBJ directive in the propeller tool. It would work similarly to OBJ but an object defined there would only get one instance irregardless of how many other objects defined it. All would reference the one instance.

Just a thought....

stevenmess2004
05-22-2008, 05:34 AM
Like Paul said it is possible to do what you want to do but it is a pain in the neck at the moment. I've been working on it on and off for a while now. However, if you don't have anything in the VAR section in your object you can use it anywhere in your application you want and it will only be included once. To show this in Paul's diagram



Main
└──Utils
└──Worker
└──Utils



Everything in the DAT section is global to every instance of the object so just put your variables there instead of in VAR.

Paul Baker
05-22-2008, 07:39 AM
The reason why globals aren't supported is that Spin enforces strong encapsulation, which is what·Object Oriented programming principles state should happen. If you look at "pure" OO languages (such as smalltalk), they too provide no facility for globals (C++ is one of the dirtiest implementations of OOL ever made because they proceeded to give you ways of circumventing OOL principles).

What I'm trying to say is that GOBALOBJ isn't actually needed, there are facilities at your disposal to implement the same objective.

OO = Object Oriented
OOL = Object Oriented Language

BTW I too have wished at times to be able to circumvent the strong encapsulation, such as trying to display data to a TV_text object from multiple objects, but with a little effort I got it to work the way I needed it to.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Post Edited (Paul Baker (Parallax)) : 5/22/2008 1:55:02 AM GMT

rokicki
05-22-2008, 08:02 AM
Oh my. This is one discussion I plan to stay out of. But if that is truly the *technical* reason why Spin doesn't support "global objects" . . . ouch.

Paul, next time just bring up politics. Or religion.

Paul Baker
05-22-2008, 08:29 AM
LOL, agreed. There are other reasons having to do with how the compiler works but·my knowledge of the inner workings of the compiler is limited to a smattering of conceptual overviews of certain aspects·(there's no real reason for me to know more) so·I provided the·philosophical reasoning behind it. An OOL was chosen for the Propeller to facilitate the free exchange of objects (there are several other reasons but this is a major one), the more exceptions you make·to OOP in a language the·greater·a risk that you cannot simply grab an object and use it free of worries.

BTW I cut the most incendiary passages out of the the previous post, I don't have time to engage in a detailed·OOP debate. One of these days I'll learn how to respond to a prickly question without opening a can of worms, but perhaps all I can do is aspire to that goal.

I would rather see some form of inherency implemented since that is a OO accepted way of dealing wth this issue.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Post Edited (Paul Baker (Parallax)) : 5/22/2008 4:05:18 PM GMT