[opengtl-commits] [326] * implement ConvertCenter::createConvertExpressions

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


Revision: 326
Author:   cyrille
Date:     2008-08-30 23:21:36 +0200 (Sat, 30 Aug 2008)

Log Message:
-----------
* implement ConvertCenter::createConvertExpressions

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h
    trunk/OpenGTL/OpenGTL/GTLCore/Type.h


Modified: trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp	2008-08-30 21:17:27 UTC (rev 325)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp	2008-08-30 21:21:36 UTC (rev 326)
@@ -23,9 +23,12 @@
 #include <map>
 
 #include "AST/ConvertExpression.h"
+#include "Type_p.h"
 
 using namespace GTLCore;
 
+//--------------------------- ConvertExpressionFactory ---------------------------//
+
 struct ConvertExpressionFactory::Private {
 };
 
@@ -39,6 +42,19 @@
 }
 
 
+//------------------------ DefaultConvertExpressionFactory -----------------------//
+
+class DefaultConvertExpressionFactory : public ConvertExpressionFactory {
+  public:
+    virtual AST::ConvertExpression* create( AST::Expression* value, const GTLCore::Type* _dstType ) const
+    {
+      return 0;
+    }
+    virtual bool canConvertBetween( const GTLCore::Type* srcType, const GTLCore::Type* dstType) const = 0;
+};
+
+//--------------------------------- ConvertCenter --------------------------------//
+
 struct ConvertCenter::Private {
   std::list< ConvertExpressionFactory* > factories;
   std::map< const GTLCore::Type*, const GTLCore::Type* > autoconversion;
@@ -53,8 +69,12 @@
   delete d;
 }
 
-AST::ConvertExpression* ConvertCenter::createConvertExpression( AST::Expression* _value, const GTLCore::Type* _dstType ) const
+AST::Expression* ConvertCenter::createConvertExpression( AST::Expression* _value, const GTLCore::Type* _dstType ) const
 {
+  if( _value->type() == _dstType )
+  {
+    return _value;
+  }
   for( std::list< ConvertExpressionFactory* >::iterator it = d->factories.begin(); it != d->factories.end(); ++it)
   {
     if( (*it)->canConvertBetween( _value->type(), _dstType ) )
@@ -67,7 +87,17 @@
 
 std::pair<AST::Expression*, AST::Expression*> ConvertCenter::createConvertExpressions( AST::Expression* value1, AST::Expression* value2 ) const
 {
-  return std::pair<AST::Expression*, AST::Expression*>(0, 0) ;
+  // Auto conversion
+  value1 = createConvertExpression( value1, autoConvertType( value1->type() ) );
+  value2 = createConvertExpression( value2, autoConvertType( value2->type() ) );
+  // If values are of the same type, there is no need to convert
+  if( value1->type() == value2->type() )
+  {
+    return std::pair<AST::Expression*, AST::Expression*>( value1, value2 );
+  }
+  // Select the conversion with the highest priority
+  const Type* type = Type::Private::selectType(value1->type(), value2->type());
+  return std::pair<AST::Expression*, AST::Expression*>( createConvertExpression(value1, type), createConvertExpression(value2, type)) ;
 }
 
 void ConvertCenter::addConvertExpressionFactory( ConvertExpressionFactory* cef)
@@ -80,7 +110,7 @@
   d->autoconversion[ _srcType ] = _dstType;
 }
 
-const GTLCore::Type* ConvertCenter::autoConvertTo( const GTLCore::Type* _srcType ) const
+const GTLCore::Type* ConvertCenter::autoConvertType( const GTLCore::Type* _srcType ) const
 {
   if( d->autoconversion.find( _srcType ) == d->autoconversion.end() )
   {

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h	2008-08-30 21:17:27 UTC (rev 325)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h	2008-08-30 21:21:36 UTC (rev 326)
@@ -44,8 +44,9 @@
       ~ConvertCenter();
       /**
        * Convert a value to a given type.
+       * If the type of the \a value is allready \a _dstType , the function will return \a value .
        */
-      AST::ConvertExpression* createConvertExpression( AST::Expression* value, const GTLCore::Type* _dstType ) const;
+      AST::Expression* createConvertExpression( AST::Expression* value, const GTLCore::Type* _dstType ) const;
       /**
        * Create the conversions for binary operations:
        *  - first autoconvert type that needs to be autoconverted.
@@ -55,9 +56,11 @@
       void addConvertExpressionFactory( ConvertExpressionFactory* );
       void addAutoConversion( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType );
       /**
-       * @return the type 
+       * @return the type to which \a _srcType needs to be converted
        */
-      const GTLCore::Type* autoConvertTo( const GTLCore::Type* _srcType ) const;
+      const GTLCore::Type* autoConvertType( const GTLCore::Type* _srcType ) const;
+      /**
+       */
     private:
       struct Private;
       Private* const d;

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-08-30 21:17:27 UTC (rev 325)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-08-30 21:21:36 UTC (rev 326)
@@ -64,6 +64,7 @@
     friend class Visitor;
     friend class VectorVisitor;
     friend class ExpressionResult;
+    friend class ConvertCenter;
     friend std::ostream& operator<< (std::ostream& ostr, const Type& type);
     public:
       enum DataType {
@@ -192,6 +193,7 @@
        * @return the size of a vector
        */
       int vectorSize() const;
+      
     private:
       struct Private;
       Private* const d;


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