Changeset 51:4d0fc10a9592 in rrlib_serialization-java


Ignore:
Timestamp:
15.11.2019 07:18:17 (8 months ago)
Author:
Max Reichardt <max.reichardt@…>
Branch:
default
Phase:
public
Message:

Adds option to ArrayBufferPortDataList to provide element data prototype instead of merely an element data type, which allows for more generic type adapter implementations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rtti/datatype/ArrayBufferPortDataList.java

    r49 r51  
    2828import org.rrlib.serialization.Serialization; 
    2929import org.rrlib.serialization.XMLSerializable; 
     30import org.rrlib.serialization.rtti.Copyable; 
     31import org.rrlib.serialization.rtti.DataType; 
    3032import org.rrlib.serialization.rtti.DataTypeBase; 
    3133import org.rrlib.xml.XMLNode; 
     
    4244    private Channel[] channels = new Channel[0]; 
    4345    private int[] dimensions = new int[1]; 
     46 
    4447    private DataTypeBase elementType; 
     48    @SuppressWarnings("rawtypes") 
     49    private Copyable elementTypePrototype; 
     50 
     51    public final static DataTypeBase TYPE = new DataType<ArrayBufferPortDataList>(ArrayBufferPortDataList.class); 
    4552 
    4653    public ArrayBufferPortDataList() {} 
     
    5764    public void setChannels(Channel[] channels) { 
    5865        this.channels = channels; 
     66    } 
     67 
     68    @SuppressWarnings("rawtypes") 
     69    public void setElementTypePrototype(Copyable prototype) { 
     70        this.elementTypePrototype = prototype; 
    5971    } 
    6072 
     
    97109    } 
    98110 
     111    @SuppressWarnings({ "unchecked", "rawtypes" }) 
     112    private BinarySerializable getTempElement() throws Exception { 
     113        if (elementType == null && elementTypePrototype == null) { 
     114            throw new RuntimeException("Temp element needs to be set before (de)serialization"); 
     115        } 
     116        if (elementTypePrototype != null) { 
     117            BinarySerializable result = (BinarySerializable)elementTypePrototype.getClass().newInstance(); 
     118            ((Copyable)result).copyFrom(elementTypePrototype); 
     119            return result; 
     120        } 
     121        return (BinarySerializable)elementType.createInstance(); 
     122    } 
     123 
    99124    @Override 
    100125    public synchronized void serialize(XMLNode node) throws Exception { 
    101         if (elementType == null) { 
    102             throw new RuntimeException("Temp element needs to be set before serialization"); 
    103         } 
     126        BinarySerializable tempElement = getTempElement(); 
    104127        int recordSize = getRecordSize(); 
    105128        int length = getSize() / recordSize; 
    106         BinarySerializable tempElement = (BinarySerializable)elementType.createInstance(); 
    107129        BinaryInputStream stream = new BinaryInputStream(this); 
    108130        for (int i = 0; i < length; i++) { 
     
    114136    @Override 
    115137    public synchronized void deserialize(XMLNode node) throws Exception { 
    116         if (elementType == null) { 
    117             throw new RuntimeException("Temp element needs to be set before deserialization"); 
    118         } 
    119         BinarySerializable tempElement = (BinarySerializable)elementType.createInstance(); 
     138        BinarySerializable tempElement = getTempElement(); 
    120139        BinaryOutputStream stream = new BinaryOutputStream(this); 
    121140        for (XMLNode child : node.children()) { 
     
    126145    } 
    127146 
    128  
     147    @Override 
     148    public void copyFrom(MemoryBuffer source) { 
     149        super.copyFrom(source); 
     150        if (source instanceof ArrayBufferPortDataList) { 
     151            ArrayBufferPortDataList other = (ArrayBufferPortDataList)source; 
     152            this.channels = other.channels; 
     153            this.dimensions = new int[other.dimensions.length]; 
     154            System.arraycopy(other.dimensions, 0, this.dimensions, 0, other.dimensions.length); 
     155            this.elementType = other.elementType; 
     156            this.elementTypePrototype = other.elementTypePrototype; 
     157        } 
     158    } 
    129159} 
Note: See TracChangeset for help on using the changeset viewer.