Changeset 353:d80caa0f753d in finroc_core-java


Ignore:
Timestamp:
14.02.2022 07:07:32 (22 months ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Tags:
tip
Message:

Reduces preference for lossy casts when deciding how to represent unknown remote data types in Java tooling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • remote/RemoteType.java

    r341 r353  
    4848        EXACT,     // Option 1: Local type is the exact counterpart to remote type 
    4949        ADAPTED,   // Option 2: Local type is adapted via a type adapter 
    50         CAST,      // Option 3: Local type is obtained via static cast from and to remote type 
     50        CAST,      // Option 3: Local type is obtained via static cast from and to remote type (conversion to and from local type is lossless) 
    5151        STRING,    // Option 4: Type's string representation is used 
    5252        XML,       // Option 5: Type's XML representation is used 
    53         EVENT,     // Option 6: Event type is used 
     53        LOSSY_CAST,// Option 6: Local type is obtained via static cast from and to remote type (conversion to and from local type is lossy) 
     54        EVENT,     // Option 7: Event type is used 
    5455        NONE       // Local type has not yet been resolved 
    5556    } 
     
    292293        } 
    293294 
    294         // Option 3: Type has a static cast operation to and from known type (underlying type is preferred, next choices are types with best local type match - and smallest size difference) 
     295        // Option 3/6: Type has a static cast operation to and from known type (underlying type is preferred, next choices are types with best local type match - and smallest size difference) 
    295296        RemoteType castedTo = null; 
     297        boolean lossyCast = false; 
    296298        if ((typeTraits & DataTypeBase.HAS_UNDERLYING_TYPE) != 0) { 
    297299            castedTo = runtime.getTypes().get(underlyingType); 
     
    309311                if (cast.getSourceType() == this && runtime.isStaticCastSupported(cast.getDestinationType(), this)) { 
    310312                    RemoteType candidate = cast.getDestinationType(); 
     313                    boolean candidateCastLossy = candidate.size < this.size; 
    311314                    if (maxCasts == 2) { 
    312315                        candidate.resolveDefaultLocalType(runtime, 1); 
    313316                    } 
    314                     if (candidate.getCastCountForDefaultLocalType() <= 1 && (castedTo == null || (candidate.localTypeMatch.ordinal() < castedTo.localTypeMatch.ordinal()) || (candidate.localTypeMatch == castedTo.localTypeMatch && Math.abs(candidate.size - this.size) < Math.abs(castedTo.size - this.size)))) { 
     317                    if (candidate.getCastCountForDefaultLocalType() <= 1 && (castedTo == null || (candidate.localTypeMatch.ordinal() < castedTo.localTypeMatch.ordinal()) || 
     318                            (candidate.localTypeMatch == castedTo.localTypeMatch && ((!candidateCastLossy) || lossyCast) && (((!candidateCastLossy) && lossyCast) || Math.abs(candidate.size - this.size) < Math.abs(castedTo.size - this.size))))) { 
    315319                        castedTo = candidate; 
     320                        lossyCast = candidateCastLossy; 
     321                        //System.out.println("Cast operation adapted type (this -> casted to): " + this.getName() + " " + this.getSize() + " -> " + castedTo.getName() + " " + castedTo.getSize()+ " " + lossyCast); 
    316322                    } 
    317323                } 
     
    320326 
    321327        localTypeCastsChecked |= (maxCasts >= 2); 
    322         if (castedTo != null) { 
     328        if (castedTo != null && ((!lossyCast) || localTypeMatch.ordinal() > LocalTypeMatch.LOSSY_CAST.ordinal())) { 
    323329            this.castedTo = castedTo; 
    324330            localDataType = castedTo.localDataType; 
    325             localTypeMatch = LocalTypeMatch.CAST; 
     331            localTypeMatch = lossyCast ? LocalTypeMatch.LOSSY_CAST : LocalTypeMatch.CAST; 
    326332            return; 
    327333        } 
     
    364370 
    365371        // Options 4 and 5: Type has a string or XML representation 
    366         // Option 6: Use empty (event) type 
     372        // Option 7: Use empty (event) type 
    367373        RemoteTypeAdapter.Default defaultAdapter = RemoteTypeAdapter.Default.getInstance(); 
    368374        defaultAdapter.handleType(this, info); 
     
    403409            break; 
    404410        case CAST: 
     411        case LOSSY_CAST: 
    405412            castedTo.deserializeData(stream, object); 
    406413            break; 
     
    429436            break; 
    430437        case CAST: 
     438        case LOSSY_CAST: 
    431439            castedTo.serializeData(stream, object); 
    432440            break; 
Note: See TracChangeset for help on using the changeset viewer.