[opengtl-commits] [719] add a function to the convert center to know wether a conversion is possible (without creating it), and wether the conversion will be lossless or not

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 719
Author:   cyrille
Date:     2009-04-11 13:57:40 +0200 (Sat, 11 Apr 2009)

Log Message:
-----------
add a function to the convert center to know wether a conversion is possible (without creating it), and wether the conversion will be lossless or not

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h
    trunk/OpenGTL/OpenGTL/GTLCore/tests/TestConvertCenter.h
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.h


Modified: trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp	2009-04-11 11:04:48 UTC (rev 718)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp	2009-04-11 11:57:40 UTC (rev 719)
@@ -150,7 +150,6 @@
     }
     
     it->second.push_back(func);
-//     return false;
   }
     return true;
 }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp	2009-04-11 11:04:48 UTC (rev 718)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp	2009-04-11 11:57:40 UTC (rev 719)
@@ -25,6 +25,7 @@
 #include "AST/ConvertExpression.h"
 #include "Debug.h"
 #include "Type_p.h"
+#include "Macros_p.h"
 
 using namespace GTLCore;
 
@@ -58,6 +59,20 @@
           or ( dstType->dataType() == Type::VECTOR and canConvertBetween( srcType, dstType->embeddedType() ) )
           or ( srcType->isNumber() and dstType->isNumber() );
     }
+    virtual ConvertCenter::ConversionQuality conversionQuality( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType ) const {
+      GTL_ASSERT( canConvertBetween(_srcType, _dstType) );
+      if( _dstType->isNumber() )
+      {
+        GTL_ASSERT( _srcType->isNumber() );
+        if( ( _dstType->isInteger() and _srcType->isFloatingPoint() ) or ( _dstType->bitsSize() < _srcType->bitsSize() ) )
+        {
+          return ConvertCenter::LOSS_CONVERSION;
+        }
+        return ConvertCenter::LOSSLESS_CONVERSION;
+      }
+      GTL_ASSERT( _dstType->dataType() == Type::VECTOR );
+      return conversionQuality(_srcType, _dstType->embeddedType());
+    }
 };
 
 //------------------------ ArrayToVectorConvertExpressionFactory -----------------------//
@@ -75,6 +90,10 @@
            and dstType->dataType() == GTLCore::Type::VECTOR
            and srcType->embeddedType() == dstType->embeddedType() );
     }
+    virtual ConvertCenter::ConversionQuality conversionQuality( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType ) const {
+      GTL_ASSERT( canConvertBetween(_srcType, _dstType) );
+      return ConvertCenter::LOSSLESS_CONVERSION;
+    }
 };
 
 //--------------------------------- ConvertCenter --------------------------------//
@@ -99,6 +118,23 @@
   delete d;
 }
 
+ConvertCenter::ConversionQuality ConvertCenter::conversionQuality( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType ) const
+{
+  if( _srcType == _dstType )
+  {
+    return NONEEDED_CONVERSION;
+  }
+  foreach( ConvertExpressionFactory* cef, d->factories )
+  {
+    if( cef->canConvertBetween(_srcType, _dstType) )
+    {
+      return cef->conversionQuality(_srcType, _dstType);
+    }
+  }
+  return NOT_CONVERTIBLE;
+}
+
+
 AST::Expression* ConvertCenter::createConvertExpression( AST::Expression* _value, const GTLCore::Type* _dstType ) const
 {
   if( _value->type() == _dstType )

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h	2009-04-11 11:04:48 UTC (rev 718)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h	2009-04-11 11:57:40 UTC (rev 719)
@@ -27,30 +27,10 @@
     class ConvertExpression;
     class Expression;
   }
+  class ConvertExpressionFactory;
   class Type;
   /**
    * @internal
-   * This class allows to create the \ref AST::ConvertExpression that can convert between two different types.
-   * @ingroup GTLCore
-   */
-  class ConvertExpressionFactory {
-    public:
-      ConvertExpressionFactory( );
-      virtual ~ConvertExpressionFactory();
-      /**
-       * Create the \ref AST::ConvertExpression that can convert the given @p value to the given @p _dstType
-       */
-      virtual AST::ConvertExpression* create( AST::Expression* value, const GTLCore::Type* _dstType ) const = 0;
-      /**
-       * @return true if this \ref ConvertExpressionFactory can convert between @p srcType and @p dstType
-       */
-      virtual bool canConvertBetween( const GTLCore::Type* srcType, const GTLCore::Type* dstType) const = 0;
-    private:
-      struct Private;
-      Private* const d;
-  };
-  /**
-   * @internal
    * This class contains informations about all the possible conversion.
    * @ingroup GTLCore
    */
@@ -58,7 +38,19 @@
     public:
       ConvertCenter();
       ~ConvertCenter();
+    public:
+      enum ConversionQuality {
+          NONEEDED_CONVERSION, ///< indicate that no conversion is needed
+          NOT_CONVERTIBLE, ///< indicates that there is no conversion between the two type
+          LOSSLESS_CONVERSION, ///< indicates that the conversion is lossless (ie from integer to float)
+          LOSS_CONVERSION ///< indicates there is a risk of losing information in the conversion (ie from float to integer)
+      };
       /**
+       * @return the conversion quality between two types.
+       */
+      ConversionQuality conversionQuality( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType ) const;
+    public:
+      /**
        * Convert a value to a given type.
        * If the type of the \a value is allready \a _dstType , the function will return \a value .
        */
@@ -81,6 +73,31 @@
       struct Private;
       Private* const d;
   };
+  /**
+   * @internal
+   * This class allows to create the \ref AST::ConvertExpression that can convert between two different types.
+   * @ingroup GTLCore
+   */
+  class ConvertExpressionFactory {
+    public:
+      ConvertExpressionFactory( );
+      virtual ~ConvertExpressionFactory();
+      /**
+       * Create the \ref AST::ConvertExpression that can convert the given @p value to the given @p _dstType
+       */
+      virtual AST::ConvertExpression* create( AST::Expression* value, const GTLCore::Type* _dstType ) const = 0;
+      /**
+       * @return true if this \ref ConvertExpressionFactory can convert between @p srcType and @p dstType
+       */
+      virtual bool canConvertBetween( const GTLCore::Type* srcType, const GTLCore::Type* dstType) const = 0;
+      /**
+       * @return the conversion quality between two types.
+       */
+      virtual ConvertCenter::ConversionQuality conversionQuality( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType ) const = 0;
+    private:
+      struct Private;
+      Private* const d;
+  };
 
 }
 

Modified: trunk/OpenGTL/OpenGTL/GTLCore/tests/TestConvertCenter.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/tests/TestConvertCenter.h	2009-04-11 11:04:48 UTC (rev 718)
+++ trunk/OpenGTL/OpenGTL/GTLCore/tests/TestConvertCenter.h	2009-04-11 11:57:40 UTC (rev 719)
@@ -17,16 +17,44 @@
  * Boston, MA 02110-1301, USA.
  */
 
-class TestConvertCenter : public GTLTest::Case {
+class TestConvertCenterConversionQuality : public GTLTest::Case {
   public:
-    TestConvertCenter() : GTLTest::Case("ConvertCenter")
+    TestConvertCenterConversionQuality() : GTLTest::Case("ConvertCenterConversionQuality")
     {
     }
     virtual void runTest()
     {
       ConvertCenter cc;
+      GTLTEST_CHECK_EQUAL( cc.conversionQuality( GTLCore::Type::UnsignedInteger32, GTLCore::Type::UnsignedInteger32 ), GTLCore::ConvertCenter::NONEEDED_CONVERSION );
+      GTLTEST_CHECK_EQUAL( cc.conversionQuality( GTLCore::Type::UnsignedInteger32, GTLCore::Type::Float ), GTLCore::ConvertCenter::LOSSLESS_CONVERSION );
+      GTLTEST_CHECK_EQUAL( cc.conversionQuality( GTLCore::Type::Half, GTLCore::Type::Float ), GTLCore::ConvertCenter::LOSSLESS_CONVERSION );
+      GTLTEST_CHECK_EQUAL( cc.conversionQuality( GTLCore::Type::Float, GTLCore::Type::Half ), GTLCore::ConvertCenter::LOSS_CONVERSION );
+      GTLTEST_CHECK_EQUAL( cc.conversionQuality( GTLCore::Type::Float, GTLCore::Type::UnsignedInteger32 ), GTLCore::ConvertCenter::LOSS_CONVERSION );
+    }
+};
+
+class TestConvertCenterAutoConversion : public GTLTest::Case {
+  public:
+    TestConvertCenterAutoConversion() : GTLTest::Case("ConvertCenterAutoConversion")
+    {
+    }
+    virtual void runTest()
+    {
+      ConvertCenter cc;
       cc.addAutoConversion( GTLCore::Type::UnsignedInteger32, GTLCore::Type::Integer32 );
       GTLTEST_CHECK_EQUAL( cc.autoConvertType( GTLCore::Type::UnsignedInteger32 ), GTLCore::Type::Integer32 );
       GTLTEST_CHECK_EQUAL( cc.autoConvertType( GTLCore::Type::Integer32 ), GTLCore::Type::Integer32 );
     }
 };
+
+
+class TestConvertCenter : public GTLTest::Suite {
+  public:
+    TestConvertCenter() : GTLTest::Suite("ConvertCenter")
+    {
+      addCase( new TestConvertCenterAutoConversion);
+      addCase( new TestConvertCenterConversionQuality);
+    }
+};
+
+

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.cpp	2009-04-11 11:04:48 UTC (rev 718)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.cpp	2009-04-11 11:57:40 UTC (rev 719)
@@ -24,11 +24,12 @@
 #include "GTLCore/Type.h"
 #include "GTLCore/AST/ConvertExpression.h"
 #include "GTLCore/ExpressionResult_p.h"
+#include "GTLCore/ExpressionGenerationContext_p.h"
+#include "GTLCore/Macros_p.h"
 
 #include "CodeGenerator_p.h"
 #include "Debug.h"
 #include "wrappers/PixelWrap_p.h"
-#include <GTLCore/ExpressionGenerationContext_p.h>
 
 using namespace OpenShiva;
 
@@ -75,3 +76,11 @@
   return (srcType->dataType() == GTLCore::Type::STRUCTURE and srcType->structName().startWith( "pixel" ) and srcType->structDataMember(PixelWrap::INDEX_DATA).type() == dstType )
       or (dstType->dataType() == GTLCore::Type::STRUCTURE and dstType->structName().startWith( "pixel" ) and dstType->structDataMember(PixelWrap::INDEX_DATA).type() == srcType );
 }
+
+GTLCore::ConvertCenter::ConversionQuality PixelConvertExpressionFactory::conversionQuality( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType ) const
+{
+  GTL_ASSERT( canConvertBetween( _srcType, _dstType) );
+  UNUSED(_srcType);
+  UNUSED(_dstType);
+  return GTLCore::ConvertCenter::LOSSLESS_CONVERSION;
+}

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.h	2009-04-11 11:04:48 UTC (rev 718)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelConvertExpressionFactory_p.h	2009-04-11 11:57:40 UTC (rev 719)
@@ -30,6 +30,7 @@
       virtual ~PixelConvertExpressionFactory();
       virtual GTLCore::AST::ConvertExpression* create( GTLCore::AST::Expression* value, const GTLCore::Type* _dstType ) const;
       virtual bool canConvertBetween( const GTLCore::Type* srcType, const GTLCore::Type* dstType) const;
+      virtual GTLCore::ConvertCenter::ConversionQuality conversionQuality( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType ) const;
   };
 }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/