Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

type-safe enum pattern

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  windowDidLoad won't get called  
Author Message
Brieuc Desoutter

External


Since: Aug 28, 2005
Posts: 18



(Msg. 1) Posted: Sun Jul 29, 2007 9:28 am
Post subject: type-safe enum pattern
Archived from groups: comp>sys>mac>programmer>help (more info?)

Hi everyone,

Does anybody has ever implemented some kind of type-safe enum pattern
in Objective-C, like the one used in Java before the introduction of
Enums in the language.

The idea is to have something similar to C enums but with object
members instead of integers. For instance, I'd like to have a enum for
length units whose member are instances of a LengthUnit class.

The requirements are :

1) The enums should allow access a enum member by name like :

LengthUnit *mile = [LengthUnit mile];

2) The enums should allow user to retrieve all members like :

NSArray *lengthUnits = [LengthUnit members];

3) It should not be possible to create new members of an enum type.

// should generate a compile time error
LengthUnit *newUnit = [[LengthUnit alloc] initWithName:@"my unit"
symbol:@"U" toMeters:0.0.254"];

I've looked at the Class Cluster pattern but it seems better suited for
"value" objects.

I've come up with a partial solution which is not really satisfying :

1) I defined protocols Unit and LengthUnit to prevent member object creation :

@protocal Unit
- (NSString*)name;
- (NSString*)symbol;
@end

@protocol LengthUnit <Unit>
- (double)toMeters:(double)value;
- (double)fromMeters:(double)value;
@end

2) I defined a class LengthUnits as follow :

@interface LengthUnits : NSObject
+ (id<LengthUnit>)meter;
+ (id<LengthUnit>)foot;
+ (id<LengthUnit>)mile:
+ (id<LengthUnit>)fathom;
....
@end

3) The implementations of LengthUnits static methods meter, foot, ....
return instances of a "private" class to conform to the LengthUnit
protocol

However I not quite satisfied with this ( the use involved ids instead
of typed pointer to LengthUnit ), it doesn't implement the members
method, and it might have a lot of default I've overlooked...

If anyone as a better solution, I would really appreciate if he could share it.

thanks

Brieuc
Back to top
Login to vote
Display posts from previous:   
Related Topics:
CFString interrupt safe? - Can the CFString functions be called from a Time Manager interrupt task (Carbon CFM)? TN1104 does not mention anything...

Thread-safe array access in Carbon? - I'm working on an application for doing USB I/O. The main problem I'm having right now is that there's an input buffer...

Using non thread safe code in a thread - I'm working with threads here, and I'm trying to fully understand what I can do in a thread and what I cant. If I have ...

what os and type should i use - im ok at java and database programming in foxpro just to give a background idea of me. i wrote this program in foxpro ...

How to type in ASCII? - Stupid question #2 of the week. I'm trying to avoid writing a printer driver for a barcode label printer. It responds ...

Is there a way to determine the type being thrown? - Earlier today I ran into the following problem: I have the following code: try { foo(); } catch(char* ch) { std...
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]