Class: SwiftGenerator::SwiftEnum

Inherits:
SwiftNonPrimitive show all
Defined in:
lib/swift_generator/code_generation/swift_class_generation.rb

Instance Attribute Summary collapse

Attributes inherited from SwiftNonPrimitive

#access_control_modifiers, #class_characteristics, #definition_set, #file_name, #inheritance_list, #initializers, #is_test_element, #is_user_editable, #methods, #properties, #source_file, #specified_type_name, #top_inner_comment_block, #type_name

Instance Method Summary collapse

Methods inherited from SwiftNonPrimitive

#comparable_properties, #persistent_properties, #resolve_property_types, #swift_type_symbol, #transient_properties

Constructor Details

#initialize(definition_set, type_name, inheritance_list = [], file_name: nil, characteristics: []) ⇒ SwiftEnum

Returns a new instance of SwiftEnum.



147
148
149
150
151
152
153
# 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

Instance Attribute Details

#enum_casesObject

Returns the value of attribute enum_cases.



145
146
147
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 145

def enum_cases
  @enum_cases
end

#enum_flavorObject

Returns the value of attribute enum_flavor.



141
142
143
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 141

def enum_flavor
  @enum_flavor
end

#enum_raw_typeObject

Returns the value of attribute enum_raw_type.



142
143
144
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 142

def enum_raw_type
  @enum_raw_type
end

#is_integral_typeObject

Returns the value of attribute is_integral_type.



143
144
145
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 143

def is_integral_type
  @is_integral_type
end

Instance Method Details

#add_case(enum_case) ⇒ Object



156
157
158
# 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) ⇒ Object



182
183
184
185
186
# 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_propertiesObject



200
201
202
203
204
205
206
207
208
209
# 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_methodsObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 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_typeObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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) ⇒ Object

Test Support



245
246
247
# 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) ⇒ Object

JSON marshaling support



234
235
236
237
# 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_generationObject



193
194
195
196
197
# 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_elementsObject



189
190
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 189

def prepare_supporting_elements()
end

#unmarshal_expression(name) ⇒ Object



239
240
241
242
# 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