Last active
June 19, 2016 16:17
-
-
Save edefazio/b1bdf42ebe38da9904c3 to your computer and use it in GitHub Desktop.
CodeMark Template for a SubWord Field mapped to bits within a 32-bit Word
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*{+packageName*/package io.semiotics.field/*}*/; | |
import io.semiotics.data.Word; | |
import io.semiotics.field.Field.Field32; | |
import io.semiotics.specify.Signifier; | |
import io.semiotics.type.InvalidBin; | |
import io.semiotics.type.InvalidForm; | |
/*{+imports*/import io.semiotics.type.primitive.IntType;/*}*/ | |
/*{+classComment}*/ | |
public class /*{+className*/_UniformField32/*}*/ | |
implements Field32 | |
{ | |
public static final String NAME = /*{+fieldName*/"IntType_Field32"/*}*/; | |
public static final /*{+type*/IntType/*}*/ TYPE = | |
/*{+typeDeclare*/IntType.INSTANCE/*}*/; | |
public static final /*{+type*/IntType/*}*/./*{+form*/IntForm/*}*/ FORM = | |
TYPE./*{+form*/IntForm/*}*/; | |
public static final int SHIFT = /*{+shift*/0/*}*/; | |
public static final int MASK = /*{+mask*/-1/*}*/; | |
public static final Signifier[] SIGNIFIERS = | |
{ /*{+signifiers*/Signifier.NONE/*}*/ }; | |
public static final FieldSpecification SPECIFICATION = | |
new FieldSpecification( /*{+className*/_UniformField32/*}*/.class, NAME, TYPE, SHIFT, "/*{+className*/_UniformField32/*}*/.INSTANCE", SIGNIFIERS ); | |
public static final /*{+className*/_UniformField32/*}*/ INSTANCE = | |
new /*{+className*/_UniformField32/*}*/(); | |
private /*{+className*/_UniformField32/*}*/() | |
{ } | |
public /*{+type*/IntType/*}*/ getType() | |
{ | |
return TYPE; | |
} | |
@Override | |
public String getName() | |
{ | |
return NAME; | |
} | |
@Override | |
public Signifier[] getSignifiers() | |
{ | |
return SIGNIFIERS; | |
} | |
@Override | |
public int getMask() | |
{ | |
return MASK; | |
} | |
@Override | |
public int getShift() | |
{ | |
return SHIFT; | |
} | |
@Override | |
public FieldSpecification getSpecification() | |
{ | |
return SPECIFICATION; | |
} | |
@Override | |
public int readRawBits( int compound ) | |
{ | |
return ( compound & MASK ) >>> SHIFT; | |
} | |
@Override | |
public int updateBin( int compound, int bin ) | |
throws InvalidBin | |
{ | |
// clear old [Bin] update with new [Bin] | |
return ( compound & ~MASK ) | alignBin ( bin ); | |
} | |
@Override | |
public int alignBin( int bin ) | |
throws InvalidBin | |
{ | |
validateBin( bin ); | |
return bin << SHIFT; | |
} | |
@Override | |
public String describeWithBin( int bin ) | |
throws InvalidBin | |
{ | |
String binary = Word.Align._32.binaryPadTo( bin , TYPE.getBitsCount() ); | |
return Word.Align._32.align( binary, SHIFT )+ " "+NAME+" ["+bin+"]->"+TYPE.format( TYPE.toForm( bin ) )+"("+TYPE.getBitsCount()+" of 32-bits)"; | |
} | |
@Override | |
public int readBin( int compound ) | |
throws InvalidBin | |
{ | |
return TYPE.validateBin( readRawBits ( compound ) ); | |
} | |
@Override | |
public int validateBin( int bin ) | |
throws InvalidBin | |
{ | |
if (! TYPE.isValidBin( bin ) ) | |
{ | |
throw new InvalidBin ("[Bin] ["+bin+"] is not valid for Field :"+NAME+" of "+TYPE.describe() ); | |
} | |
return bin; | |
} | |
public int toBin ( /*{+typeForm*/int/*}*/ form ) | |
{ | |
return FORM.toBin( form ); | |
} | |
public /*{+typeForm*/int/*}*/ toForm ( int bin ) | |
throws InvalidBin | |
{ | |
return FORM.toForm( bin ); | |
} | |
/** reads and returns the Form from the compound */ | |
public /*{+typeForm*/int/*}*/ read ( int compound ) | |
throws InvalidBin | |
{ | |
return toForm ( readRawBits ( compound ) ); | |
} | |
public int update ( int compound, /*{+typeForm*/int/*}*/ form ) | |
throws InvalidForm | |
{ | |
try | |
{ | |
return updateBin( compound, FORM.toBin( form ) ); | |
} | |
catch ( InvalidForm ivf ) | |
{ | |
throw new InvalidForm( "Invalid Form \""+form+"\" for "+describe(), ivf ); | |
} | |
} | |
@Override | |
public int updateForm( int compound, Object form ) | |
throws InvalidForm | |
{ | |
if ( form instanceof /*{+typeFormObject*/Integer/*}*/ ) | |
{ | |
return update ( compound, ( (/*{+typeFormObject*/Integer/*}*/)form ) ); | |
} | |
throw new InvalidForm ("Form \""+form+"\" is not an Integer"); | |
} | |
@Override | |
public Object validateForm ( Object form ) | |
throws InvalidForm | |
{ | |
if (! TYPE.isValidForm( form ) ) | |
{ | |
throw new InvalidForm( "Invalid Form \""+form+"\" for "+describe() ); | |
} | |
return form; | |
} | |
public void validateForm ( /*{+typeForm*/int/*}*/ form ) | |
throws InvalidForm | |
{ | |
try | |
{ | |
FORM.validateForm( form ); | |
} | |
catch ( InvalidForm ivf ) | |
{ | |
throw new InvalidForm ("Form \"" + form + "\" is invalid for " + NAME + ": " + TYPE.describe(), ivf ); | |
} | |
} | |
public boolean isValidForm ( /*{+typeForm*/int/*}*/ form ) | |
{ | |
return FORM.isValidForm( form ); | |
} | |
@Override | |
public String format( Object form ) | |
throws InvalidForm | |
{ | |
if ( form instanceof /*{+typeFormObject*/Integer/*}*/ ) | |
{ | |
describeForm( ( (/*{+typeFormObject*/Integer/*}*/)form ) ); | |
} | |
throw new InvalidForm ("Form \""+form+"\" is not of type /*{+typeFormObject*/Integer/*}*/"); | |
} | |
public String describeForm( /*{+typeForm*/int/*}*/ form ) | |
throws InvalidForm | |
{ | |
try | |
{ | |
String formatted = TYPE.format( form ); | |
for ( int i = 0; i < SIGNIFIERS.length; i++ ) | |
{ | |
formatted = SIGNIFIERS[ i ].signify( formatted ); | |
} | |
return NAME +":"+formatted; | |
} | |
catch ( InvalidForm ivf ) | |
{ | |
throw new InvalidForm ("Form \"" + form + "\" is invalid for " + NAME + ": " + TYPE.describe(), ivf ); | |
} | |
} | |
@Override | |
public String describe() | |
{ | |
return Word.Align.leftPadZerosTo( ""+getMask(), 32 ).replace('0', ' ') + " "+NAME+":"+TYPE.describe(); | |
} | |
public String toString() | |
{ | |
return describe(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment