[opengtl-commits] [318] * add autoconversion to the convert center

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


Revision: 318
Author:   cyrille
Date:     2008-08-19 12:06:01 +0200 (Tue, 19 Aug 2008)

Log Message:
-----------
* add autoconversion to the convert center
* add (small) testing of the convert center

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
    trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h
    trunk/OpenGTL/OpenGTL/GTLCore/tests/TestGTLCore.cpp

Added Paths:
-----------
    trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h


Added: trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.cpp	                        (rev 0)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.cpp	2008-08-19 10:06:01 UTC (rev 318)
@@ -0,0 +1,37 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "ConvertExpression.h"
+
+using namespace GTLCore;
+using namespace GTLCore::AST;
+
+ConvertExpression::ConvertExpression( AST::Expression* _value ) : m_value(_value)
+{
+}
+
+void ConvertExpression::markAsReturnExpression()
+{
+  m_value->markAsReturnExpression();
+}
+
+bool ConvertExpression::isConstant() const
+{
+  return m_value->isConstant();
+}

Added: trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h	                        (rev 0)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h	2008-08-19 10:06:01 UTC (rev 318)
@@ -0,0 +1,38 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _AST_CONVERT_EXPRESSION_H_
+#define _AST_CONVERT_EXPRESSION_H_
+
+#include "GTLCore/AST/Expression.h"
+
+namespace GTLCore {
+  namespace AST {
+    class ConvertExpression : public Expression {
+      public:
+        ConvertExpression( AST::Expression* _value );
+        virtual void markAsReturnExpression();
+        virtual bool isConstant() const;
+      private:
+        AST::Expression* m_value;
+    };
+  }
+}
+
+#endif

Modified: trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-08-06 08:08:01 UTC (rev 317)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-08-19 10:06:01 UTC (rev 318)
@@ -29,6 +29,7 @@
 # Internal files
   AST/AccessorExpression.cpp
   AST/BinaryExpression.cpp
+  AST/ConvertExpression.cpp
   AST/CoumpoundExpression.cpp
   AST/Expression.cpp
   AST/FunctionDeclaration.cpp

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp	2008-08-06 08:08:01 UTC (rev 317)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.cpp	2008-08-19 10:06:01 UTC (rev 318)
@@ -20,6 +20,7 @@
 #include "ConvertCenter_p.h"
 
 #include <list>
+#include <map>
 
 using namespace GTLCore;
 
@@ -51,6 +52,7 @@
 
 struct ConvertCenter::Private {
   std::list< ConvertExpressionFactory* > factories;
+  std::map< const GTLCore::Type*, const GTLCore::Type* > autoconversion;
 };
 
 ConvertCenter::ConvertCenter() : d(new Private)
@@ -67,12 +69,27 @@
   return 0;
 }
 
-std::pair<AST::ConvertExpression*, AST::ConvertExpression*> ConvertCenter::createConvertExpression( AST::Expression* value1, AST::Expression* value2 ) const
+std::pair<AST::Expression*, AST::Expression*> ConvertCenter::createConvertExpressions( AST::Expression* value1, AST::Expression* value2 ) const
 {
-  return std::pair<AST::ConvertExpression*, AST::ConvertExpression*>(0, 0) ;
+  return std::pair<AST::Expression*, AST::Expression*>(0, 0) ;
 }
 
 void ConvertCenter::addConvertExpressionFactory( ConvertExpressionFactory* cef)
 {
   d->factories.push_back( cef );
 }
+
+void ConvertCenter::addAutoConversion( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType )
+{
+  d->autoconversion[ _srcType ] = _dstType;
+}
+
+const GTLCore::Type* ConvertCenter::autoConvertTo( const GTLCore::Type* _srcType ) const
+{
+  if( d->autoconversion.find( _srcType ) == d->autoconversion.end() )
+  {
+    return _srcType;
+  } else {
+    return d->autoconversion[ _srcType ];
+  }
+}

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h	2008-08-06 08:08:01 UTC (rev 317)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ConvertCenter_p.h	2008-08-19 10:06:01 UTC (rev 318)
@@ -43,9 +43,22 @@
     public:
       ConvertCenter();
       ~ConvertCenter();
+      /**
+       * Convert a value to a given type.
+       */
       AST::ConvertExpression* createConvertExpression( AST::Expression* value, const GTLCore::Type* _dstType ) const;
-      std::pair<AST::ConvertExpression*, AST::ConvertExpression*> createConvertExpression( AST::Expression* value1, AST::Expression* value2 ) const;
+      /**
+       * Create the conversions for binary operations:
+       *  - first autoconvert type that needs to be autoconverted.
+       *  - then convert the two given values to the highest priority type
+       */
+      std::pair<AST::Expression*, AST::Expression*> createConvertExpressions( AST::Expression* value1, AST::Expression* value2 ) const;
       void addConvertExpressionFactory( ConvertExpressionFactory* );
+      void addAutoConversion( const GTLCore::Type* _srcType, const GTLCore::Type* _dstType );
+      /**
+       * @return the type 
+       */
+      const GTLCore::Type* autoConvertTo( const GTLCore::Type* _srcType ) const;
     private:
       struct Private;
       Private* const d;

Modified: trunk/OpenGTL/OpenGTL/GTLCore/tests/TestGTLCore.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/tests/TestGTLCore.cpp	2008-08-06 08:08:01 UTC (rev 317)
+++ trunk/OpenGTL/OpenGTL/GTLCore/tests/TestGTLCore.cpp	2008-08-19 10:06:01 UTC (rev 318)
@@ -26,6 +26,7 @@
 #include "../String.h"
 #include "../Type.h"
 #include "../Value.h"
+#include "../ConvertCenter_p.h"
 
 using namespace GTLCore;
 
@@ -35,6 +36,7 @@
 #include "TestValue.h"
 #include "TestPixelDescription.h"
 #include "TestScopedName.h"
+#include "TestConvertCenter.h"
 
 class TestArray : public GTLTest::Case {
   public:
@@ -65,4 +67,5 @@
 GTLTEST_MAIN_ADD_CASE(TestString)
 GTLTEST_MAIN_ADD_CASE(TestErrorMessage)
 GTLTEST_MAIN_ADD_CASE(TestScopedName)
+GTLTEST_MAIN_ADD_CASE(TestConvertCenter)
 GTLTEST_MAIN_END()


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