Changeset 352:73bc21c0f4a5 in finroc_core-java


Ignore:
Timestamp:
13.02.2022 22:20:08 (16 months ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

Adds native support for enum-based flags remote types (-> both string and numeric representations are available in tools)

Location:
remote
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • remote/RemoteTypeAdapter.java

    r329 r352  
    2929import org.finroc.core.datatype.Event; 
    3030import org.finroc.core.datatype.XML; 
     31import org.rrlib.logging.Log; 
     32import org.rrlib.logging.LogLevel; 
    3133import org.rrlib.serialization.BinaryInputStream; 
    3234import org.rrlib.serialization.BinaryOutputStream; 
     
    123125    public static class Default extends RemoteTypeAdapter { 
    124126 
    125         public enum Type { INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT, DOUBLE, ENUM, BINARY_SERIALIZABLE, ARRAY, STRING_SERIALIZABLE, XML_SERIALIZABLE, TIMESTAMP_ONLY } 
     127        public enum Type { INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT, DOUBLE, ENUM, BINARY_SERIALIZABLE, ARRAY, STRING_SERIALIZABLE, XML_SERIALIZABLE, TIMESTAMP_ONLY, ENUM_FLAGS8, ENUM_FLAGS16, ENUM_FLAGS32, ENUM_FLAGS64 } 
    126128 
    127129        private static final DataTypeBase[] LIST_TYPES = new DataTypeBase[] { IntList.TYPE8, IntList.TYPE8U, IntList.TYPE16, IntList.TYPE16U, IntList.TYPE32, IntList.TYPE32U, IntList.TYPE64, IntList.TYPE64, FloatList.TYPE, DoubleList.TYPE }; 
     
    174176 
    175177        /** 
    176          * Is this type adapter suitable for adapting the remote container type? (list or array) 
     178         * Is this type adapter suitable for adapting the remote container type? (list, array, or enum-based flags) 
    177179         * 
    178180         * @param remoteType Remote type to check 
     
    184186            boolean listType = (remoteType.getTypeTraits() & DataTypeBase.CLASSIFICATION_LIST) != 0; 
    185187            boolean arrayType = (remoteType.getTypeTraits() & DataTypeBase.CLASSIFICATION_ARRAY) != 0; 
     188            boolean enumBasedFlags = (remoteType.getTypeTraits() & DataTypeBase.CLASSIFICATION_ENUM_BASED_FLAGS) != 0; 
    186189 
    187190            if (listType || arrayType) { 
     
    202205                        return true; 
    203206                    } 
     207                } 
     208            } else if (enumBasedFlags && remoteType.getUnderlyingType() != 0) { 
     209                RemoteType elementType = runtime.getRemoteTypes().get(remoteType.getElementType()); 
     210                RemoteType underlyingType = runtime.getRemoteTypes().get(remoteType.getUnderlyingType()); 
     211                if (elementType.getEnumConstants() != null) { 
     212                    int size = underlyingType.getSize(); 
     213                    Type typeEnum = size == 1 ? Type.ENUM_FLAGS8 : (size == 2 ? Type.ENUM_FLAGS16 : (size == 4 ? Type.ENUM_FLAGS32 : (size == 8 ? Type.ENUM_FLAGS64 : null))); 
     214                    if (typeEnum == null) { 
     215                        Log.log(LogLevel.WARNING, "Remote enum-based flags type '" + remoteType.getName() + "' has invalid type size " + size); 
     216                        return false; 
     217                    } 
     218                    adapterInfo.customAdapterData1 = typeEnum; 
     219                    adapterInfo.customAdapterData2 = elementType; 
     220                    adapterInfo.localType = EnumBasedFlags.TYPE; 
     221                    adapterInfo.networkEncoding = Serialization.DataEncoding.BINARY; 
     222                    return true; 
    204223                } 
    205224            } 
     
    285304            case TIMESTAMP_ONLY: 
    286305                break; 
     306            case ENUM_FLAGS8: 
     307                ((EnumBasedFlags)object.getData()).set(stream.readByte() & 0xFF, (RemoteType)adapterInfo.customAdapterData2); 
     308                break; 
     309            case ENUM_FLAGS16: 
     310                ((EnumBasedFlags)object.getData()).set(stream.readShort() & 0xFFFF, (RemoteType)adapterInfo.customAdapterData2); 
     311                break; 
     312            case ENUM_FLAGS32: 
     313                ((EnumBasedFlags)object.getData()).set(stream.readInt() & 0xFFFFFFFFL, (RemoteType)adapterInfo.customAdapterData2); 
     314                break; 
     315            case ENUM_FLAGS64: 
     316                ((EnumBasedFlags)object.getData()).set(stream.readLong(), (RemoteType)adapterInfo.customAdapterData2); 
     317                break; 
    287318            } 
    288319        } 
     
    340371            case TIMESTAMP_ONLY: 
    341372                break; 
     373            case ENUM_FLAGS8: 
     374                stream.writeByte((int)((EnumBasedFlags)object.getData()).getRaw()); 
     375                break; 
     376            case ENUM_FLAGS16: 
     377                stream.writeShort((int)((EnumBasedFlags)object.getData()).getRaw()); 
     378                break; 
     379            case ENUM_FLAGS32: 
     380                stream.writeInt((int)((EnumBasedFlags)object.getData()).getRaw()); 
     381                break; 
     382            case ENUM_FLAGS64: 
     383                stream.writeLong(((EnumBasedFlags)object.getData()).getRaw()); 
     384                break; 
    342385            } 
    343386        } 
Note: See TracChangeset for help on using the changeset viewer.