class SwiftGenerator::SwiftEnum

Attributes

enum_cases[RW]
enum_flavor[RW]
enum_raw_type[RW]
is_integral_type[RW]

Public Class Methods

new( definition_set, type_name, inheritance_list=[], file_name: nil, characteristics:[] ) click to toggle source
Calls superclass method SwiftGenerator::SwiftNonPrimitive::new
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 147
def initialize( definition_set, type_name, inheritance_list=[], file_name: nil, characteristics:[]  )
        determine_raw_type( inheritance_list )
        super( definition_set, type_name, inheritance_list=inheritance_list, file_name: file_name, characteristics:characteristics )

        @is_integral_type = $SwiftIntegralTypes.include? @enum_raw_type
        @enum_cases = []
end

Public Instance Methods

add_case( enum_case ) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 156
def add_case( enum_case )
        @enum_cases << enum_case
end
determine_raw_type(inheritace_list) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 182
def determine_raw_type(inheritace_list)
        return if inheritace_list.empty?
        possible_raw_type = inheritace_list[0]
        @enum_raw_type = possible_raw_type if $SwiftLegalEnumTypeNames.include? possible_raw_type
end
make_enumeration_properties() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 200
def make_enumeration_properties()
        # The count of all cases for convenience
        p_count = SwiftProperty.new(self, 'caseCount',  :Int, initialization_value:self.enum_cases.count )
        p_count.property_qualifiers = 'static'

        # An array of all cases in declaration order
        case_list = @enum_cases.map{ |a_case| ".#{a_case.case_name}" }.join( ', ' )
        p_all_cases = SwiftProperty.new(self, 'allCases', self.type_name.to_sym , collection_type: :array,  initialization_value:"[#{case_list}]" )
        p_all_cases.property_qualifiers = 'static'
end
make_indexing_methods() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 211
def make_indexing_methods
        # .toIndex() -> Int
        to_index_m = SwiftMethod.new(self, "toIndex", '', 'Int', comment: '/// USI standard enum method to get the case count')
        to_index_m << '// Support for indexing for hashing, etc.'

        to_index_m << "switch( self ) {"
        i=0
        for a_case in self.enum_cases
                to_index_m._i "case #{a_case.case_name} : return #{i}"
                to_index_m._o ""
                i += 1
        end
        to_index_m.ii "default : return 0"
        to_index_m << "}"

        # Not Yet Required
        # static fromIndex( Int ) -> Enum
        # from_index_m = SwiftMethod.new(self, "fromIndex", 'index:Int', type_name, comment: '/// USI standard enum method to get the case count')
        # from_index_m.func_qualifiers = 'static'
        # from_index_m << "return #{@type_name}.allCases[ index ]"  #Note: this is not safe
end
make_property_type() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 160
def make_property_type()
        # Make a type for this class so that references to this class can be resolved
        # Includes code for hashing this enum

        test_value_lambda = lambda{|num| make_test_value(num) }
        property_type = StringEnumPropertyType.new( self, @enum_raw_type.to_sym, test_value:test_value_lambda )
        property_type.hashable_value_lambda = lambda{|var_name, is_optional|
                if is_optional
                        return "#{var_name}?.toIndex()"
                else
                        return "#{var_name}.toIndex()"
                end
        }

        #TODO Fix this Horror
        property_type.custom_unmarshaling = lambda{|var_name, unwrapped_var|
                "#{var_name} = #{unmarshal_expression(unwrapped_var)}"
        }

        return self.swift_type_symbol, property_type
end
make_test_value(index) click to toggle source

Test Support

# File lib/swift_generator/code_generation/swift_class_generation.rb, line 245
def make_test_value(index)
        return "#{@type_name}.allCases[ #{index} % #{@type_name}.caseCount ]"
end
marshal_expression( name ) click to toggle source

JSON marshaling support

# File lib/swift_generator/code_generation/swift_class_generation.rb, line 234
def marshal_expression( name )
        #Probably only works for String enums
        return "#{name}.rawValue"
end
prepare_for_generation() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 193
def prepare_for_generation()
        make_enumeration_properties
        make_indexing_methods
        # make_test_support() if characteristics.include? :make_test_support
end
prepare_supporting_elements() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 189
def prepare_supporting_elements()
end
unmarshal_expression( name ) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 239
def unmarshal_expression( name )
        #Probably only works for String enums
        return "#{@type_name}( rawValue:#{name} )"
end