Skip to content

Commit

Permalink
Fixed issue with using multiple enum items in a bitcase.
Browse files Browse the repository at this point in the history
  • Loading branch information
moaxcp committed Nov 15, 2021
1 parent c9c8904 commit 86ec839
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ package com.github.moaxcp.x11protocol.xcbparser
import com.github.moaxcp.x11protocol.generator.Conventions
import com.github.moaxcp.x11protocol.xcbparser.expression.Expression
import com.github.moaxcp.x11protocol.xcbparser.expression.Expressions
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.TypeName

class JavaBitcaseInfo {
Expression maskField
ClassName enumType
String enumItem
Set<JavaEnumRef> enumRefs

JavaBitcaseInfo(XResult result, JavaType javaType, XBitcaseInfo bitcaseInfo) {
maskField = Expressions.getExpression(javaType, bitcaseInfo.expression)
enumItem = Conventions.getEnumValueName(bitcaseInfo.enumItem)
enumType = result.resolveXType(bitcaseInfo.enumType).javaType.className
enumRefs = bitcaseInfo.enumRefs.collect {
new JavaEnumRef(enumType: result.resolveXType(it.enumType).javaType.className, enumItem: Conventions.getEnumValueName(it.enumItem))
}
}

CodeBlock getExpression() {
CodeBlock.join(enumRefs.collect {
CodeBlock.of('$T.$L.isEnabled($L)', it.enumType, it.enumItem, maskField.getExpression(TypeName.INT))
}, '&&')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ abstract class JavaClass implements JavaType {
CodeBlock.Builder writeProtocol = CodeBlock.builder()
protocol.each {
if(it instanceof JavaProperty && it.bitcaseInfo) {
writeProtocol.beginControlFlow('if($T.$L.isEnabled($L))', it.bitcaseInfo.enumType, it.bitcaseInfo.enumItem, it.bitcaseInfo.maskField.getExpression(TypeName.INT))
writeProtocol.beginControlFlow('if($L)', it.bitcaseInfo.getExpression())
it.addWriteCode(writeProtocol)
writeProtocol.endControlFlow()
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.moaxcp.x11protocol.xcbparser

import com.squareup.javapoet.ClassName
import groovy.transform.EqualsAndHashCode

@EqualsAndHashCode
class JavaEnumRef {
ClassName enumType
String enumItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class JavaPrimativeListProperty extends JavaListProperty {
}

if(bitcaseInfo) {
return CodeBlock.of('($T.$L.isEnabled($L) ? $L : 0)', bitcaseInfo.enumType, bitcaseInfo.enumItem, bitcaseInfo.maskField.getExpression(TypeName.INT), actualSize)
return CodeBlock.of('($L ? $L : 0)', bitcaseInfo.expression, actualSize)
}
return actualSize
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,18 @@ class JavaPrimitiveProperty extends JavaProperty {
}
if(bitcaseInfo) {
String fieldName = bitcaseInfo.getMaskField().fieldRefs[0].fieldName
CodeBlock.Builder builder = CodeBlock.builder()

bitcaseInfo.enumRefs.each {
builder.addStatement('$LEnable($T.$L)', fieldName, it.enumType, it.enumItem)
}
methods += [
MethodSpec.methodBuilder(name)
.addModifiers(Modifier.PUBLIC)
.addParameter(enumClassName, name)
.returns(javaClass.builderClassName)
.addStatement('this.$L = $L', name, conversion)
.addStatement('$LEnable($T.$L)', fieldName, bitcaseInfo.enumType, bitcaseInfo.enumItem)
.addCode(builder.build())
.addStatement('return this')
.build()
]
Expand Down Expand Up @@ -257,7 +262,7 @@ class JavaPrimitiveProperty extends JavaProperty {
}

if(bitcaseInfo) {
return CodeBlock.of('($T.$L.isEnabled($L) ? $L : 0)', bitcaseInfo.enumType, bitcaseInfo.enumItem, bitcaseInfo.maskField.getExpression(TypeName.INT), actualSize)
return CodeBlock.of('($L ? $L : 0)', bitcaseInfo.expression, actualSize)
}
return actualSize
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ abstract class JavaProperty implements JavaUnit, JavaReadParameter {
return
}
if(bitcaseInfo) {
code.beginControlFlow('if($T.$L.isEnabled($L))', bitcaseInfo.enumType, bitcaseInfo.enumItem, bitcaseInfo.maskField.getExpression(TypeName.INT))
code.beginControlFlow('if($L)', bitcaseInfo.expression)
code.addStatement('$L = $L', name, readCode)
code.addStatement('javaBuilder.$1L($1L)', name)
code.endControlFlow()
Expand All @@ -165,13 +165,18 @@ abstract class JavaProperty implements JavaUnit, JavaReadParameter {
List<MethodSpec> getBuilderMethods(ClassName outer) {
if(bitcaseInfo) {
String fieldName = bitcaseInfo.maskField.fieldRefs[0].fieldName
CodeBlock.Builder builder = CodeBlock.builder()

bitcaseInfo.enumRefs.each {
builder.addStatement('$LEnable($T.$L)', fieldName, it.enumType, it.enumItem)
}
return [
MethodSpec.methodBuilder(name)
.addModifiers(Modifier.PUBLIC)
.addParameter(typeName, name)
.returns(javaClass.builderClassName)
.addStatement('this.$1L = $1L', name)
.addStatement('$LEnable($T.$L)', fieldName, bitcaseInfo.enumType, bitcaseInfo.enumItem)
.addCode(builder.build())
.addStatement('return this')
.build()
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class JavaTypeListProperty extends JavaListProperty {
CodeBlock getSizeExpression() {
CodeBlock actualSize = CodeBlock.of('$T.sizeOf($L)', ClassName.get(basePackage, 'XObject'), name)
if(bitcaseInfo) {
return CodeBlock.of('($T.$L.isEnabled($L) ? $L : 0)', bitcaseInfo.enumType, bitcaseInfo.enumItem, bitcaseInfo.maskField.getExpression(TypeName.INT), actualSize)
return CodeBlock.of('($L ? $L : 0)', bitcaseInfo.expression, actualSize)
}
return actualSize
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.moaxcp.x11protocol.xcbparser

import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.TypeName

class JavaTypeProperty extends JavaProperty {

Expand Down Expand Up @@ -45,7 +44,7 @@ class JavaTypeProperty extends JavaProperty {
CodeBlock getSizeExpression() {
CodeBlock actualSize = CodeBlock.of('$L.getSize()', name)
if(bitcaseInfo) {
return CodeBlock.of('($T.$L.isEnabled($L) ? $L : 0)', bitcaseInfo.enumType, bitcaseInfo.enumItem, bitcaseInfo.maskField.getExpression(TypeName.INT), actualSize)
return CodeBlock.of('($L ? $L : 0)', bitcaseInfo.expression, actualSize)
}
return actualSize
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import groovy.util.slurpersupport.Node

class XBitcaseInfo {
Node expression
String enumType
String enumItem
Set<XUnitEnumRef> enumRefs
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.moaxcp.x11protocol.xcbparser

import groovy.transform.EqualsAndHashCode

@EqualsAndHashCode
class XUnitEnumRef {
String enumType
String enumItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class XUnitSwitchBitcase extends XUnitSwitch {
return
}
if(it.name() == 'bitcase') {
String enumRef
String enumItem
Set<XUnitEnumRef> enumRefs = []
it.childNodes().each { Node bitcaseNode ->
if(bitcaseNode.name() == 'enumref') {
enumRef = bitcaseNode.attributes().get('ref')
enumItem = bitcaseNode.text()
String enumType = bitcaseNode.attributes().get('ref')
String enumItem = bitcaseNode.text()
enumRefs.add(new XUnitEnumRef(enumType: enumType, enumItem: enumItem))
} else {
XUnit unit = parseXUnit(result, bitcaseNode, new XBitcaseInfo(expression: expression, enumType: enumRef, enumItem: enumItem))
XUnit unit = parseXUnit(result, bitcaseNode, new XBitcaseInfo(expression: expression, enumRefs: enumRefs))
fields.add(unit)
}
}
Expand Down

0 comments on commit 86ec839

Please sign in to comment.