types - Dynamic Primitive in Objective-C -


i have value stored as uint64_t (the maximum needed app). need integer math number, size depends on user specifies.

i need function takes uint64_t , bit-size casts proper primitive , returns it.

@interface eaeintegervalue : nsobject <nscopying> @property uint64_t storageobject;  - (id)init; - (id)initwithvalue:(uint64_t)value;  - (uint64_t)getvalueuint64t; - (int64_t)getvalueint64t; - (uint32_t)getvalueuint32t; - (int32_t)getvalueint32t; - (uint16_t)getvalueuint16t; - (int16_t)getvalueint16t; - (uint8_t)getvalueuint8t; - (int8_t)getvalueint8t;  // function concerned with: - (id)getvalue:(uint8_t)bitsize; @end 

pseudocode getvalue:

- (id)getvalue:(uint8_t)bitsize {     if (bitsize == 8) return [self getvalueint8t];     if (bitsize == 16) return [self getvalueint16t];     if (bitsize == 32) return [self getvalueint32t];     if (bitsize == 64) return [self getvalueint64t];     //...unsigned...     @throw... } 

i getting complaint returning primitive id. understand why error, don't see way wrap value object , retain dynamic nature of function. necessary retain correct size integer, , function calls should not expected manually. calling function might this:

- (eaeintegervalue*)domath:(int)bitsize {     // important done in correct bit size mode.     uint64_t value = [val1 getvalue:bitsize] / [val1 getvalue:bitsize];      // don't care casted 64-bit int later.     return [[eaeintegervalue alloc] initwithvalue:value]; } 

worst case, can (hopefully can appreciate why i'm trying avoid it. have 20-30 of these functions):

- (eaeintegervalue*)domath:(int)bitsize :(bool)issigned {     uint64_t value;     if (bitsize == 8 && issigned) value = [val1 getvalueint8t] / [val1 getvalueint8t];     else if (bitsize == 8) value = [val1 getvalueuint8t] / [val1 getvalueuint8t];     else if (bitsize == 16 && issigned) value = [val1 getvalueint16t] / [val1 getvalueint16t];     else if (bitsize == 16) value = [val1 getvalueuint16t] / [val1 getvalueuint16t];     else if (bitsize == 32 && issigned) value = [val1 getvalueint32t] / [val1 getvalueint32t];     else if (bitsize == 32) value = [val1 getvalueuint32t] / [val1 getvalueuint32t];     else if (bitsize == 64 && issigned) value = [val1 getvalueint64t] / [val1 getvalueint64t];     else if (bitsize == 64) value = [val1 getvalueuint64t] / [val1 getvalueuint64t];      return [[eaeintegervalue alloc] initwithvalue:value]; } 

wow, that's lot of code , lot of unit tests!

you might able pre-processor macros. long macro check values being passed in, along lines of...

#define getvalue(obj, bitsize) ((bitsize) == 8 ? [obj getvalueint8t] : (...)) 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -