gluecodium icon indicating copy to clipboard operation
gluecodium copied to clipboard

Support enumeration mask

Open dev2575 opened this issue 4 years ago • 4 comments

Swift relies on OptionSet to handle masks of enumeration values. Unless we missed it, there doesn't seem to be a way to support this in GlueCodium. Instead, we are forced to exchange a List of distinct enumeration values, which is less than optimum.

dev2575 avatar Dec 02 '21 18:12 dev2575

How exactly do you want to support it? Which type should represent it in C++? In other languages (if it's relevant)?

DanielKamkha avatar Dec 03 '21 06:12 DanielKamkha

I believe the C++ and Java code generated is fine as is since we can support regular enums and masks of enums with it.

However, Swift crashes if the C++ pass a mask of enums with the current code generated. I think having an option in the IDL to indicate which syntax to output for Swift would be useful.

Mode 1, the current syntax: public enum MyEnum : UInt32, CaseIterable, Codable { case enum_1 = value_1 case enum_2 = value_2 ... }

Mode 2, generate a struct that extends OptionSet: struct MyEnum: OptionSetType { let rawValue: TYPE

init(rawValue: TYPE) { self.rawValue = rawValue }

static let enum_1 = MyEnum(rawValue: value_1)
static let enum_2 = MyEnum(rawValue: value_2)

} TYPE would typically be a UInt32 or UInt64.

dev2575 avatar Dec 03 '21 21:12 dev2575

According to OptionSet documentation (https://developer.apple.com/documentation/swift/optionset), the raw values of individual enumerators must all be powers of 2 to conform to this protocol. This means that C++ code also needs to be different from a regular enum. And, in turn, C++ code affects all other languages as well.

So "enums as flags" needs to be a general feature, not limited to Swift only. Still feasible, just more work.

DanielKamkha avatar Dec 06 '21 08:12 DanielKamkha

I agree that to use masks and bit-wise operations, the numbers must all be a power of 2. You could do something to automate this but since it is already possible to do it by providing the values in the enum's IDL, I did not list this as a requirement. A simple documentation of that requirement could be enough. If you do something for Java/C++, I think it would be good to still allow users to define the values as some people may want to use a specific range of values (i.e. 0x1000, 0x2000 .... instead of 0x1, 0x2, ...).

dev2575 avatar Dec 06 '21 21:12 dev2575

Here is the final approach I settled on. For this LIME IDL snippet

@Swift(OptionSet)
enum MyFlags {
    ONE = 1,
    TWO = 2,
    THREE = 4
}
class UseMyFlags {
    static fun getFlags(): Set<MyFlags>
}

the Swift code will have MyFlags generated as a struct conforming to OptionSet protocol (enumerators represented as static let). The Swift return value of getFlags() will be MyFlags, while sill being std::unordered_set<MyFlags> in C++.

DanielKamkha avatar Aug 18 '22 08:08 DanielKamkha

Hi Daniel,

Thank your for the update. I haven't tested it yet, but could you please clarify why you use 'std::unordered_set<MyFlags> in C++' instead of just a simple MyFlags containing the mask of enumerations?

Best, Francois


From: Daniel Kamkha @.> Sent: Thursday, August 18, 2022 2:50 AM To: heremaps/gluecodium @.> Cc: dev2575 @.>; Assign @.> Subject: Re: [heremaps/gluecodium] Support enumeration mask (Issue #1197)

Closed #1197https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fheremaps%2Fgluecodium%2Fissues%2F1197&data=05%7C01%7C%7C119b1e314eef4abd1dd008da80ff069b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637964130052776710%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=XAHqA6QMc3UyTPIxVOX5Sp6IyYbFLfQjQ79jedmlo6I%3D&reserved=0 as completed via #1461https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fheremaps%2Fgluecodium%2Fpull%2F1461&data=05%7C01%7C%7C119b1e314eef4abd1dd008da80ff069b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637964130052776710%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=DjxLi7nFjPxyd1%2FmhMuAOhhcumqBGzlrAZkBAAQOzJg%3D&reserved=0.

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fheremaps%2Fgluecodium%2Fissues%2F1197%23event-7212783033&data=05%7C01%7C%7C119b1e314eef4abd1dd008da80ff069b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637964130052932922%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=NTO2SpAPqmZRUKylrK9ece9777pgZFKbXyYNlWZ04i0%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FATM62UR3A5ZX4OFD5B2YM63VZYBMZANCNFSM5JH2VU4Q&data=05%7C01%7C%7C119b1e314eef4abd1dd008da80ff069b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637964130052932922%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=FhKxfxY7ehQ5PrkB2eeOhQQlHzVK9g3sFPytmGjFkao%3D&reserved=0. You are receiving this because you were assigned.Message ID: @.***>

dev2575 avatar Aug 18 '22 16:08 dev2575