🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.next.software
Article #1087

Re: class instances from class name strings

#1087
From: Gordie Freedman
Date: Tue, 13 Oct 1992 01:54
57 lines
2320 bytes
In article 2tD@oceania.com, chris (Chris Larson) writes:
>I would really like to be able to setup a list of character strings that represent  
>classes, and at run time be able to create an instance of the class from the class name.
>
>An example:
>
>classList[] = {
>"Class1",
>"Class2"};
>
>then at run time do some type of alloc & init from the class name. ie:
>
>newClass = [[classList[0] alloc] init]
>
>Any ideas?
>
>Chris
>-- 
>Chris Larson  <==>  chris@oceania.com  <==>  NeXT Mail is cool
>Oceania Health Care Systems                      Palo Alto, CA
>The Software Ranch                               Park City, UT

I believe all classes are represented by names that are accessible somehow.
These may not be the names you want to have, however they will be consistent and
available. You do some transformation on the name to get the class. I think you
can probably get this out of the NeXT Developer documentation on Objective C.
Try searching for getNamedObject or something like that ...

If this doesn't do what you wnt (or if I am imagining that it exists), you could
have a class that manages name to class mappings, and have a dispatch function
for alloc init such as:

@implementation ClassManager

Class + getClass:(const char* const) name
{
	Class class;
	// Map the class name somehow to the correct class	
	return class;
}
@end

And in some code:

id newObject;
Class class;

class = [ClassManager getClass:"name"];
newObject = [[[ClassManager getClass:"name"] alloc] init];

My syntax is probably off but you get the idea

I suspect (and have actual concurrance from a real NeXT programmer) that this is probably what the runtime system is doing anyway ...

Maybe somebody who really knows this will explain
---
Gordie Freedman: gordie@kaleida.com Sigs are for kids
Technical Details
Message-ID: <73305@apple.Apple.COM>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!ames!data.nas.nasa.gov!taligent!apple!moon!gordie
References: <BvvAv9.2tD@oceania.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!ames!data.nas.nasa.gov!taligent!apple!moon!gordie
From: gordie@duts.ccc.amdahl.com (Gordie Freedman)
Newsgroups: comp.sys.next.software
Subject: Re: class instances from class name strings
Message-ID: <73305@apple.Apple.COM>
Date: 13 Oct 92 01:54:22 GMT
References: <BvvAv9.2tD@oceania.com>
Sender: daemon@Apple.COM
Reply-To: gordie@duts.ccc.amdahl.com
Organization: Happy hacker bait and tackle shop
Lines: 59