On 11 December 2012 20:33, Ciprian Teodorov <ciprian.teodorov@gmail.com> wrote:
Hi guys,
today I have commited to the NativeBoost repository a new core class, the NBExternalEnumerationType, which reifies the enum declarations in C and offers a nice interface (especially in terms of debug/inspect).
To use it just subclass the NBExternalEnumeration and add an #enumDecl method to the class side such as:
NBExternalEnumeration subclass: #NBTestEnumeration instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'NativeBoost-Tests'
NBTestEnumeration class>>enumDecl ^ {(#AAA -> 1). (#BBB -> 2). (#CCC -> 3). (#DDD -> 2400)} asDictionary
just one comment: imo, it would be better to not force users to remember that they should use complicated syntax in this method. i propose following syntax: NBTestEnumeration class>>enumDecl ^ #( #AAA 1 #BBB -> 2 #CCC -> 3 #DDD -> 2400 ) then you can have NBTestEnumeration class>>initializeEnum dict := Dictionary newFromPairs: self enumDecl ..... then #initialize should just do 'self initializeEnum'
DO NOT FORGET to call the #initialize method on your class. The rest is automatically done ;)
You can use your new enum in two ways:
add it to a client class poolDictionaries list (see #NBExternalEnumTests for an example), and then just write CCC in your code -- CCC here is an item of your enum or you can send the name of an item to your class --- NBTestEnumeration DDD
The NBExternalEnumeration implements (instance and class) some more API methods like:
#itemAt: retrieves the item having a specific value --- NBTestEnumeration itemAt: 2 #includes: checks the existence of a specific item in the enum --- NBTestEnumeration includes: #AAA
Having this already gives us a nice interface to working with enums. Now I plan to improve a little bit on performance and usage scenarios:
the possibility to accept smalltalk integers as valid input/output for functions(or callbacks) that use enums as arguments/return. support the different representation types for the enum values (int/uint)
to my thinking, it not worth implementing separate type converter for enums.. just put a note that if people would like to deal nicely with enums returned by function(s), they should use a converter , something like that: enumValue := MyEnumeration fromInteger: (self callSomeFunctionReturningInt) then this method can implement converting from int->enum object, as well as do a range checking etc. in same way, if people want to use 'enumValue' for passing it as parameter to external function, they should not pass it directly , but use 'enumValue value' In general, it would be cool to have good 'enum type' converter.. (despite that i will never use it, because to my taste its not really worth it, and i can live with integers and in most cases you hide such low-level details from end-user anyways ;)) but for those, who want more clarity and better structure in their code, of course, they can use it .
So that will be for now. Feel free to try it out and send me any comments and/or improvement ideas, It will be my pleasure to improve it. Cheers, -- Dr. Ciprian TEODOROV Ingénieur Développement CAO
tél : 06 08 54 73 48 mail : ciprian.teodorov@gmail.com www.teodorov.ro
-- Best regards, Igor Stasenko.