Fix Mac OS X compilation
[scilab.git] / scilab / modules / ast / includes / types / int.hxx
index 2363b22..a1f1266 100644 (file)
 #include "internal.hxx"
 #include "types_transposition.hxx"
 #include "configvariable.hxx"
+#include "type_traits.hxx"
 
 #include <sstream>
 #include <string>
+#include <limits>       // std::numeric_limits
 #include "tostring_common.hxx"
 
 namespace types
@@ -135,7 +137,7 @@ public :
             }
         }
 
-        if (memcmp(ArrayOf<T>::get(), pb->get(), GenericType::getSize() * sizeof(int)) != 0)
+        if (memcmp(ArrayOf<T>::get(), pb->get(), GenericType::getSize() * sizeof(T)) != 0)
         {
             return false;
         }
@@ -161,13 +163,29 @@ protected :
     inline InternalType::ScilabId   getId(void);
 
 private :
-    virtual bool subMatrixToString(std::wostringstream& ostr, int* _piDims, int _iDims)
+    virtual bool subMatrixToString(std::wostringstream& ostr, int* _piDims, int /*_iDims*/)
     {
         int iCurrentLine = 0;
         int iLineLen = ConfigVariable::getConsoleWidth();
         int iMaxLines = ConfigVariable::getConsoleLines();
 
-        if (ArrayOf<T>::isScalar())
+        if (GenericType::isIdentity())
+        {
+            ostr << L"eye *" << std::endl << std::endl;
+            int iWidth = 0;
+            if (isSigned())
+            {
+                getSignedIntFormat(ArrayOf<T>::get(0), &iWidth);
+                addSignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
+            }
+            else
+            {
+                getUnsignedIntFormat(ArrayOf<T>::get(0), &iWidth);
+                addUnsignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
+            }
+            ostr << std::endl;
+        }
+        else if (GenericType::isScalar())
         {
             //scalar
             int iWidth  = 0;
@@ -175,8 +193,16 @@ private :
             _piDims[1]  = 0;
             int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-            getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-            addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
+            if (isSigned())
+            {
+                getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
+            }
+            else
+            {
+                getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
+            }
             ostr << std::endl;
         }
         else if (GenericType::getCols() == 1)
@@ -191,7 +217,14 @@ private :
                 _piDims[0] = i;
                 int iWidth = 0;
                 int iPos = ArrayOf<T>::getIndex(_piDims);
-                getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                if (isSigned())
+                {
+                    getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                }
+                else
+                {
+                    getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                }
                 iWidthMax = std::max(iWidthMax, iWidth);
             }
 
@@ -208,7 +241,14 @@ private :
                 _piDims[0]  = i;
                 int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
+                if (isSigned())
+                {
+                    addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
+                }
+                else
+                {
+                    addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
+                }
                 ostr << std::endl;
             }
         }
@@ -226,9 +266,17 @@ private :
                 _piDims[1]  = i;
                 int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                if (isSigned())
+                {
+                    getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                }
+                else
+                {
+                    getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                }
+
                 iLen = iWidth + static_cast<int>(ostemp.str().size());
-                if (iLen > iLineLen)
+                if (iLen > iLineLen && iLastVal != i)
                 {
                     //Max length, new line
                     iCurrentLine += 4; //"column x to Y" + empty line + value + empty line
@@ -238,19 +286,27 @@ private :
                         return false;
                     }
 
-                    ostr << std::endl << L"       column " << iLastVal + 1 << L" to " << i << std::endl << std::endl;
+                    addColumnString(ostr, iLastVal + 1, i);
                     ostr << ostemp.str() << std::endl;
                     ostemp.str(L"");
                     iLastVal = i;
                 }
 
-                addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
+                if (isSigned())
+                {
+                    addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
+                }
+                else
+                {
+                    addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
+                }
             }
 
             if (iLastVal != 0)
             {
-                ostr << std::endl << L"       column " << iLastVal + 1 << L" to " << GenericType::getCols() << std::endl << std::endl;
+                addColumnString(ostr, iLastVal + 1, GenericType::getCols());
             }
+
             ostemp << std::endl;
             ostr << ostemp.str();
         }
@@ -274,11 +330,18 @@ private :
                     _piDims[1]  = iCols1;
                     int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                    getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                    if (isSigned())
+                    {
+                        getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                    }
+                    else
+                    {
+                        getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+                    }
                     piSize[iCols1] = std::max(piSize[iCols1], iWidth);
                 }
 
-                if (iLen + piSize[iCols1] > iLineLen)
+                if (iLen + piSize[iCols1] > iLineLen && iCols1 != iLastCol)
                 {
                     //find the limit, print this part
                     for (int iRows2 = this->m_iRows2PrintState ; iRows2 < this->getRows() ; iRows2++)
@@ -291,8 +354,9 @@ private :
                             if (this->m_iRows2PrintState == 0 && iRows2 != 0)
                             {
                                 //add header
-                                ostr << std::endl << L"       column " << iLastCol + 1 << L" to " << iCols1 << std::endl << std::endl;
+                                addColumnString(ostr, iLastCol + 1, iCols1);
                             }
+
                             ostr << ostemp.str();
                             this->m_iRows2PrintState = iRows2;
                             this->m_iCols1PrintState = iLastCol;
@@ -301,12 +365,18 @@ private :
 
                         for (int iCols2 = iLastCol ; iCols2 < iCols1 ; iCols2++)
                         {
-                            int iWidth  = 0;
                             _piDims[0]  = iRows2;
                             _piDims[1]  = iCols2;
                             int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                            addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
+                            if (isSigned())
+                            {
+                                addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
+                            }
+                            else
+                            {
+                                addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
+                            }
                         }
                         ostemp << std::endl;
                     }
@@ -316,7 +386,7 @@ private :
                     if (this->m_iRows2PrintState == 0)
                     {
                         iCurrentLine += 3;
-                        ostr << std::endl << L"       column " << iLastCol + 1 << L" to " << iCols1 << std::endl << std::endl;
+                        addColumnString(ostr, iLastCol + 1, iCols1);
                     }
 
                     ostr << ostemp.str();
@@ -337,7 +407,7 @@ private :
                     if (this->m_iRows2PrintState == 0 && iLastCol != 0)
                     {
                         //add header
-                        ostr << std::endl << L"       column " << iLastCol + 1 << L" to " << GenericType::getCols() << std::endl << std::endl;
+                        addColumnString(ostr, iLastCol + 1, GenericType::getCols());
                     }
 
                     ostr << ostemp.str();
@@ -348,20 +418,27 @@ private :
 
                 for (int iCols2 = iLastCol ; iCols2 < GenericType::getCols() ; iCols2++)
                 {
-                    int iWidth  = 0;
                     _piDims[0]  = iRows2;
                     _piDims[1]  = iCols2;
                     int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                    addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
+                    if (isSigned())
+                    {
+                        addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
+                    }
+                    else
+                    {
+                        addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
+                    }
                 }
                 ostemp << std::endl;
             }
 
             if (this->m_iRows2PrintState == 0 && iLastCol != 0)
             {
-                ostr << std::endl << L"       column " << iLastCol + 1 << L" to " << GenericType::getCols() << std::endl << std::endl;
+                addColumnString(ostr, iLastCol + 1, GenericType::getCols());
             }
+
             ostr << ostemp.str();
         }
 
@@ -438,6 +515,16 @@ private :
     {
         return is_same_int<T, unsigned long long>::value;
     }
+
+    bool isUnsigned()
+    {
+        return !isSigned();
+    }
+
+    bool isSigned()
+    {
+        return std::numeric_limits<T>::is_signed;
+    }
 };
 
 template<> inline InternalType::ScilabType Int<char>::getType()
@@ -506,6 +593,88 @@ template<> inline InternalType::ScilabId Int<unsigned long long>::getId()
     return isScalar() ? IdScalarUInt64 : IdUInt64;
 }
 
+// Specializations
+template<> inline std::wstring Int<char>::getTypeStr()
+{
+    return L"int8";
+}
+
+template<> inline std::wstring Int<short>::getTypeStr()
+{
+    return L"int16";
+}
+
+template<> inline std::wstring Int<int>::getTypeStr()
+{
+    return L"int32";
+}
+
+template<> inline std::wstring Int<long long>::getTypeStr()
+{
+    return L"int64";
+}
+
+template<> inline std::wstring Int<unsigned char>::getTypeStr()
+{
+    return L"uint8";
+}
+
+template<> inline std::wstring Int<unsigned short>::getTypeStr()
+{
+    return L"uint16";
+}
+
+template<> inline std::wstring Int<unsigned int>::getTypeStr()
+{
+    return L"uint32";
+}
+
+template<> inline std::wstring Int<unsigned long long>::getTypeStr()
+{
+    return L"uint64";
+}
+
+template<> inline void Int<char>::whoAmI()
+{
+    std::cout << "types::Int8";
+}
+
+template<> inline void Int<short>::whoAmI()
+{
+    std::cout << "types::Int16";
+}
+
+template<> inline void Int<int>::whoAmI()
+{
+    std::cout << "types::Int32";
+}
+
+template<> inline void Int<long long>::whoAmI()
+{
+    std::cout << "types::Int64";
+}
+
+template<> inline void Int<unsigned char>::whoAmI()
+{
+    std::cout << "types::UInt8";
+}
+
+template<> inline void Int<unsigned short>::whoAmI()
+{
+    std::cout << "types::UInt16";
+}
+
+template<> inline void Int<unsigned int>::whoAmI()
+{
+    std::cout << "types::UInt32";
+}
+
+template<> inline void Int<unsigned long long>::whoAmI()
+{
+    std::cout << "types::UInt64";
+}
+
+
 typedef Int<char> Int8;
 typedef Int<short> Int16;
 typedef Int<int> Int32;