[colobot] 122/377: Moving CBotStringArray class in its own header and source files.

Didier Raboud odyx at moszumanska.debian.org
Wed Mar 30 13:34:05 UTC 2016


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch debian/master
in repository colobot.

commit 67dff4ef6506755eb7312f0ec305d69f56d2a04d
Author: Grunaka <dev at romainbreton.fr>
Date:   Mon Nov 16 22:17:33 2015 +0100

    Moving CBotStringArray class in its own header and source files.
---
 src/CBot/CBotDll.h                          |  21 ----
 src/CBot/CBotProgram.h                      |   1 +
 src/CBot/CBotString.cpp                     | 144 ----------------------------
 src/CBot/CBotStringArray.cpp                | 136 ++++++++++++++++++++++++++
 src/CBot/{CBotUtils.h => CBotStringArray.h} |  92 ++++++++++--------
 src/CBot/CBotToken.h                        |   2 +
 src/CBot/CBotUtils.cpp                      |  20 ++++
 src/CBot/CBotUtils.h                        |  42 ++++++++
 src/CBot/CMakeLists.txt                     |   1 +
 9 files changed, 252 insertions(+), 207 deletions(-)

diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h
index 56b4364..16b400e 100644
--- a/src/CBot/CBotDll.h
+++ b/src/CBot/CBotDll.h
@@ -419,27 +419,6 @@ private:
     static const char * MapIdToString(EID id);
 };
 
-
-// Class used to array management
-
-class CBotStringArray : public CBotString
-{
-private:
-    int                m_nSize;                    // number of elements
-    int                m_nMaxSize;                    // reserved size
-    CBotString*        m_pData;                    // ^data
-
-public:
-                    CBotStringArray();
-                    ~CBotStringArray();
-    void            SetSize(int nb);
-    int             GetSize();
-    void            Add(const CBotString& str);
-    CBotString&        operator[](int nIndex);
-
-    CBotString&        ElementAt(int nIndex);
-};
-
 ///////////////////////////////////////////////////////////////////////////////
 // routines for file management  (* FILE)
     FILE*        fOpen(const char* name, const char* mode);
diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h
index 02aa537..bef08a8 100644
--- a/src/CBot/CBotProgram.h
+++ b/src/CBot/CBotProgram.h
@@ -20,6 +20,7 @@
 #pragma once
 
 // Modules inlcude
+#include "CBotStringArray.h"
 
 // Local include
 
diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp
index 1f31f7f..12cc546 100644
--- a/src/CBot/CBotString.cpp
+++ b/src/CBot/CBotString.cpp
@@ -524,147 +524,3 @@ const char * CBotString::MapIdToString(EID id)
         return emptyString;
     }
 }
-
-///////////////////////////////////////////////////////////////////////////////////////////
-// arrays of strings
-
-CBotStringArray::CBotStringArray()
-{
-    m_pData = nullptr;
-    m_nSize = m_nMaxSize = 0;
-}
-
-CBotStringArray::~CBotStringArray()
-{
-    SetSize(0);                    // destroys data !
-}
-
-
-int CBotStringArray::GetSize()
-{
-    return m_nSize;
-}
-
-void CBotStringArray::Add(const CBotString& str)
-{
-    SetSize(m_nSize+1);
-
-    m_pData[m_nSize-1] = str;
-}
-
-///////////////////////////////////////////////////////////////////////
-// utility routines
-
-static inline void ConstructElement(CBotString* pNewData)
-{
-    memset(pNewData, 0, sizeof(CBotString));
-}
-
-static inline void DestructElement(CBotString* pOldData)
-{
-    pOldData->~CBotString();
-}
-
-static inline void CopyElement(CBotString* pSrc, CBotString* pDest)
-{
-    *pSrc = *pDest;
-}
-
-static void ConstructElements(CBotString* pNewData, int nCount)
-{
-    while (nCount--)
-    {
-        ConstructElement(pNewData);
-        pNewData++;
-    }
-}
-
-static void DestructElements(CBotString* pOldData, int nCount)
-{
-    while (nCount--)
-    {
-        DestructElement(pOldData);
-        pOldData++;
-    }
-}
-
-// set the array size
-
-void CBotStringArray::SetSize(int nNewSize)
-{
-    if (nNewSize == 0)
-    {
-        // shrink to nothing
-
-        DestructElements(m_pData, m_nSize);
-        delete[] reinterpret_cast<unsigned char *>(m_pData);
-        m_pData = nullptr;
-        m_nSize = m_nMaxSize = 0;
-    }
-    else if (m_pData == nullptr)
-    {
-        // create one with exact size
-        m_pData = reinterpret_cast<CBotString*> (new unsigned char[nNewSize * sizeof(CBotString)]);
-
-        ConstructElements(m_pData, nNewSize);
-
-        m_nSize = m_nMaxSize = nNewSize;
-    }
-    else if (nNewSize <= m_nMaxSize)
-    {
-        // it fits
-        if (nNewSize > m_nSize)
-        {
-            // initialize the new elements
-
-            ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize);
-
-        }
-
-        else if (m_nSize > nNewSize)  // destroy the old elements
-            DestructElements(&m_pData[nNewSize], m_nSize-nNewSize);
-
-        m_nSize = nNewSize;
-    }
-    else
-    {
-        // otherwise, grow array
-        int nGrowBy;
-        {
-            // heuristically determine growth when nGrowBy == 0
-            //  (this avoids heap fragmentation in many situations)
-            nGrowBy = std::min(1024, std::max(4, m_nSize / 8));
-        }
-        int nNewMax;
-        if (nNewSize < m_nMaxSize + nGrowBy)
-            nNewMax = m_nMaxSize + nGrowBy;  // granularity
-        else
-            nNewMax = nNewSize;  // no slush
-
-        CBotString* pNewData = reinterpret_cast<CBotString*> (new unsigned char[nNewMax * sizeof(CBotString)]);
-
-        // copy new data from old
-        memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString));
-
-        // construct remaining elements
-        ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize);
-
-
-        // Get rid of old stuff (note: no destructors called)
-        delete[] reinterpret_cast<unsigned char *>(m_pData);
-        m_pData = pNewData;
-        m_nSize = nNewSize;
-        m_nMaxSize = nNewMax;
-    }
-}
-
-
-CBotString& CBotStringArray::operator[](int nIndex)
-{
-    return ElementAt(nIndex);
-}
-
-CBotString& CBotStringArray::ElementAt(int nIndex)
-{
-    return m_pData[nIndex];
-}
diff --git a/src/CBot/CBotStringArray.cpp b/src/CBot/CBotStringArray.cpp
new file mode 100644
index 0000000..ecf53bf
--- /dev/null
+++ b/src/CBot/CBotStringArray.cpp
@@ -0,0 +1,136 @@
+/*
+ * This file is part of the Colobot: Gold Edition source code
+ * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
+ * http://epsitec.ch; http://colobot.info; http://github.com/colobot
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://gnu.org/licenses
+ */
+
+// Modules inlcude
+#include "CBotStringArray.h"
+
+#include "CBotUtils.h"
+
+// Local include
+
+// Global include
+
+
+////////////////////////////////////////////////////////////////////////////////
+CBotStringArray::CBotStringArray()
+{
+    m_pData = nullptr;
+    m_nSize = m_nMaxSize = 0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CBotStringArray::~CBotStringArray()
+{
+    SetSize(0);                    // destroys data !
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int CBotStringArray::GetSize()
+{
+    return m_nSize;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CBotStringArray::Add(const CBotString& str)
+{
+    SetSize(m_nSize+1);
+
+    m_pData[m_nSize-1] = str;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CBotStringArray::SetSize(int nNewSize)
+{
+    if (nNewSize == 0)
+    {
+        // shrink to nothing
+
+        DestructElements(m_pData, m_nSize);
+        delete[] reinterpret_cast<unsigned char *>(m_pData);
+        m_pData = nullptr;
+        m_nSize = m_nMaxSize = 0;
+    }
+    else if (m_pData == nullptr)
+    {
+        // create one with exact size
+        m_pData = reinterpret_cast<CBotString*> (new unsigned char[nNewSize * sizeof(CBotString)]);
+
+        ConstructElements(m_pData, nNewSize);
+
+        m_nSize = m_nMaxSize = nNewSize;
+    }
+    else if (nNewSize <= m_nMaxSize)
+    {
+        // it fits
+        if (nNewSize > m_nSize)
+        {
+            // initialize the new elements
+
+            ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize);
+
+        }
+
+        else if (m_nSize > nNewSize)  // destroy the old elements
+            DestructElements(&m_pData[nNewSize], m_nSize-nNewSize);
+
+        m_nSize = nNewSize;
+    }
+    else
+    {
+        // otherwise, grow array
+        int nGrowBy;
+        {
+            // heuristically determine growth when nGrowBy == 0
+            //  (this avoids heap fragmentation in many situations)
+            nGrowBy = std::min(1024, std::max(4, m_nSize / 8));
+        }
+        int nNewMax;
+        if (nNewSize < m_nMaxSize + nGrowBy)
+            nNewMax = m_nMaxSize + nGrowBy;  // granularity
+        else
+            nNewMax = nNewSize;  // no slush
+
+        CBotString* pNewData = reinterpret_cast<CBotString*> (new unsigned char[nNewMax * sizeof(CBotString)]);
+
+        // copy new data from old
+        memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString));
+
+        // construct remaining elements
+        ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize);
+
+
+        // Get rid of old stuff (note: no destructors called)
+        delete[] reinterpret_cast<unsigned char *>(m_pData);
+        m_pData = pNewData;
+        m_nSize = nNewSize;
+        m_nMaxSize = nNewMax;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CBotString& CBotStringArray::operator[](int nIndex)
+{
+    return ElementAt(nIndex);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CBotString& CBotStringArray::ElementAt(int nIndex)
+{
+    return m_pData[nIndex];
+}
diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotStringArray.h
similarity index 51%
copy from src/CBot/CBotUtils.h
copy to src/CBot/CBotStringArray.h
index 354595b..761f874 100644
--- a/src/CBot/CBotUtils.h
+++ b/src/CBot/CBotStringArray.h
@@ -27,51 +27,59 @@
 // Global include
 
 /*!
- * \brief MakeListVars Transforms the array of pointers to variables in a
- * chained list of variables
- * \param ppVars
- * \param bSetVal
- * \return
+ * \brief The CBotStringArray class Class used to arrays of strings management.
  */
-CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false);
+class CBotStringArray : public CBotString
+{
+public:
 
-/*!
- * \brief TypeParam
- * \param p
- * \param pile
- * \return
- */
-CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile);
+    /*!
+     * \brief CBotStringArray
+     */
+    CBotStringArray();
 
-/*!
- * \brief ArrayType
- * \param p
- * \param pile
- * \param type
- * \return
- */
-CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type);
+    /*!
+     * \brief ~CBotStringArray
+     */
+    ~CBotStringArray();
 
-/*!
- * \brief WriteWord
- * \param pf
- * \param w
- * \return
- */
-bool WriteWord(FILE* pf, unsigned short w);
+    /*!
+     * \brief SetSize Set the array size.
+     * \param nb
+     */
+    void SetSize(int nb);
 
-/*!
- * \brief WriteString
- * \param pf
- * \param s
- * \return
- */
-bool WriteString(FILE* pf, CBotString s);
+    /*!
+     * \brief GetSize
+     * \return
+     */
+    int GetSize();
 
-/*!
- * \brief WriteFloat
- * \param pf
- * \param w
- * \return
- */
-bool WriteFloat(FILE* pf, float w);
+    /*!
+     * \brief Add
+     * \param str
+     */
+    void Add(const CBotString& str);
+
+    /*!
+     * \brief operator []
+     * \param nIndex
+     * \return
+     */
+    CBotString& operator[](int nIndex);
+
+    /*!
+     * \brief ElementAt
+     * \param nIndex
+     * \return
+     */
+    CBotString& ElementAt(int nIndex);
+
+private:
+
+    //! Number of elements.
+    int m_nSize;
+    //! Reserved size.
+    int m_nMaxSize;
+    CBotString* m_pData;
+};
diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h
index b82cdfe..e8fd043 100644
--- a/src/CBot/CBotToken.h
+++ b/src/CBot/CBotToken.h
@@ -22,6 +22,8 @@
 // Modules inlcude
 #include "CBotDll.h"
 
+#include "CBotStringArray.h"
+
 // Local include
 
 // Global include
diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp
index 2df62cc..824b3fd 100644
--- a/src/CBot/CBotUtils.cpp
+++ b/src/CBot/CBotUtils.cpp
@@ -140,3 +140,23 @@ bool WriteFloat(FILE* pf, float w)
 
     return (lg == 1);
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void ConstructElements(CBotString* pNewData, int nCount)
+{
+    while (nCount--)
+    {
+        ConstructElement(pNewData);
+        pNewData++;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DestructElements(CBotString* pOldData, int nCount)
+{
+    while (nCount--)
+    {
+        DestructElement(pOldData);
+        pOldData++;
+    }
+}
diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h
index 354595b..f25c8c6 100644
--- a/src/CBot/CBotUtils.h
+++ b/src/CBot/CBotUtils.h
@@ -75,3 +75,45 @@ bool WriteString(FILE* pf, CBotString s);
  * \return
  */
 bool WriteFloat(FILE* pf, float w);
+
+/*!
+ * \brief ConstructElement
+ * \param pNewData
+ */
+static inline void ConstructElement(CBotString* pNewData)
+{
+    memset(pNewData, 0, sizeof(CBotString));
+}
+
+/*!
+ * \brief DestructElement
+ * \param pOldData
+ */
+static inline void DestructElement(CBotString* pOldData)
+{
+    pOldData->~CBotString();
+}
+
+/*!
+ * \brief CopyElement
+ * \param pSrc
+ * \param pDest
+ */
+static inline void CopyElement(CBotString* pSrc, CBotString* pDest)
+{
+    *pSrc = *pDest;
+}
+
+/*!
+ * \brief ConstructElements
+ * \param pNewData
+ * \param nCount
+ */
+void ConstructElements(CBotString* pNewData, int nCount);
+
+/*!
+ * \brief DestructElements
+ * \param pOldData
+ * \param nCount
+ */
+void DestructElements(CBotString* pOldData, int nCount);
diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt
index 80cb2d0..28c7320 100644
--- a/src/CBot/CMakeLists.txt
+++ b/src/CBot/CMakeLists.txt
@@ -10,6 +10,7 @@ set(SOURCES
     CBotUtils.cpp
     CBotDefParam.cpp
     CBotCallMethode.cpp
+    CBotStringArray.cpp
     CBotInstr/CBotInstr.cpp
     CBotInstr/CBotWhile.cpp
     CBotInstr/CBotDo.cpp

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list