[comp.sys.mac.programmer] newD

jsorenso@thesis1.med.uth.tmc.edu (JEFFREY MARIUS SORENSON) (06/25/91)

I'm working on a project which involves lots of small
"direct" (implemented as pointers) objects in ThinkC.
As it stands, each time I create one of these objects
I have to go through the sequence:

size=sizeof(ClassName);
newObject=(ClassName*)NewPtr(size);
blessD(newObject, ClassName);

To create (allocate) an indirect object, there is 
a function Symantec provided.
void	*new(void*);

Now since I might have hundreds of different classes,
I would like to write a functions, analogous to Symantec's
new(), called newD() that allocates and blesses a direct object.

This would seem feasible, so I created one with the same
arguments as Symantec's new(). So Ideally I could write:
newObject=newD(ClassName);

However, since ClassName is passed as a pointer to void, I
can't seem to get the size of the class from within my newD()
function.			void	*newD(void	*dummyClassName);

since dummyClassName is just a pointer, sizeof(dummyClassName)
is simply 4.  You can't call sizeof(*dummyClassName), because
that is an illegal use of the void type.  Now I'm sure that this pointer
to className must contain some sort of information about the size of
the class.  But how do I get to it?  How does Symantec do it? (with their
new() function, one passes the class name as a pointer to void).
Has anyone been able to write a newD() function?  If so, I would appreciate
any tips.  If it is for some reason impossible, I would like to know that too
so I can give it up.  I'm curious about why Symantec didn't provide a newD()
function.  I'm guessing that either they thought it would be easy enought for
me to implement myself, or that there is something about using a pointer instead
of a handle that makes it too difficult/impossible. Any clues?
-- 
-------------------------------------------------------------------------------
jsorenso@thesis1.med.uth.tmc.edu | "No matter where you go, there you are"
				 |				-BB
-------------------------------------------------------------------------------

mlh1@gte.com (Michael Hackney) (06/27/91)

In article <5149@lib.tmc.edu> jsorenso@thesis1.med.uth.tmc.edu (JEFFREY MARIUS SORENSON) writes:
>  I'm working on a project which involves lots of small
>  "direct" (implemented as pointers) objects in ThinkC.
>  As it stands, each time I create one of these objects
>  I have to go through the sequence:
>
>    size=sizeof(ClassName);
>    newObject=(ClassName*)NewPtr(size);
>    blessD(newObject, ClassName);
>

Why not create a new "root" superclass (call it, say, CDObject) that looks
and works exactly like CObject except it is declared as "direct"?

This greatly simplifies your problem and you can mix direct and indirect
classes in your application.  You could also modify CObject, in which case
all of the TCL would inherit "direct".  This is NOT RECOMMENDED.

One other point:  I do not know what your need for direct classes is, but
you could also just use indirect classes and call "HLock(myClass);" to
lock them in memory.  You can then safely pass pointers to any of the
class' instance variables, etc and when you are done "HUnLock(myClass);"
will ease memory management!

Good luck.

time@ice.com (Tim Endres) (06/27/91)

In article <5149@lib.tmc.edu>, jsorenso@thesis1.med.uth.tmc.edu (JEFFREY MARIUS SORENSON) writes:
> the class.  But how do I get to it?  How does Symantec do it? (with their
> new() function, one passes the class name as a pointer to void).
> Has anyone been able to write a newD() function?  If so, I would appreciate
> any tips.  If it is for some reason impossible, I would like to know that too
> so I can give it up.  I'm curious about why Symantec didn't provide a newD()
> function.  I'm guessing that either they thought it would be easy enought for
> me to implement myself, or that there is something about using a pointer instead
> of a handle that makes it too difficult/impossible. Any clues?

you can bet your life that the pointer to the class points to the
data you need. Follow this pointer in some assembler for a while and
you should be able to figure it out.

-------------------------------------------------------------
Tim Endres                |  time@ice.com
ICE Engineering           |  uupsi!ice.com!time
8840 Main Street          |  Voice            FAX
Whitmore Lake MI. 48189   |  (313) 449 8288   (313) 449 9208
-------- USENET: A slow moving self parody.....